IntBooleanType.java

  1. package org.itracker.persistence.dao;

  2. import org.hibernate.HibernateException;
  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. /**
  10.  * A Hibernate <code>UserType</code> to map a Java boolean or java.lang.Boolean
  11.  * property to an SQL column of type INT with the values 0 (= false) or 1 (= true).
  12.  * <p/>
  13.  * <p>This is necessary because of the legacy ITracker 2.x database schema
  14.  * which uses INT instead of BOOLEAN or BIT, which aren't supported by all
  15.  * databases (e.g. DB2). </p>
  16.  *
  17.  * @author johnny
  18.  */
  19. public final class IntBooleanType implements UserType {

  20.     private static final int[] SQL_TYPES = {Types.INTEGER};

  21.     /**
  22.      * Default constructor, required by Hibernate.
  23.      */
  24.     public IntBooleanType() {
  25.     }

  26.     public void nullSafeSet(PreparedStatement statement, Object value, int index)
  27.             throws HibernateException, SQLException {
  28.         if (value == null) {
  29.             statement.setNull(index, Types.INTEGER);
  30.         } else {
  31.             final int intValue = ((Boolean) value).booleanValue() ? 1 : 0;
  32.             statement.setInt(index, intValue);
  33.         }
  34.     }

  35.     public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
  36.             throws HibernateException, SQLException {
  37.         final int value = resultSet.getInt(names[0]);
  38.         return resultSet.wasNull() ? null : (value == 1);
  39.     }

  40.     public int hashCode(Object x) throws HibernateException {
  41.         /* Apparently, there's no need to check for nulls here. */
  42.         assert (x != null);

  43.         return x.hashCode();
  44.     }

  45.     public Serializable disassemble(Object value) throws HibernateException {
  46.         return (Serializable) value;
  47.     }

  48.     public Object deepCopy(Object value) throws HibernateException {
  49.         return value;
  50.     }

  51.     public int[] sqlTypes() {
  52.         return SQL_TYPES;
  53.     }

  54.     public Class<Boolean> returnedClass() {
  55.         return Boolean.class;
  56.     }

  57.     public Object replace(Object original, Object target, Object owner) throws HibernateException {
  58.         return original;
  59.     }

  60.     public boolean isMutable() {
  61.         return false;
  62.     }

  63.     /**
  64.      * Implements persistence equality.
  65.      */
  66.     public boolean equals(Object x, Object y) throws HibernateException {
  67.         if (x == y) {
  68.             return true;
  69.         }

  70.         if (x == null || y == null) {
  71.             return false; // test (x == y) has been done before and was false.
  72.         }
  73.         return x.equals(y);
  74.     }

  75.     public Object assemble(Serializable cached, Object owner) throws HibernateException {
  76.         return cached;
  77.     }

  78. }