-->
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.  [ 5 posts ] 
Author Message
 Post subject: Custom Type inside Hibernate <id> tag?
PostPosted: Mon Aug 09, 2004 8:36 pm 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
Hibernate version: 2.1.4

My question is quite naive really.

I am wondering if it is possible to use a custom type (a class that I have defined in java) as the primary key of another custom type.

i.e. use an object as the <id> of another. I need some sample syntax please if it's possible!

Also, I need to be able to use the generator "assigned" (not "foreign").

Thank you for your help!
~Lucas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 6:37 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
java class:
Code:
public class Warehouse implements Serializable
{
   private UniqueKey id;

mapping:
Code:
<hibernate-mapping>
   <class name="ru.xxx.common.security.Warehouse" table="`Warehouse`" batch-size="10">

      <cache usage="nonstrict-read-write"/>

      <id name="id" column="`WarehouseId`" type="ru.xxx.hibernate.UniqueKeyType" access="field">
         <generator class="ru.xxx.hibernate.UniqueKeyGenerator"/>
      </id>

      <version name="version" column="`Version`" />

      <property name="name" column="`WarehouseName`" access="field"/>
   </class>
</hibernate-mapping>


Custom type for UniqueKey:
Code:
/**
* Hibernate class for mapping ru.incom.solaris.common.UniqueKey into SQL BIGINT.
*
* @author shl
* @see ru.incom.solaris.common.UniqueKey
* @link http://hibernate.org/50.html
*/
public class UniqueKeyType implements UserType
{
   private static final int SQL_TYPES[] = {Types.BIGINT};

   public int[] sqlTypes()
   {
      return SQL_TYPES;
   }

   public boolean isMutable()
   {
      return false;
   }

   public Class returnedClass()
   {
      return UniqueKey.class;
   }

   public boolean equals(Object x, Object y)
   {
      return (x == y) || (x != null && y != null && x.equals(y));
   }

   public Object deepCopy(Object value)
   {
      return value;
   }

   public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
         throws HibernateException, SQLException
   {
      long value = rs.getLong(names[0]);

      if (rs.wasNull())
         return null;

      return UniqueKey.valueOf(new Long(value));
   }

   public void nullSafeSet(PreparedStatement st, Object value, int index)
         throws HibernateException, SQLException
   {
      if (value == null)
      {
         st.setNull(index, Types.BIGINT);
      }
      else
      {
         long longValue = -1;

         // shl: it was _FIXME: sometimes we get value instanceof String, should be instanceof UniqueKey
         // shl: is it actual now? I mean such check UniqueKey or String?
         if (value instanceof UniqueKey)
         {
            longValue = ((UniqueKey) value).toLong().longValue();
         }
         else if (value instanceof String)
         {
            System.err.println("[WARNING] " + this.getClass() + ".nullSafeSet()" +
                  " Parameter value: " + value + " operandType: " + value.getClass() +
                  ". Should be: " + UniqueKey.class + " or " + String.class);
            
            longValue = Long.valueOf(value.toString()).longValue();
         }
         else
         {
            throw new IllegalArgumentException("Illegal operandType: " + value.getClass() +
                  " of parameter value: " + value +
                  ". Should be: " + UniqueKey.class + " or " + String.class);
         }

         st.setLong(index, longValue);
      }
   }
}


For more examples Search the site for "UserType" key word.

_________________
Leonid Shlyapnikov


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 12:30 pm 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
Thank you, I have only one question about your UniqueKeyType class:


Are the methods you have implemented (eg equals, deppCopy, nullSafeGet, nullSafeSet, etc) required by Hibernate in order to do the key generation?

Thanks again, this appears to be what I was looking for!

~Karokain


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 12:42 pm 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
I think I understand now... those methods belong to the UserType interface which we implement here in the UniqueKeyType class...

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 2:36 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
karokain wrote:
I think I understand now... those methods belong to the UserType interface which we implement here in the UniqueKeyType class...

Now you will have to implement UniqueKeyGenerator if you are not going to use assigned generator. If you need I can post my one.

_________________
Leonid Shlyapnikov


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.