| 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_person | id_address | IDX | | 0 | id_address1 | 0 | | 0 | id_address2 | 1 |
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?
|
|
|