top of page
Search
Writer's pictureLeonard Anghel

How To Add An Entity Inside A Spring Projection (No Associations)

Updated: May 10, 2020

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".



481 views0 comments

Recent Posts

See All

How To Bulk Updates

Motivation: This article is useful if you need a fast way to update a significant amount of data in the database. Bulk operations...

Comments


bottom of page