Hibernate Many-to-many association: left hand side collection contains elements, but right hand side collection is empty

前端 未结 2 1120
轮回少年
轮回少年 2021-01-02 15:15

I got a problem with a many to many association in my persistence layer. My scenario is the following:

A user can has several roles and a role can have several user

2条回答
  •  甜味超标
    2021-01-02 16:06

    When dealing with a bidirectional many-to-many association you have to maintain both ends of the association. In your case, you have to add the user to the role as well. Adding the role to the user isn't sufficient to establish a bidirectional association as you can read in book Java Persistance with Hibernate:

    As always, a bidirectional association (no matter of what multiplicity) requires that you set both ends of the association.

提交回复
热议问题