-->
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: Newbie question
PostPosted: Wed Feb 22, 2006 11:53 am 
Newbie

Joined: Mon Feb 20, 2006 3:27 pm
Posts: 4
This has to be a very big newbie question, but I apologize I couldn't find an answer.

I have tables with auto_increment primary keys on them.

I just need to insert an object.. then get the ID out that my MySql database generated for my inserted row... and guarantee that it's the right ID (I dont feel safe doing a select MAX, etc)

Thanks, I apologize for the extreme newbie question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 3:20 pm 
Beginner
Beginner

Joined: Wed Feb 08, 2006 5:45 pm
Posts: 23
Location: Phoenix, AZ
If you use session.save( Object obj ) it will return the generated identifer as a Serializable. If you are using an Integer as the id then your code would look something like this:

Code:
Integer id = (Integer)session.save(myObjectWithoutAnIdSet);


Hope this helps,
-B

_________________
Please rate me if you found my post useful.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 4:45 pm 
Newbie

Joined: Wed Nov 23, 2005 5:01 am
Posts: 15
Indeed the BLudkiewicz is right if you use that form of save, but you can also get the generated id from your object after your object has been saved.

session.saveOrUpdate(yourObject);
id= yourObject.getId();

or
id = session.getIdentifier(yourObject);

The choice is yours and of course depends by your possibility to have control to the id of your object.

Hope this helps


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.