permaculture playing cards*
The moose likes Object Relational Mapping and the fly likes [Hibernate] many-to-many, invalid primary key in the join table? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "[Hibernate] many-to-many, invalid primary key in the join table?" Watch "[Hibernate] many-to-many, invalid primary key in the join table?" New topic
Author

[Hibernate] many-to-many, invalid primary key in the join table?

Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
Hi. I am having some problems with my hibernate mapping. Please take a look at the hbm files:

and

The list is necessary for the address, as I want to preserve the order of these elements (well, the real problem has different classes, but this is the simplest one that depicts what my question is).
The DDL that get's generated is:

The code for the classes is the simplest possible, just to make SchemaExport work:

As you can see, the join table has the primary key wrong, right? I would like it to be (person_id, adress_id). When it is like this, it could violate the contraint - a single person could be in different Address's collections, and on the same position (so that person_id and IDX are the same, but not address_id). Why is the IDX in the join table anyway, not in the Person table?
Please, help me configure it properly.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
The IDX is in the join table because since its a list hibernate should maintain the order of how items are added to the list

person.getAddresses().add(address1);
person.getAddresses().add(address2);

this would generate some items in the Person_Address join table, and to know that address1 is at index 0 and address2 is at index 1 it needs the IDX so that when you get back the data from the DB the order is preserved.

id_personid_addressIDX
0id_address10
0id_address21




for your real problem i can't help you. i usually write my DDL by hand and then map it to hibernate using annotations, i never generate DDL SQL automatically.




Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
I thought maybe it would be stored in the Person table, don't know why. Thanks.
 
 
subject: [Hibernate] many-to-many, invalid primary key in the join table?
 
Similar Threads
persisting a collection - one-to-many mapping
Help with mapping set - Hibernate
Mapping Exception
Collection of values question
Hibernate parent/child and foreign key relationships mapping