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).
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.
Joined: Dec 30, 2008
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?
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.
Joined: Dec 30, 2008
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):