[hibernate-dev] Bidirectional one-to-one, both sides are owners, using @PrimaryKeyJoinColumn

Jan-Willem Gmelig Meyling jan-willem at youngmediaexperts.nl
Wed Jan 15 04:20:13 EST 2020


Hi Gail,

The mapping is IMO invalid. For removal etc. to work properly, the direction needs to be deducible. However, I’ve observed this mapping as a  workaround for supporting lazy loading on either side of the association, prior to our fixes in 5.4 with HHH-12842 [1] .  The mapping works  in my experience,  but with quirks that are to be expected. With Hibernate 5.4 there is no reason to map this at all: if the association is non-optional and non-nullable, also the non-owning side of the association can be lazy loaded.



Kind regards,

Jan-Willem


[1] https://github.com/hibernate/hibernate-orm/pull/2612


On 15 Jan 2020, at 05:18, Gail Badner <gbadner at redhat.com<mailto:gbadner at redhat.com>> wrote:

Hi,

Does Hibernate support having both entities involved in a one-to-one
association own its side of the association when using
@PrimaryKeyJoinColumn?

My guess is that this is not supported by Hibernate. I just want to confirm.

For example:

@Entity
public class Employee {

   @EmbeddedId
   private CompositeKey id;

   @OneToOne(optional = false)
   @PrimaryKeyJoinColumns({
         @PrimaryKeyJoinColumn(name = "ID1", referencedColumnName = "ID1"),
         @PrimaryKeyJoinColumn(name = "ID2", referencedColumnName =
"ID2"), })
   })
   private EmployeeInfo employeeInfo;
   ...
}

@Entity
public class EmployeeInfo {

   @EmbeddedId
   private CompositeKey id;

   @OneToOne(optional = false)
   @PrimaryKeyJoinColumns({
         @PrimaryKeyJoinColumn(name = "ID1", referencedColumnName = "ID1"),
         @PrimaryKeyJoinColumn(name = "ID2", referencedColumnName =
"ID2"), })
   })
   private Employee employee;
...
}

In this particular case, the foreign keys have been deleted, so it's
possible to load and delete these entities.

An attempt to insert an entity on either side results in:
org.hibernate.PropertyValueException: not-null property references a null
or transient value

When converted to use @MapsId and @JoinColumns instead, Hibernate throws:
org.hibernate.MappingException: cyclic dependency in derived identities

Please let me know...

Thanks,
Gail
_______________________________________________
hibernate-dev mailing list
hibernate-dev at lists.jboss.org<mailto:hibernate-dev at lists.jboss.org>
https://lists.jboss.org/mailman/listinfo/hibernate-dev



More information about the hibernate-dev mailing list