many-to-many relationship with an attribute on the relation

Discussions

EJB design: many-to-many relationship with an attribute on the relation

  1. Hi all,

    Is it possible to have a client (a session bean) handling this kind of relationship transparently when an attribute is associated to the relation?

    Here is a comprehensive description of the issue:


    Data Model
    ----------

      -------------------
     ¦ STUDENT ¦
     ¦ - student_id (pk) ¦
     ¦ - name ¦
     ¦ ¦
      -------------------
               ¦1
               ¦
               ¦
               ¦
               ¦n
      -------------------
     ¦ GRADE ¦
     ¦ - course_id (fk) ¦
     ¦ - student_id (fk) ¦
     ¦ - grade (integer) ¦ <-- this is the issue
      -------------------
               ¦n
               ¦
               ¦
               ¦
               ¦1
      ------------------
     ¦ COURSE ¦
     ¦ - course_id (pk) ¦
     ¦ - code ¦
     ¦ ¦
      ------------------
      
    Is it possible to map this to a N-M relationship between a Student class and a Course class?
    The answer is usually yes, but this time the "grade" attribute causes me a lot of trouble.

    Is it possible to do something like:

    student.getCourses().add(course,8)

    with 8 being the grade for this course and this student?

    The idea is that a call to the add() method would trigger an insert of a new row in the 'GRADE' table. getCourses() could return a Map, with a Course as key and an Integer (the grade) as value.

    If, as it seems, this is not possible in a simple declarative way, we would then fully promote GRADE as a plain EJB, and have two 1-N relationships, but I would like to hide the 'Grade' entity inside the Java code.

    Thanks for your help,

    Chris.
  2. I'd prefer to keep the Course and Student entities grade agnostic, and introduce a GradeController SLSB that manipulates the Grade table directly. What I'm wary of is introducing "simple" relationship accessors into an entity that:
    - would then share responsibility for the CRUD of the grade with another entity in the association
    - might easily develop its own business rules (e.g. notification if a grade is changed)