[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Hibernate fetching the last row / row with greatest id

Posted on 2005-05-06
8
Medium Priority
?
1,479 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:letsbedecent
  • 4
  • 2
  • 2
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13948649
What do u need it for?
0
 

Author Comment

by:letsbedecent
ID: 13949459
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 ??

Thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 13949584
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Accepted Solution

by:
aozarov earned 2000 total points
ID: 13949621
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).
0
 
LVL 92

Expert Comment

by:objects
ID: 13949628
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.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13949634
Why? (assuming you populated "updateUser" column and having it in your where cluase)
0
 

Author Comment

by:letsbedecent
ID: 14096576
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 ??

regards
0
 
LVL 92

Expert Comment

by:objects
ID: 14098749
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question