Take your skills to the next level!
The Persistence Hub is the place to be for every Java developer. It gives you access to all my premium video courses, 2 monthly Q&A calls, monthly coding challenges, a community of like-minded developers, and regular expert sessions.
Join the Persistence Hub!
Hibernate Tips
is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question you like me to answer, please leave a comment below.
Question:
How can I order the elements of an annotate relationship without writing my own query?
Solution:
JPA supports the
@OrderBy
annotation which you can add to a relationship attribute as you can see in the following code snippet.
@ManyToMany
@JoinTable(name="BookAuthor",
joinColumns={@JoinColumn(name="bookId", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="authorId", referencedColumnName="id")})
@OrderBy(value = "lastName ASC")
private Set<Author> authors = new HashSet<Author>();
In this example, I want to order the Authors who have written a specific book by their last name. You can do this by adding the @OrderBy annotation to the relationship and specifying the ORDER BY statement in its value attribute. In this case, I define an ascending order for the lastName attribute of the Author entity.
If you want to order by multiple attributes, you can provide them as a comma-separated list as you know it from SQL or JPQL queries.
Hibernate uses the value of the annotation to create an ORDER BY statement when it fetches the related entities from the database.
05:22:13,930 DEBUG [org.hibernate.SQL] –
select authors0_.bookId as bookId1_2_0_,
authors0_.authorId as authorId2_2_0_,
author1_.id as id1_0_1_,
author1_.firstName as firstNam2_0_1_,
author1_.lastName as lastName3_0_1_,
author1_.version as version4_0_1_
from BookAuthor authors0_ inner join Author author1_
on authors0_.authorId=author1_.id
where authors0_.bookId=?
order by author1_.lastName asc
Hibernate Tips Book
Get more recipes like this one in my new book Hibernate Tips: More than 70 solutions to common Hibernate problems.
It gives you more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching, and statically and dynamically defined queries.
Get it now!
Post navigation
Similar Posts
Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question for a future Hibernate Tip, please leave a comment below. Question: Updating both ends of a bidirectional association is an error-prone task. What’s the best way to implement it in…
Hibernate Tips |
JPA
ByThorben Janssen
Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question for a future Hibernate Tip, please leave a comment below. Question: I need to map a one-to-one association in which the primary key value of one entity is also used…
Hibernate 5 |
Hibernate Advanced
ByThorben Janssen
Storing the creation timestamp or the timestamp of the last update is a common requirement for modern applications. It sounds like a simple requirement, but for a huge application, you don’t want to set a new update timestamp in every use case that changes the entity.
Hibernate Tips
ByThorben Janssen
Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question for a future Hibernate Tip, please post a comment below. Question: “One of my entities uses an @IdentityClass to map a composite primary key, and I need to map a…
Hibernate Tips |
JPA
ByThorben Janssen
Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question for a future Hibernate Tip, please leave a comment below. Question: I saw JPQL queries using JOIN, LEFT JOIN and JOIN FETCH statement. What are the differences between these 3…
SQL is an incredibly powerful query language. It provides sheer endless possibilities to extract and transform information. One example of that is a window function. It enables you to perform operations on subsets of the table rows available in your query. The PostgreSQL documentation explains window functions as: A window function performs a calculation across a set…
Hi Keerthi,
Yes, it makes sense because the HashSet gets only used when you instantiate a new entity object. When you read it from the database, Hibernate uses its own Set implementation, which keeps the defined order.
Regards,
Thorben
If the @OrderBy column is _not_ specified, will the association use one of the join columns to order by implicitly, or will the ordering of the result set potentially be non-deterministic?
If you don’t specify the order, then it’s undefined. That means it depends on the internal implementation of your database and might not be deterministic.