[英]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.