使用hibernate / JPA注释映射多字段主键的问题 [英] Mapping issue with multi-field primary keys using hibernate/JPA annotations

查看:175
本文介绍了使用hibernate / JPA注释映射多字段主键的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了使用多字段主键的数据库。我有一个情况,我有一个主和详细信息表,其中详细信息表的主键包含字段也是外键的主表。像这样:

 主键主域:
master_pk_1

详细主键字段:
master_pk_1
details_pk_2
details_pk_3

hibernate / JPA注释如下:

  @Id 
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = idGenerator)
@Column(name =master_pk_1)
private long masterPk1;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name =master_pk_1,referencedColumnName =master_pk_1)
private List< Details> details = new ArrayList< Details>();

在详细类中,我已经定义了id和后面的引用:

  @EmbeddedId 
@AttributeOverrides({
@AttributeOverride(name =masterPk1,column = @Column(name = master_pk_1)),
@AttributeOverride(name =detailsPk2,column = @Column(name =details_pk_2)),
@AttributeOverride(name =detailsPk2,column = @Column =details_pk_2))})
private DetailsPrimaryKey detailsPrimaryKey = new DetailsPrimaryKey();

@ManyToOne
@JoinColumn(name =master_pk_1,referencedColumnName =master_pk_1,insertable = false)
private Master master;

所有这一切的目标是我可以创建一个新的主人,添加一些细节,并且保存时,JPA / Hibernate会在masterPk1字段中为master生成新的id,并自动将其传递给详细记录,并将其存储在DetailsPrimaryKey类中匹配的masterPk1字段中。至少这是我一直在看的文档暗示。



实际发生的是,hibernate似乎corectly创建和更新数据库中的记录,但不通过在内存中细节类的关键。



我还发现,如果没有将 insertable = true 添加到后面引用master,hibernate会创建sql,在insert语句中有两次master_pk_1字段,导致数据库抛出异常。



我的问题是这个注释的排列是否正确?

解决方案

还可以添加DetailsPrimaryKey映射吗?



在你的映射中看起来很奇怪的是,你在映射两次'master_pk_1'列在细节故事。通过添加insertable = false选项,现在插入工作,但我猜更新仍然不工作(实际上,当您在实体中多次映射列时,必须使用insertable = false,updatable = false注释除了其中的一个) / p>

为了纠正这一点,我想你应该移动@ManyToOne Master到detail id(删除insertable = false选项)。



您还应该向@OneToMany注释中添加选项mappedBy =detailsPrimaryKey.master,如果您希望子对象被自动保存,也可以是级联。



(请参阅 http://beavercreekconsulting.com/blog/2008/10/hibernate-annotations-for-a-one-to-many-mapping/ 你会看到一个解决与你的问题非常相似的代码)


I'm stuck with a database which is using multi-field primary keys. I have a situation where I have a master and details table, where the details table's primary key contains fields which are also the foreign key's the the master table. Like this:

Master primary key fields:
    master_pk_1

Details primary key fields:
    master_pk_1
    details_pk_2
    details_pk_3

In the Master class we define the hibernate/JPA annotations like this:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idGenerator")
@Column(name = "master_pk_1")
private long masterPk1;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name = "master_pk_1", referencedColumnName = "master_pk_1")
private List<Details> details = new ArrayList<Details>();

And in the details class I have defined the id and back reference like this:

@EmbeddedId
@AttributeOverrides( { 
        @AttributeOverride( name = "masterPk1", column = @Column(name = "master_pk_1")),
        @AttributeOverride(name = "detailsPk2", column = @Column(name = "details_pk_2")),
        @AttributeOverride(name = "detailsPk2", column = @Column(name = "details_pk_2")) })
private DetailsPrimaryKey detailsPrimaryKey = new DetailsPrimaryKey();

@ManyToOne
@JoinColumn(name = "master_pk_1", referencedColumnName = "master_pk_1", insertable=false)
private Master master;

The goal of all of this was that I could create a new master, add some details to it, and when saved, JPA/Hibernate would generate the new id for master in the masterPk1 field, and automatically pass it down to the details records, storing it in the matching masterPk1 field in the DetailsPrimaryKey class. At least that's what the documentation I've been looking at implies.

What actually happens is that hibernate appears to corectly create and update the records in the database, but not pass the key to the details classes in memory. Instead I have to manually set it myself.

I also found that without the insertable=true added to the back reference to master, that hibernate would create sql that had the master_pk_1 field listed twice in the insert statement, resulting in the database throwing an exception.

My question is simply is this arrangement of annotations correct? or is there a better way of doing it?

解决方案

Could you add the DetailsPrimaryKey mapping also ?

What seems strange in your mapping is that you are mapping twice the 'master_pk_1' column in the details tale. By adding the insertable=false option, now inserting works, but i guess updates still not works (in fact when you map columns more than once in an entity you must annotate all but one of them with insertable=false, updatable=false)

To correct that, i think you should move the @ManyToOne Master to the detail id (removing the insertable=false option).

You should also add the option mappedBy="detailsPrimaryKey.master" to the @OneToMany annotation, and maybe also a cascade if you wish the childs to be automatically saved.

(look at http://beavercreekconsulting.com/blog/2008/10/hibernate-annotations-for-a-one-to-many-mapping/ you will see a code solving something quite similar to your problem)

这篇关于使用hibernate / JPA注释映射多字段主键的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆