jpa - 具有复合主的 JPA 实体也是同一个表的外键
问题描述
我有两个表 A 和 B。表 A 中有两列 col1 和 col2(这两列都是主键,即与 col1 和 col2 复合)。表 B 中有一列,表 A 中的两列都指向该列,即 col1 和 col2 是与表 B 中的列相关的外键。
如何为表 A 实现 JPA 实体?
谢谢
解决方案
那么您可以通过以下代码实现:
@Embeddable
public class AID {
public int xID;
public int yId;
}
@Entity
public class A {
@EmbeddedId
public AID id;
@OneToMany(mappedBy="A")
public Collection<B> b;
}
@Entity
public class Meeting {
@ID
@GeneratedValue
public Long id;
@MapsId("aID")
@JoinColumns({
@JoinColumn(name="xID", referencedColumnName="xID"),
@JoinColumn(name="yId", referencedColumnName="yId")
})
@ManyToOne
public A a;
}
推荐阅读
- javascript - 重定向弹出窗口并发布消息
- perl - 使用 ExifTool 从两个文件(一个原始文件,一个 XMP)读取
- python - 如何在python中将字符串转换为字节?
- c# - 如何修复“CCertRequest::Submit: RPC 服务器不可用。0x800706ba”错误?
- python - 来自 int(base64.urlsafe_b64decode(mystr)) 的“无效文字”
- node.js - 使用 puppeteer 在 node.js 应用程序上进行密集 pdf 生成任务的可扩展性?
- java - 实例内的 Arraylist 不是私有的
- node.js - 语法错误:在 AngularJS 中替换控制器后出现意外令牌 <
- php - Wordpress 中的 PHP 内部简码函数
- c - 库存计划,可能的边界值问题?