1

Let's say I have a spring-boot-based project, that uses spring data jpa to define entities and map them to a postgres db.

The entity has some @ManyToOne annotatated "joins" - with eager fetch set. I can not change it. Part of the project relies on this eagerness.

I want to be able to write a custom @Query("...") in my entity's repository such as I can explicitly - for this specific query - avoid the eagerness.

Is there a syntax to achieve it?

keep in mind that the end goal is to be able to update a field for all retrieved values

1 Answer 1

2

You can use EntityGraph annotation before your Query to define a query's fetch plan. If you define an empty attribute paths for it you are basic saying to don't fetch anything, otherwise you can specify the attributes to eagerly fetch:

// No eager fetching @EntityGraph(attributePaths = {}) @Query(...) // Eager fetch products @EntityGraph(attributePaths = {"products"}) @Query(...) 
Sign up to request clarification or add additional context in comments.

3 Comments

unfortunately I cannot use this pattern because of github.com/spring-projects/spring-data-jpa/issues/3682
The fix for #3682 was released with Spring Boot 3.4.1. Are you saying you're not allowed to upgrade Spring Boot?
well, in the project I'm working on I'm stuck on 3.3.5

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.