Showing posts with label hibernate. Show all posts
Showing posts with label hibernate. Show all posts

Hibernate GenericEnumUserType

Hibernate usertype for getting/setting a custom value instead of using @Enumerated(EnumType.STRING) or @Enumerated(EnumType.Ordinal)


UserType source:

package example.usertype;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.type.NullableType;
import org.hibernate.type.TypeFactory;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;

/**
* generic enum user type based on: http://www.hibernate.org/272.html
*
* @param enumClass
* @param identifierMethod
* @param valueOfMethod
*/
public class GenericEnumUserType implements UserType, ParameterizedType {
private static final String DEFAULT_IDENTIFIER_METHOD_NAME = "name";
private static final String DEFAULT_VALUE_OF_METHOD_NAME = "valueOf";

@SuppressWarnings("unchecked")
private Class<? extends Enum> enumClass;
private Class<?> identifierType;
private Method identifierMethod;
private Method valueOfMethod;
private NullableType type;
private int[] sqlTypes;

public void setParameterValues(Properties parameters) {
String enumClassName = parameters.getProperty("enumClass");
try {
enumClass = Class.forName(enumClassName).asSubclass(Enum.class);
} catch (ClassNotFoundException cfne) {
throw new HibernateException("Enum class not found", cfne);
}

String identifierMethodName = parameters.getProperty(
"identifierMethod", DEFAULT_IDENTIFIER_METHOD_NAME);

try {
identifierMethod = enumClass.getMethod(identifierMethodName,
new Class[0]);
identifierType = identifierMethod.getReturnType();
} catch (Exception e) {
throw new HibernateException("Failed to obtain identifier method",
e);
}

type = (NullableType) TypeFactory.basic(identifierType.getName());

if (type == null)
throw new HibernateException("Unsupported identifier type "
+ identifierType.getName());

sqlTypes = new int[] { type.sqlType() };

String valueOfMethodName = parameters.getProperty("valueOfMethod",
DEFAULT_VALUE_OF_METHOD_NAME);

try {
valueOfMethod = enumClass.getMethod(valueOfMethodName,
new Class[] { identifierType });
} catch (Exception e) {
throw new HibernateException("Failed to obtain valueOf method", e);
}
}

@SuppressWarnings("unchecked")
public Class returnedClass() {
return enumClass;
}

public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
Object identifier = type.get(rs, names[0]);
if (identifier == null) {
return null;
}

// fix for fixed length char datatype (db2)
if (identifier instanceof String) {
identifier = ((String) identifier).trim();
}

Object value;
try {
value = valueOfMethod
.invoke(enumClass, new Object[] { identifier });
} catch (Exception e) {
throw new HibernateException(
"Exception while invoking valueOf method '"
+ valueOfMethod.getName() + "' of "
+ "enumeration class '" + enumClass + "'", e);
}

if (value == null) {
throw new HibernateException(
"Impossible to translate identifier-value '" + identifier
+ "' to enum of type '" + enumClass + "'");
}

return value;

}

public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {

if (value == null) {
st.setNull(index, type.sqlType());
} else {
Object identifier;
try {
identifier = identifierMethod.invoke(value, new Object[0]);
} catch (Exception e) {
throw new HibernateException(
"Exception while invoking identifierMethod '"
+ identifierMethod.getName() + "' of "
+ "enumeration class '" + enumClass + "'", e);
}

if (identifier == null) {
throw new HibernateException(
"Disallowed to update/insert null identifier-value for non-null enum-value of type '"
+ enumClass + "'");
}

type.set(st, identifier, index);
}

}

public int[] sqlTypes() {
return sqlTypes;
}

public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return cached;
}

public Object deepCopy(Object value) throws HibernateException {
return value;
}

public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}

public boolean equals(Object x, Object y) throws HibernateException {
return x == y;
}

public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}

public boolean isMutable() {
return false;
}

public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original;
}
}


Enum source:

package example.model;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

public enum Direction {

INCOMING('I'), //
OUTGOING('O'); //

private static final Map<Character, Direction> idMap = new HashMap<Character, Direction>();

static {
for (Direction direction : EnumSet.allOf(Direction.class)) {
idMap.put(direction.getId(), direction);
}
}

private final Character id;

private Direction(Character id) {
this.id = id;
}

public static Direction valueById(Character id) {
return idMap.get(id);
}

public Character getId() {
return id;
}

}


Usage:

@Type(type = "example.usertype.GenericEnumUserType", parameters = {
@Parameter(name = "enumClass", value = "example.model.Direction"),
@Parameter(name = "identifierMethod", value = "getId"),
@Parameter(name = "valueOfMethod", value = "valueById") })
@Column(name = "DIRECTION", nullable = false, length = 1)
public Direction getDirection() {
return this.direction;
}

public void setDirection(Direction direction) {
this.direction = direction;
}

Hibernate StringTrimUserType

Use for fixed length column definitions. (e.g. DB2 'char' type)

Source:

package example.usertype;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.Hibernate;
import org.hibernate.usertype.UserType;

/**
*
* based on www.hibernate.org/388.html
*/

public class StringTrimUserType implements UserType {

/**
*
* default constructor
*/

public StringTrimUserType() {
}

/**
*
* @see org.hibernate.usertype.UserType#sqlTypes()
*/

public int[] sqlTypes() {
return new int[] { Types.CHAR };
}

/**
*
* @see org.hibernate.usertype.UserType#returnedClass()
*/

@SuppressWarnings("unchecked")
public Class returnedClass() {
return String.class;
}

/**
*
* @see org.hibernate.usertype.UserType#equals(java.lang.Object,
* java.lang.Object)
*/

public boolean equals(Object x, Object y) {
return (x == y) || (x != null && y != null && (x.equals(y)));
}

/**
*
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
* java.lang.String[], java.lang.Object)
*/

public Object nullSafeGet(ResultSet inResultSet, String[] names, Object o) throws SQLException {

String val = (String) Hibernate.STRING.nullSafeGet(inResultSet, names[0]);
return val == null ? null : val.trim();
}

/**
*
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
* java.lang.Object, int)
*/

public void nullSafeSet(PreparedStatement inPreparedStatement, Object o,
int i)

throws SQLException {

String val = (String) o;
inPreparedStatement.setString(i, val);
}

/**
*
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/

public Object deepCopy(Object o) {
if (o == null) {
return null;
}
return new String(((String) o));
}

/**
*
* @see org.hibernate.usertype.UserType#isMutable()
*/

public boolean isMutable() {
return false;
}

/**
*
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,
* java.lang.Object)
*/

public Object assemble(Serializable cached, Object owner) {
return cached;
}

/**
*
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/

public Serializable disassemble(Object value) {
return (Serializable) value;
}

/**
*
* @see org.hibernate.usertype.UserType#replace(java.lang.Object,
* java.lang.Object, java.lang.Object)
*/

public Object replace(Object original, Object target, Object owner) {
return original;
}

/**
*
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/

public int hashCode(Object x) {
return x.hashCode();
}

}



Usage:

@Column(name = "CMODULE", unique = true, nullable = false, length = 10, columnDefinition = "char")
@Type(type = "example.usertype.StringTrimUserType")
public String getModuleCode() {
return this.moduleCode;
}

public void setModuleCode(String moduleId) {
this.moduleCode = moduleId;
}