Fetch Type LAZY still causes Eager loading Hibernate Spring data

后端 未结 1 402
粉色の甜心
粉色の甜心 2021-02-10 16:49

I have created a simple Spring boot project with Spring data.

I have a TagGroup Entity which has one to many relation with Tags.

 @Entity
 @Table(name =          


        
相关标签:
1条回答
  • 2021-02-10 17:21

    This is because of property spring.jpa.open-in-view=true.

    As per the spring-boot-configuration Spring boot applications use spring.jpa.open-in-view=true.

    With this property it

    Register OpenEntityManagerInViewInterceptor. Binds a JPA EntityManager to the thread for the entire processing of the request.

    So in your case, subsequently when you call the getTagList() i.e., retrieve the tagList, it subsequently fires another query to fetch the tagList as the the EntityManager is still open.

    As you might know, LazyInitializationException is never thrown if the entityManager that has loaded the parent is still open.

    To override this, you can add spring.jpa.open-in-view=false in your application.properties/application.yml and then you should see LazyInitializationException.

    0 讨论(0)
提交回复
热议问题