Selecting the right information in the right format
Hey,
I'm doing many coaching projects right now, and there's one question that repeatedly pops up:
"What kind of projection should we best use for our queries?"
The answer to that question is simple:
If you want to change the information you select, use an entity projection. In all other cases, an unmanaged DTO projection provides better performance.
(I explain all of that in more detail in this article.)
The next question is then usually:
"Do we really need to create a new DTO projection for every use case?"
No, you don't need to use a use case-specific projection.
If multiple use cases require the exact same information, you can define 1 projection and reuse it. In most cases, that also means you can reuse the query statement.
But please don't create a DTO that matches your entity class. DTO projections provide better performance because they avoid entities' management overhead and only include the information you need. And that means they almost always can be much smaller than your entity classes.
Speak soon,
Thorben
Check out the latest tutorials on the blog.
Stay up to date with the latest releases and features in the Java persistence world. Read the blog.Watch my tutorials on YouTube.
You prefer videos over written content? Then my YouTube channel is the right place for you.Get my Hibernate Tips ebook.
The ebook is packed with ready-to-use solutions to help you solve your next Hibernate problem even quicker. Get it here.Join the Persistence Hub.
The Persistence Hub teaches you everything you need to know on your journey to becoming a Java persistence expert. Get access to video courses, Expert Sessions with amazing guests, monthly Java Persistence News, and more. Become a member.