Hi.
I'm using an enumerated type representing a small number of user roles.
The enumerated class:
Code:
import java.io.Serializable;
import net.sf.hibernate.PersistentEnum;
import ks.rah.commons.Group;
public class InternalRole implements Serializable, PersistentEnum {
private final int code;
private InternalRole(int code) {
this.code = code;
}
public static final InternalRole USER = new InternalRole(0);
public static final InternalRole POWERUSER = new InternalRole(1);
public static final InternalRole ADMINISTRATOR = new InternalRole(2);
public int toInt() { return code; }
public static InternalRole fromInt(int code) {
switch (code) {
case 0: return USER;
case 1: return POWERUSER;
case 2: return ADMINISTRATOR;
default: throw new RuntimeException("Unknown internal role");
}
}
}
And I read posts about making this a UserType
Code:
public class InternalRoleType implements Serializable, net.sf.hibernate.UserType {
private static final Class CLAZZ = InternalRole.class;
private static final int[] SQL_TYPES = new int[] { java.sql.Types.INTEGER };
public InternalRoleType()
{
}
public Class returnedClass()
{
return CLAZZ ;
}
public int[] sqlTypes()
{
return SQL_TYPES ;
}
public boolean isMutable()
{
return false;
}
public Object deepCopy( Object obj )
throws net.sf.hibernate.HibernateException
{
return obj;
}
public boolean equals( Object obj1, Object obj2 )
throws net.sf.hibernate.HibernateException
{
if( obj1 == obj2 ) return true;
if( obj1 == null || obj2 == null ) return false;
return ((InternalRole)obj1).equals( (InternalRole)obj2 );
}
public Object nullSafeGet( java.sql.ResultSet resultSet, String[] names, Object owner )
throws net.sf.hibernate.HibernateException, java.sql.SQLException
{
Integer value = (Integer)Hibernate.INTEGER.nullSafeGet( resultSet, names[0] );
try
{
if (value == null)
return InternalRole.USER;
else
return InternalRole.fromInt( value.intValue() );
}
catch( Exception e )
{
}
return InternalRole.USER;
}
public void nullSafeSet( java.sql.PreparedStatement preparedStatement, Object obj, int index )
throws net.sf.hibernate.HibernateException, java.sql.SQLException
{
InternalRole level = (InternalRole) obj;
Hibernate.INTEGER.nullSafeSet( preparedStatement, new Integer(level.toInt()), index );
}
}
Mapping for the User class that contains this UserType (snippet)
Code:
..
<hibernate-mapping>
<class
name="ks.rah.radius.domain.User"
table="MASTERUSER"
dynamic-update="false"
dynamic-insert="false"
>
...
...
<property
name="internalRole"
type="ks.rah.radius.domain.model.InternalRoleType"
update="true"
insert="true"
column="ROLEINTERNAL"
not-null="false"
unique="false"
/>
...
...
I'm using Spring framework 1.0 with Hibernate support and it all worked like a clockwork until I tried implementing the UserType, MySQL but I suspect there's something I'm overlooking in the code.
The exception I get is
Code:
Data access failure: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of ks.rah.radius.domain.User.setInternalRole; nested exception is net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of ks.rah.radius.domain.User.setInternalRole
and lots more....
Does anybody recognize this?
Sincerely,
/C