I am using hbm2java from version 2.1.3 of the Hibernate tools. I am trying to use a UserType to model a persistent enum. The xml in my mapping file looks like this:
Code:
<property
name="type"
type="com.foo.domain.entity.RestrictionTypeUserType"
not-null="true"
column="RESTRICTIONTYPEID"
update="false">
</property>
The class I am actually trying to map this property to is com.foo.domain.entity.RestrictionType. However, the java code that gets generate for the xml above looks like this:
Code:
public com.foo.domain.entity.RestrictionTypeUserType getType() {
return this.type;
}
public void setType(com.foo.domain.entity.RestrictionTypeUserType type) {
this.type = type;
}
I was expecting that my 'type' property would be of type RestrictionType. Do I not understand how this works or am I doing something wrong?
This is what I get when I log at a debug level:
Code:
[hbm2java] 2005-01-10 17:37:09,330 DEBUG [net.sf.hibernate.tool.hbm2java.ClassMapping] - <Processing mapping for class: com.foo.domain.entity.Restriction>
[hbm2java] 2005-01-10 17:37:09,705 INFO [net.sf.hibernate.tool.hbm2java.Generator] - <Generating 1 in C:\Dev\code\newarch\persistence\src>
[hbm2java] 2005-01-10 17:37:09,736 DEBUG [net.sf.hibernate.tool.hbm2java.Generator] - <Writing C:\Dev\code\newarch\persistence\src\com\foo\domain\entity\BaseRestriction.java>
Here is my RestrictionTypeUserType class:
Code:
public class RestrictionTypeUserType implements UserType
{
private static final int[] SQL_TYPES = {Types.NUMERIC};
public int[] sqlTypes() { return SQL_TYPES; }
public Class returnedClass() { return RestrictionType.class; }
public boolean equals(Object x, Object y) { return x == y; }
public Object deepCopy(Object value) { return value; }
public boolean isMutable() { return false; }
public Object nullSafeGet(ResultSet resultSet,
String[] names,
Object owner)
throws HibernateException, SQLException {
int i = resultSet.getInt(names[0]);
return resultSet.wasNull() ? RestrictionType.READ_WRITE : RestrictionType.getInstance(i);
}
public void nullSafeSet(PreparedStatement statement,
Object value,
int index)
throws HibernateException, SQLException {
if (value == null) {
statement.setNull(index, Types.NUMERIC);
//statement.setInt(index, RestrictionType.READ_WRITE.getId());
} else {
statement.setInt(index, ((RestrictionType)value).getId());
}
}
/**
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object x) throws HibernateException
{
return ((RestrictionType)x).getId();
//return ((RestrictionType)x).toString().hashCode();
}
/**
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
public Serializable disassemble(Object value) throws HibernateException
{
//return new Integer(((RestrictionType)value).getId());
return (RestrictionType)value;
}
/**
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
*/
public Object assemble(Serializable cached, Object owner) throws HibernateException
{
//return RestrictionType.getInstance(((Integer)cached).intValue());
return (RestrictionType)cached;
}
/**
* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Object replace(Object original, Object target, Object owner) throws HibernateException
{
// TODO WHAT GOES HERE???
return null;
}
}