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

Gail Badner gbadner at redhat.com
Thu Jan 16 13:06:44 EST 2020


Hi Jan-Willem,

Thanks for responding.

I have also heard from Steve offlist that this mapping is not valid.

Also, please see below...

On Wed, Jan 15, 2020 at 1:20 AM Jan-Willem Gmelig Meyling <
jan-willem at youngmediaexperts.nl> wrote:

> 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] .
>

Yes, I was thinking that as well.


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

Bytecode enhancement is required for the non-owning side to be lazily
loaded.

With bytecode enhancement and enhancement-as-proxy enabled, the association
does not get set as an uninitialized proxy, even though the ID for the
associated entity is the same as the ID for the entity owner. I've created
https://hibernate.atlassian.net/browse/HHH-13814 for that case.

I believe the fix for HHH-13814 could also apply when bytecode enhancement
is not enabled.

Regards,
Gail


>
>
> 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> 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
> https://lists.jboss.org/mailman/listinfo/hibernate-dev
>
>
>


More information about the hibernate-dev mailing list