I have a my own UserType that maps type to two varchar columsn in the database.
In my code I am trying to set a value of this type and save it to the database however this value is not saving. It does however save if create a new version of the type and then save it to the database.
Does not work :(
Code:
Opsr opsr2 = (Opsr) sessionManager.getSession().load(Opsr.class, opsr.getId());
[b]opsr2.getPsd().getAddress().getLine1().setValue("this shold be the value");[/b]
tx = sessionManager.getSession().beginTransaction();
sessionManager.getSession().saveOrUpdate(opsr2);
tx.commit();
Works
Code:
Opsr opsr2 = (Opsr) sessionManager.getSession().load(Opsr.class, opsr.getId());
[b]opsr2.getPsd().getAddress().setLine1(new ErroredString("this should be the value"));[/b]
tx = sessionManager.getSession().beginTransaction();
sessionManager.getSession().saveOrUpdate(opsr2);
tx.commit();
Is this the correct behaviour of a user type?
Just incase here is the user type
Code:
public class ErroredStringType implements UserType {
private static final int[] SQL_TYPES = { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR };
/**
* @see net.sf.hibernate.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return SQL_TYPES;
}
/**
* @see net.sf.hibernate.UserType#returnedClass()
*/
public Class returnedClass() {
return ErroredString.class;
}
/**
* @see net.sf.hibernate.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(Object x, Object y) throws HibernateException {
if (x == null || y == null) {
return false;
}
if (x == y) {
return true;
}
return x.equals(y);
}
/**
* @see net.sf.hibernate.UserType#nullSafeGet(java.sql.ResultSet,
* java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException,
SQLException {
String value = resultSet.getString(names[0]);
if (resultSet.wasNull()) {
// last column was SQL NULL
}
String errors = resultSet.getString(names[1]);
if (resultSet.wasNull()) {
// last column was SQL NULL
}
String icrValue = resultSet.getString(names[2]);
if (resultSet.wasNull()) {
// last column was SQL NULL
}
String icrException = resultSet.getString(names[3]);
if (resultSet.wasNull()) {
// last column was SQL NULL
}
return new ErroredString(value, errors, icrValue, icrException);
}
/**
* @see net.sf.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement,
* java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException,
SQLException {
if (value == null) {
statement.setNull(index, Types.VARCHAR);
statement.setNull(index + 1, Types.VARCHAR);
statement.setNull(index + 2, Types.VARCHAR);
statement.setNull(index + 3, Types.VARCHAR);
} else {
String str = ((ErroredString) value).getValue();
String error = ((ErroredString) value).getError();
String icrValue = ((ErroredString) value).getIcrValue();
String icrException = ((ErroredString) value).getIcrException();
if (str == null) {
statement.setNull(index, Types.VARCHAR);
} else {
statement.setString(index, str);
}
if (error == null) {
statement.setNull(index + 1, Types.VARCHAR);
} else {
statement.setString(index + 1, error);
}
if (icrValue == null) {
statement.setNull(index + 2, Types.VARCHAR);
} else {
statement.setString(index + 2, icrValue);
}
if (icrException == null) {
statement.setNull(index + 3, Types.VARCHAR);
} else {
statement.setString(index + 3, icrException);
}
}
}
/**
* @see net.sf.hibernate.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object arg0) throws HibernateException {
return arg0;
}
/**
* @see net.sf.hibernate.UserType#isMutable()
*/
public boolean isMutable() {
return true;
}
Thanks of any help