Hibernate fetching the last row / row with greatest id

How can we do that with hibernate ?? Do we have to write a HQL or Is there any function ??

If we have to write HQL can u tell me how to write that ??

Thanks you
Who is Participating?
aozarovConnect With a Mentor Commented:
Hibernate is using JDBC. If JDBC doesn't have a good answer for that then probably neither will Hibernate.
In jdbc you can do:
1. use scrollable result set [if your driver supports it], go to last and get count and go back X (bad if table big is as you select all).
2. start by doing select count(*) or select max(id) for that table and then fetch the results by doing where id > count - X or using DB specific features (like Oracle rownum, where rownum > X)

In Hibernate you can do the same by either using Query#scroll [if your driver supports it]
or using HQL to get the size/max id then you can do another HQL select with either constraining the query yourself or using
Query#setFirstResult and Query#setMaxResults (see the generated sql to validate that Hibernte does it in an optimial way [probably
by using DB specific limits on the query])

Try to keep the information abbout the id entered in some way (form hidden field/part of the query string/session) .
Using this saved information might make even more sense if you want to show the same set of recoreds the user entered even if
some other user enter some more in the meantime (that can also be achived by adding a "updateUser" column).
What do u need it for?
letsbedecentAuthor Commented:
Here is the scenario.,

There is a user(website's user)  who needs to enter a number of users(customer) records.,
There is a page, which has a form containing user related fields.

Now when the user enters a users(customer) information and clicks the next button, to enter the next users(customer) information, the next button functions as a submit and submits the users(customer) information to the database.  
Now, if the person entering the users(customer) information thinks that he entered the wrong information for the previous user, he might want to hit the back button, which should load the users information last entered, I assumed that if  i can fetch the last record entered (the record with max id) i can pre-populate the form with his information on hitting back button.

Any ideas ??

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Why don't you save the primary key after the user submits the information and storing that in the session.
Or store the record itself in the session.
Getting the max or count is a bad idea though unless the app will only have one simultaneous session as it will eventually return you the wrong users record.
Why? (assuming you populated "updateUser" column and having it in your where cluase)
letsbedecentAuthor Commented:
This was not my need for the current application (that i am working on).

I just got this doubt, while i was imagining some functionality that i may be required for some applications.

So, i thought if there is a straightforward solution, i better know it. But, it seems like it should be tested first.

I think it will take time for me to write the necessary code and test it (lot of time) as i am just a beginner.

I need your help in distributing the points though, i dont know how long i can wait before i can give the points ?? and also since i've not tested it yet, i dont whom to give and how much to give ??

you can leave it open as long as you want as far as I'm concerned, perhaps post a comment every week or two to indicate to EE that you are still interested so they don't clean it up.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.