IntelliJ open source
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Object Relational Mapping
 
RSS feed
 
New topic
Author

Many to Many with out primary keys

chiranjeevi gunturu
Ranch Hand

Joined: Mar 02, 2007
Messages: 42

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
Cameron Wallace McKenzie
author and cow tipper
Bartender

Joined: Aug 26, 2006
Messages: 4602

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

Author of Hibernate Made Easy, What is WebSphere???, Portlet Programming Made Easy and the SCJA Certification Guides
My Hibernate and JPA Tutorials * My Mock Java Certification Exams * My Online Java Tutorials * My CBT Portlet Tutorials.

chiranjeevi gunturu
Ranch Hand

Joined: Mar 02, 2007
Messages: 42

Would that be possible if i have composite keys in both the tables?

Thanks
Chiranjeevi
Cameron Wallace McKenzie
author and cow tipper
Bartender

Joined: Aug 26, 2006
Messages: 4602

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

Author of Hibernate Made Easy, What is WebSphere???, Portlet Programming Made Easy and the SCJA Certification Guides
My Hibernate and JPA Tutorials * My Mock Java Certification Exams * My Online Java Tutorials * My CBT Portlet Tutorials.

Justin Chi
Greenhorn

Joined: Sep 09, 2009
Messages: 13

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.

This message was edited 1 time. Last update was at by Justin Chi

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Messages: 8544

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?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
chiranjeevi gunturu
Ranch Hand

Joined: Mar 02, 2007
Messages: 42

Thank you very much all. Now i came to an idea on this. I will try it now!!
Thanks,
Chiranjeevi
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Object Relational Mapping
 
RSS feed
 
New topic
The most intelligent Java IDE