-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to assign @GeneratedValue id before save()
PostPosted: Thu Jun 05, 2008 5:23 pm 
Is there a way to ask hibernate to populate the id from the generator before save()?

Such as:
Code:
session.assignId(child1);

or maybe:
Code:
IdentifierGenerator generator = session.getGenerator("seqGen");
child1.setId(generator.generate());


Top
  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 11:49 am 
Newbie

Joined: Fri Jun 06, 2008 11:38 am
Posts: 1
You can make use of the generators provided by hibernate.

somme of the generators are

Increment - which reads the last value from the table, increments by 1 and use that as ID
Assigned - which takes the value given by you as the ID
native - does follow some strategy
and there are many...
GUID

You need to choose what what best suits your requirement.

I prefer to spcify them in the mapping configuration file rather than messing up my code with the configurations.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 11:50 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Hibernate will do this as the save method is called. However, this doesn't mean the transaction is committed. So, while Hibernate will do this during the save, it's not necessarily permanently written to the database.

Perhaps you could tell us why you need to do this and we might have some other ideas to help you out?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 1:05 pm 
I want to do it as a solution to the Equals and HashCode problems. See http://www.hibernate.org/109.html


Top
  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 1:19 pm 
-deleted-


Last edited by Edsunn on Fri Jun 06, 2008 1:42 pm, edited 1 time in total.

Top
  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 1:19 pm 
I reread my first post, and I see where I wasn't clear. I know that hibernate will assign an id as part of a save(), I am looking to get it assigned before that in order to avoid problems.

The code would look something like:


Code:
session.assignId(child1);
parent.addChild(child1);
session.save(parent);


Top
  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 5:10 pm 
So I think this might work:
Code:
e.setId(((EntityPersister)session.getSessionFactory().getClassMetadata(entity.getClass())).getIdentifierGenerator().generate((SessionImplementor)session, entity));


In order to make sure the setting of the id is in its own transaction, I would use one of the new org.hibernate.id.enhanced.* generators.

I think there is a small performance hit on persisting new entities-- I think hibernate normally takes a shortcut and immediately does an insert when the id is null-- but in this case, the id will already be set-- so hibernate will first do a query, and then decide it needs to do an insert.


Top
  
 
 Post subject: Re: How to assign @GeneratedValue id before save()
PostPosted: Tue Oct 04, 2011 12:10 pm 
Newbie

Joined: Tue Oct 04, 2011 11:30 am
Posts: 1
Hi, I'm having a similar problem. I need to get the next Id for the object I'm saving and use it to create a generated login, i.e: login = user.getFirstName() + user.getUserId()

I've used the code above to get me the next user id but when I call getHibernateTemplate().save(user); the userId is incremented again with the next userId. Here is a sample of the code I'm trying to use to do this:

Code:
Integer userId = (Integer) ((EntityPersister)getSessionFactory().getClassMetadata(user.getClass())).getIdentifierGenerator().generate((SessionImplementor)getSession(), user); //Id here = 1
user.setUserId(userId);
user.setLogin(user.getFirstName() + userId);
return getHibernateTemplate().save(user);//after saving Id here = 2


Note that I'm using the Spring 2.5.6 HibernateTemplate class to call Hibernate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.