1 package de.test.schemaexport.domain;
3 import org.hibernate.HibernateException;
4 import org.hibernate.engine.spi.SessionImplementor;
5 import org.hibernate.usertype.EnhancedUserType;
6 import org.hibernate.usertype.ParameterizedType;
8 import java.io.Serializable;
9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import java.sql.Types;
13 import java.util.Properties;
16 * This Hibernate UserType is used to map JDK 5 Enums directly in a column.
17 * Define this UserType in your hbm.xml mapping file like the following example:
19 public class EnumUserType implements EnhancedUserType, ParameterizedType {
21 private Class<Enum> enumClass;
24 public void setParameterValues(Properties parameters) {
25 String enumClassName = parameters.getProperty("enumClassName");
27 enumClass = (Class<Enum>) Class.forName(enumClassName);
28 } catch (ClassNotFoundException cnfe) {
29 throw new HibernateException("Enum class not found", cnfe);
34 public Object assemble(Serializable cached, Object owner) throws HibernateException {
39 public Object deepCopy(Object value) throws HibernateException {
44 public Serializable disassemble(Object value) throws HibernateException {
49 public boolean equals(Object x, Object y) throws HibernateException {
54 public int hashCode(Object x) throws HibernateException {
59 public boolean isMutable() {
64 public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor sessionImplementor, Object owner)
65 throws HibernateException, SQLException {
66 String name = rs.getString(names[0]);
67 return rs.wasNull() ? null : Enum.valueOf(enumClass, name);
71 public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor sessionImplementor)
72 throws HibernateException, SQLException {
74 st.setNull(index, Types.VARCHAR);
76 st.setString(index, ((Enum) value).name());
81 public Object replace(Object original, Object target, Object owner) throws HibernateException {
86 public Class returnedClass() {
91 public int[] sqlTypes() {
92 return new int[]{Types.VARCHAR};
96 public Object fromXMLString(String xmlValue) {
97 return Enum.valueOf(enumClass, xmlValue);
101 public String objectToSQLString(Object value) {
102 return '\'' + ((Enum) value).name() + '\'';
106 public String toXMLString(Object value) {
107 return ((Enum) value).name();