Home > Enterprise >  What is better to use with JPA FetchType.LAZY @Transactional or @NamedEntityGraph
What is better to use with JPA FetchType.LAZY @Transactional or @NamedEntityGraph

Time:01-09

I used Spring Boots @Transactional on the Service Layer to avoid the Lazyinitializationexception. But is there an advantage of using @NamedEntityGraph instead of @Transactional to avoid Lazyinitializationexception?

CodePudding user response:

@NamedEntityGraph not only avoid Lazyinitializationexception but also helps optimizing the number of requests sent to avoid N 1 effect. This may be a valuable advantage but needs more work to ensure that every needed object will be fetched before the end of the hibernate session.

  • Related