Situation
使用Spring (数据)和Hibernate。relationship.
@OneToOne的实体共享相同的密钥(使用@PrimaryKeyJoinColumn))
问题
<代码>F 223
在守则中制定:
a = repositoryForA.findById(a);
b = a.getB();
a.setAttribute(abc);
b.setAttribute(xyz);
repositoryForA.save(a);然而,我看到的行为是,总是同时存储A和B(通过测试和hibernate日志验证)。
我试过什么
entityManager.detach(b); repository.save(a)
a.setB(null)和保存前分离
这个是可能的吗?为了这个目的,这种关系可能不好吗?
实体
@Entity
public class A {
@Id
@GeneratedValue
protected Long id;
@OneToOne
@PrimaryKeyJoinColumn
private B b;
// Some attributes...
}
@Entity
public class B {
@Id
protected Long id;
@OneToOne(mappedBy = "b")
@MapsId
@JoinColumn(name = "id")
private A a;
// Some attributes...
}发布于 2021-06-21 22:18:18
我使用了@K.Nicholas提出的类似解决方案。我刚刚创建了实体B的副本来执行临时计算。我没有给repositoryForB.save(b)打电话。因此,在保存a时,只存储a本身的属性。
https://stackoverflow.com/questions/68072175
复制相似问题