aspose file tools*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
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 ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The return type is missing

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


[My Blog]
All roads lead to JavaRanch
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Jan 10, 2002
Posts: 61241
    
  66

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Jan 10, 2002
Posts: 61241
    
  66

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Jan 10, 2002
Posts: 61241
    
  66

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Jan 10, 2002
Posts: 61241
    
  66

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: 3832

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