-->
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.  [ 1 post ] 
Author Message
 Post subject: Custom Type Identifier
PostPosted: Fri May 18, 2007 9:10 am 
Newbie

Joined: Mon Apr 24, 2006 4:08 am
Posts: 5
Hi @all!

I searched for this problem a long time, but I can not resolve the problem. For my application I liked to create an own ID class that behaves like a Long. Since I am not able to inherit from Long, cause someone could to evil things with this ;-), I came up with the solution of implementing an UserType.
Everything works fine, except that all collections are empty. It does not matter what kind of collection. The Hibernate debuglog shows that the execution works fine all Elements of the collection are found, but if the object with this collection is returned, the collection is empty. I checked my database logs too, every query is set right. (To cut this article i did not attached the log from hibernate debug level, but could attach it if someone could help me with it).
Maybe someone has an idea or a different solution for my needs.

Now I like to introduce more details:

All ids of my domain objects must implements a markerinterface Id. I created an GenericDao and GenericService that works with the ids of the objects. First they were all java.lang.Long. But I got a databasemodel problem for a relationtable that contains three foreign keys being the primary key in that relation.
So there are now two types of Ids java.lang.Long and relationTableId.

To avoid this I introduced the construct with the makerinterface id. Ids of the type java.lang.Long now became BaseId implementing the markerinterface.
For this I created a UserType IdUserTypeImpl that converts between the BaseId and SQL. Following relevant code:
Code:
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
         throws HibernateException, SQLException {      
      return new BaseId(resultSet.getLong(names[0]));      
   }

   public void nullSafeSet(PreparedStatement statement, Object value, int index)
         throws HibernateException, SQLException {      
      if (value == null) {
         statement.setNull(index, Hibernate.LONG.sqlType());
         } else {
         statement.setLong(index, ((BaseId)value).getId());
         }
   }
   
   public Class returnedClass() {      
      return de.unilog.avinci.mypm.model.domainobject.id.Id.class;
   }

   public int[] sqlTypes() {
      return new int[]{ Hibernate.LONG.sqlType() };
   }


A mapping snip is:
Code:
<id name="id" column="USER_ID" type="de.unilog.avinci.mypm.model.domainobject.id.IdUserTypeImpl">
        <meta attribute="use-in-tostring">true</meta>
       <meta attribute="use-in-equals">true</meta>
       <meta attribute="gen-property">false</meta>
        <generator class="de.unilog.avinci.mypm.model.domainobject.generator.BaseIdGenerator"></generator>
     </id>


BaseId implements Serializable and provides methods for toString returning the Long value as String and a equals method.

Everything works fine, except of collections.
Mapping snip:
Code:
<set name="roles" table="USER_USERROLE" lazy="false">
        <key column="USER_ID"/>        
      <many-to-many column="USERROLE_ID" class="UserRole"/>
     </set>


This works with Ids of the type java.lang.Long but not with my userType. Anyone an idea or a different solution?


Thanks in advance,

Danny


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

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.