jpa hibernate一对一更新
jpa hibernate one-to-one update
我正在开发公寓管理软件,但遇到了问题。
我有两个实体:
@Entity
public class Tenant extends AbstractEntity { //AbstractEntity contains the id
@Column(nullable = false)
private int number;
@OneToOne
private Apartment apartment;
}
和
@Entity
public class Apartment extends AbstractEntity { //AbstractEntity contains the id
@Column(nullable = false)
private int number;
@OneToOne
private Tenant tenant;
}
但是当我这样做时
EntityManager em = emProvider.get();
em.getTransaction().begin();
em.merge(apartment);
em.flush();
em.getTransaction().commit();
它只会将租户保存到公寓中,但我希望它还能将公寓更新到租户中。
我真的需要将公寓字段设置到租户中吗?还是有办法简单地解决它?
谢谢
亲切地,
巴斯沃
您需要在您的公寓实体中声明 CascadeType.ALL
。查看示例
@OneToOne(cascade = CascadeType.ALL)
private Tenant tenant;
CascadeType.ALL 用于所有 CRUD 操作。调整 CascadeType 取决于您的应用需求。
我正在开发公寓管理软件,但遇到了问题。
我有两个实体:
@Entity
public class Tenant extends AbstractEntity { //AbstractEntity contains the id
@Column(nullable = false)
private int number;
@OneToOne
private Apartment apartment;
}
和
@Entity
public class Apartment extends AbstractEntity { //AbstractEntity contains the id
@Column(nullable = false)
private int number;
@OneToOne
private Tenant tenant;
}
但是当我这样做时
EntityManager em = emProvider.get();
em.getTransaction().begin();
em.merge(apartment);
em.flush();
em.getTransaction().commit();
它只会将租户保存到公寓中,但我希望它还能将公寓更新到租户中。
我真的需要将公寓字段设置到租户中吗?还是有办法简单地解决它? 谢谢
亲切地, 巴斯沃
您需要在您的公寓实体中声明 CascadeType.ALL
。查看示例
@OneToOne(cascade = CascadeType.ALL)
private Tenant tenant;
CascadeType.ALL 用于所有 CRUD 操作。调整 CascadeType 取决于您的应用需求。