Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Many to Many with out primary keys

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I got a scenario in many to many association between tables.
I created two tables(TableA and TableB) which are not having primary keys at all.
Now i need to create many to many association to between these two tables. Is this possible ? if yes how to implement that?
Please answer me .
Thanks in advance.

Chiranjeevi
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need primary keys to do a relational mapping. There must be some way to uniquely identify a row, is there not?

-Cameron McKenzie
 
chiranjeevi gunturu
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would that be possible if i have composite keys in both the tables?

Thanks
Chiranjeevi
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For sure, just treat the composite keys like primary keys, and that should do the trick.

How have you mapped the composite keys? There are a variety of ways.

-Cameron McKenzie
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chiranjeevi gunturu wrote:Hi,
I got a scenario in many to many association between tables.
I created two tables(TableA and TableB) which are not having primary keys at all.
Now i need to create many to many association to between these two tables. Is this possible ? if yes how to implement that?
Please answer me .
Thanks in advance.

Chiranjeevi



You have to create a new table for many to many association between TableA and TableB, suppose it is TEST_AB and A is POJO for TableA ,B is POJO for TableB.
Then in A.hbm.xml
.......
<set name="tableABSet" table="TEST_AB">
<key column="A.columnInDB" />
<many-to-many column="B.columnInDB" class="B" />
</set>
.......
Then Hibernate will automaticly populate the data into TEST_AB table in DB whenever you add B to A.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chiranjeevi gunturu wrote:Would that be possible if i have composite keys in both the tables?

Thanks
Chiranjeevi


Composite keys are primary keys - why use them instead of a surrogate or natural key?
 
chiranjeevi gunturu
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much all. Now i came to an idea on this. I will try it now!!
Thanks,
Chiranjeevi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic