-->
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 Identifier generation
PostPosted: Mon Jul 25, 2005 9:07 am 
Newbie

Joined: Mon Jun 13, 2005 2:02 am
Posts: 7
how can we generate Identifiers manually without specifing any of the identifiergenerators

suppose i am using an hbm.xml file like this

<id name="userId" column="userId" type="integer">
<generator class="utils.IdGenerator"/>
</id>

what all things have to be followed for the class IdGenerator to generate an id for the table


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 2:40 pm 
Newbie

Joined: Tue Jul 05, 2005 11:47 pm
Posts: 15
Location: Argentina
Your class must implement org.hibernate.id.IdentifierGenerator

Here you have an example.


Top
 Profile  
 
 Post subject: I did it but.....
PostPosted: Tue Jul 26, 2005 2:55 am 
Newbie

Joined: Mon Jun 13, 2005 2:02 am
Posts: 7
I implemented the class like this

public class IdGenerator implements IdentifierGenerator {
public static long id=0;

public Serializable generate(SessionImplementor session, Object obj)
throws HibernateException
{
return IdentifierGeneratorFactory.createNumber(id++,IdGenerator.class);
}


}

and in the hbm file

<id column="user_id" type="long">
<generator class="util.IdGenerator"/>
</id>

i am getting an error like this


org.hibernate.id.IdentifierGenerationException: this id generator generates long, integer, short
at org.hibernate.id.IdentifierGeneratorFactory.createNumber(IdentifierGeneratorFactory.java:109)
at net.assyst.peopleplus.utils.IdGenerator.generate(IdGenerator.java:36)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:85)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:429)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:424)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 3:05 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
first of all i don't know which hibernate-version you're using ...

but take a look in the source of "IdentifierGeneratorFactory" ... (one big advantage of open source ;) )

Here's the code from v2.18

Code:
   static Number createNumber(long value, Class clazz) throws IdentifierGenerationException {
      if ( clazz==Long.class ) {
         return new Long(value);
      }
      else if ( clazz==Integer.class ) {
         return new Integer( (int) value );
      }
      else if ( clazz==Short.class ) {
         return new Short( (short) value );
      }
      else {
         throw new IdentifierGenerationException("this id generator generates long, integer, short");
      }
   }


Then it's quite clear why you're getting the exception ;)

HiH
curio

Btw: providing all required information is leading to much faster help ;)


Top
 Profile  
 
 Post subject: thanks
PostPosted: Tue Jul 26, 2005 3:26 am 
Newbie

Joined: Mon Jun 13, 2005 2:02 am
Posts: 7
thanks it worked


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.