简体   繁体   English

如何在非主键上使用休眠注释设置单向一对一关系?

[英]how to set unidirectional one-to-one relationship using hibernate annotation on a non-primary key?

i have set an unidirectional one-to-one mapping tables. 我已经设置了单向一对一映射表。 here is my simplified version of its implementation. 这是其实现的简化版本。

// part of parent table called "Person"
@Id
@Column(name="id")
private String id;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="id", referencedColumnName = "person_id")
private PersonDetails details;

and then... 接着...

// part of child table called "PersonDetails"
@Id
@Column(name="id")
private String Id;

@Column(name="person_id")
private String personId;

this does not seem to work because hibernate sql log shows that hibernate is trying to do a join on Person and PersonDetails on " Person.Id=PersonDetails.Id" when what i really want is "Person.Id=PersonDetails.personId". 这似乎不起作用,因为当我真正想要的是“ Person.Id = PersonDetails.personId”时,hibernate sql日志显示hibernate试图对“ Person.Id = PersonDetails.Id”上的Person和PersonDetails进行联接。

how can i achieve this? 我怎样才能做到这一点?

I think you should move the owning side into PersonDetails and use an inverse side into Person. 我认为您应该将拥有方移到PersonDetails中,并将相反方移到Person中。 This will imply using a bidirectional association but I've been successfully using this pattern in one of my previous projects: 这将意味着使用双向关联,但是我已经在之前的项目之一中成功使用了这种模式:

Person: 人:

// part of parent table called "Person"
@Id
@Column(name="id")
private String id;

@OneToOne(mappedBy="person", cascade = CascadeType.ALL)
private PersonDetails details;

PersonDetails: 人员详细信息:

@Id
@Column(name="id")
private String Id;

@OneToOne
@JoinColumn(name="person_id")
private Person person;

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

相关问题 JPA / Hibernate使用共享主键进行单向一对一映射 - JPA / Hibernate unidirectional one-to-one mapping with shared primary key 如何 map 一对多结果到使用非主键连接的 Hibernate 实体? - How to map a one-to-many result to a Hibernate entity which is joined using a non-primary key? Spring Boot / Hibernate:如何定义一对一的单向关系 - Spring Boot/Hibernate: How to define one-to-one unidirectional relationship 休眠外键一对一单向 - Hibernate One-to-One unidirectional on foreign key 如何正确设置休眠单向一对一映射 - how to properly set up hibernate unidirectional one-to-one mapping 如何正确级联在Hibernate 3.6中保存主键上的一对一双向关系 - How do I properly cascade save a one-to-one, bidirectional relationship on primary key in Hibernate 3.6 如何确保 hibernate 5 在与共享主键的一对一关系中以正确的顺序持续存在 - How to make sure hibernate 5 persists in the correct order in a one-to-one relationship with shared primary key 使用连接表的Hibernate一对一单向映射 - Hibernate One-to-One Unidirectional mapping using join table Spring Hibernate查询与非主键的一对一关系 - Spring Hibernate Query One to One relationship with non primary key 休眠5,将外键作为主键,单向一对一 - hibernate 5, foreign key as primary key, unidirectional one to one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM