• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the input!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic