Hibernate foreign key with a part of composite primary key(使用复合主键的一部分休眠外键)
问题描述
我必须使用 Hibernate,但我不太确定如何解决这个问题,我有 2 个表,其 1..n 关系如下:
我如何使用 Hibernate 来管理这个?
我不知道如何声明包含主键一部分的外键.
我的数据库架构是从 Hibernate 模型生成的.
这个问题我找到了两个解决方案.
第一个是一种解决方法,不如第二个那么整洁.
将B
实体的主键定义为包含col_a
、col_b
和col_c
的复合键,并且首先应该是主键,定义为唯一约束.缺点是列 col_c
在概念上并不是主键的一部分.
@EntityA类{@ID私人int b;@ID私人int c;}@实体@Table(uniqueConstraints = {@UniqueConstraint(columnNames = { "a", "b" }) })B类{@ID私人int a;@ID@ManyToOne(可选=假)@JoinColumns(值 = {@JoinColumn(name = "b", referencedColumnName = "b"),@JoinColumn(name = "c", referencedColumnName = "c") })私人A实体A;}
第二个使用 @EmbeddedId
和 @MapsId
注释,完全符合我一开始就想做的事情.
@EntityA类{@ID私人int b;@ID私人int c;}@可嵌入类 BKey {私人int a;私人int b;}@实体B类{@EmbeddedId私有 BKey 主键;@MapsId("b")@ManyToOne(可选=假)@JoinColumns(值 = {@JoinColumn(name = "b", referencedColumnName = "b"),@JoinColumn(name = "c", referencedColumnName = "c") })私人A实体A;}
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;
}
这篇关于使用复合主键的一部分休眠外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用复合主键的一部分休眠外键


- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01