hibernate insert to a collection causes a delete then all the items in the collection to be inserted again

后端 未结 7 1672
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-17 21:09

I have a many to may relationship CohortGroup and Employee. Any time I insert an Employee into the CohortGroup hibernate deletes the group from the resolution table and ins

7条回答
  •  甜味超标
    2020-12-17 21:44

    I have inserts acting the way I expect them to now. Thanks to Pascal and z5h, I have learned a lot. I believe I have the hashCode and equals implemented correctly. This never solved the problem for me though. Instead I have implemented an Intermediate Entity.

    For what it is worth below are the mapping in my Employee, CohortGroup, and now CohortGroupMemeber classes.

    Employee:

    @OneToMany(mappedBy="member")
    public List getMemberGroups(){
       return memberGroups;
    }
    public void setMemberGroups(List grps){
       memberGroups = grps;
    }
    

    CohortGroupMember

    @ManyToOne
    @JoinColumn(name="USERID")
    public Employee getMember(){
        return emp;
    }
    public void setMember(Employee e){
        emp = e;
    }
    @ManyToOne
    @JoinColumn(name="COHORT_GROUPID")
    public CohortGroup getGroup(){
        return group;
    }
    public void setGroup(CohortGroup cg){
        group   = cg;
    }
    

    CohortGroup

    @OneToMany(mappedBy="group")
    public List getMembers(){
        return members;
    }
    public void setMembers(List emps){
        members = emps;
    }
    

    The book I followed for this is Java Persistence with Hibernate chapter 7.2.3

提交回复
热议问题