jsr338-experts@jpa-spec.java.net

[jsr338-experts] _at_OneToOne(optional=true) and _at_PrimaryKeyJoinColumn

From: Emmanuel Bernard <emmanuel.bernard_at_jboss.com>
Date: Fri, 2 Sep 2011 10:51:52 +0200

Hi all,

I need your spec lead opinion on something. I think there is a small hole in the spec that we ought to close.

Hibernate have some users that have questioned our implementation and our reading of the spec. It seems more a case left behind to me.

 @Entity
 class User {
     @Id String username;

     @OneToOne(optional=true) @PrimaryKeyJoinColumn
     Profile getProfile() { ... };
 }

The question boils down to should we allow the User table to contains ids not present in the Profile table ie should we relax the foreign key constraint between User and Profile in this case because of optional=true?

optional=true
Whether the association is optional. If set to false then a non-null relationship must always exist.

But elsewhere the spec mention the need to have foreign keys (in general).

Frankly, a user without a profile is a valid case in my opinion and since the User PK is also the FK we can't let it go null.

So far Hibernate does not let you have User ids not present in Profile unless you use an additional specific annotation named @NotFound(IGNORE) which is used for databases with FK pointing to non existent entires (yuk). According to users, (some) other providers use the alternative approach.

To add to this debate, I don't think you can have optional=true and use derived identities as we might not have the entity to extract the id from.

It's a bit confusing but I wanted your opinion on this. I could go either way. This is another write up on the subject that might be clearer http://lists.jboss.org/pipermail/hibernate-dev/2011-August/006980.html

Emmanuel