This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Problem with joining of two tables with Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem with joining of two tables with Hibernate" Watch "Problem with joining of two tables with Hibernate" New topic
Author

Problem with joining of two tables with Hibernate

Sverre Moe
Ranch Hand

Joined: Jul 10, 2007
Posts: 110
I have a Hibernate mapping against a database table which is working. However I have another table which contains extra information. I've tried a join in the mapping file but somehow it fails.

Lets call them Table1 og Table2.
Table1 has an attribute TAB1_Var_ID(not a primary key)
Table2 has a primary key TAB2_Var_ID which is connected to the TAB1_Var_ID
There is a many-to-one relationship.
Table1 can contain many TAB2_Var_ID

I am fairly new with Hibernate and don't have a clue what the cause of my problem is. Heres is my join mapping



The Class for Table1 inherits from a parent class which contains some of the values retrieved from persistence. tab1VarID is an variable in the parent class.

The error message I get from UnitTest is a MappingException

Foreign key (Table2 [TAB2_Var_ID]) must have same number of columns as the referenced primary key (Table1 [primaryKey1, primaryKey2, primaryKey3])


But I thought that property-ref "Specifies that the foreign key refers to columns that are not the primary key of the orginal table.". And the original table is Table1. Or is it?

Edit: I receive the same error with and withouth the property-ref in <key>

It seems that the tables are reversed. It is saying something about the foreign key which is in Table2. But the foreign key is in Table1.

TAB1_Var_ID in Table1 is not defined as a foreign-key in the SQL level. That is a database design I cannot meddle with
[ August 06, 2007: Message edited by: Sverre Moe ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with joining of two tables with Hibernate