Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA Unidirectional 1to1 mapping issue: A Foreign key has the wrong number of column. should be 2

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

JPA is most unhappy about it.

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

Any insights?

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return type is missing

Strange. What does Credentials look like ? What kind of indexes (pks) are on it ? Is credentialsId unique ?
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit]
Oops! Nevermind, I completely misread.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:The return type is missing
The ... is a macro!

Strange. What does Credentials look like ?

Just a POJO. It has no reference back to Customer.

What kind of indexes (pks) are on it ? Is credentialsId unique ?

There isn't any schema yet. Hibernate isn't getting to the point where it creates the actual tables, so the database is empty.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't any schema yet. Hibernate isn't getting to the point where it creates the actual tables, so the database is empty.

But the Credentials entity has some annotations in it, hasn't it ? @Id and other kind of annotations. Can you post your Credentials class ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, here's Credentials:


There's a reason for the public fields as opposed to a bean-patterned class. But that shouldn't affect JPA.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"extends Model" ? What's in there ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using a tool to generate the schema ? Or are you letting Hibernate generate the schema at startup, via the hibernate.hbm2ddl.auto property in persistence.xml ?

Also, I don't know if this is related, but what did you set hibernate.dialect to ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:"extends Model" ? What's in there ?


Well, that was the clue. Removing that causes JPA to correctly create the schema.

Model is a Decorator from a framework I'm evaluating. Apparently it's doing something that's interfering with my annotations....

Thanks Christophe, I might not have tried removing that if you hadn't poked at it!

Now I guess it's time to ping the framework guys...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad you've found the source of the problem.
 
Gijsbert Peijs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi BB,

This discussion is old and you've probably fixed your problems and are happy with the (fantastic imho) Play framework or moved on to another.

The error is caused by the fact that you try to extend play.jpa.Model which normally is a good plan but it assumes you let Play generate a Long as your id automatically. In this case you're using an Integer as id which you manage yourself. Play supports this without any problem but you have to extend the GenericModel super class instead of Model. That will (or would have) fixed your problems.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup.... sorry I should have followed up on this when the problem was resolved.

Extending GenericModel did the trick as the schema I am mapping onto is old and I could not change the id's to longs as it is used by many other legacy applications that cannot be changed.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the CodeRanch, Gijsbert Peijs!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic