[hibernate-dev] Is unidirectional one-to-one referencing a non-primary key valid
Gail Badner
gbadner at redhat.com
Wed Mar 7 17:30:41 EST 2018
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
More information about the hibernate-dev
mailing list