File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate2, composite-id, mappingException" Watch "Hibernate2, composite-id, mappingException" New topic
Author

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
- FOO_ID, LANGUAGE

The table BAR has the columns
- BAR_ID, LANGUAGE, FOO_ID



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!
Thomas


pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
hey

not sure whats wrong... some ideas:
- change your BAR table to
BAR_ID, LANGUAGE_BAR, FOO_ID, LANGUAGE_FOO
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.


Pascal
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!
Thomas

* 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.


Thanks!
Thomas
[ November 30, 2006: Message edited by: Thomas Rochon ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate2, composite-id, mappingException
 
Similar Threads
org.hibernate. MappingException
.MappingException
MappingException
Hibernate mapping of many-to-many relationship with a joining table
MappingException