简体   繁体   English

休眠主外键字段

[英]Hibernate Primary Foreign Key field

I have 2 simple tables in database. 我在数据库中有2个简单的表。 They look like: 他们看着像是:

T1                                      T2
ID    NUMBER     Primary key            ID    NUMBER    Primary key & Foreign key from T1
Value VARCHAR                           Value VARCHAR

How does hibernate entity for T2 look like? T2的休眠实体看起来如何? I tried to do it with @Embeddable class containing T1 mapped-class object but this doesn't work. 我尝试使用包含T1映射类对象的@Embeddable类来执行此操作,但这不起作用。 Thanks. 谢谢。

UPD: the full use case when I need such a structure is below: I have business entities tables, containing some data for specific business users, and Company table with Id and value fields too, and I want to create CompanyToBEntity table, containing data about what company can access what object.(objects are any row of bus.entities). UPD:当我需要这种结构时,其完整用例如下:我有一些业务实体表,其中包含一些特定业务用户的数据,还有具有ID和值字段的Company表,并且我想创建CompanyToBEntity表,其中包含有关什么公司可以访问什么对象。(对象是bus.entities的任何行)。

So I think this structure fits this case. 因此,我认为这种结构适合这种情况。

pic describing it better: 图片更好地描述了它:

在此处输入图片说明

You can try Unidirectional one-to-one association vi primary key association something like - 您可以尝试Unidirectional one-to-one association vi primary key association ,例如-

T1 Mapping T1对应

@Id  
@Column(name="ID")  
private Integer ID; 

T2 Mapping T2对应

@OneToOne(cascade=CascadeType.ALL)  
@PrimaryKeyJoinColumn  
private T1 t1;

For more reference you visit here , the example is based on hbm.xml 有关更多参考,请访问此处 ,该示例基于hbm.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM