Hibernate外键与复合主键的一部分 [英] Hibernate foreign key with a part of composite primary key

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

问题描述




我必须使用Hibernate,我不太清楚如何解决这个问题,我有两个表,1..n关系像这样:

 
-------
TABLE_A
-------
col_b(pk)
col_c(pk)
[其他字段]

-------
TABLE_B
-------
col_a (pk)
col_b(pk)(fk TABLE_A.col_b)
col_c(fk TABLE_A.col_c)
[其他字段]

我如何用Hibernate来管理它?



我不知道如何声明一个包含主要部分的外键key。



我的数据库模式是从Hibernate模型生成的。 我已经找到了解决这个问题的两个解决方案。

第一个解决方法是一个解决方法,不像第二个解决方案那么简单。 b

B 实体的主键定义为包含 col_a col_b co l_c ,首先应该是主键,定义为唯一约束。缺点是列 col_c 在概念上并不是主键的一部分。

  @Entity 
class A {
@Id
private int b;
@Id
private int c;

$ b $实体
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {a,b})})
class B {
@Id
private int a;

@Id
@ManyToOne(可选= false)
@JoinColumns(value = {
@JoinColumn(name =b,referencedColumnName =b) ,
@JoinColumn(name =c,referencedColumnName =c)})
private一个entityA;





第二个使用 @EmbeddedId @MapsId 注释,并且完全是我最初想做的事情。

  @Entity 
class A {
@Id
private int b;
@Id
private int c;
}

@Embeddable
class BKey {
private int a;
private int b;
}

@Entity
class B {
@EmbeddedId
private BKey primaryKey;

@MapsId(b)
@ManyToOne(可选= false)
@JoinColumns(value = {
@JoinColumn(name =b,referencedColumnName =b),
@JoinColumn(name =c,referencedColumnName =c)})
private一个entityA;
}



I have to work with Hibernate and I am not very sure how to solve this problem, I have 2 tables with a 1..n relationship like this:

-------
TABLE_A
-------
col_b (pk)
col_c (pk)
[other fields]

-------
TABLE_B
-------
col_a (pk)
col_b (pk) (fk TABLE_A.col_b)
col_c (fk TABLE_A.col_c)
[other fields]

How can I manage this with Hibernate?

I do not have any idea how to declare a foreign key that would contain a part of primary key.

My database schema is generated from the Hibernate model.

解决方案

I have found two solutions to this problem.

The first one is rather a workaround and is not so neat as the second one.

Define the primary key of the B entity as composite key containing col_a, col_b, and col_c and what was supposed to be the primary key in the first place, define as unique constraint. The disadvantage is that the column col_c is not really conceptually a part of primary key.

@Entity
class A {
  @Id
  private int b;
  @Id
  private int c;
}

@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = { "a", "b" }) })
class B {
  @Id
  private int a;

  @Id
  @ManyToOne(optional = false)
  @JoinColumns(value = {
          @JoinColumn(name = "b", referencedColumnName = "b"),
          @JoinColumn(name = "c", referencedColumnName = "c") })
  private A entityA;
}

The second uses @EmbeddedId and @MapsId annotations and does exactly what I wanted to be done at the very beginning.

@Entity
class A {
  @Id
  private int b;
  @Id
  private int c;
}

@Embeddable
class BKey {
  private int a;
  private int b;
}

@Entity
class B {
  @EmbeddedId
  private BKey primaryKey;

  @MapsId("b")
  @ManyToOne(optional = false)
  @JoinColumns(value = {
          @JoinColumn(name = "b", referencedColumnName = "b"),
          @JoinColumn(name = "c", referencedColumnName = "c") })
  private A entityA;
}

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

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