首页 > 解决方案 > Spring java一对一关系将实体重新分配给所有者

问题描述

我有一个与 B 一对一相关的实体 A。我创建了一个外键:A.b_id <==> B.id

 A { @OneToOne(orphanRemoval = true, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER) @JoinColumn(name = "b_id") private B b; }

在某些时候,我会做 A.setB(new B());

但后来我希望能够将其更改为 => A.setB(new B2());

这样做时,我收到以下错误:

update A set ....., b_id=? where id=? []; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

知道我错过了什么吗?

b_id有一个不为空的约束。

映射看起来像

A { 
    @OneToOne(orphanRemoval = true, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER) 
    @JoinColumn(name = "b_id") 
    private B b; 
} 

标签: javaspringjpaone-to-one

解决方案


推荐阅读