Closed slkmikhail closed 1 year ago
The default entity graph
concept introduced a long time ago by this library is not needed anymore (see https://github.com/Cosium/spring-data-jpa-entity-graph/issues/44#issuecomment-642469698 for the rationale).
So I would just tell you to stop using the default entity graph
feature.
The default entity graph
is a good staff to avoid a suggestion which EG should be used for standard jpa methods every time when should be fetched additional data (less code)
More flexibale feature would be to use the default entity graph
as is, but have capability to:
default entity graph
dynamicly by passing additional EG to some method
PersonEntityGraph.____(APPEND) .....
default entity graph
for some methods by an annotation or another way (in my case)The default entity graph is a good staff to avoid a suggestion which EG should be used for standard jpa methods every time when should be fetched additional data (less code)
You can now achieve the same behaviour by only relying on your entity attributes annotations. Here is an example:
public class Product {
@ManyToOne(fetch = FetchType.LAZY)
private Brand brand;
@ManyToOne(fetch = FetchType.EAGER)
private Manufacturer manufacturer;
}
In this example, the default entity graph is (manufacturer)
.
As I know, unfortunately, the FetchType.EAGER
strategy is also prone to N+1 query issues. That is not a good solution for fetch a lot of data.
In additionally, using FetchType.EAGER
during
@Lock(LockModeType.PESSIMISTIC_WRITE)
Optional<Person> findAndLockById(UUID id)
I get left join
, but I don't want to fetch any additional data for that moment.
The solution is to declare empty grapth with FETCH
type not to use FetchType.EAGER
, but, as I know, I can not use empty graph
You can use default
interface methods to pass "default" entity like this:
default Optional<Person> findByName(UUID id) {
return findByName(id, NamedEntityGraph.loading("foo.default"));
}
Optional<Person> findByName(UUID id, EntityGraph entityGraph);
See also https://github.com/Cosium/spring-data-jpa-entity-graph/issues/73#issue-1330079585
What steps will reproduce the problem?
What is the expected output? Find and lock without entity graph What happens instead?
Environment:
Is there some way not to use default entity graph for some jpa methods?