How to resolve LazyInitializationException in Spring Data JPA?

后端 未结 6 1563

I have to classes that have a one-to-many-relation. When I try to access the lazily loaded collection I get the LazyInitializationException. I searching the web

6条回答
  •  感动是毒
    2020-12-05 11:36

    You have 2 options.

    Option 1 : As mentioned by BetaRide, use the EAGER fetching strategy

    Option 2 : After getting the user from database using hibernate, add the below line in of code to load the collection elements:

    Hibernate.initialize(user.getCreatedJobs())
    

    This tells hibernate to initialize the collection elements

提交回复
热议问题