Hibernate - 复合主键包含外键

Hibernate - Composite Primary Key contains Foreign Key(Hibernate - 复合主键包含外键)

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

问题描述

我有一个类似的问题如下,但解决方案没有解决我的问题.

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

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

我正在尝试加入 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.

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

本文标题为:Hibernate - 复合主键包含外键

基础教程推荐