Java Hibernate注解与外键关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3350922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate annotations and foreign key relationship
提问by user378101
I have a domain object annotated like this for hibernate support.
我有一个像这样注释的域对象以支持休眠。
@Entity
@Table(name = "INPUT")
public class AppInput {
/**
* Unique id for this request
*/
@Id
@GeneratedValue
@Column(name = "INPUT_ID")
private long requestId;
/**
*
*/
@Column(name = "EMAIL_ID")
private String emailId;
/**
*
*/
@Column(name = "REQUEST_DATE")
private Date requestDate;
/**
*
*/
@Column(name = "INPUT_STATUS")
private char status;
/**
*
*/
@Column(name = "EXPECTED_ORDER_DATE")
private Date expectedOrdDt;
//Getter and setters
}
The property emailId is a foreign key referring to say emailId column in User table. Lets say i add a property like this to AppInput.javaprivate User userDetails;
How do i annotate this so that, whenever i fetch AppInput from db, the corresponding user details also get populated?
属性 emailId 是一个外键,指的是 User 表中的 emailId 列。假设我将这样的属性添加到 AppInput.javaprivate User userDetails;
我如何对此进行注释,以便每当我从数据库中获取 AppInput 时,相应的用户详细信息也会被填充?
采纳答案by Pascal Thivent
The property emailId is a foreign key referring to say emailId column in User table.
属性 emailId 是一个外键,指的是 User 表中的 emailId 列。
Then don't add the emailId
property, add a User
.
然后不要添加emailId
属性,添加一个User
.
(...) How do i annotate this so that, whenever i fetch AppInput from db, the corresponding user details also get populated?
(...) 我如何对此进行注释,以便每当我从数据库中获取 AppInput 时,也会填充相应的用户详细信息?
Not sure since it could be a ManyToOne
or OneToOne
but I'll assume it's a ManyToOne
:
不确定,因为它可能是一个ManyToOne
或OneToOne
但我假设它是一个ManyToOne
:
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="USERDETAILS_EMAIL_ID", referencedColumnName="EMAIL_ID")
private User userDetails;
The fetch
annotation element is for demonstration purpose, EAGER
being actually the default value. The name
and referencedColumn
annotation elements in JoinColumn
are also optional. Below a short summary from the JPA specification:
该fetch
注释元素用于演示目的,EAGER
实际上是默认值。中的name
和referencedColumn
注释元素 JoinColumn
也是可选的。以下是 JPA 规范的简短摘要:
11.1.21 JoinColumn Annotation
The
JoinColumn
annotation is used to specify a column for joining an entity association or element collection.Table 20 lists the annotation elements that may be specified for the
JoinColumn
annotation and their default values.If the
JoinColumn
annotation itself is defaulted, a single join column is assumed and the default values described in Table 20 apply.The
name
annotation element defines the name of the foreign key column. The remaining annotation elements (other thanreferencedColumnName
) refer to this column and have the same semantics as for theColumn
annotation.If the
referencedColumnName
element is missing, the foreign key is assumed to refer to the primary key of the referenced table.
11.1.21 JoinColumn 注解
的
JoinColumn
注释用于接合的实体关联或元素集合指定列。表 20 列出了可以为
JoinColumn
注释指定的注释元素 及其默认值。如果
JoinColumn
注释本身是默认值,则假定为单个连接列,并应用表 20 中描述的默认值。该
name
注释元素定义了外键列的名称。其余的注释元素(除了referencedColumnName
)引用此列并且具有与Column
注释相同的语义。如果
referencedColumnName
缺少该 元素,则假定外键引用所引用表的主键。
See the Table 20in the spec for complete and exhaustive details.
有关完整和详尽的详细信息,请参阅规范中的表 20。