因为CountryCode是从其它系统导过来的,有时出现了空格,所以自定义了一个TrimmedString
<hibernate-mapping>
<class name="au.com.at2.dictionaries.country.Country" table="COUNTRY" lazy="false">
<id name="countryCode" column="CountryCode" type="au.com.at2.hibernate.TrimmedString " />
<property name="countryName" column="CountryName"/>
<property name="internationalPrefixCode" column="InternationalPrefixCode"/>
</class>
<query name="Country.findAll" cacheable="true"
cache-region="/au/com/at2/dictionaries/AllCountries">
from Country c order by c.countryName
</query>
</hibernate-mapping>
public class TrimmedString implements UserType {
public TrimmedString() {
super();
}
public int[] sqlTypes() {
return new int[] { Types.CHAR };
}
public Class returnedClass() {
return String.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
return (x == y) || (x != null && y != null && (x.equals(y)));
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
String val = rs.getString(names[0]);
return val != null ? val.trim() : null;
}
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
String val = (String) value;
st.setString(index, val != null ? val.trim() : null);
}
public Object deepCopy(Object value) throws HibernateException {
if (value == null)
return null;
return new String((String) value);
}
public boolean isMutable() {
return false;
}
public Object assemble(Serializable serializable, Object object)
throws HibernateException {
return serializable;
}
public Serializable disassemble(Object object) throws HibernateException {
return (Serializable) object;
}
public int hashCode(Object object) throws HibernateException {
return object.hashCode();
}
public Object replace(Object object, Object object1, Object object2)
throws HibernateException {
return object;
}