问题
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.
- Hibernate fails to start because if
mappedBy
is specified than I cannot defineJoinTable
- I can remove the
JoinTable
Hibernate complainsleft and right hand sides of a binary logic operator were incompatible
- 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