These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: manyToMany mappings with multiple fields
PostPosted: Mon Aug 31, 2009 12:47 pm 
Newbie

Joined: Wed Jun 24, 2009 11:34 am
Posts: 13
I have a complex type that I wanted to talk through on the forum before I figured it all out.
I have a course, material, and menumapping. Fields in menumapping table are as follows:
id
materialID
courseID
parent
Basically, I need a group of root menu items to be made from Materials depending upon the course selected, then each of those materials maps out other sub menu items from other materials. So a material has a list of other materials (which also must all comply to the courseID given). This pattern continues until there are no more materials with parent items attached to them.
So for instance, if we were checking courseID 2 - it would find the root elements, and then map out more materials based on their parent. I got this mapping to work when I had it on OneToMany relationship with just a Material and Course classes, but then it was tied to one course (because the Material class only had a field for one Course and one Parent ) I need there to be many materials mapped to many different courses?
I found this:
http://tadtech.blogspot.com/2007/09/hib ... in_03.html
which is close to what I need, but it does not seem to build out items to the same item - like Materials to Materials which is my case. Any help is appreciated.


Top
 Profile  
 
 Post subject: Re: manyToMany mappings with multiple fields
PostPosted: Tue Sep 01, 2009 4:14 am 
Newbie

Joined: Wed Sep 19, 2007 11:30 pm
Posts: 12
Location: indonesia
For example you have 3 tables like this:

1. FOO : FOO_ID, FOO_NAME
2. BAR : BAR_ID, BAR_NAME
3. FOO_BAR_MAP : FOO_BAR_MAP_ID, FOO_ID, BAR_ID

Then you can say that FOO has many-to-many relationship with BAR.

For example their mapping is Class Foo and Bar, then with Hibernate mapping you can specify something like this in the mapping of Foo:

<set name="bars" lazy="true" order-by="FOO_BAR_MAP_ID" cascade="none">
<key column="FOO_ID" />
<one-to-many class="Bar" />
</set>

Foo class should contains a member Set<Bar> bars

_________________
An incredible programmer is worthier than a common programmer because of his/her ideas, not his/her codes.


Top
 Profile  
 
 Post subject: Re: manyToMany mappings with multiple fields
PostPosted: Tue Sep 01, 2009 8:51 am 
Newbie

Joined: Wed Jun 24, 2009 11:34 am
Posts: 13
Thanks for the reply.

What if foo needs to build an array of subfoos inside of it based on whether or not bar is set to the number selected?

I have a course, a person selects a course. We get all of the top level Materials, inside each Material is another array of materials, but all of them have to have the same courseID. So the mapping of the Materials is a one to many relationship on itself, but it has to only select items that are of a course id selected.

Basically, I am building a dynamic submenu system, which could have multiple levels of menus, but they each belong to a course. I did this correctly when I had course attached to the Material class, but that limited me in terms of how many courses I could associate with a single course - namely, I could only have one. So I made a linked table, but I cannot figure out how to get courseID, materialID, and parent (which decides the order of menu level for material) to play nice with each other.

Also, I am using annotations too, so if someone can speak in that language instead of the tags, that would help me too.

TIA


Top
 Profile  
 
 Post subject: Re: manyToMany mappings with multiple fields
PostPosted: Tue Sep 01, 2009 11:06 am 
Newbie

Joined: Wed Sep 19, 2007 11:30 pm
Posts: 12
Location: indonesia
Will it solve the problem if you create a different Table/Object for the Sub Materials?
I never had any experience in a table having a relationship to itself. Maybe it will work you might try that.

_________________
An incredible programmer is worthier than a common programmer because of his/her ideas, not his/her codes.


Top
 Profile  
 
 Post subject: Re: manyToMany mappings with multiple fields
PostPosted: Tue Sep 01, 2009 12:48 pm 
Newbie

Joined: Wed Jun 24, 2009 11:34 am
Posts: 13
Actually mapping to itself works surprisingly well. That isn't the problem. The problem is that I need it to map now based on another constraint - which course. That is where I am getting into trouble. I have thought about joins, joinTables, joinColumns, foreignKeys, and I just am not sure how that relationship works really. I am not even sure if I can just use a select statement to make the call for the course, and have all mappings work with just that courseID. I am just struggling with that right now. And I am not experienced with joinTables and foreign keys and not sure how to write them or set up the relationship that fits with mine.


Top
 Profile  
 
 Post subject: Re: manyToMany mappings with multiple fields
PostPosted: Tue Sep 01, 2009 1:32 pm 
Newbie

Joined: Thu Jul 02, 2009 2:06 pm
Posts: 6
toddcoulson wrote:
Basically, I need a group of root menu items to be made from Materials depending upon the course selected, then each of those materials maps out other sub menu items from other materials. So a material has a list of other materials (which also must all comply to the courseID given). This pattern continues until there are no more materials with parent items attached to them.

This is not a many-to-many relationship. This is some sort of hierarchical recursive one. And I'm afraid I don't know much about how to deal with it in Hibernate. These are always problematic to work through. The data structure is clear, but your code may have to run numerous queries to fetch the data, recursively adding each material encountered to some processing queue until the unprocessed portion of the queue is empty. It's not particularly hard to code this, but there are often performance penalties. If you can keep this data in long-term memory, or load the entire table quickly and easily when needed, it shouldn't be too bad. If not, watch out for those infinite loops!

toddcoulson wrote:
I found this:
http://tadtech.blogspot.com/2007/09/hib ... in_03.html
which is close to what I need, but it does not seem to build out items to the same item - like Materials to Materials which is my case. Any help is appreciated.

This is not really like your case, at least as I understand it. This is a classic example of a many-to-many relationship. One person can have many different addresses. And one address can house many people. This is usually modeled in a database with an additional table that has person_id and address_id columns, in which a single entry means that a particular person is associated with a certain address. This Hibernate handles fine. But I don't think it applies to your problem.

Good luck,

-- Scott


Top
 Profile  
 
 Post subject: Re: manyToMany mappings with multiple fields
PostPosted: Tue Sep 01, 2009 3:27 pm 
Newbie

Joined: Wed Jun 24, 2009 11:34 am
Posts: 13
Exactly what my problem is Scott you identified it and described it better than I did. Thanks. Has anyone else ever dealt with this type of recursive mapping before? Does anyone have advice on how to set it up with two tables and annotations? Thanks.


Top
 Profile  
 
 Post subject: Re: manyToMany mappings with multiple fields
PostPosted: Wed Sep 02, 2009 11:46 am 
Newbie

Joined: Thu Jul 02, 2009 2:06 pm
Posts: 6
toddcoulson wrote:
Does anyone have advice on how to set it up with two tables and annotations? Thanks.

That depends. If the data sets aren't too large to hold in memory, then you are probably best just using simple annotations, selecting the entire table and dealing with the hierarchical relationships in memory. If they are too large, I can't think of anything for which Hibernate will be much of a help. You'll still have to annotate the tables as plain entities and deal with their relationships manually. But you'll have to do it more often! Image

Good luck.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.