Class GenericEnumUserType
- java.lang.Object
-
- com.atlassian.confluence.impl.hibernate.GenericEnumUserType
-
- All Implemented Interfaces:
Serializable
,org.hibernate.usertype.EnhancedUserType
,org.hibernate.usertype.ParameterizedType
,org.hibernate.usertype.UserType
public class GenericEnumUserType extends Object implements org.hibernate.usertype.EnhancedUserType, org.hibernate.usertype.ParameterizedType, Serializable
This is a verbatim copy of the utility class from atlassian-hibernate5.2-extras, which should be removed if and when Confluence updates it's atlassian-hibernate-extras dependencyImplements a generic enum user type identified/represented by a single identifier/column.
- The enum type being represented by the certain user type must be set by using the 'enumClass' property.
- The identifier representing a enum value is retrieved by the identifierMethod. The name of the identifier method can be specified by the 'identifierMethod' property and by default the getId() method is used.
- The identifier type is automatically determined by the return-type of the identifierMethod.
- The valueOfMethod is the name of the static factory method returning the enumeration object being represented by the given identifier. The valueOfMethod's name can be specified by setting the 'valueOfMethod' property. The default valueOfMethod's name is 'fromId'.
Example of an enum type represented by an int value:
package com.atlassian.demo; public enum SimpleNumber { UNKNOWN(-1), ZERO(0), ONE(1), TWO(2), THREE(3); public int getId() { return value; } public SimpleNumber fromId(int value) { switch(value) { case 0: return ZERO; case 1: return ONE; case 2: return TWO; case 3: return THREE; default: return UNKNOWN; } } }
Using JPA, the mapping would look like this:
@Type(type = "com.atlassian.stash.internal.hibernate.GenericEnumUserType", parameters = { @Parameter(name = "enumClass", value = "com.atlassian.demo.SimpleNumber"), @Parameter(name = "identifierMethod", value = "getId"), @Parameter(name = "valueOfMethod", value = "fromId")}) private SimpleNumber randomNumber;
In this example, the properties for the
GenericEnumUserType
are fully specified to make the example more clear on the type's usage. However, because the identifier and valueOf methods follow standard naming, properties for them may be omitted from the mapping to reduce configuration.- Since:
- 6.1
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description GenericEnumUserType()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
assemble(Serializable cached, Object owner)
Object
deepCopy(Object value)
Serializable
disassemble(Object value)
boolean
equals(Object x, Object y)
Object
fromXMLString(String xmlValue)
int
hashCode(Object x)
boolean
isMutable()
Object
nullSafeGet(ResultSet rs, String[] names, org.hibernate.engine.spi.SharedSessionContractImplementor session, Object owner)
void
nullSafeSet(PreparedStatement st, Object value, int index, org.hibernate.engine.spi.SharedSessionContractImplementor session)
String
objectToSQLString(Object value)
Object
replace(Object original, Object target, Object owner)
Class
returnedClass()
void
setParameterValues(Properties parameters)
int[]
sqlTypes()
String
toXMLString(Object value)
-
-
-
Method Detail
-
assemble
public Object assemble(Serializable cached, Object owner) throws org.hibernate.HibernateException
- Specified by:
assemble
in interfaceorg.hibernate.usertype.UserType
- Throws:
org.hibernate.HibernateException
-
deepCopy
public Object deepCopy(Object value) throws org.hibernate.HibernateException
- Specified by:
deepCopy
in interfaceorg.hibernate.usertype.UserType
- Throws:
org.hibernate.HibernateException
-
disassemble
public Serializable disassemble(Object value) throws org.hibernate.HibernateException
- Specified by:
disassemble
in interfaceorg.hibernate.usertype.UserType
- Throws:
org.hibernate.HibernateException
-
equals
public boolean equals(Object x, Object y) throws org.hibernate.HibernateException
- Specified by:
equals
in interfaceorg.hibernate.usertype.UserType
- Throws:
org.hibernate.HibernateException
-
fromXMLString
public Object fromXMLString(String xmlValue)
- Specified by:
fromXMLString
in interfaceorg.hibernate.usertype.EnhancedUserType
-
hashCode
public int hashCode(Object x) throws org.hibernate.HibernateException
- Specified by:
hashCode
in interfaceorg.hibernate.usertype.UserType
- Throws:
org.hibernate.HibernateException
-
isMutable
public boolean isMutable()
- Specified by:
isMutable
in interfaceorg.hibernate.usertype.UserType
-
nullSafeGet
public Object nullSafeGet(ResultSet rs, String[] names, org.hibernate.engine.spi.SharedSessionContractImplementor session, Object owner) throws org.hibernate.HibernateException, SQLException
- Specified by:
nullSafeGet
in interfaceorg.hibernate.usertype.UserType
- Throws:
org.hibernate.HibernateException
SQLException
-
nullSafeSet
public void nullSafeSet(PreparedStatement st, Object value, int index, org.hibernate.engine.spi.SharedSessionContractImplementor session) throws org.hibernate.HibernateException, SQLException
- Specified by:
nullSafeSet
in interfaceorg.hibernate.usertype.UserType
- Throws:
org.hibernate.HibernateException
SQLException
-
objectToSQLString
public String objectToSQLString(Object value)
- Specified by:
objectToSQLString
in interfaceorg.hibernate.usertype.EnhancedUserType
-
replace
public Object replace(Object original, Object target, Object owner) throws org.hibernate.HibernateException
- Specified by:
replace
in interfaceorg.hibernate.usertype.UserType
- Throws:
org.hibernate.HibernateException
-
returnedClass
public Class returnedClass()
- Specified by:
returnedClass
in interfaceorg.hibernate.usertype.UserType
-
setParameterValues
public void setParameterValues(Properties parameters)
- Specified by:
setParameterValues
in interfaceorg.hibernate.usertype.ParameterizedType
-
sqlTypes
public int[] sqlTypes()
- Specified by:
sqlTypes
in interfaceorg.hibernate.usertype.UserType
-
-