View Javadoc

1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * HibernateEnum.java 
6    * 
7    * Created on Oct 25, 2004 at 5:27:38 PM
8    *
9    */
10  package ca.uhn.cache.internal.hibernate;
11  
12  import java.sql.PreparedStatement;
13  import java.sql.ResultSet;
14  import java.sql.SQLException;
15  import java.sql.Types;
16  
17  import net.sf.hibernate.HibernateException;
18  import net.sf.hibernate.UserType;
19  
20  import org.apache.commons.lang.enum.Enum;
21  import org.apache.commons.lang.enum.EnumUtils;
22  
23  /***
24   * <p>
25   * Used to allow Hibernate to persist Jakarta Commons Enums.
26   * </p>
27   * 
28   * Example usage:
29   * <pre>
30   * 
31   * public class Gender extends Enum {
32   *    ...
33   * }
34   * 
35   * public class HibernateGender extends HibernateEnumUserType {
36   *      public HibernateGender() {
37   *          super( Gender.class );
38   *      }
39   * }
40   * 
41   * public class Person {
42   *      ...
43   *      /**
44   *       * @hibernate.property class="HibernateGender"
45   *       *
46   *      public Gender getGender() {
47   *          return myGender;
48   *      }
49   * }
50   * </pre>
51   * @author <a href="mailto:alexei.guevara@uhn.on.ca"> </a>
52   */
53  public abstract class HibernateEnum implements UserType {
54      
55      private static final int[] SQL_TYPES = { Types.VARCHAR };
56      
57      private final Class myEnumType;
58      
59      protected HibernateEnum( Class theEnumType ) {
60          if ( ! Enum.class.isAssignableFrom(theEnumType) ) {
61              throw new IllegalArgumentException( "theEnumType must extend org.apache.commons.lang.enum.Enum" );
62          }
63          myEnumType = theEnumType;
64      }
65  
66      /***
67       * {@inheritDoc}
68       */
69      public int[] sqlTypes() {
70          return SQL_TYPES;
71      }
72  
73      /***
74       * {@inheritDoc}
75       */
76      public Class returnedClass() {
77          return this.getClass();
78      }
79  
80      /***
81       * {@inheritDoc}
82       */
83      public Object deepCopy(Object theValue) {
84          return theValue;
85      }
86  
87      /***
88       * {@inheritDoc}
89       */
90      public boolean isMutable() {
91          return false;
92      }
93  
94      /***
95       * {@inheritDoc}
96       */
97      public Object nullSafeGet(ResultSet theResultSet, String[] theNames, Object theOwner) 
98          throws HibernateException, SQLException {
99          
100         Object retVal;
101 
102         String name = theResultSet.getString(theNames[0]);
103         retVal = EnumUtils.getEnum( myEnumType, name );
104         
105         return retVal;
106     }
107 
108     /***
109      * {@inheritDoc}
110      */
111     public void nullSafeSet(PreparedStatement theStatement, Object theValue, int theIndex) 
112         throws HibernateException, SQLException {
113 
114         theStatement.setString( theIndex, ((Enum) theValue).getName() );
115     }
116 
117     /***
118      * {@inheritDoc}
119      */
120     public boolean equals(Object theX, Object theY) throws HibernateException {
121         return theX == theY;
122     }
123 }