[英]Hibernate - synchronize two properties mapping the same column
Is it possible to keep sync two properties on the same entity mapped to the same column (with basic JPA, or with hibernate)? 是否可以在映射到同一列的同一实体上保持同步两个属性(使用基本JPA或使用hibernate)?
I have two properties: 我有两个属性:
@JoinColumn(referencedColumnName = "id", insertable = false, name = "parent", updatable = false)
@ManyToOne(fetch = FetchType.LAZY)
private Person parent;
@Column(name = "parent")
private Integer parentId;
Basically, I would like the following: 基本上,我想要以下内容:
System.out.println(element.getParentId());
System.out.println(element.getParent().getId());
element.setParentId(2);
System.out.println(element.getParentId());
System.out.println(element.getParent().getId());
This to print "3,3,2,2" instead of "3,3,2,3". 这打印“3,3,2,2”而不是“3,3,2,3”。
Is it possible (without writing a custom lazy-loading on the getParent() getter?) 是否可以(没有在getParent()getter上编写自定义延迟加载?)
Thx in advance Thx提前
IIRC, Hibernate will not do that for you. IIRC,Hibernate不会为你做那件事。 You're the one responsible if you have something like that (I remembered doing something similiar).
如果你有这样的东西,你就是那个人(我记得做过类似的事情)。
The first solution will be to save the entity first, then refresh using the entity manager. 第一种解决方案是先保存实体,然后使用实体管理器进行刷新。
element.setParentId(2);
entityManager.merge(element);
entityManager.refresh(element);
Refresh will make it fetch the object from the database again, and it will have the correct value. 刷新将使它再次从数据库中获取对象,并且它将具有正确的值。
The second option which I did, was to change the setter of the entity to maintain the state. 我做的第二个选项是更改实体的setter以维持状态。 Something like:
就像是:
public void setParent(Parent parent) {
this.parent = parent;
parentId = parent.id;
}
Try this 试试这个
@JoinColumn(referencedColumnName = "id", insertable = false, name = "parent")
@ManyToOne(fetch = FetchType.LAZY)
private Person parent;
@Column(name = "parent")
private Integer parentId;
If that don't work: 如果那不起作用:
Try this if parentId is the primary key of the Parent entity: 如果parentId是Parent实体的主键,请尝试此操作:
@JoinColumn(referencedColumnName = "id", insertable = false, name = "parent")
@ManyToOne(fetch = FetchType.LAZY)
private Person parent;
@Id
@GeneratedValue(strategy = GenerateType.AUTO)
@Column(name = "parent")
private Integer parentId;
Try this: 尝试这个:
@JoinColumn(referencedColumnName = "id", name = "parent")
@ManyToOne(fetch = FetchType.LAZY)
private Person parent;
@Column(name = "parent")
private Integer parentId;
If that don't work: 如果那不起作用:
Try this: 尝试这个:
@JoinColumn(name = "parent")
@ManyToOne(fetch = FetchType.LAZY)
private Person parent;
@Column(name = "parent")
private Integer parentId;
You can have two properties for the same column like this : 您可以为同一列提供两个属性,如下所示:
@JoinColumn(name = "CGRADO_CODIGO", referencedColumnName = "CGRADO_CODIGO")
@ManyToOne
@NotFound(action=NotFoundAction.IGNORE)
private SipreGrado sipreGrado;
@Column(name = "CGRADO_CODIGO",insertable=false,updatable=false)
private String sipreGradoCodigo;
Your output in the inspect code will be like this : 检查代码中的输出将如下所示:
sipreGradoCodigo "001"
sipreGrado null
or 要么
sipreGradoCodigo "001"
sipreGrado Entity SipreGrado ["001","other property","etc"]
Remember if you have sometimes the entity NULL you can skip it with that annotation 请记住,如果有时实体为NULL,则可以使用该注释跳过它
@NotFound(action=NotFoundAction.IGNORE)
Also,Remember to set 另外,请记住设置
insertable=false,updatable=false
for the one that you dont want to include in the insert/update queries. 对于您不希望包含在插入/更新查询中的那个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.