The moose likes Object Relational Mapping and the fly likes JPA Unidirectional 1to1 mapping issue: A Foreign key has the wrong number of column. should be 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "JPA Unidirectional 1to1 mapping issue: A Foreign key has the wrong number of column. should be 2" Watch "JPA Unidirectional 1to1 mapping issue: A Foreign key has the wrong number of column. should be 2" New topic
Author

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

Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

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?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Christophe Verré
Marshal

Joined: Nov 24, 2005
Posts: 14361

The return type is missing

Strange. What does Credentials look like ? What kind of indexes (pks) are on it ? Is credentialsId unique ?


[SCBCD Wall of Fame] [My Blog]
All roads lead to JavaRanch
Help Japan. Make a donation.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 2771

[edit]
Oops! Nevermind, I completely misread.
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

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é
Marshal

Joined: Nov 24, 2005
Posts: 14361

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 opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

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é
Marshal

Joined: Nov 24, 2005
Posts: 14361

"extends Model" ? What's in there ?
Christophe Verré
Marshal

Joined: Nov 24, 2005
Posts: 14361

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 opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

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é
Marshal

Joined: Nov 24, 2005
Posts: 14361

I'm glad you've found the source of the problem.
Gijsbert Peijs
Greenhorn

Joined: Jan 23, 2012
Posts: 1
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 opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

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

Joined: Mar 24, 2008
Posts: 3563

And welcome to the CodeRanch, Gijsbert Peijs!


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
 
 
subject: JPA Unidirectional 1to1 mapping issue: A Foreign key has the wrong number of column. should be 2
 
Threads others viewed
Persistence Example
Foreign key & EJB3
OneToMany Unidirectional strange behavior ?
issue that should be really simple
OneToMany Unidirectional relationship without Join table (JSR 220)
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com