[英]jpa hibernate one-to-one update
I'm working on an apartment management software and I'm having an issue. 我正在开发公寓管理软件,但遇到了问题。
There is two entities I have : 我有两个实体:
@Entity
public class Tenant extends AbstractEntity { //AbstractEntity contains the id
@Column(nullable = false)
private int number;
@OneToOne
private Apartment apartment;
}
and 和
@Entity
public class Apartment extends AbstractEntity { //AbstractEntity contains the id
@Column(nullable = false)
private int number;
@OneToOne
private Tenant tenant;
}
But when I do 但是当我这样做
EntityManager em = emProvider.get();
em.getTransaction().begin();
em.merge(apartment);
em.flush();
em.getTransaction().commit();
It only save the Tenant into the Apartment but I would like it also update the Apartment into the Tenant. 它仅将租户保存到公寓中,但我也希望将公寓更新为租户。
Do I really need to set the apartment field into the tenant or there is a way to fix it simply? 我是否真的需要将“公寓”字段设置为租户,或者有一种简单的解决方法? Thanks 谢谢
Cordially, Baskwo 亲切地,Baskwo
You need to declare CascadeType.ALL
in your Apartment entity. 您需要在Apartment实体中声明CascadeType.ALL
。 See sample 查看样本
@OneToOne(cascade = CascadeType.ALL)
private Tenant tenant;
CascadeType.ALL is for all CRUD operation. CascadeType.ALL用于所有CRUD操作。 Adjust CascadeType depends on your application needs. 调整CascadeType取决于您的应用程序需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.