我有以下单向多对一映射
@Entity
public class Item implements Serializable {
private Integer id;
private Double amount;
private Country origin;
@ManyToOne(optional=true)
@JoinColumn
public Country getOrigin() {
return this.origin;
}
}
@Entity
public class Country implements Serializable{
private String code;
private String desc;
}
假设该关系是可选的,因此我尝试使用下面的代码将其更新为 null 来删除该关系
Country country = null;
//item is detached
item.setOrigin(country);
em.merge(item);
但结果却是关系并没有消除。
但是,如果 country
不为空并且系统可以更新数据库中的关系,则此代码可以正常工作。
如果该字段为空,它只是简单地忽略该字段。
有人可以指出可以更改哪些设置才能达到我想要的结果吗?
附注请注意,我不想删除实体 Country
,只是删除它们之间的关系。
最佳答案
谢谢大家,这是一个错误的问题。它确实有效。
只是存在一些客户端问题,向其提交了错误的数据。
关于java - 使用 merge() 删除 JPA 中的多对一关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9427326/