JPA trouble with OneToOne relationship: A Foreign key refering has the wrong number of column. should be 2

8,836 views
Skip to first unread message

Bear

unread,
Feb 24, 2011, 5:50:11 PM2/24/11
to play-framework
OK, I've goggled around, but haven't found anything that's helped me
out.

I'm getting the error message:
A JPA error occurred (Unable to build EntityManagerFactory): A Foreign
key referring Credentials from Customer has the wrong number of
column. should be 2

I want a unidirectional one-to-one mapping from Customer to
Credentials -- I do not need or want any reference from Credentials to
Customer.

So I defined in Customer:

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "credentialsId")
public getCredentials() { ... }

JPA is most unhappy about it as noted above.

Any google result I looked at either discussed ManyToOne mappings, or
composite foreign keys, which are not applicable here.

Any insights?

(Cross-posted to CodeRanch.com:
http://www.coderanch.com/t/528597/ORM/java/JPA-Unidirectional-mapping-Foreign-key)

Alex Jarvis

unread,
Feb 24, 2011, 6:09:59 PM2/24/11
to play-framework
Take a look at the examples here:

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-mapping-association

@PrimaryKeyJoinColumn is probably what you need... although to be
honest, I think you can get it to work with just the @OneToOne
annotation in Play.
> (Cross-posted to CodeRanch.com:http://www.coderanch.com/t/528597/ORM/java/JPA-Unidirectional-mapping...)

Bear

unread,
Feb 24, 2011, 7:47:00 PM2/24/11
to play-framework
Hey Alex, perhaps I'm reading things incorrectly, but it seems to me
as if @PrimaryKeyJoinColumn is used when the primary key values of the
corresponding entities in the two tables will be the same. That's not
the case here, the primary keys of the tables are completely
independent.

I also changed the class to adhere to the Play convention of using
public fields and got rid of the getters/setters. The Customer class
now contains:

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "credentialsId")
public Credentials credentials;

But the same error persists. Any further thoughts?



On Feb 24, 5:09 pm, Alex Jarvis <alex.jarv...@gmail.com> wrote:
> Take a look at the examples here:
>
> http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ent...

Bear

unread,
Feb 24, 2011, 9:19:19 PM2/24/11
to play-framework
Another piece of information -- if I remove the "extends Model" from
the class, all is well and the database tables are created.

Manuel Bernhardt

unread,
Feb 25, 2011, 2:54:52 AM2/25/11
to play-fr...@googlegroups.com, Bear
On Fri, Feb 25, 2011 at 3:19 AM, Bear <mike.b...@gmail.com> wrote:
> Another piece of information -- if I remove the "extends Model" from
> the class, all is well and the database tables are created.

Do you happen to have a different primary key for your entity than
"id"? The play.db.jpa.Model class defines an "id" field that is
annotated with ID. If you already have an ID, extend
play.db.jpa.GenericModel instead and override the _key() method so it
returns the PK

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

Bear

unread,
Feb 25, 2011, 11:12:35 AM2/25/11
to play-framework
DOH! <slaps-forehear> That was it!

The schema defines primary keys with the name id, but they're ints,
not longs. (Not my choice -- sigh)

Extending GenericModel did the trick. Thanks!

On Feb 25, 1:54 am, Manuel Bernhardt <bernhardt.man...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages