I recently had the need to map a one-to-one entity from an embbeded entity:
@Entity
public class A {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Embedded
private B b;
//getters and setters
}
@Embeddable
public class B {
@OneToOne(mappedBy="a", cascade = CascadeType.ALL, orphanRemoval = true)
private C c;
//getters and setters
}
@Entity
public class C {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne
@JoinColumn(name="a_id")
private A a;
//other fields, getters and setters
}
This mapping works correctly when we create, update the information for entity c and delete a (and consequently deletes c).
The problem is when we try to remove C through an update, what really happens is that hibernate updates entity C and sets the a_id field to null. This causes objects C not attached to any entity A.