Package org.hibernate.type
Class CustomType
- java.lang.Object
-
- org.hibernate.type.AbstractType
-
- org.hibernate.type.CustomType
-
- All Implemented Interfaces:
java.io.Serializable
,BasicType
,DiscriminatorType
,IdentifierType
,LiteralType
,ProcedureParameterExtractionAware
,ProcedureParameterNamedBinder
,StringRepresentableType
,Type
,VersionType
public class CustomType extends AbstractType implements IdentifierType, DiscriminatorType, VersionType, BasicType, StringRepresentableType, ProcedureParameterNamedBinder, ProcedureParameterExtractionAware
AdaptsUserType
to the genericType
interface, in order to isolate user code from changes in the internal Type contracts.- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class org.hibernate.type.AbstractType
LEGACY_DEFAULT_SIZE, LEGACY_DICTATED_SIZE
-
-
Constructor Summary
Constructors Constructor Description CustomType(UserType userType)
CustomType(UserType userType, java.lang.String[] registrationKeys)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object
assemble(java.io.Serializable cached, SharedSessionContractImplementor session, java.lang.Object owner)
Reconstruct the object from its disassembled state.boolean
canDoExtraction()
Can the given instance of this type actually perform the parameter value extractions?boolean
canDoSetting()
Can the given instance of this type actually set the parameter value by namejava.lang.Object
deepCopy(java.lang.Object value, SessionFactoryImplementor factory)
Return a deep copy of the persistent state, stopping at entities and at collections.Size[]
defaultSizes(Mapping mapping)
Defines the column sizes to use according to this type if the user did not explicitly say (and if noType.dictatedSizes(org.hibernate.engine.spi.Mapping)
were given).Size[]
dictatedSizes(Mapping mapping)
Return the column sizes dictated by this type.java.io.Serializable
disassemble(java.lang.Object value, SharedSessionContractImplementor session, java.lang.Object owner)
Return a disassembled representation of the object.boolean
equals(java.lang.Object obj)
java.lang.Object
extract(java.sql.CallableStatement statement, int startIndex, SharedSessionContractImplementor session)
Perform the extractionjava.lang.Object
extract(java.sql.CallableStatement statement, java.lang.String[] paramNames, SharedSessionContractImplementor session)
Perform the extractionjava.lang.Object
fromStringValue(java.lang.String string)
Consume the given string representation back into this types java form.java.lang.Object
fromXMLString(java.lang.String xml, Mapping factory)
int
getColumnSpan(Mapping session)
How many columns are used to persist this type.java.util.Comparator
getComparator()
Get a comparator for version values.int
getHashCode(java.lang.Object x)
Get a hash code, consistent with persistence "equality".java.lang.String
getName()
Returns the abbreviated name of the type.java.lang.String[]
getRegistrationKeys()
Get the names under which this type should be registered in the type registry.java.lang.Class
getReturnedClass()
UserType
getUserType()
int
hashCode()
boolean
isDirty(java.lang.Object old, java.lang.Object current, boolean[] checkable, SharedSessionContractImplementor session)
Should the parent be considered dirty, given both the old and current value?boolean
isEqual(java.lang.Object x, java.lang.Object y)
Compare two instances of the class mapped by this type for persistence "equality" (equality of persistent state).boolean
isMutable()
Are objects of this type mutable.java.lang.Object
next(java.lang.Object current, SharedSessionContractImplementor session)
Increment the version.java.lang.Object
nullSafeGet(java.sql.ResultSet rs, java.lang.String[] names, SharedSessionContractImplementor session, java.lang.Object owner)
Extract a value of themapped class
from the JDBC result set.java.lang.Object
nullSafeGet(java.sql.ResultSet rs, java.lang.String columnName, SharedSessionContractImplementor session, java.lang.Object owner)
Extract a value of themapped class
from the JDBC result set.void
nullSafeSet(java.sql.CallableStatement statement, java.lang.Object value, java.lang.String name, SharedSessionContractImplementor session)
Bind a value to the JDBC prepared statement, ignoring some columns as dictated by the 'settable' parameter.void
nullSafeSet(java.sql.PreparedStatement st, java.lang.Object value, int index, boolean[] settable, SharedSessionContractImplementor session)
Bind a value represented by an instance of themapped class
to the JDBC prepared statement, ignoring some columns as dictated by the 'settable' parameter.void
nullSafeSet(java.sql.PreparedStatement st, java.lang.Object value, int index, SharedSessionContractImplementor session)
Bind a value represented by an instance of themapped class
to the JDBC prepared statement.java.lang.String
objectToSQLString(java.lang.Object value, Dialect dialect)
Convert the value into a string representation, suitable for embedding in an SQL statement as a literal.java.lang.Object
replace(java.lang.Object original, java.lang.Object target, SharedSessionContractImplementor session, java.lang.Object owner, java.util.Map 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.java.lang.Object
seed(SharedSessionContractImplementor session)
Generate an initial version.int[]
sqlTypes(Mapping pi)
Return the JDBC types codes (perTypes
) for the columns mapped by this type.java.lang.Object
stringToObject(java.lang.String xml)
Convert the value from the mapping file to a Java object.boolean[]
toColumnNullness(java.lang.Object value, Mapping mapping)
Given an instance of the type, return an array of boolean, indicating which mapped columns would be null.java.lang.String
toLoggableString(java.lang.Object value, SessionFactoryImplementor factory)
Generate a representation of the value for logging purposes.java.lang.String
toString(java.lang.Object value)
Render the value to the string representation.java.lang.String
toXMLString(java.lang.Object value, SessionFactoryImplementor factory)
-
Methods inherited from class org.hibernate.type.AbstractType
beforeAssemble, compare, getHashCode, getSemiResolvedType, hydrate, isAnyType, isAssociationType, isCollectionType, isComponentType, isDirty, isEntityType, isEqual, isModified, isSame, replace, resolve, semiResolve
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.hibernate.type.Type
beforeAssemble, compare, getHashCode, getSemiResolvedType, hydrate, isAnyType, isAssociationType, isCollectionType, isComponentType, isDirty, isEntityType, isEqual, isModified, isSame, replace, resolve, resolve, semiResolve
-
-
-
-
Constructor Detail
-
CustomType
public CustomType(UserType userType) throws MappingException
- Throws:
MappingException
-
CustomType
public CustomType(UserType userType, java.lang.String[] registrationKeys) throws MappingException
- Throws:
MappingException
-
-
Method Detail
-
getUserType
public UserType getUserType()
-
getRegistrationKeys
public java.lang.String[] getRegistrationKeys()
Description copied from interface:BasicType
Get the names under which this type should be registered in the type registry.- Specified by:
getRegistrationKeys
in interfaceBasicType
- Returns:
- The keys under which to register this type.
-
sqlTypes
public int[] sqlTypes(Mapping pi)
Description copied from interface:Type
Return the JDBC types codes (perTypes
) for the columns mapped by this type. NOTE: The number of elements in this array matches the return fromType.getColumnSpan(org.hibernate.engine.spi.Mapping)
.
-
dictatedSizes
public Size[] dictatedSizes(Mapping mapping) throws MappingException
Description copied from interface:Type
Return the column sizes dictated by this type. For example, the mapping for achar
/Character
would have a dictated length limit of 1; for a string-basedUUID
would have a size limit of 36; etc. NOTE: The number of elements in this array matches the return fromType.getColumnSpan(org.hibernate.engine.spi.Mapping)
.- Specified by:
dictatedSizes
in interfaceType
- Parameters:
mapping
- The mapping object :/- Returns:
- The dictated sizes.
- Throws:
MappingException
- Generally indicates an issue accessing the passed mapping object.
-
defaultSizes
public Size[] defaultSizes(Mapping mapping) throws MappingException
Description copied from interface:Type
Defines the column sizes to use according to this type if the user did not explicitly say (and if noType.dictatedSizes(org.hibernate.engine.spi.Mapping)
were given). NOTE: The number of elements in this array matches the return fromType.getColumnSpan(org.hibernate.engine.spi.Mapping)
.- Specified by:
defaultSizes
in interfaceType
- Parameters:
mapping
- The mapping object :/- Returns:
- The default sizes.
- Throws:
MappingException
- Generally indicates an issue accessing the passed mapping object.
-
getColumnSpan
public int getColumnSpan(Mapping session)
Description copied from interface:Type
How many columns are used to persist this type. Always the same assqlTypes(mapping).length
- Specified by:
getColumnSpan
in interfaceType
- Parameters:
session
- The mapping object :/- Returns:
- The number of columns
-
getReturnedClass
public java.lang.Class getReturnedClass()
Description copied from interface:Type
The class returned byType.nullSafeGet(java.sql.ResultSet, java.lang.String[], org.hibernate.engine.spi.SharedSessionContractImplementor, java.lang.Object)
methods. This is used to establish the class of an array of this type.- Specified by:
getReturnedClass
in interfaceType
- Returns:
- The java type class handled by this type.
-
isEqual
public boolean isEqual(java.lang.Object x, java.lang.Object y) throws HibernateException
Description copied from interface:Type
Compare two instances of the class mapped by this type for persistence "equality" (equality of persistent state). This should always equate to some form of comparison of the value's internal state. As an example, for something like a date the comparison should be based on its internal "time" state based on the specific portion it is meant to represent (timestamp, date, time).- Specified by:
isEqual
in interfaceType
- Overrides:
isEqual
in classAbstractType
- 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
-
getHashCode
public int getHashCode(java.lang.Object x)
Description copied from interface:Type
Get a hash code, consistent with persistence "equality". Again for most types the normal usage is to delegate to the value'shashCode
.- Specified by:
getHashCode
in interfaceType
- Overrides:
getHashCode
in classAbstractType
- Parameters:
x
- The value for which to retrieve a hash code- Returns:
- The hash code
-
nullSafeGet
public java.lang.Object nullSafeGet(java.sql.ResultSet rs, java.lang.String[] names, SharedSessionContractImplementor session, java.lang.Object owner) throws java.sql.SQLException
Description copied from interface:Type
Extract a value of themapped class
from the JDBC result set. Implementors should handle possibility of null values.- Specified by:
nullSafeGet
in interfaceType
- Parameters:
rs
- The result set from which to extract value.names
- the column names making up this type value (use to read from result set)session
- The originating sessionowner
- the parent entity- Returns:
- The extracted value
- Throws:
java.sql.SQLException
- An error from the JDBC driver- See Also:
alternative, 2-phase property initialization
-
nullSafeGet
public java.lang.Object nullSafeGet(java.sql.ResultSet rs, java.lang.String columnName, SharedSessionContractImplementor session, java.lang.Object owner) throws java.sql.SQLException
Description copied from interface:Type
Extract a value of themapped class
from the JDBC result set. Implementors should handle possibility of null values. This form might be called if the type is known to be a single-column type.- Specified by:
nullSafeGet
in interfaceType
- Parameters:
rs
- The result set from which to extract value.columnName
- the column name making up this type value (use to read from result set)session
- The originating sessionowner
- the parent entity- Returns:
- The extracted value
- Throws:
java.sql.SQLException
- An error from the JDBC driver
-
assemble
public java.lang.Object assemble(java.io.Serializable cached, SharedSessionContractImplementor session, java.lang.Object owner)
Description copied from interface:Type
Reconstruct the object from its disassembled state. This method is the reciprocal ofType.disassemble(java.lang.Object, org.hibernate.engine.spi.SharedSessionContractImplementor, java.lang.Object)
- Specified by:
assemble
in interfaceType
- Overrides:
assemble
in classAbstractType
- Parameters:
cached
- the disassembled state from the cachesession
- the originating sessionowner
- the parent entity object- Returns:
- the (re)assembled object
-
disassemble
public java.io.Serializable disassemble(java.lang.Object value, SharedSessionContractImplementor session, java.lang.Object owner)
Description copied from interface:Type
Return a disassembled representation of the object. This is the value Hibernate will use in second level caching, so care should be taken to break values down to their simplest forms; for entities especially, this means breaking them down into their constituent parts.- Specified by:
disassemble
in interfaceType
- Overrides:
disassemble
in classAbstractType
- Parameters:
value
- the value to cachesession
- the originating sessionowner
- optional parent entity object (needed for collections)- Returns:
- the disassembled, deep cloned state
-
replace
public java.lang.Object replace(java.lang.Object original, java.lang.Object target, SharedSessionContractImplementor session, java.lang.Object owner, java.util.Map copyCache) throws HibernateException
Description copied from interface:Type
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.- Specified by:
replace
in interfaceType
- 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
-
nullSafeSet
public void nullSafeSet(java.sql.PreparedStatement st, java.lang.Object value, int index, boolean[] settable, SharedSessionContractImplementor session) throws java.sql.SQLException
Description copied from interface:Type
Bind a value represented by an instance of themapped class
to the JDBC prepared statement, 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.- Specified by:
nullSafeSet
in interfaceType
- 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:
java.sql.SQLException
- An error from the JDBC driver
-
nullSafeSet
public void nullSafeSet(java.sql.PreparedStatement st, java.lang.Object value, int index, SharedSessionContractImplementor session) throws java.sql.SQLException
Description copied from interface:Type
Bind a value represented by an instance of themapped class
to the JDBC prepared statement. Implementors should handle possibility of null values. A multi-column type should bind parameters starting from index.- Specified by:
nullSafeSet
in interfaceType
- Parameters:
st
- The JDBC prepared statement to which to bindvalue
- the object to writeindex
- starting parameter bind indexsession
- The originating session- Throws:
java.sql.SQLException
- An error from the JDBC driver
-
toXMLString
public java.lang.String toXMLString(java.lang.Object value, SessionFactoryImplementor factory)
-
fromXMLString
public java.lang.Object fromXMLString(java.lang.String xml, Mapping factory)
-
getName
public java.lang.String getName()
Description copied from interface:Type
Returns the abbreviated name of the type.
-
deepCopy
public java.lang.Object deepCopy(java.lang.Object value, SessionFactoryImplementor factory) throws HibernateException
Description copied from interface:Type
Return a deep copy of the persistent state, stopping at entities and at collections.- Specified by:
deepCopy
in interfaceType
- Parameters:
value
- The value to be copiedfactory
- The session factory- Returns:
- The deep copy
- Throws:
HibernateException
- An error from Hibernate
-
isMutable
public boolean isMutable()
Description copied from interface:Type
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.)
-
stringToObject
public java.lang.Object stringToObject(java.lang.String xml)
Description copied from interface:IdentifierType
Convert the value from the mapping file to a Java object.- Specified by:
stringToObject
in interfaceIdentifierType
- Parameters:
xml
- the value of discriminator-value or unsaved-value attribute- Returns:
- The converted value of the string representation.
-
objectToSQLString
public java.lang.String objectToSQLString(java.lang.Object value, Dialect dialect) throws java.lang.Exception
Description copied from interface:LiteralType
Convert the value into a string representation, suitable for embedding in an SQL statement as a literal.- Specified by:
objectToSQLString
in interfaceLiteralType
- Parameters:
value
- The value to convertdialect
- The SQL dialect- Returns:
- The value's string representation
- Throws:
java.lang.Exception
- Indicates an issue converting the value to literal string.
-
getComparator
public java.util.Comparator getComparator()
Description copied from interface:VersionType
Get a comparator for version values.- Specified by:
getComparator
in interfaceVersionType
- Returns:
- The comparator to use to compare different version values.
-
next
public java.lang.Object next(java.lang.Object current, SharedSessionContractImplementor session)
Description copied from interface:VersionType
Increment the version.- Specified by:
next
in interfaceVersionType
- Parameters:
current
- the current versionsession
- The session from which this request originates.- Returns:
- an instance of the type
-
seed
public java.lang.Object seed(SharedSessionContractImplementor session)
Description copied from interface:VersionType
Generate an initial version.- Specified by:
seed
in interfaceVersionType
- Parameters:
session
- The session from which this request originates.- Returns:
- an instance of the type
-
toLoggableString
public java.lang.String toLoggableString(java.lang.Object value, SessionFactoryImplementor factory) throws HibernateException
Description copied from interface:Type
Generate a representation of the value for logging purposes.- Specified by:
toLoggableString
in interfaceType
- Parameters:
value
- The value to be loggedfactory
- The session factory- Returns:
- The loggable representation
- Throws:
HibernateException
- An error from Hibernate
-
toColumnNullness
public boolean[] toColumnNullness(java.lang.Object value, Mapping mapping)
Description copied from interface:Type
Given an instance of the type, return an array of boolean, indicating which mapped columns would be null.- Specified by:
toColumnNullness
in interfaceType
- Parameters:
value
- an instance of the typemapping
- The mapping abstraction- Returns:
- array indicating column nullness for a value instance
-
isDirty
public boolean isDirty(java.lang.Object old, java.lang.Object current, boolean[] checkable, SharedSessionContractImplementor session) throws HibernateException
Description copied from interface:Type
Should the parent be considered dirty, given both the old and current value?- Specified by:
isDirty
in interfaceType
- Parameters:
old
- the old valuecurrent
- 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
-
toString
public java.lang.String toString(java.lang.Object value) throws HibernateException
Description copied from interface:StringRepresentableType
Render the value to the string representation.- Specified by:
toString
in interfaceStringRepresentableType
- Parameters:
value
- The value to render to string.- Returns:
- The string representation
- Throws:
HibernateException
- Problem rendering
-
fromStringValue
public java.lang.Object fromStringValue(java.lang.String string) throws HibernateException
Description copied from interface:StringRepresentableType
Consume the given string representation back into this types java form.- Specified by:
fromStringValue
in interfaceStringRepresentableType
- Parameters:
string
- The string representation to be consumed.- Returns:
- The java type representation
- Throws:
HibernateException
- Problem consuming
-
canDoSetting
public boolean canDoSetting()
Description copied from interface:ProcedureParameterNamedBinder
Can the given instance of this type actually set the parameter value by name- Specified by:
canDoSetting
in interfaceProcedureParameterNamedBinder
- Returns:
true
indicates that @{link #nullSafeSet} calls will not fail
-
nullSafeSet
public void nullSafeSet(java.sql.CallableStatement statement, java.lang.Object value, java.lang.String name, SharedSessionContractImplementor session) throws java.sql.SQLException
Description copied from interface:ProcedureParameterNamedBinder
Bind a value to the JDBC prepared statement, ignoring some columns as dictated by the 'settable' parameter. Implementors should handle the possibility of null values. Does not support multi-column type- Specified by:
nullSafeSet
in interfaceProcedureParameterNamedBinder
- Parameters:
statement
- The CallableStatement to which to bindvalue
- the object to writename
- parameter bind namesession
- The originating session- Throws:
java.sql.SQLException
- An error from the JDBC driver
-
canDoExtraction
public boolean canDoExtraction()
Description copied from interface:ProcedureParameterExtractionAware
Can the given instance of this type actually perform the parameter value extractions?- Specified by:
canDoExtraction
in interfaceProcedureParameterExtractionAware
- Returns:
true
indicates that @{link #extract} calls will not fail due toIllegalStateException
.
-
extract
public java.lang.Object extract(java.sql.CallableStatement statement, int startIndex, SharedSessionContractImplementor session) throws java.sql.SQLException
Description copied from interface:ProcedureParameterExtractionAware
Perform the extraction- Specified by:
extract
in interfaceProcedureParameterExtractionAware
- Parameters:
statement
- The CallableStatement from which to extract the parameter value(s).startIndex
- The parameter index from which to start extracting; assumes the values (if multiple) are contiguoussession
- The originating session- Returns:
- The extracted value.
- Throws:
java.sql.SQLException
- Indicates an issue calling into the CallableStatement
-
extract
public java.lang.Object extract(java.sql.CallableStatement statement, java.lang.String[] paramNames, SharedSessionContractImplementor session) throws java.sql.SQLException
Description copied from interface:ProcedureParameterExtractionAware
Perform the extraction- Specified by:
extract
in interfaceProcedureParameterExtractionAware
- Parameters:
statement
- The CallableStatement from which to extract the parameter value(s).paramNames
- The parameter names.session
- The originating session- Returns:
- The extracted value.
- Throws:
java.sql.SQLException
- Indicates an issue calling into the CallableStatement
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
-