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

Gail Badner gbadner at redhat.com
Tue Jan 14 23:18:09 EST 2020


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


More information about the hibernate-dev mailing list