@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.

更新:感谢大家的评论;我取得了一些进展.我做了一些调整,我认为它更接近(我已经更新了上面的代码).然而,现在问题出在插入上.父对象似乎保存得很好,但是子对象没有保存,我已经能够确定的是休眠没有填写子对象的(复合)主键的 parentId 部分,所以我我收到一个非唯一错误:

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]]

我在自己的代码中填充了 namepos 属性,但我当然不知道父 ID,因为它还没有保存然而.关于如何说服 hibernate 填写此内容的任何想法?

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.