简体   繁体   English

JPA单向@OneToOne,应在另一个表中创建列

[英]Jpa unidirectional @OneToOne where columns should be created in another table

I have Employee and Department requirement, where I want a field department in the employee entity, but I want department to hold foreign key, rather than employee holding the DEPT_ID. 我有“员工和部门”要求,我希望在员工实体中建立一个现场部门,但我希望部门持有外键,而不是持有DEPT_ID的员工。 The later can be easily achieved using @JoinColumn. 使用@JoinColumn可以轻松实现后者。 Mapped by is not an option, as it is unidirectional. 映射不是一个选项,因为它是单向的。

Do you know if there is any way to do so? 你知道有什么办法吗?

It is exactly the JoinColumn you should use and set the attributes of the annotation the correct way. 正是您应该使用JoinColumn并以正确的方式设置注释的属性。

class Department {

  @OneToOne
  @JoinColumn(name="ID",referencedColumnName = "here comes the foreign key departmentID ")
  Employee employee;
}

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

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