JPA OneToMany - Collection is null

前端 未结 3 1080
长发绾君心
长发绾君心 2021-01-13 06:52

I\'m trying to set up a bidirectional relationship using JPA. I understand that it\'s the responsability of the application to maintain both sides of the relationship.

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-13 07:16

    Try to set the fetch type association property to eager on the OneToMany side. Indeed, you may leave this part (this.library.getBooks().add(this)) to be written within a session:

    Library l = new Library();    
    Book b = new Book();
    b.setLibrary(l);
    l.getBooks().add(b);
    

提交回复
热议问题