Hibernate 外键作为主键的一部分
我必须使用 Hibernate,但不太确定如何解决这个问题,我有 2 个具有 1..n 关系的表,如下所示:
------- TABLE_A ------- first_id (pk) second_id (pk) [other fields] ------- TABLE_B ------- first_id (pk)(fk TABLE_A.first_id) second_id (pk)(fk TABLE_A.second_id) third_id (pk) [other fields]
我如何使用 Hibernate 来管理这个问题???
我不知道如何管理第二个表的主键......
I have to work with hibernate and not very sure how solve this problem, I've 2 table with a 1..n relationship like this:
------- TABLE_A ------- first_id (pk) second_id (pk) [other fields] ------- TABLE_B ------- first_id (pk)(fk TABLE_A.first_id) second_id (pk)(fk TABLE_A.second_id) third_id (pk) [other fields]
How can I manage this with Hibernate???
I don't have idea how to manage the primary key for the second table...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Hibernate 参考文档<中有一个与您的情况完全相似的示例/a>.就在这个示例之前,您会找到解释。下面是与您的问题相匹配的示例(用户是表 A,客户是表 B):
注意:如果您为这两个表提供了代理标识符,则会简单得多。除非您被迫处理遗留模式,否则请帮自己一个忙并使用代理键。
There is an example which is completely similar to your case in the Hibernate reference documentation. Just before this example, you'll find the explanations. Here's the example, which matches your problem (User is table A, and Customer is table B):
Note: it would be much much simpler of you had a surrogate identifier for those two tables. Unless you're forced to deal with a legacy schema, do yourself a favor and use surrogate keys.
使用 @PrimaryKeyJoinColumn 和 @PrimaryKeyJoinColumns 注释。来自 Hibernate 手册:
Use
@PrimaryKeyJoinColumn
and@PrimaryKeyJoinColumns
annotations. From Hibernate manual:对于表A,您可以使用embeddedId
对于表B,您也可以使用Idclass,如下所示
For Table A you can make you of embeddedId
For Table B you can use Idclass also as shown below