-->
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.  [ 6 posts ] 
Author Message
 Post subject: Retriving last record in a table
PostPosted: Thu Oct 20, 2005 8:25 am 
Newbie

Joined: Thu Oct 20, 2005 7:21 am
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3

Name and version of the database you are using: Postgresql 8.0.3

Just a quick question, I've read through any related material I could find though none really covered the area I'm looking for. When I save an item to my database, I want to retrieve the id of this record, as it is generated in the save command, I never have it in my code. Basically what I would like to do is just retrieve the last record in the table, without querying the table to return all the records. Is there a specific command to return the last item saved to a table?

Thanks for any help.
Michael.


Top
 Profile  
 
 Post subject: Re: Retriving last record in a table
PostPosted: Thu Oct 20, 2005 8:27 am 
Newbie

Joined: Thu Oct 20, 2005 7:21 am
Posts: 4
mmcallis wrote:
Is there a specific command to return the last item saved to a table?


Sorry should have said specific command to return the last record in the database.

Thanks
Michael.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:28 am 
Newbie

Joined: Wed Apr 20, 2005 4:14 am
Posts: 10
Location: Netherlands
You'll probably have a method getId() and setId() in your objects.

Code:
Cat c = new Cat() //c.getId() will give the default value, usually -1.
c.name='Peter';
Session.save(c); //Here, the method c.setId() is invoked.

System.out.println(c.getId()); //Here, you'll see which primary key Hibernate has given the object.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:42 am 
Newbie

Joined: Thu Oct 20, 2005 7:21 am
Posts: 4
Yeah should have gave more info sorry. My cat object has a getId but id in the table is of type sequence, so getId() would return null, as the cat object only has the name variable lets say initialised. Like I said the id is generated in hibernate when saving to the table. The id is never in my code. I need a quick way to get the last record from the table. Thanks for the help, should have made this clearer at the time.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 9:10 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
mmcallis wrote:
Yeah should have gave more info sorry. My cat object has a getId but id in the table is of type sequence, so getId() would return null, as the cat object only has the name variable lets say initialised. Like I said the id is generated in hibernate when saving to the table. The id is never in my code. I need a quick way to get the last record from the table. Thanks for the help, should have made this clearer at the time.


If Hibernate is generating the ID, it will set the value on your object.

If you just want the highest ID #, you can SELECT MAX(ID) FROM YOUR_TABLE.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 9:25 am 
Newbie

Joined: Thu Oct 20, 2005 7:21 am
Posts: 4
Ah thanks didn't realise Hibernate would have updated the values of the object, thought it would have left the object unchanged. Brilliant thanks.
Michael.


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