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

前端 未结 2 452
借酒劲吻你
借酒劲吻你 2020-11-29 23:37

I have an one-to-one, bidirectional entity relationship with shared keys. When I attempt to save the owner of the association I get a \"null id generated\" exception against

2条回答
  •  無奈伤痛
    2020-11-30 00:34

    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; 
    
        ...
    }
    

提交回复
热议问题