简体   繁体   English

拥有共享主键的Hibernate拥有实体端

[英]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将是外键。

  1. 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的一面是反面。

  2. 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.

相关问题 Hibernate Java 中的关系映射 - 保存拥有方实体 - Relationship mapping in Hibernate Java - save owning side entity 在休眠中添加共享主键和主键 - Adding shared primary key and a primary key in hibernate JPA /休眠-共享主键 - JPA/Hibernate - shared primary key Hibernate,如果拥有的实体相同,则使用已保存在数据库中的实体与拥有方链接,而不是创建新实体 - Hibernate, use the entity already persisted in DB to link with owning side instead of creating a new one if the owned entity is same 使用共享主键(双向)休眠 @OneToOne。 依赖实体未保留在 DB 中。 - Hibernate @OneToOne with Shared Primary Key(bidirectional). Dependent entity not persisted in DB. 为什么在 JPA 2/Hibernate 中使用共享主键时实体需要可序列化? - Why does an Entity need to be serializable when using shared primary key in JPA 2/Hibernate? 如何在不通过休眠中的共享主键以一对一关系插入子对象的情况下保存父实体? - How to save parent entity without inserting child in one to one relationship by shared primary key in hibernate? 休眠:在HQL中使用共享主键 - Hibernate: using shared primary key in HQL 共享主键生成。 冬眠 - Shared primary key generation. Hibernate 从Hibernate中的非拥有实体查询拥有实体 - Querying the owning entity from the non-owning entity in Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM