-->
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.  [ 3 posts ] 
Author Message
 Post subject: Something like Mysql_Insert_Id()?
PostPosted: Mon Jan 30, 2006 5:32 pm 
Newbie

Joined: Tue Jan 24, 2006 9:55 pm
Posts: 7
* Hibernate version: 3.1 *

Hi, i was wondering if there's something in Hibernate like MySQL_Insert_Id() function in PHP / MySQL. I need to know what will be the next identifier in some table (for some object) before calling save(Object), because I need to construct a fileName property of that object using the identifier which I then obviously need before making the actual insert.

I think there must be some trivial solution, but I've been searching the forum for many queries and didn't find anything relevant. Wasn't really sure what words to search for though.

So is there a simple way to find out the identifier for an object that is going to be inserted?

Thanks for help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 6:53 pm 
Newbie

Joined: Thu Jan 19, 2006 8:24 pm
Posts: 5
The solution I found is I create a basic object with some initial data (Nothing or perhaps some info I already have available, not keyed to the ID of the object,) save it to the database, then the ID of my POJO is set to the ID of the inserted object in the database.

After this has happened, the data objects I need the key for, are added to this original basic object, then saved (saveOrUpdate(basicObject)) again to save the data object with the appropriate key.

Code:
Transaction trx = session.beginTransaction();
Object basic = new Object();
basic.setDate(System.currentTimeMillis());
session.save(basic);
Object nameObject = new Object();
nameObject.setId(basic.getId());
nameObject.setName("My Name Here");
session.saveOrUpdate(nameObject);
basic.setNameObject(nameObject);
session.saveOrUpdate(basic);
trx.commit();


This is just a basic example that may lead you on you way.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 9:30 am 
Newbie

Joined: Tue Jan 24, 2006 9:55 pm
Posts: 7
Thanks, it worked indeed.


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