Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Using Hibernate Query Language to get the top rows of query results

Posted on 2006-04-04
Medium Priority
Last Modified: 2008-02-07
Hello - I need to use Hibernate to query my SQL Server database and determine if a new password equals any of the user's past 6 passwords.  Below is a query that will search the PASSWORD_HISTORY table and if no records are returned, it is ok to use the new one.  Can you please let me know how I would do this using HQL?

Thank you

SELECT password FROM
(SELECT TOP 6 password FROM password_history WHERE user_id = userId01 ORDER BY pwd_stamp) AS PWD
WHERE password = 'password01'
Question by:sb1977
LVL 92

Expert Comment

ID: 16375792
depends on your mappings

Accepted Solution

mrigank earned 600 total points
ID: 16380754
Write an HQL to return all the previous passwords for that User as a List.
Order by the timestamp.

Then while iterating through the List, go only  through the first 6 entries in the list.

Author Comment

ID: 16384963
I was trying to speed up the time it takes to execute the query, so I was thinking about using "count" and using native sql inside hibernate.  Unfortunately, I didn't have much luck with that, but did find a way to specify only returning the top six rows, but I still have to iterate through the list.  Here is what I came up with:

results = session.createQuery("FROM PasswordHistory AS pwdHis WHERE pwdHis.username = :user)")

Thanks for your help

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

564 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