JPA: Circular relations: diff between EclipseLink and Hiberante

≡放荡痞女 提交于 2020-02-23 06:31:06

问题


We have an entity Role which could be either users or groups. In the groups we have set permissions. Therefore a user belongs to a group. Additionally groups can belong to other groups, i.e. one could make a hierarchy of permissions.

The idea is to store the relations from e.g. user -> group into a separate db table role_ref.

@Table(name = "role__parent")
@IdClass(RoleAssociationKey.class)
@Data
public class RoleAssociation implements Serializable {

    @Id
    @JoinColumn(name = "has_parent_role_id")
    private Role node;

    @Id
    @JoinColumn(name = "is_parent_for_role_id")
    private Role parent;
    ...

In EclipseLink I have in Role

// bi-directional many-to-many association to parents of a Role
@OneToMany(mappedBy = "node", orphanRemoval = true, cascade = { CascadeType.ALL })
@JoinTable(name = "role__parent", joinColumns = {
        @JoinColumn(name = "is_parent_for_role_id") }, inverseJoinColumns = {
                @JoinColumn(name = "has_parent_role_id") })
private List<RoleAssociation> parentRoles;

But Hibernate complains about this construct and I have severe problems to figure out the proper definition.

  1. Hibernate fails to start because if mappedBy is specified than I cannot define JoinTable
  2. I can remove the JoinTable Hibernate complains left and right hand sides of a binary logic operator were incompatible
  3. If I remove mappedBy than I get ...has the wrong number of column. should be 2

I think the main problem is that RoleAssociation doesn't have its own @Id as a sequence. The Key is a compound of two ids - which are both required. If I would have it differently it might work. But since it works in EclipseLink I would say its a valid construct.

Is there a way to specify it to have it running in Hibernate as well?

来源:https://stackoverflow.com/questions/59388329/jpa-circular-relations-diff-between-eclipselink-and-hiberante

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!