Motivation:
You need to fetch an entity because you plan to modify it. But, you need it next to other read-only data in a Spring Projection.
Description:
If for some reason (not relevant here), you need to load an entity in your Spring projection, then this application shows you how to do it via an example. In this case, there are two entities, Author and Book, that have no materialized association between them, but, they share the genre attribute. We use this attribute to join authors with books via JPQL.
And, we want to fetch in a Spring projection the authors as entities, Author, and only the title of the books.
Key points:
First, write two unrelated entities (e.g., Author and Book)
Next, write the proper Spring projection interface having public Author getAuthor() and public String getTitle():
And, write a JPQL to fetch data:
Finally, call fetchAll() from a service-method:
Tam Ta Da Dam! :) The complete application is available on GitHub.
If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices".
Comments