-->
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.  [ 4 posts ] 
Author Message
 Post subject: some help plz
PostPosted: Sat Nov 05, 2005 8:35 am 
Beginner
Beginner

Joined: Thu Oct 20, 2005 12:35 pm
Posts: 21
Hi,

I am challenged with a problem many others have chalenged before me. I have a many to many relationship that has an extra attribute in the association table.

I've read trough parts of the forum, and altough the question arrises severql times, people are always refered to looking trough the forum or the documention. So far I have come to te Following Conclusion:

I need to create an extra Class referring to the association table, and make
relationships between the other classes. That is a many to one and a one to many relationship.

I will now build a specific example so I can make the problems that i still have to deal with clear:

Suppose we have a student and A course. The relation is specified by the student following a course. But the student follows a course on a specific level. So that yields the following:

Student -----------Follows---------Course
|
level attribute


So suppose a make some mappings, and a create 3 classes: Student, Follows, Course
A make mapping files for those 3 classes using many to one and one to many relationships.
Now i want to get All the Courses a student follows, so i do:

myfollow= myStudent.getFollows();
List myCourses= myfollow.getCourses();

==> So far so good, but what if I want to retrieve the level on which my student follows a course ??? How do I request that ? And how do I specifie the level attribute in my mapping file(s) ?

If someone could point my into the right direction that would be greatly appreciated, just a reference to which part of the documentation or forum threads that discusses this subject I also fine.

Thx in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 8:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Your "ERD" makes no sense. Do you mean to say that "level" is actually on the Follows?

I assume what you really mean to say is that there is a "many to many" between Student and Course, but that that association is further qualified by some level attribute. BTW, speaking from a purely data modeling perspective, what you have then is no longer a many to many (because you no longer have a simple association table). What you have instead is a new Entity within your ERD with two many to ones.

So two choices:
1) Map this as a true entity in Hibernate (using a <class/> tag for your ~"many to many" table)
2) Map this association using a component to represent the association and level.

This is all covered in the docs. I'll leave it to you to research the rest of the solution.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 12:23 pm 
Beginner
Beginner

Joined: Thu Oct 20, 2005 12:35 pm
Posts: 21
OK, i looced into the docs, and found an example that is very related to my problem:
e.g.: The Customer - Order - LineItem - Product example

more specific those last three.

So all i need to know now is how would one retrieve the quantity of a certain product that has been ordered ( =lineItem)

Or is this just impossible (would be strange ) and if it can be done, what is the effect in the mapping files ?

Thx


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 9:37 pm 
Beginner
Beginner

Joined: Fri Oct 28, 2005 10:46 am
Posts: 37
As steve said, the LineItem, which corresponds to your "Follows" is an entity and should be mapped as such in your mapping file(s). If I understand your problem domain correctly, your "Follows" object should look something like:

Code:
public class Follows {
    private int level;
    private Student student;
    private Course course;

    // Appropriate constructors, bean methods, etc.
}


See Bidirectional associations in Chapter 7 of the reference guide for help on making the single-valued end of the relationship (the student and/or course). If you're having trouble with the Collections in Student and/or Course, look toward the beginning of Chapter 7.

Once you get it mapped, finding the level is a simple matter of loading the appropriate Follows object and reading the level property.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.