We Recommend

Java How to Program Java How to Program
Takes a new tools-based approach to Web application development that uses Netbeans 5.5 and Java Studio Creator 2 to create and consume Web Services. Features new AJAX-enabled, Web applications built with JavaServer Faces (JSF), Java Studio Creator 2 and the Java Blueprints AJAX Components. Includes new topics throughout, such as JDBC 4, SwingWorker for multithreaded GUIs, GroupLayout, Java Desktop Integration Components (JDIC), and much more.


Posted By

narkisr on 03/12/08


Tagged

java mapping enum Hibernate


Versions (?)


Java 5 Enum HIbernate mapping


Published in: Java 


This is an example of how to map a Java 5 enum when using Hibernate ORM.


  1. import org.hibernate.HibernateException;
  2. import org.hibernate.usertype.ParameterizedType;
  3. import org.hibernate.usertype.UserType;
  4. import java.io.Serializable;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Types;
  9. import java.util.Properties;
  10.  
  11. /**
  12. * This class implements the Type that will be used in the hbm file, notice that
  13. * the JavaEnumToString & StringToJavaEnum implementation isn't provided
  14. * (all they do is to convert Enum<->String in a consistent way, same goes for
  15. * StringUtils (replaceable with apache commons StringUtils).
  16. */
  17. public class JavaEnumUserType implements UserType, ParameterizedType {
  18.  
  19. private static final int[] SQL_TYPE = new int[]{Types.VARCHAR};
  20.  
  21. private Class<? extends Enum> enumClass;
  22.  
  23.  
  24. private static JavaEnumToString javaEnumToString = new JavaEnumToString();
  25.  
  26. private static StringToJavaEnum stringToJavaEnum = new StringToJavaEnum();
  27.  
  28. public void setParameterValues(Properties parameters) {
  29. String enumClassName = parameters.getProperty("enumClass");
  30. try {
  31. enumClass = Class.forName(enumClassName).asSubclass(Enum.class);
  32. } catch (ClassNotFoundException e) {
  33. throw new HibernateException("Class " + enumClassName + " not found ", e);
  34. }
  35. }
  36.  
  37. public Object assemble(Serializable cached, Object owner)
  38. throws HibernateException {
  39. return cached;
  40. }
  41.  
  42. public Object deepCopy(Object value) throws HibernateException {
  43. return value;
  44. }
  45.  
  46. public Serializable disassemble(Object value) throws HibernateException {
  47. return (Enum) value;
  48. }
  49.  
  50. public boolean equals(Object x, Object y) throws HibernateException {
  51. return x == y;
  52. }
  53.  
  54. public int hashCode(Object x) throws HibernateException {
  55. return x.hashCode();
  56. }
  57.  
  58. public boolean isMutable() {
  59. return false;
  60. }
  61.  
  62. public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
  63. String value = rs.getString(names[0]);
  64. if (rs.wasNull() || StringUtils.isNullOrEmpty(value)) {
  65. return null;
  66. }
  67. return stringToJavaEnum.doConvert(value.trim(), enumClass);
  68. }
  69.  
  70. public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
  71. if (value == null) {
  72. st.setNull(index, Types.VARCHAR);
  73. } else {
  74. String stringValue = javaEnumToString.doConvert((Enum) value);
  75. st.setString(index, stringValue);
  76. }
  77. }
  78.  
  79. public Object replace(Object original, Object target, Object owner)
  80. throws HibernateException {
  81. return original;
  82. }
  83.  
  84. public Class returnedClass() {
  85. return enumClass;
  86. }
  87.  
  88. public int[] sqlTypes() {
  89. return SQL_TYPE;
  90. }
  91.  
  92. }
  93.  
  94. // For example the following bean:
  95.  
  96. public class DummyBean{
  97.  
  98. private DummyEnum dummyProp;
  99.  
  100. public DummyEnum getDummyProp() {
  101. return dummyProp;
  102. }
  103.  
  104. public void setPageDescriptor(DummyEnum _dummyProp) {
  105. this.dummyProp = _dummyProp;
  106. }
  107.  
  108. }
  109.  
  110. // will be mapped in this fashion:
  111.  
  112. <hibernate-mapping>
  113. <class name="com.dummy.DummyBean" table="DUMMY_TABLE">
  114.  
  115. <property name="dummyProp" column="DUMMY_PROP">
  116. <type name="com.dummy.JavaEnumUserType">
  117. <param name="enumClass">com.dummy.DummyEnum</param>
  118. </type>
  119. </property>
  120. </hibernate-mapping>

Report this snippet 

You need to login to post a comment.