hibernate复合主键包含一个复合外键,如何映射这个 [英] hibernate composite Primary key contains a composite foreign key, how to map this

查看:117
本文介绍了hibernate复合主键包含一个复合外键,如何映射这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里搜索,没有找到任何类似的主题,所以我发布了一个新问题。



我正在Hibernate上使用现有的数据库。我们不允许更改表格结构和数据。应用程序正在从数据库中读取数据,并基于某种逻辑迁移到另一个数据存储。



现在问题是关于复合PK映射。例如

表A有一个复合PK。

 表A 
--------
a1(pk)
a2(pk)
a3(pk)
a4(pk)
foo
bar
========

表B有一个复合PK而且这个组合PK的一部分是A的PK,这里也以FK的形式工作。

 表B 
--------
a1(fk,pk)
a2(fk,pk)
a3(fk,pk)
a4(fk,pk)
b1(pk)
b2(pk)
b3 pk)
foo
bar
========



我尝试了几种方法,但都没有成功。任何人都可以告诉一个可用的Hibernate映射解决方案吗在注释风格中更好。

解决方案

在B的pk Class中将A的实体对象设置为@ManyToOne。



所以如果你有

  Class A 
Class APK - A的主键

B
类BPK - B的主键。

BPK 将包含 A 作为属性

  @Embeddable 
public class BPK implements serializable {
....
private A a;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name =...,referencedColumnName =...),
@JoinColumn(name =...,referencedColumnName =...),
...
})
public getA(){
return这是;


文档


@Embeddable继承
的拥有实体的访问类型,除非Hibernate
特定注释@AccessType使用
。使用
@JoinColumns元素(基本上是@JoinColumn数组)的关联定义了复合外键(如果不是使用默认敏感值的

。它
被认为是一个很好的做法,
明确表示referencedColumnNames
。否则,Hibernate将
假设您使用与主键
声明中相同的
列顺序。



I searched there, and didn't find any similar topic, so I am posting a new question.

I am working with Hibernate on an existing Database. The table structure and data we are not allowed to change. The application is reading data from the database and migrating to another datastore based on some logic.

Now the problem is about a composite PK mapping. e.g.

Table A has a composite PK.

Table A
--------
a1 (pk)
a2 (pk)
a3 (pk)
a4 (pk)
foo
bar
========

Table B has a composite PK too, and one part of this composite PK is A's PK, here is working as FK as well.

Table B
--------
a1 (fk,pk)
a2 (fk,pk)
a3 (fk,pk)
a4 (fk,pk)
b1 (pk)
b2 (pk)
b3 (pk)
foo
bar
========

I tried several ways, and none of them works. Can anyone tell a working Hibernate mapping solution? better in annotation style.

解决方案

Set up A's entity object as a @ManyToOne in B's pk Class.

So if you have

Class A
Class APK - A's Primary Key

Class B
Class BPK - B's primary Key.

BPK will contain A as an attribute

@Embeddable
public class BPK implements serializable {
  ....
  private A a;

  @ManyToOne(fetch=FetchType.EAGER)
  @JoinColumns ({
    @JoinColumn(name="...", referencedColumnName = "..."),
    @JoinColumn(name="...", referencedColumnName = "..."),
    ...
  })
  public getA() {
    return this.a;
  }
}

From the documentation

@Embeddable inherit the access type of its owning entity unless the Hibernate specific annotation @AccessType is used. Composite foreign keys (if not using the default sensitive values) are defined on associations using the @JoinColumns element, which is basically an array of @JoinColumn. It is considered a good practice to express referencedColumnNames explicitly. Otherwise, Hibernate will suppose that you use the same order of columns as in the primary key declaration.

这篇关于hibernate复合主键包含一个复合外键,如何映射这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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