[hibernate-dev] Is unidirectional one-to-one referencing a non-primary key valid

Steve Ebersole steve at hibernate.org
Thu Mar 8 14:22:06 EST 2018


For sure our model can handle this mapping, although maybe only from the
other side (that's generally the more natural mapping) - internally it's
called a "logical many-to-one".  Personally I'd say there is nothing wrong
with the mapping per-se.


On Wed, Mar 7, 2018 at 4:36 PM Gail Badner <gbadner at redhat.com> wrote:

> Hi,
>
> This is an unusual mapping. My gut feeling is that it is not a valid
> mapping, but I don't see anything in the spec that would indicate it is
> invalid.
>
> Here is the mapping:
>
> @Entity
> public class Product {
>    @Id
>    @Column(name = "id")
>    private int id;
>
>    @OneToOne(cascade = CascadeType.ALL)
>    @JoinColumn(name = "id", referencedColumnName = "productId",
> insertable = false, updatable = false)
>    private ProductInfo productInfo;
>
> }
>
> @Entity
> public class ProductInfo{
>    @Id
>    private int id;
>
>    @Column(name = "productId", unique = true, updatable = false)
>    private int productId;
> }
>
> Hibernate ignores referencedColumnName = "productId" and assumes that
> Product and ProductInfo share the same ID value.
> When the IDs are not the same, Product#productInfo will be null.
>
> It seem to me that the foreign key column should be
> ProductInfo#productId and should reference Product#id, but this
> doesn't make sense
> for a unidirectional one-to-one owned by Product.
>
> IMO, a bidirectional @OneToOne with ProductInfo owning the association
> would make more sense.
>
> A test case can be found at [1]
>
> Is the mapping invalid, or is this a bug in Hibernate?
>
> Thanks,
> Gail
> [1]
> https://github.com/gbadner/hibernate-test-case-templates/commit/d806d4ef5cf35da85efc51ce70c5e0648ce89006
> _______________________________________________
> 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