File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate mapping of many-to-many relationship with a joining table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate mapping of many-to-many relationship with a joining table" Watch "Hibernate mapping of many-to-many relationship with a joining table" New topic
Author

Hibernate mapping of many-to-many relationship with a joining table

Anthony Abuan
Greenhorn

Joined: Dec 30, 2008
Posts: 3
I'm a newbie in hibernate but i know the basic mapping like one-to-many and many-to-many. However, i was stuck on a certain scenario and i've been trying to solve this and search through the internet but unfortunately couldn't find any solution.

The scenario basically is i have many-to-many relationship (foo and bar see example below).

--------------
HBM/Class Foo
--------------
<hibernate-mapping>
<class table="foo">
<set inverse="true" name="bars" table="foo_bar">
<key>
<column name="foo_id"/>
</key>
<many-to-many column="bar_id" class="Bar"/>
</set>
</class>
</hibernate-mapping>


--------------
Table foo
--------------
foo_id (pk)

--------------
Table foo_bar
--------------
foo_id (pk)
bar_id (pk)




--------------
HBM/Class Bar
--------------
<hibernate-mapping>
<class table="Bar">
<set inverse="true" name="foos" table="foo_bar">
<key>
<column name="bar_id"/>
</key>
<many-to-many column="foo_id" class="Foo"/>
</set>
</class>
</hibernate-mapping>


--------------
Table bar
--------------
bar_id (pk)




The 3rd table (which joins the two tables having many-to-many relationship - in this case "bar") has a many-to-manny relationship to another table which we can call Users.

Hence, the User table looks like this:

--------------
Table user
--------------
user_id (pk)


And the 3rd table joining foo_bar and user tables which we can call foo_bar_users looks like this:

--------------
Table foo_bar_user
--------------
user_id (pk) (fk from user)
foo_id (pk) (fk from foo_bar)
bar_id (pk) (fk from foo_bar)


Now, this is the part where i'm stuck. How do i map users table to foo_bar with componsite_id and no class since it's used only to join 2 table with many-to-many relationship?

Here's the mapping i tried to work on but i couldn't complete it. I appreciate your help.

--------------
HBM/Class User
--------------
<hibernate-mapping>
<class table="User">
<set inverse="true" name="fooBarUsers" table="foo_bar_user">
<key>
<column name="user_id"/>
<column name="foo_id"/>
<column name="bar_id"/>
</key>
<many-to-many column="foo_id" class="???"/>
</set>
</class>
</hibernate-mapping>
Angel Taveras
Ranch Hand

Joined: Nov 13, 2008
Posts: 84

Hello Anthony and welcome to Java Ranch. I seens like you're needing a map. When you're dealing with more that three entities all together you need a map.

Something like this



I don't understand from where do you gonna start the mapping to the "map" but i'll give two scenarios:

From The Entity User and Using the Foo as a key for the Map



From the Entity User and Using Bar as a key for the map



If you ever need to map to more that three entities you could use a composite.

Additionally and regarding to other issue on your mappings it's the use of the attribute inverse you should set inverse in any of the side of the
relationship between two entities but not on both sides.

Regards,
Anthony Abuan
Greenhorn

Joined: Dec 30, 2008
Posts: 3
Hi Angel, thanks for the help. But when i said mapping, i meant the hibernate mapping and not the java map collection. I am using Set for all my example. My problem comes in when creating a hibernate mapping many-to-many relationship of tables foo_bar and user (in hibernate you can skip creating a class for the joining table by many-to-many). As you can see, foo_bar doesn't have a java class because I used many-to-many relationship with foo and bar tables. Is there anyway in hibernate i can create relational mapping with foo_bar and user without creating a class for foo_bar? If so, how should i implement it this scenario?

Thanks a lot for your help.
Angel Taveras
Ranch Hand

Joined: Nov 13, 2008
Posts: 84

Hello, in my given example foo_bar doesn't have a mapped entity on hibernate. As you said, it's just used as a many-to-many table. When i said that you might be needing a map, it's on the ternary relationship, elsewhere you could map it with any other collection set,list,bag.

regards
Anthony Abuan
Greenhorn

Joined: Dec 30, 2008
Posts: 3
Angel, thank you. Your reply was really helpful. I have another scenario though that i need to solve. Have patience on me on this, I'm still learn this stuff. Anyway, given the table below (in addition to what we have here in this thread):

--------------
Table Message
--------------
message_id (pk)


--------------
Table Files
--------------
file_id (pk)


--------------
Table message_files (joining table for message and files)
--------------
message_id (pk)
file_id (pk)


Then another table is created which we can call foo_bar_messages. This is to join message_files and foo_bar_users with many-to-many relationship. Hence the table will look like this:


--------------
Table foo_bar_messages
--------------
foo_id (pk)
bar_id (pk)
message_id (pk)
file_id (pk)


How can you now map foo_bar_messages with foo, bar, messages and files?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate mapping of many-to-many relationship with a joining table