休眠-复合主键包含外键 [英] Hibernate - Composite Primary Key contains Foreign Key

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

问题描述

我有一个与下面类似的问题,但是解决方案无法解决我的问题.

I have a similar question as below, but the solution didn't solve my problem.

休眠复合主键键包含一个复合外键,该键如何映射

我正在尝试联接2个表,每个表都有一个带有部分外键引用的复合主键.

I am trying to join 2 tables, each having a composite primary key with partial foreign key reference.

Table A
--------
f1 (pk)
f2 (pk)
f3 (pk)
f4 (pk)

Table B
--------
f1 (pk, fk)
f2 (pk, fk)
f5 (pk)
f6 (pk)

I created A, APK, B, BPK

在A:

private Set<B> bSet;
@OneToMany(targetEntity=B.class, cascade = CascadeType.ALL, mappedBy= "bpk.a")    
public Set<MovesEntity> getBSet() {
    return bSet;
}

在BPK中:

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumns({
    @JoinColumn(name="f1", referencedColumnName="f1", nullable=false, insertable=false, updatable = false),
    @JoinColumn(name="f2", referencedColumnName="f2", nullable=false, insertable=false, updatable = false)
})
public A getA() {
    return a;
}

以上方法给了我这个例外:

The above approach gives me this Exception:

AnnotationException: referencedColumnNames(f1, f2) of entity.BPK.bpk.a 
referencing com.example.entity.A not mapped to a single property

可以请您帮忙吗?

推荐答案

假设f1和F2唯一标识A并存在于APK中,则可以通过几种方式使用JPA 2.0的派生ID.最容易显示的是:

Assuming f1 and F2 uniquely identify A and exist within APK, you can use JPA 2.0's derived IDs for this in a few ways. Easiest to show would be:

@Entity
@IdClass(BPK.class)
public class B {
  @ID
  String f5;
  @ID
  String f6;
  @ID
  @ManyToOne(fetch=FetchType.EAGER)
  @JoinColumns({
    @JoinColumn(name="f1", referencedColumnName="f1", nullable=false),
    @JoinColumn(name="f2", referencedColumnName="f2", nullable=false)
  })
  A a;
}

public class BPK {
  String f5;
  String f6;
  APK a;
}

关键点是B对A的引用控制了外键字段f1和f2,并且A的主键用于B的主键中-与关系的名称相同.映射它的另一种方法是将B的PK设置为一个嵌入式ID,但是嵌入式ID仍不能具有引用映射,因此它可能看起来像:

Key points here are that B has a reference to A that control the foriegn key fields f1 and f2, and A's primary key is used within B's primary key - with the same name as the relationship. Another way to map it would be to make B's PK an embeddid id, but embedded IDs still cannot have reference mappings, so it might look:

@Entity
@IdClass(BPK.class)
public class B {
  @EmbeddedId
  BPK pk;
  @MapsId("apk")
  @ManyToOne(fetch=FetchType.EAGER)
  @JoinColumns({
    @JoinColumn(name="f1", referencedColumnName="f1", nullable=false),
    @JoinColumn(name="f2", referencedColumnName="f2", nullable=false)
  })
  A a;
}

@Embeddable
public class BPK {
  String f5;
  String f6;
  APK apk;
}

请注意mapsId-这告诉JPA嵌入式'apk'引用中的列使用从A提取的引用映射中的外键字段.JPA将从引用映射中为您填充外键,如果您正在使用测序.

Notice the mapsId - this tells JPA that the columns in the embedded 'apk' reference use the foreign key fields from the reference mapping as pulled from A. JPA will populate the foreign keys for you from the reference mapping, important if you are using sequencing.

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

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