I decided to implement a UserType...it is as close to a hibernate configuration as I can get...here's the code...
package model;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
public class UpperCaseUserType implements UserType {
private static final int[] TYPES = {Types.VARCHAR};
public int[] sqlTypes() {
return TYPES;
}
public Class returnedClass() {
return String.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) {
return true;
}
if (null == x || null == y) {
return false;
}
return new EqualsBuilder().append(x, y).isEquals();
}
public int hashCode(Object o) throws HibernateException {
return new HashCodeBuilder().append(o).toHashCode();
}
public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
return ((String) Hibernate.STRING.nullSafeGet(resultSet, strings[0])).toUpperCase();
}
public void nullSafeSet(PreparedStatement preparedStatement, Object object, int i) throws HibernateException, SQLException {
String string = ((String) object).toUpperCase();
Hibernate.STRING.nullSafeSet(preparedStatement, string, i);
}
public Object deepCopy(Object o) throws HibernateException {
if (null == o) {
return null;
}
return new String(o.toString());
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object o) throws HibernateException {
return (String) o;
}
public Object assemble(Serializable serializable, Object o) throws HibernateException {
return serializable;
}
public Object replace(Object o, Object arg1, Object arg2) throws HibernateException {
return o;
}
}
Consider this property element
<property name="serialNumber" type="model.UpperCaseUserType">
<column name="SERIAL_NUMBER" length="20" not-null="true" unique="true" />
</property>
So the reasoning...As hibernate inserts the data, this type will convert the string to uppercase. As hibernate selects data, the same thing happens. The advantage this class has over just changing the bean's get/set to uppercase everything is when I use a Criteria to select on serialNumber. Hibernate will also uppercase my parameter as it will cast/apply the same type as defined in the table configuration.
Therefore, I don't need to remember to manually uppercase all of my search criteria for serial numbers...hibernate takes care of that for me...that's exactly what I'm trying to achieve here!
I have a JUnit that demonstrates all of this stuff, but I think my answer is way too big as it is...