I decided to implement UserType ... it is as close as possible to the sleep mode configuration, since I can get ... here 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, reasoning ... When hibernate inserts data, this type converts the string to uppercase. When hibernate selects the data, the same thing happens. The advantage of this class is that it just changed the bean get / set value to uppercase, and all this when I use the criteria for selection in serialNumber. Hibernate will also smooth out my parameter, as it will apply / apply the same type as in the table configuration.
So I don’t have to forget to manually write down all my search criteria for serial numbers ... hibernate will take care of this for me ... this is exactly what I am trying to achieve here!
I have a JUnit that demonstrates all this, but I think my answer is too big as it is ...
Tim reddy
source share