Interface Type
-
- All Superinterfaces:
Serializable
- All Known Subinterfaces:
AdjustableBasicType<J>
,AssociationType
,BasicPluralType<C,E>
,BasicType<T>
,CompositeType
,CompositeTypeImplementor
,ConvertedBasicType<J>
,DiscriminatorType<O>
- All Known Implementing Classes:
AbstractSingleColumnStandardBasicType
,AbstractStandardBasicType
,AbstractType
,AnyType
,ArrayType
,BagType
,BasicArrayType
,BasicCollectionType
,BasicTypeImpl
,CollectionType
,ComponentType
,ConvertedBasicArrayType
,ConvertedBasicCollectionType
,ConvertedBasicTypeImpl
,CustomCollectionType
,CustomMutabilityConvertedBasicTypeImpl
,CustomType
,DiscriminatorType
,DiscriminatorTypeImpl
,EmbeddedComponentType
,EntityType
,IdentifierBagType
,ImmutableNamedBasicTypeImpl
,JavaObjectType
,ListType
,ManyToOneType
,MapType
,MetaType
,NamedBasicTypeImpl
,NullType
,OneToOneType
,OrderedMapType
,OrderedSetType
,SerializableToBlobType
,SerializableType
,SetType
,SortedMapType
,SortedSetType
,SpecialOneToOneType
,StandardBasicTypeTemplate
@Internal public interface Type extends Serializable
Defines a mapping between a Java type and one or more JDBC types, as well as describing the in-memory semantics of the given Java type, including:- how to compare values and check for "dirtiness",
- how to clone values, and
- how to assemble/disassemble values for storage in the second-level cache.
An application-defined custom types could, in principle, implement this interface directly, but it's safer to implement the more stable interface
UserType
.An implementation of this interface must certainly be thread-safe. Ideally, it should also be immutable.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Object
assemble(Serializable cached, SharedSessionContractImplementor session, Object owner)
Reconstruct the object from its disassembled state.void
beforeAssemble(Serializable cached, SharedSessionContractImplementor session)
Called before assembling a query result set from the query cache, to allow batch fetching of entities missing from the second-level cache.int
compare(Object x, Object y)
Perform aComparator
-style comparison of the given values.int
compare(Object x, Object y, SessionFactoryImplementor sessionFactory)
Object
deepCopy(Object value, SessionFactoryImplementor factory)
Return a deep copy of the persistent state, stopping at entities and at collections.default Serializable
disassemble(Object value, SessionFactoryImplementor sessionFactory)
Return a disassembled representation of the object.Serializable
disassemble(Object value, SharedSessionContractImplementor session, Object owner)
Return a disassembled representation of the object.int
getColumnSpan(Mapping mapping)
How many columns are used to persist this type?int
getHashCode(Object x)
Get a hash code, consistent with persistence "equality".int
getHashCode(Object x, SessionFactoryImplementor factory)
Get a hash code, consistent with persistence "equality".String
getName()
Returns the abbreviated name of the type.Class<?>
getReturnedClass()
The class handled by this type.int[]
getSqlTypeCodes(Mapping mapping)
boolean
isAnyType()
Return true if the implementation is castable toAnyType
.boolean
isAssociationType()
Return true if the implementation is castable toAssociationType
.boolean
isCollectionType()
Return true if the implementation is castable toCollectionType
.boolean
isComponentType()
Return true if the implementation is castable toCompositeType
.boolean
isDirty(Object oldState, Object currentState, boolean[] checkable, SharedSessionContractImplementor session)
Should the parent be considered dirty, given both the old and current value?boolean
isDirty(Object old, Object current, SharedSessionContractImplementor session)
Should the parent be considered dirty, given both the old and current value?boolean
isEntityType()
Return true if the implementation is castable toEntityType
.boolean
isEqual(Object x, Object y)
Compare two instances of the class mapped by this type for persistence "equality", that is, equality of persistent state.boolean
isEqual(Object x, Object y, SessionFactoryImplementor factory)
Compare two instances of the class mapped by this type for persistence "equality", that is, equality of persistent state.boolean
isModified(Object dbState, Object currentState, boolean[] checkable, SharedSessionContractImplementor session)
Has the value been modified compared to the current database state? The difference between this and theisDirty(java.lang.Object, java.lang.Object, org.hibernate.engine.spi.SharedSessionContractImplementor)
methods is that here we need to account for "partially" built values.boolean
isMutable()
Are objects of this type mutable with respect to the referencing object? Entities and collections are considered immutable because they manage their own internal state.boolean
isSame(Object x, Object y)
Compare two instances of the class mapped by this type for persistence "equality", that is, equality of persistent state, taking a shortcut for entity references.void
nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SharedSessionContractImplementor session)
Bind a value represented by an instance of themapped class
to the given JDBCPreparedStatement
, ignoring some columns as dictated by thesettable
parameter.void
nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
Bind a value represented by an instance of themapped class
to the given JDBCPreparedStatement
, ignoring some columns as dictated by thesettable
parameter.Object
replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map<Object,Object> copyCache)
During merge, replace the existing (target) value in the entity we are merging to with a new (original) value from the detached entity we are merging.Object
replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map<Object,Object> copyCache, ForeignKeyDirection foreignKeyDirection)
During merge, replace the existing (target) value in the entity we are merging to with a new (original) value from the detached entity we are merging.boolean[]
toColumnNullness(Object value, Mapping mapping)
Given an instance of the type, return an array ofboolean
values indicating which mapped columns would be null.String
toLoggableString(@Nullable Object value, SessionFactoryImplementor factory)
Generate a representation of the given value for logging purposes.
-
-
-
Method Detail
-
isAssociationType
boolean isAssociationType()
Return true if the implementation is castable toAssociationType
. This does not necessarily imply that the type actually represents an association. Shortcut fortype instanceof AssociationType
.- Returns:
- True if this type is also an
AssociationType
implementor; false otherwise.
-
isCollectionType
boolean isCollectionType()
Return true if the implementation is castable toCollectionType
. Shortcut fortype instanceof CollectionType
A
CollectionType
is additionally anAssociationType
; so if this method returns true,isAssociationType()
should also return true.- Returns:
- True if this type is also a
CollectionType
implementor; false otherwise.
-
isEntityType
boolean isEntityType()
Return true if the implementation is castable toEntityType
. Shortcut fortype instanceof EntityType
.An
EntityType
is additionally anAssociationType
; so if this method returns true,isAssociationType()
should also return true.- Returns:
- True if this type is also an
EntityType
implementor; false otherwise.
-
isAnyType
boolean isAnyType()
Return true if the implementation is castable toAnyType
. Shortcut fortype instanceof AnyType
.An
AnyType
is additionally anAssociationType
; so if this method returns true, thenisAssociationType()
should also return true.- Returns:
- True if this type is also an
AnyType
implementor; false otherwise.
-
isComponentType
boolean isComponentType()
Return true if the implementation is castable toCompositeType
. Shortcut fortype instanceof CompositeType
.A component type may own collections or associations and hence must provide certain extra functionality.
- Returns:
- True if this type is also a
CompositeType
implementor; false otherwise.
-
getColumnSpan
int getColumnSpan(Mapping mapping) throws MappingException
How many columns are used to persist this type?Always the same as
getSqlTypCodes(mapping).length
.- Parameters:
mapping
- The mapping object :/- Returns:
- The number of columns
- Throws:
MappingException
- Generally indicates an issue accessing the passed mapping object.
-
getSqlTypeCodes
int[] getSqlTypeCodes(Mapping mapping) throws MappingException
Return the JDBC types codes as defined byTypes
orSqlTypes
for the columns mapped by this type.The number of elements in this array must match the return from
getColumnSpan(org.hibernate.engine.spi.Mapping)
.- Parameters:
mapping
- The mapping object :/- Returns:
- The JDBC type codes.
- Throws:
MappingException
- Generally indicates an issue accessing the passed mapping object.
-
getReturnedClass
Class<?> getReturnedClass()
The class handled by this type.- Returns:
- The java type class handled by this type.
-
isSame
boolean isSame(Object x, Object y) throws HibernateException
Compare two instances of the class mapped by this type for persistence "equality", that is, equality of persistent state, taking a shortcut for entity references.For most types this should boil down to an equality comparison of the given values, and it's reasonable to simply delegate to
isEqual(Object, Object)
. But for associations the semantics are a bit different.- Parameters:
x
- The first valuey
- The second value- Returns:
- True if there are considered the same (see discussion above).
- Throws:
HibernateException
- A problem occurred performing the comparison
-
isEqual
boolean isEqual(Object x, Object y) throws HibernateException
Compare two instances of the class mapped by this type for persistence "equality", that is, equality of persistent state. For most types this could simply delegate toequals()
.This should always equate to some form of comparison of the value's internal state. As an example, for Java's
Date
class, the comparison should be of its internal state, but based only on the specific part which is persistent (the timestamp, date, or time).- Parameters:
x
- The first valuey
- The second value- Returns:
- True if there are considered equal (see discussion above).
- Throws:
HibernateException
- A problem occurred performing the comparison
-
isEqual
boolean isEqual(Object x, Object y, SessionFactoryImplementor factory) throws HibernateException
Compare two instances of the class mapped by this type for persistence "equality", that is, equality of persistent state. For most types this could simply delegate toisEqual(Object, Object)
.This should always equate to some form of comparison of the value's internal state. As an example, for Java's
Date
class, the comparison should be of its internal state, but based only on the specific part which is persistent (the timestamp, date, or time).- Parameters:
x
- The first valuey
- The second valuefactory
- The session factory- Returns:
- True if there are considered equal (see discussion above).
- Throws:
HibernateException
- A problem occurred performing the comparison
-
getHashCode
int getHashCode(Object x) throws HibernateException
Get a hash code, consistent with persistence "equality". For most types this could simply delegate to the given value'shashCode
.- Parameters:
x
- The value for which to retrieve a hash code- Returns:
- The hash code
- Throws:
HibernateException
- A problem occurred calculating the hash code
-
getHashCode
int getHashCode(Object x, SessionFactoryImplementor factory) throws HibernateException
Get a hash code, consistent with persistence "equality". For most types this could simply delegate togetHashCode(Object)
.- Parameters:
x
- The value for which to retrieve a hash codefactory
- The session factory- Returns:
- The hash code
- Throws:
HibernateException
- A problem occurred calculating the hash code
-
compare
int compare(Object x, Object y)
Perform aComparator
-style comparison of the given values.- Parameters:
x
- The first valuey
- The second value- Returns:
- The comparison result.
- See Also:
Comparator.compare(Object, Object)
-
compare
int compare(Object x, Object y, SessionFactoryImplementor sessionFactory)
-
isDirty
boolean isDirty(Object old, Object current, SharedSessionContractImplementor session) throws HibernateException
Should the parent be considered dirty, given both the old and current value?- Parameters:
old
- the old valuecurrent
- the current valuesession
- The session from which the request originated.- Returns:
- true if the field is dirty
- Throws:
HibernateException
- A problem occurred performing the checking
-
isDirty
boolean isDirty(Object oldState, Object currentState, boolean[] checkable, SharedSessionContractImplementor session) throws HibernateException
Should the parent be considered dirty, given both the old and current value?- Parameters:
oldState
- the old valuecurrentState
- the current valuecheckable
- An array of booleans indicating which columns making up the value are actually checkablesession
- The session from which the request originated.- Returns:
- true if the field is dirty
- Throws:
HibernateException
- A problem occurred performing the checking
-
isModified
boolean isModified(Object dbState, Object currentState, boolean[] checkable, SharedSessionContractImplementor session) throws HibernateException
Has the value been modified compared to the current database state? The difference between this and theisDirty(java.lang.Object, java.lang.Object, org.hibernate.engine.spi.SharedSessionContractImplementor)
methods is that here we need to account for "partially" built values. This is really only an issue with association types. For most type implementations it is enough to simply delegate toisDirty(java.lang.Object, java.lang.Object, org.hibernate.engine.spi.SharedSessionContractImplementor)
.- Parameters:
dbState
- the database state, in a "hydrated" form, with identifiers unresolvedcurrentState
- the current state of the objectcheckable
- which columns are actually checkablesession
- The session from which the request originated.- Returns:
- true if the field has been modified
- Throws:
HibernateException
- A problem occurred performing the checking
-
nullSafeSet
void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SharedSessionContractImplementor session) throws HibernateException, SQLException
Bind a value represented by an instance of themapped class
to the given JDBCPreparedStatement
, ignoring some columns as dictated by thesettable
parameter. Implementors should handle the possibility of null values. A multi-column type should bind parameters starting fromindex
.- Parameters:
st
- The JDBC prepared statement to which to bindvalue
- the object to writeindex
- starting parameter bind indexsettable
- an array indicating which columns to bind/ignoresession
- The originating session- Throws:
HibernateException
- An error from HibernateSQLException
- An error from the JDBC driver
-
nullSafeSet
void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException
Bind a value represented by an instance of themapped class
to the given JDBCPreparedStatement
, ignoring some columns as dictated by thesettable
parameter. Implementors should handle the possibility of null values. A multi-column type should bind parameters starting fromindex
.- Parameters:
st
- The JDBC prepared statement to which to bindvalue
- the object to writeindex
- starting parameter bind indexsession
- The originating session- Throws:
HibernateException
- An error from HibernateSQLException
- An error from the JDBC driver
-
toLoggableString
String toLoggableString(@Nullable Object value, SessionFactoryImplementor factory) throws HibernateException
Generate a representation of the given value for logging purposes.- Parameters:
value
- The value to be loggedfactory
- The session factory- Returns:
- The loggable representation
- Throws:
HibernateException
- An error from Hibernate
-
getName
String getName()
Returns the abbreviated name of the type.- Returns:
- the Hibernate type name
-
deepCopy
Object deepCopy(Object value, SessionFactoryImplementor factory) throws HibernateException
Return a deep copy of the persistent state, stopping at entities and at collections.- Parameters:
value
- The value to be copiedfactory
- The session factory- Returns:
- The deep copy
- Throws:
HibernateException
- An error from Hibernate
-
isMutable
boolean isMutable()
Are objects of this type mutable with respect to the referencing object? Entities and collections are considered immutable because they manage their own internal state.- Returns:
- boolean
-
disassemble
default Serializable disassemble(Object value, SessionFactoryImplementor sessionFactory) throws HibernateException
Return a disassembled representation of the object. This is the representation that is stored in the second-level cache.A reference to an associated entity should be disassembled to its primary key value.
A high-quality implementation of this method should ensure that:
Objects.equals(disassemble(x,s), disassemble(y,s))
== isEqual(x,y,sf)and that:
Objects.equals(x, assemble(disassemble(x,s),s,o))
That is, the implementation must be consistent with
isEqual(Object, Object, SessionFactoryImplementor)
and withassemble(Serializable, SharedSessionContractImplementor, Object)
.- Parameters:
value
- the value to cachesessionFactory
- the session factory- Returns:
- the disassembled, deep cloned state
- Throws:
HibernateException
- An error from Hibernate
-
disassemble
Serializable disassemble(Object value, SharedSessionContractImplementor session, Object owner) throws HibernateException
Return a disassembled representation of the object. This is the representation that is stored in the second-level cache.A reference to an associated entity should be disassembled to its primary key value.
- Parameters:
value
- the value to cachesession
- the originating sessionowner
- optional parent entity object (needed for collections)- Returns:
- the disassembled, deep cloned state
- Throws:
HibernateException
- An error from Hibernate
-
assemble
Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) throws HibernateException
Reconstruct the object from its disassembled state. This function is the inverse ofdisassemble(Object, SharedSessionContractImplementor, Object)
.- Parameters:
cached
- the disassembled state from the cachesession
- the originating sessionowner
- the parent entity object- Returns:
- the (re)assembled object
- Throws:
HibernateException
- An error from Hibernate
-
beforeAssemble
void beforeAssemble(Serializable cached, SharedSessionContractImplementor session)
Called before assembling a query result set from the query cache, to allow batch fetching of entities missing from the second-level cache.- Parameters:
cached
- The keysession
- The originating session
-
replace
Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map<Object,Object> copyCache) throws HibernateException
During merge, replace the existing (target) value in the entity we are merging to with a new (original) value from the detached entity we are merging. For immutable objects, or null values, it is safe to simply return the first parameter. For mutable objects, it is safe to return a copy of the first parameter. For objects with component values, it might make sense to recursively replace component values.- Parameters:
original
- the value from the detached entity being mergedtarget
- the value in the managed entitysession
- The originating sessionowner
- The owner of the valuecopyCache
- The cache of already copied/replaced values- Returns:
- the value to be merged
- Throws:
HibernateException
- An error from Hibernate
-
replace
Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map<Object,Object> copyCache, ForeignKeyDirection foreignKeyDirection) throws HibernateException
During merge, replace the existing (target) value in the entity we are merging to with a new (original) value from the detached entity we are merging. For immutable objects, or null values, it is safe to simply return the first parameter. For mutable objects, it is safe to return a copy of the first parameter. For objects with component values, it might make sense to recursively replace component values.- Parameters:
original
- the value from the detached entity being mergedtarget
- the value in the managed entitysession
- The originating sessionowner
- The owner of the valuecopyCache
- The cache of already copied/replaced valuesforeignKeyDirection
- For associations, which direction does the foreign key point?- Returns:
- the value to be merged
- Throws:
HibernateException
- An error from Hibernate
-
toColumnNullness
boolean[] toColumnNullness(Object value, Mapping mapping)
Given an instance of the type, return an array ofboolean
values indicating which mapped columns would be null.- Parameters:
value
- an instance of the typemapping
- The mapping abstraction- Returns:
- array indicating column nullness for a value instance
-
-