[英]Hibernate owning entity side with shared primary key
I was reading the following article : 我正在阅读以下文章:
http://websystique.com/hibernate/hibernate-one-to-one-bidirectional-with-shared-primary-key-annotation-example/ http://websystique.com/hibernate/hibernate-one-to-one-bidirectional-with-shared-primary-key-annotation-example/
and I was confused with two things : 我对两件事感到困惑:
1- who is the owning side of the entity. 1-谁是实体的拥有方。 from the article : "we should tell hibernate that it's the Student class which will manage the relationship."
从文章中得出:“我们应该告诉休眠状态,它将由学生类来管理关系。”
However the student entity is annotated with mappedBy, and I know that any entity annotated with mappedBy is the inverse side. 但是,该学生实体使用mapedBy进行注释,并且我知道任何使用mapedBy进行注释的实体都是反面。 can anyone please explain this ?
谁能解释一下吗?
@Entity
@Table(name = "STUDENT")
public class Student {
@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
private long id;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "SECTION")
private String section;
@OneToOne(mappedBy="student", cascade = CascadeType.ALL)
private Address address;
2- which ID is the foreign key? 2-外键是哪个ID? the @PrimaryKeyJoinColumn is used in Address entity , is that means the Address Id is the primary and foreign key ?
@PrimaryKeyJoinColumn用于Address实体,是否意味着Address ID是主键和外键? in the following article they have the same example but its unidirectional :
在下面的文章中,它们具有相同的示例,但它是单向的:
http://websystique.com/hibernate/hibernate-one-to-one-unidirectional-with-shared-primary-key-annotation-example/ http://websystique.com/hibernate/hibernate-one-to-one-unidirectional-with-shared-primary-key-annotation-example/
you can notice the location of @PrimaryKeyJoinColumn is changed and still the address is the foreign key. 您会注意到@PrimaryKeyJoinColumn的位置已更改,但该地址仍然是外键。 which Id will be the foreign key.
该ID将是外键。
In this example, Address is indeed the owning side of the association. 在此示例中,地址确实是关联的拥有方。 You're absoluetely right: the side that has the mappedBy attrobute is the inverse side.
您绝对正确:具有mappedBy attrobute的一面是反面。
Since Address is the owner side, and since its ID is also a join column, it should also be a foreign key (just like a join column of a ManyToOne association should also be a FK). 由于Address是所有者方,并且因为它的ID也是连接列,所以它也应该是外键(就像ManyToOne关联的连接列也应该是FK)一样。 Note that this choice means that an address can't exist without a student.
请注意,此选择意味着没有学生就无法存在地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.