Package org.hibernate.usertype
Interface UserType<J>
-
- All Known Subinterfaces:
EnhancedUserType<J>
,UserVersionType<T>
- All Known Implementing Classes:
BaseUserTypeSupport
,EnumType
,RevisionTypeType
,StaticUserTypeSupport
,UserTypeLegacyBridge
,UserTypeSupport
public interface UserType<J>
This interface should be implemented by user-defined "types". A "type" class is not the actual property type - it is a class that knows how to serialize instances of another class to and from JDBC.This interface
- abstracts user code from future changes to the
Type
interface, - simplifies the implementation of custom types and
- hides certain "internal" interfaces from user code.
Implementors must be immutable and must declare a public default constructor.
The actual class mapped by a
UserType
may be just about anything.CompositeUserType
provides an extended version of this interface that is useful for more complex cases.Alternatively, custom types could implement
Type
directly or extend one of the abstract classes inorg.hibernate.type
. This approach risks future incompatible changes to classes or interfaces in that package.- See Also:
Type
,CustomType
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description J
assemble(Serializable cached, Object owner)
Reconstruct an object from the cacheable representation.J
deepCopy(J value)
Return a deep copy of the persistent state, stopping at entities and at collections.Serializable
disassemble(J value)
Transform the object into its cacheable representation.boolean
equals(J x, J y)
Compare two instances of the class mapped by this type for persistence "equality".default long
getDefaultSqlLength(Dialect dialect, JdbcType jdbcType)
default int
getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType)
default int
getDefaultSqlScale(Dialect dialect, JdbcType jdbcType)
default JdbcType
getJdbcType(TypeConfiguration typeConfiguration)
int
getSqlType()
Return the SQL type code for the column mapped by this type.default BasicValueConverter<J,Object>
getValueConverter()
Returns the converter that this user type uses for transforming from the domain type, to the relational type, ornull
if there is no conversion.int
hashCode(J x)
Get a hashcode for the instance, consistent with persistence "equality"boolean
isMutable()
Are objects of this type mutable?J
nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner)
Retrieve an instance of the mapped class from a JDBC resultset.void
nullSafeSet(PreparedStatement st, J value, int index, SharedSessionContractImplementor session)
Write an instance of the mapped class to a prepared statement.J
replace(J detached, J managed, Object owner)
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.Class<J>
returnedClass()
The class returned bynullSafeGet()
.
-
-
-
Method Detail
-
getSqlType
int getSqlType()
Return the SQL type code for the column mapped by this type. The codes are generally defined onorg.hibernate.type.SqlTypes
, but could be database-specific codes- See Also:
SqlTypes
-
equals
boolean equals(J x, J y)
Compare two instances of the class mapped by this type for persistence "equality". Equality of the persistent state.
-
hashCode
int hashCode(J x)
Get a hashcode for the instance, consistent with persistence "equality"
-
nullSafeGet
J nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException
Retrieve an instance of the mapped class from a JDBC resultset. Implementors should handle possibility of null values.- Throws:
SQLException
-
nullSafeSet
void nullSafeSet(PreparedStatement st, J value, int index, SharedSessionContractImplementor session) throws SQLException
Write an instance of the mapped class to a prepared statement. Implementors should handle possibility of null values. A multi-column type should be written to parameters starting fromindex
.- Throws:
SQLException
-
deepCopy
J deepCopy(J value)
Return a deep copy of the persistent state, stopping at entities and at collections. It is not necessary to copy immutable objects, or null values, in which case it is safe to simply return the argument.- Parameters:
value
- the object to be cloned, which may be null- Returns:
- Object a copy
-
isMutable
boolean isMutable()
Are objects of this type mutable?- Returns:
- boolean
-
disassemble
Serializable disassemble(J value)
Transform the object into its cacheable representation. At the very least this method should perform a deep copy if the type is mutable. That may not be enough for some implementations, however; for example, associations must be cached as identifier values. (optional operation)- Parameters:
value
- the object to be cached- Returns:
- a cacheable representation of the object
-
assemble
J assemble(Serializable cached, Object owner)
Reconstruct an object from the cacheable representation. At the very least this method should perform a deep copy if the type is mutable. (optional operation)- Parameters:
cached
- the object to be cachedowner
- the owner of the cached object- Returns:
- a reconstructed object from the cacheable representation
-
replace
J replace(J detached, J managed, Object owner)
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:
detached
- the value from the detached entity being mergedmanaged
- the value in the managed entity- Returns:
- the value to be merged
-
getJdbcType
default JdbcType getJdbcType(TypeConfiguration typeConfiguration)
-
getValueConverter
default BasicValueConverter<J,Object> getValueConverter()
Returns the converter that this user type uses for transforming from the domain type, to the relational type, ornull
if there is no conversion. Note that it is vital to provide a converter if a column should be mapped to multiple domain types, as Hibernate will only select a column once and materialize values asJdbcMapping.getJdbcJavaType()
. Support for multiple domain type representations works by converting objects of that type to the domain type.
-
-