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, BottomType, CollectionType, ComponentType, ConvertedBasicArrayType, ConvertedBasicCollectionType, ConvertedBasicTypeImpl, CustomCollectionType, CustomMutabilityConvertedBasicTypeImpl, CustomMutabilityConvertedPrimitiveBasicTypeImpl, CustomType, DiscriminatorType, DiscriminatorTypeImpl, EmbeddedComponentType, EntityType, IdentifierBagType, ImmutableNamedBasicTypeImpl, JavaObjectType, ListType, ManyToOneType, MapType, MetaType, NamedBasicTypeImpl, NullType, OneToOneType, OrderedMapType, OrderedSetType, ProcessorSessionFactory.Component, QueryParameterJavaObjectType, SerializableToBlobType, SerializableType, SetType, SortedMapType, SortedSetType, SpecialOneToOneType, StandardBasicTypeTemplate, UserComponentType

@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 Details

    • isAssociationType

      boolean isAssociationType()
      Return true if the implementation is castable to AssociationType. This does not necessarily imply that the type actually represents an association. Shortcut for type instanceof AssociationType.
      Returns:
      True if this type is also an AssociationType implementor; false otherwise.
    • isCollectionType

      boolean isCollectionType()
      Return true if the implementation is castable to CollectionType. Shortcut for type instanceof CollectionType

      A CollectionType is additionally an AssociationType; 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 to EntityType. Shortcut for type instanceof EntityType.

      An EntityType is additionally an AssociationType; 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 to AnyType. Shortcut for type instanceof AnyType.

      An AnyType is additionally an AssociationType; so if this method returns true, then isAssociationType() 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 to CompositeType. Shortcut for type 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 by Types or SqlTypes 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 class handled by this type.
    • getReturnedClassName

      default String getReturnedClassName()
      The qualified name of the class handled by this type.
      Returns:
      The qualified Java class name.
      Since:
      6.5
    • isSame

      boolean isSame(@Nullable Object x, @Nullable 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 value
      y - 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(@Nullable Object x, @Nullable 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 to equals().

      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 value
      y - The second value
      Returns:
      True if there are considered equal (see discussion above).
      Throws:
      HibernateException - A problem occurred performing the comparison
    • isEqual

      boolean isEqual(@Nullable Object x, @Nullable 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 to isEqual(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 value
      y - The second value
      factory - 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's hashCode.
      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 to getHashCode(Object).
      Parameters:
      x - The value for which to retrieve a hash code
      factory - The session factory
      Returns:
      The hash code
      Throws:
      HibernateException - A problem occurred calculating the hash code
    • getTypeForEqualsHashCode

      default @Nullable Type getTypeForEqualsHashCode()
      The type to use for equals() and hashCode() computation. When null, use Object.equals(Object) and Object.hashCode(). This is useful to avoid mega-morphic callsites.
    • compare

      int compare(@Nullable Object x, @Nullable Object y)
      Perform a Comparator-style comparison of the given values.
      Parameters:
      x - The first value
      y - The second value
      Returns:
      The comparison result.
      See Also:
    • compare

      int compare(@Nullable Object x, @Nullable Object y, SessionFactoryImplementor sessionFactory)
    • isDirty

      boolean isDirty(@Nullable Object old, @Nullable Object current, SharedSessionContractImplementor session) throws HibernateException
      Should the parent be considered dirty, given both the old and current value?
      Parameters:
      old - the old value
      current - the current value
      session - 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(@Nullable Object oldState, @Nullable 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 value
      currentState - the current value
      checkable - An array of booleans indicating which columns making up the value are actually checkable
      session - 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(@Nullable Object dbState, @Nullable Object currentState, boolean[] checkable, SharedSessionContractImplementor session) throws HibernateException
      Has the value been modified compared to the current database state? The difference between this and the isDirty(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 to isDirty(java.lang.Object, java.lang.Object, org.hibernate.engine.spi.SharedSessionContractImplementor).
      Parameters:
      dbState - the database state, in a "hydrated" form, with identifiers unresolved
      currentState - the current state of the object
      checkable - which columns are actually checkable
      session - 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, @Nullable Object value, int index, boolean[] settable, SharedSessionContractImplementor session) throws HibernateException, SQLException
      Bind a value represented by an instance of the mapped class to the given JDBC PreparedStatement, ignoring some columns as dictated by the settable parameter. Implementors should handle the possibility of null values. A multi-column type should bind parameters starting from index.
      Parameters:
      st - The JDBC prepared statement to which to bind
      value - the object to write
      index - starting parameter bind index
      settable - an array indicating which columns to bind/ignore
      session - The originating session
      Throws:
      HibernateException - An error from Hibernate
      SQLException - An error from the JDBC driver
    • nullSafeSet

      void nullSafeSet(PreparedStatement st, @Nullable Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException
      Bind a value represented by an instance of the mapped class to the given JDBC PreparedStatement, ignoring some columns as dictated by the settable parameter. Implementors should handle the possibility of null values. A multi-column type should bind parameters starting from index.
      Parameters:
      st - The JDBC prepared statement to which to bind
      value - the object to write
      index - starting parameter bind index
      session - The originating session
      Throws:
      HibernateException - An error from Hibernate
      SQLException - 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 logged
      factory - 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

      @Nullable Object deepCopy(@Nullable 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 copied
      factory - 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 @Nullable Serializable disassemble(@Nullable 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 with assemble(Serializable, SharedSessionContractImplementor, Object).

      Parameters:
      value - the value to cache
      sessionFactory - the session factory
      Returns:
      the disassembled, deep cloned state
      Throws:
      HibernateException - An error from Hibernate
    • disassemble

      @Nullable Serializable disassemble(@Nullable Object value, @Nullable SharedSessionContractImplementor session, @Nullable 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 cache
      session - the originating session
      owner - optional parent entity object (needed for collections)
      Returns:
      the disassembled, deep cloned state
      Throws:
      HibernateException - An error from Hibernate
    • assemble

      @Nullable Object assemble(@Nullable Serializable cached, SharedSessionContractImplementor session, Object owner) throws HibernateException
      Reconstruct the object from its disassembled state. This function is the inverse of disassemble(Object, SharedSessionContractImplementor, Object).
      Parameters:
      cached - the disassembled state from the cache
      session - the originating session
      owner - the parent entity object
      Returns:
      the (re)assembled object
      Throws:
      HibernateException - An error from Hibernate
    • beforeAssemble

      @Deprecated(forRemoval=true, since="6.6") void beforeAssemble(Serializable cached, SharedSessionContractImplementor session)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Is not called anymore
      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 key
      session - The originating session
    • replace

      @Nullable Object replace(@Nullable Object original, @Nullable 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 merged
      target - the value in the managed entity
      session - The originating session
      owner - The owner of the value
      copyCache - The cache of already copied/replaced values
      Returns:
      the value to be merged
      Throws:
      HibernateException - An error from Hibernate
    • replace

      @Nullable Object replace(@Nullable Object original, @Nullable 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 merged
      target - the value in the managed entity
      session - The originating session
      owner - The owner of the value
      copyCache - The cache of already copied/replaced values
      foreignKeyDirection - For associations, which direction does the foreign key point?
      Returns:
      the value to be merged
      Throws:
      HibernateException - An error from Hibernate
    • toColumnNullness

      boolean[] toColumnNullness(@Nullable Object value, Mapping mapping)
      Given an instance of the type, return an array of boolean values indicating which mapped columns would be null.
      Parameters:
      value - an instance of the type
      mapping - The mapping abstraction
      Returns:
      array indicating column nullness for a value instance