Hello,
I am new to UserTypes because I am migrating from PersistentEnum's (in Hibernate version 2.1.4 with MySQL 4.0.17). I have created a UserType which is shown below. The problem is that if I modify ONLY the UserType property and call saveOrUpdate, the new value will not get saved (but no exception is thrown). But, if I modify the UserType property AND another property as well and call saveOrUpdate, both fields will be saved. Can anyone explain what is going on? Thank you very much for your help.
Joe Hudson
------------------------------------------
// UserType
public class MessageStatusUserType implements UserType {
public Object deepCopy(Object value) throws HibernateException {
if (null == value) return null;
else return MessageStatus.getInstance(((MessageStatus) value).toString());
}
public boolean equals(Object x, Object y) throws HibernateException {
if (null == x || null == y) return false;
else if (x instanceof MessageStatus && y instanceof MessageStatus) {
return x.equals(y);
}
else return false;
}
public boolean isMutable() {
return true;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
return MessageStatus.getInstance(rs.getString(names[0]));
}
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
if (null == value) st.setNull(index, Types.VARCHAR);
else st.setString(index, ((MessageStatus) value).toString());
}
public Class returnedClass() {
return MessageStatus.class;
}
public int[] sqlTypes() {
return new int[] {Types.VARCHAR};
}
}
// Object
public class MessageStatus implements Serializable {
public static final MessageStatus NEW = new MessageStatus("N");
public static final MessageStatus READ = new MessageStatus("R");
private String value;
protected MessageStatus(String value) {
this.value = value;
}
public static MessageStatus getInstance (String value) {
if (value.equals("N")) return NEW;
else if (value.equals("R")) return READ;
else return null;
}
public String getDisplayValue () {
if (value.equals("N")) return "New";
else if (value.equals("R")) return "Read";
else return "Unknown";
}
public String toString() {
return value;
}
public boolean equals (Object obj) {
if (null == obj || (!(obj instanceof MessageStatus))) return false;
else return ((MessageStatus) obj).toString().equals(obj.toString());
}
}
|