• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

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

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought maybe it would be stored in the Person table, don't know why. Thanks.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic