@OneToMany和复合主键?

@OneToMany和复合主键?

问题描述:

我使用的是注释(春季)Hibernate的,我有这已有序的对象,多到一的关系这其中的子对象具有复合主键,一个组件是一个外键回父对象的id

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, one component of which is a foreign key back to the id of the parent object.

结构看起来是这样的:

+=============+                 +================+
| ParentObj   |                 | ObjectChild    |
+-------------+ 1          0..* +----------------+
| id (pk)     |-----------------| parentId       |
| ...         |                 | name           |
+=============+                 | pos            |
                                | ...            |
                                +================+

我已经尝试了各种注释,其中没有一个似乎工作的组合。这是最接近我已经能想出:

I've tried a variety of combinations of annotations, none of which seem to work. This is the closest I've been able to come up with:

@Entity
public class ParentObject {
    @Column(nullable=false, updatable=false)
    @Id @GeneratedValue(generator="...")
    private String id;

    @OneToMany(mappedBy="parent", fetch=FetchType.EAGER, cascade={CascadeType.ALL})
    @IndexColumn(name = "pos", base=0)
    private List<ObjectChild> attrs;

    ...
}

@Entity
public class ChildObject {
    @Embeddable
    public static class Pk implements Serializable {
        @Column(nullable=false, updatable=false)
        private String parentId;

        @Column(nullable=false, updatable=false)
        private String name;

        @Column(nullable=false, updatable=false)
        private int pos;

        @Override
        public String toString() {
            return new Formatter().format("%s.%s[%d]", parentId, name, pos).toString();
        }

        ...
    }

    @EmbeddedId
    private Pk pk;

    @ManyToOne
    @JoinColumn(name="parentId")
    private ParentObject parent;

    ...
}

我来到这个实验很长的回合中,我的大多数其他的尝试取得了哪些休眠甚至不能加载因各种原因实体后。

I arrived at this after a long bout of experimentation in which most of my other attempts yielded entities which hibernate couldn't even load for various reasons.

更新:感谢所有的意见;我已经取得了一些进展。我做了一些调整,我认为它更接近(我已经更新上面的code)。然而,现在的问题是,在插入。父对象似乎保存很好,但子对象不节能,而且我已经能够确定的是,Hibernate是不填写子对象的(复合)主键的parentId一部分,所以我m到处一个不唯一的错误:

UPDATE: Thanks all for the comments; I have made some progress. I've made a few tweaks and I think it's closer (I've updated the code above). Now, however, the issue is on insert. The parent object seems to save fine, but the child objects are not saving, and what I've been able to determine is that hibernate is not filling out the parentId part of the (composite) primary key of the child objects, so I'm getting a not-unique error:

org.hibernate.NonUniqueObjectException:
   a different object with the same identifier value was already associated 
   with the session: [org.kpruden.ObjectChild#null.attr1[0]]

我填充名称 POS 在我自己的code属性,但当然我的不知道父ID,因为它尚未保存。如何说服休眠任何想法,以填补这一点?

I'm populating the name and pos attributes in my own code, but of course I don't know the parent ID, because it hasn't been saved yet. Any ideas on how to convince hibernate to fill this out?

谢谢!

多的实验和挫折之后,我最终确定,我不能这样做正是我想要的。

After much experimentation and frustration, I eventually determined that I cannot do exactly what I want.

最后,我继续给孩子对象自身的合成关键,让Hibernate管理。这是一个不理想的,因为最关键的是几乎一样大的数据的其余部分,但它的作品。

Ultimately, I went ahead and gave the child object its own synthetic key and let Hibernate manage it. It's a not ideal, since the key is almost as big as the rest of the data, but it works.