We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

sb1977
sb1977 asked
on
Medium Priority
1,317 Views
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'
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
depends on your mappings
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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)")
   .setParameter("user",userName)
   .setMaxResults(6).list();

Thanks for your help
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.