3

I have Student and Group entities. Student can have many groups and Group can have many students too. so in database I should have middle class and change many-to-many relation to one-to-many and many-to-one.

My question is how should I design class diagram in oop?

If middle table does not have any extra field(just student_id and group_id) then I think no middle class is need in oop, is this true or not?

2 Answers 2

4

You don't need a class for that, but maybe you want one. At the database design level, I like to avoid linking tables and think of those tables as first class entities instead. So if we have student and class tables for example, instead of creating a student_class linking table to satisfy that many-to-many relationship, we could call it something like enrollment.

Now, at the OOP level, we don't have to think about whether we should write something like student.addClass(class) or class.addStudent(student). Instead we can write new Enrollment(student, class). And, if we ever decide that we need to store more data about that relationship, we don't have to deal with awkward problems that come from linking tables having extra fields (especially with respect to ORMs and so on). Since Enrollment is a first-class entity, it can have any other properties we want in addition to student and class.

If you can conceptualize the relationship between a student and a group as its own entity, it would probably be cleanest to model it that way.

1

That is correct. You simply have a property of groups (which makes more sense to me than students, but that's a matter of opinion) that is an enumerable collection of students that belong to the group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.