My solution was to duplicate the information of the relation one to one in the entity A
@Entity
public class A {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Embedded
private B b;
@OneToOne(mappedBy="a", cascade = CascadeType.ALL, orphanRemoval = true)
private C c;
public void setB(final Optional<B> b) {
b.ifPresentOrElse(newB -> {
newB.getC().ifPresent(c -> {
c.setA(this);
this.b = b;
}, () -> {
this.c = null;
this.b = null;
});
}
// other getters and setters
}
Is there any way to not duplicate the information of entity C in A and maintain the correct behavior?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…