-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: hbm2java property type not correct with UserType
PostPosted: Mon Jan 10, 2005 8:26 pm 
Newbie

Joined: Mon Jan 10, 2005 8:03 pm
Posts: 2
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;
    }
}



Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 12:34 pm 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
you must add a meta-attribute in the hbm-file ...

Code:
<property
    name="type"
    type="com.foo.domain.entity.RestrictionTypeUserType"
    not-null="true"
    column="RESTRICTIONTYPEID"
    update="false">
   <meta attribute="property-type">com.foo.domain.entity.RestrictionType</meta>
</property>


that will do it ...

gtx
curio


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.