[英]How to define a Hibernate OneToOne unidirectional mapping in Parent where the FK column is in the child?
Background: I'm in the process of upgrading to Hibernate 6.1.4 (from 5.3.x) and have run into problems with OneToOne bidirectional mappings (which appears to be a bug, and I've written up).背景:我正在升级到 Hibernate 6.1.4(从 5.3.x)并且遇到了 OneToOne 双向映射的问题(这似乎是一个错误,我已经写了)。 I'm looking for a workaround that doesn't require changing the schema and am considering making the mapping unidirectional but have run into an issue.
我正在寻找一种不需要更改模式的解决方法,并且正在考虑使映射成为单向的,但遇到了问题。
Here's a simplified version of the starting point:这是起点的简化版本:
@Entity
@Table(name = "PARENT_T")
public class Parent {
@Id
@Column(name = "PARENT_PK")
private Integer id;
@OneToOne(targetEntity = Child.class, mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Child child;
// getters and setters...
}
@Entity
@Table(name = "PARENT_T")
public class Child {
@Id
@Column(name = "CHILD_PK")
private Integer id;
@OneToOne(targetEntity = Parent.class, fetch = FetchType.EAGER)
@JoinColumn(name = "PARENT_FK", nullable = false)
private Parent parent;
// getters and setters...
}
So, I would like to remove the Child-to-Parent mapping, and just map the attribute:所以,我想删除 Child-to-Parent 映射,只删除 map 属性:
@Column(name = "PARENT_FK", nullable = false)
private Long parentFK;
However, this means that the mappedBy = "parent"
in the Parent is no longer valid.但是,这意味着 Parent 中的
mappedBy = "parent"
不再有效。 I can add a JoinColumn annotation, but per the docs, the JoinColumn name is in the source entity (here, Parent):我可以添加一个 JoinColumn 注释,但根据文档,JoinColumn 名称在源实体中(此处为 Parent):
The name of the foreign key column.
外键列的名称。 The table in which it is found depends upon the context.
它所在的表取决于上下文。
If the join is for a OneToOne or ManyToOne mapping using a foreign key mapping strategy, the foreign key column is in the table of the source entity or embeddable.
如果联接是针对使用外键映射策略的 OneToOne 或 ManyToOne 映射,则外键列在源实体的表中或可嵌入。
I saw a suggestion to use a OneToMany mapping, since:我看到了使用 OneToMany 映射的建议,因为:
If the join is for a unidirectional OneToMany mapping using a foreign key mapping strategy, the foreign key is in the table of the target entity.
如果连接是针对使用外键映射策略的单向 OneToMany 映射,则外键位于目标实体的表中。
... and then treat it as a One-to-One. ...然后将其视为一对一。 However, this seems like a kludge.
然而,这似乎是一种拼凑。
So: Is there a way to map a OneToOne relationship where the foreign key column lies with the target entity (here: Child), rather than the source (here: Parent)?所以:有没有办法 map 一个 OneToOne 关系,其中外键列位于目标实体(此处:Child),而不是源(此处:Parent)?
Conceptually, I'm just looking for a table equivalent of mappedBy in the annotation, Something like: @OneToOne(targetEntity = Child.class, mappedByColumn = "PARENT_FK", cascade = CascadeType.ALL, fetch = FetchType.LAZY)从概念上讲,我只是在注释中寻找与 mappedBy 等效的表,例如:@OneToOne(targetEntity = Child.class, mappedByColumn = "PARENT_FK", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
Thanks!谢谢!
Try this mapping here:在此处尝试此映射:
@MapsId
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_PK", referencedColumnName = "PARENT_FK")
private Child child;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.