How to resolve LazyInitializationException in Spring Data JPA?

后端 未结 6 1556

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:40

    Change

    @OneToMany(mappedBy = "creator")
    private Set createdJobs = new HashSet<>();
    

    to

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "creator")
    private Set createdJobs = new HashSet<>();
    

    Or use Hibernate.initialize inside your service, which has the same effect.

提交回复
热议问题