File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate2, composite-id, mappingException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate2, composite-id, mappingException" Watch "Hibernate2, composite-id, mappingException" New topic

Hibernate2, composite-id, mappingException

Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
Hey folks,

I really searched the forums for my problem but I couldn't find a working solution for my problem, which is :

- I've got two mappings.
- Both have a composite-id.
- One mapped class has a Set of the second mapped class
- on server startup I get an Hibernate MappingException:
Foreign key (BAR [FOO_ID,LANGUAGE])) must have same number of columns as the referenced primary key (FOO [FOO_ID])

The Mappings are as stated below:

The class com.Foo has the fields
- fooId, language (String)
- bars (Set)

The class com.Bar has the fields
- barId, language

The table FOO has the columns

The table BAR has the columns

O.k. - Sorry for this mega text but I REALLY have no clue
how to solve my problem. Hopefully somebody in here has.

For today I will shutdown my computer and hope that some
wise pixie will drop me a sulotion that night

Thanx a lot in advance!

pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547

not sure whats wrong... some ideas:
- change your BAR table to
perhaps hibernate is complaining because you are sharing the LANGUAGE column as part of the PK and as part of the FK ?

- Do not use compund primary keys at all unless you are required to do so (legacy DB). Instead use a surrogate key. Easier to handle, also in relationships.

Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
Thanks a lot for your reply, Pascal!

Unfortunately I'm not able to change any table names because it's a legacy DB.
I found a mapping example on the Hiberante homepage which I will translate for my case. But I guess you're right, regarding your suggestion about the LANGUAGE column. If that's the problem, I've got a tiny problem

Well ... I will check the alternatives ... and maybe visit this forum again.

Finally, thanks again!

* follow-up *

O.k. - I've checked the manuals in more detail and now the error message seems to make more sense to me. If I got it right, then the FK MUST have the same (number of) keys as the PK of the table I want to reference to (that's exactly what the error message is saying).

At this point I'm in deep trouble because the legacy DB model was designed by some kind of evil wizard. I have a bunch of tables, all with two or more PKs. Most of them do not have a matching FK combination for the given PK combinations of the tables they should reference to. I'm not allowed to modify the existing tables but forced to map them into Hibernate. I'm trapped within a nightmare ...

Well ... I will check the alternatives ... and maybe visit this forum again.

[ November 30, 2006: Message edited by: Thomas Rochon ]
I agree. Here's the link:
subject: Hibernate2, composite-id, mappingException
jQuery in Action, 3rd edition