JPA / Hibernate OneToMany Mapping, using a composite PrimaryKey

前端 未结 2 1655
孤城傲影
孤城傲影 2020-12-28 08:47

I\'m currently struggling with the right mapping annotations for a scenario using a composite Primary Key Class. First, what I am trying to achieve in words:

I have

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-28 09:40

    I've fixed a similar problem by modifying the mappedBy attribute of the parent class (using dotted notation)

    public class Group{
        ...
        @OneToMany(mappedBy = "fieldAccessRulePK.group")
        private Set fieldAccessRules;
        ... 
    }
    

    and by keeping the annotations in the PK class:

    @Embeddable
    public class FieldAccessRulePK implements Serializable{
        private String fieldKey;
    
        @ManyToOne
        @JoinColumn(name = "group_id")
        private Group group;
        ...
    }
    

提交回复
热议问题