How do I properly cascade save a one-to-one, bidirectional relationship on primary key in Hibernate 3.6

Specification says that derived entity should be the owning side of the relationship:

2.4.1 Primary Keys Corresponding to Derived Identities

The identity of an
entity may be derived from the
identity of another entity (the
“parent” entity) when the former
entity (the “dependent” entity) is the
owner of a many-to-one or one-to-one
relationship to the parent entity and
a foreign key maps the relationship
from dependent to parent.

In your case LeadAffiliate is derived, so it should be the owner, when Lead should be marked as non-owning side by mappedBy. The following works in both 3.5.0 and 3.5.6:

public class Lead { 
    @Id @GeneratedValue
    private Long leadId; 
 
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "lead")
    private LeadAffiliate leadAffiliate; 

    ...
}

.

public class LeadAffiliate {  
    @Id
    private Long leadId;  
  
    @OneToOne @MapsId
    private Lead lead; 

    ...
}

Leave a Comment