HIBERNATE JBoss.org
 |  Register  | 
     
News 
About 
   Feature List 
   Road Map 
Documentation 
   Related Projects 
   External Documentation 
Download 
Forum & Mailinglists 
Support & Training 
JIRA Issue Tracking
Wiki Community Area


Hibernate Public Training Courses


Get Hibernate in Action eBook!


JavaWorld 2003 Finalist


Jolt Award 2004 Winner
      
Documentation > Community Area > UserType to persist Boolean as CHAR

UserType to persist Boolean as CHAR

We store our Booleans as Y/N in Oracle (common practice in my experience). Below is a user type that will do the conversion for you so you can keep your POJO's as true business objects:

public class CustomBooleanType implements UserType {

    public CustomBooleanType() {

    }

    public int[] sqlTypes() {

        return new int[] { Types.CHAR };

    }

    public Class returnedClass() {

        return Boolean.class;

    }

    public boolean equals(Object x, Object y) throws HibernateException {

        return (x == y) || (x != null && y != null && (x.equals(y)));

    }

    public Object nullSafeGet(ResultSet inResultSet, String[] names, Object o)

                                            throws HibernateException, SQLException {

        String val = (String)Hibernate.STRING.nullSafeGet(inResultSet, names[0]);

        return BooleanUtil.parseYN(val);

    }

    public void nullSafeSet(PreparedStatement inPreparedStatement, Object o, int i)

                                            throws HibernateException, SQLException {

        String val = BooleanUtil.toYNString((Boolean)o);

        inPreparedStatement.setString(i, val);

    }

    public Object deepCopy(Object o) throws HibernateException {

        if (o==null) return null;

        return new Boolean(((Boolean)o).booleanValue());

    }

    public boolean isMutable() {

        return false;

    }

}

      

coWiki