@OneToMany and composite primary keys?

前端 未结 9 1738
温柔的废话
温柔的废话 2020-12-01 03:31

I\'m using Hibernate with annotations (in spring), and I have an object which has an ordered, many-to-one relationship which a child object which has a composite primary key

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 04:12

    After spending three days on this, I think I have found a solution, but to be honest, I don't like it and it can definitely be improved. However, it works and solves our problem.

    Here is your entity constructor, but you could also do it in the setter method. Also, I used a Collection object but it should be same or similar with List:

    ...
    public ParentObject(Collection children) {
        Collection occ = new ArrayList();
        for(ObjectChild obj:children){
            obj.setParent(this);
            occ.add(obj);
        }
        this.attrs = occ;
    }
    ...
    

    Basically, as someone else suggested, we must first manually set all the children's parent id before saving the parent (along with all children)

提交回复
热议问题