HQL - find entity where property x is highest
Can HQL queries do this?
"get the UserEn开发者_如何学Pythontity where the property creationTimestamp is the most recent of all UserEntities".
Essentially a query to return the "newest user" in our program where each UserEntity has a field mapped to a timestamp column in our database.
Querying for the highest creationTimestamp would look like this:
from UserEntity where creationTimestamp = max(creationTimestamp)
If you want to return only a single instance and ignore other results that have the same (highest) value on that property, you can use query.uniqueResult()
.
See Chapter 14. HQL: The Hibernate Query Language for further reference.
The HQL query to list the users from newest first is:
from UserEntities
order by creationTimestamp desc
Use setMaxResults
to limit the result set to just one user.
Have you looked at the HQL order-by
clause?
精彩评论