aspose file tools*
The moose likes Beginning Java and the fly likes confused on the modeling and handling of bi-directional many to many relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "confused on the modeling and handling of bi-directional many to many relationship" Watch "confused on the modeling and handling of bi-directional many to many relationship" New topic
Author

confused on the modeling and handling of bi-directional many to many relationship

Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
Greetings,

I think I've confused myself in a weird object-oriented manner, I hope you can help me to straight out things alittle...

Here's my request for clarification and thank you for your time =)

A student can enroll in several courses and a course can enroll several students.


To model that relationship in an object-oriented manner, I have a hashset of students in course Class and a hashset of courses in student Class.

When student A object is created using NEW, it is then added to course A. Now I want to add student A to course B as well...do I created another student A to add to course B collections? What about the collections of courses in student? And there's a problem with duplication of students attributes in courses and courses' attributes. How should I model this many to many relationship between student and course?

And where should store the awarded grades, should it be an instance variables of student or course?

Thanks again!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10926
    
  12

think about the real world. Student 'Fred' enrolls in a college, and is given a student number/ID. at that time, he has NO classes. similarly, the school creates a list of courses to offer in the next term (each with a course ID), and none initially have any students registered.

Then one day, registration starts. Student 'Fred' picks the classes he wants. At that time, two things happen: 1) Fred's list of classes gets populated with the specific class' course IDs he wants. 2) The classes he signs up for get populated with his student number.

So...I would NOT add a student to a class when the student object is created. I would create a list (probably a Map of some kind) of all students. Each Student should have a list of classes (perhaps an array, if you want to limit how many classes they can take).

Then do the same thing for classes - generate a collection of all of them. THEN write the code that assigns the student to the class and the class to the student at the same time.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
fred rosenberger wrote:

So...I would NOT add a student to a class when the student object is created. I would create a list (probably a Map of some kind) of all students. Each Student should have a list of classes (perhaps an array, if you want to limit how many classes they can take).

Then do the same thing for classes - generate a collection of all of them. THEN write the code that assigns the student to the class and the class to the student at the same time.


Hi Fred,

Thank you for responding, please bear with me. After the creation of a student object, it is added to a hashmap of all students. Inside every student, there is a hashmap of classes. I do the same for classes. when a student is added to the class from the hashmap of all students, is that student removed from that hashmap? Or an instance of that is copied to the class?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10926
    
  12

neither.

when "the student is added to the course's set", you're not ACTUALLY putting the student object in the course's set.

Think of it this way...

I have a list of houses I own. When I buy a new house, i add it to my list. I don't put the ACTUAL HOUSE in my filing cabinet. All I need to add is the address - how to GET to that house.

So, when i add a student to a course, I just need to know the memory address where that object is. A copy of the address is added to the course's list of students.
Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
thanks for the input!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused on the modeling and handling of bi-directional many to many relationship
 
Similar Threads
Class association problem
Doubt in Mapping( associations of entities)
disappearing collection items
Passing Objects Created By User to Array in Different Class
hibernate mapping 3 entities to 1 join table