Mapping documents:
<property
name="password"
type="blah.MD5sumType"
column="PASSWD"
/>
Code:
package blah;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;
public class MD5sumType implements UserType {
public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
}
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() : "";
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
String passwd = (String)value;
// **********************************
// generate md5sum for passwd
// ************************************
st.setString(index, passwd);
}
public Object deepCopy(Object value) throws HibernateException {
if (value == null) return null;
return new String((String)value);
}
public boolean isMutable() {
return true;
}
}