| Author |
many to many
|
Nawar Gailani
Ranch Hand
Joined: Feb 08, 2005
Posts: 46
|
|
Hi there, If I have many to many relation, how can I map this in Java? For example, i have students and courses, every student may have one or more courses, and every course may have one or more students. When I write a class represent student, I write it this way? class Student { private String name; private Course course; ...... } or like this? class Student { private String name; private Vector courses; ...... } The second seems more logical to me when I search for a specific student, but I don't know if I'm missing something! Thanks in advance
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26168
|
|
Nawar, You would need the second one as a student can be in multiple courses. If the course needs to know who is registered, it would need to have a Collection of students. I'm moving this to our OO forum since it is about modeling Java rather than databases.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Joel McNary
Bartender
Joined: Aug 20, 2001
Posts: 1815
|
|
...But it could be about modeling either! Typically, in the database, there is a cross-relation table (which can be invisible to the object model) So your database structure would be something like: While the code could look something like: (Accessors left out as an exercise for the user.) Note that student has a courseList and course has a studentList.
|
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
|
 |
Nawar Gailani
Ranch Hand
Joined: Feb 08, 2005
Posts: 46
|
|
Jeanne: Yes, I thought the second is the correct. Thanks Jeanne. Sorry about the posting in the wrong forum or section. Joel: Yes, I already did the linking table, but I didn't mentioned it for simplicity. Thanks Joel for your time explaining with code.
|
 |
 |
|
|
subject: many to many
|
|
|