View Javadoc
1   package org.itracker.persistence.dao;
2   
3   import org.hibernate.HibernateException;
4   import org.hibernate.usertype.UserType;
5   
6   import java.io.Serializable;
7   import java.sql.PreparedStatement;
8   import java.sql.ResultSet;
9   import java.sql.SQLException;
10  import java.sql.Types;
11  
12  /**
13   * A Hibernate <code>UserType</code> to map a Java boolean or java.lang.Boolean
14   * property to an SQL column of type INT with the values 0 (= false) or 1 (= true).
15   * <p/>
16   * <p>This is necessary because of the legacy ITracker 2.x database schema
17   * which uses INT instead of BOOLEAN or BIT, which aren't supported by all
18   * databases (e.g. DB2). </p>
19   *
20   * @author johnny
21   */
22  public final class IntBooleanType implements UserType {
23  
24      private static final int[] SQL_TYPES = {Types.INTEGER};
25  
26      /**
27       * Default constructor, required by Hibernate.
28       */
29      public IntBooleanType() {
30      }
31  
32      public void nullSafeSet(PreparedStatement statement, Object value, int index)
33              throws HibernateException, SQLException {
34          if (value == null) {
35              statement.setNull(index, Types.INTEGER);
36          } else {
37              final int intValue = ((Boolean) value).booleanValue() ? 1 : 0;
38              statement.setInt(index, intValue);
39          }
40      }
41  
42      public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
43              throws HibernateException, SQLException {
44          final int value = resultSet.getInt(names[0]);
45          return resultSet.wasNull() ? null : (value == 1);
46      }
47  
48      public int hashCode(Object x) throws HibernateException {
49          /* Apparently, there's no need to check for nulls here. */
50          assert (x != null);
51  
52          return x.hashCode();
53      }
54  
55      public Serializable disassemble(Object value) throws HibernateException {
56          return (Serializable) value;
57      }
58  
59      public Object deepCopy(Object value) throws HibernateException {
60          return value;
61      }
62  
63      public int[] sqlTypes() {
64          return SQL_TYPES;
65      }
66  
67      public Class<Boolean> returnedClass() {
68          return Boolean.class;
69      }
70  
71      public Object replace(Object original, Object target, Object owner) throws HibernateException {
72          return original;
73      }
74  
75      public boolean isMutable() {
76          return false;
77      }
78  
79      /**
80       * Implements persistence equality.
81       */
82      public boolean equals(Object x, Object y) throws HibernateException {
83          if (x == y) {
84              return true;
85          }
86  
87          if (x == null || y == null) {
88              return false; // test (x == y) has been done before and was false. 
89          }
90          return x.equals(y);
91      }
92  
93      public Object assemble(Serializable cached, Object owner) throws HibernateException {
94          return cached;
95      }
96  
97  }