Results 1 to 2 of 2

Thread: @ManyToMany relationship attributes

  1. #1
    Join Date
    Dec 2010
    Posts
    26

    Default @ManyToMany relationship attributes

    Hi

    I have two entities Employee and Project in my application and there is many to many relationship between them. The application uses JPA and Hibernate. I have three tables:
    Code:
    employee: empId, name
    project: projId, name
    emp_proj: empId, projId
    
    class Employee{
       ...
       @ManyToMany
       @JoinTable(.....)
       Collection<Project> projects;
       ....
    }
    
    class Project
       ...
       @ManyToMany(mappedby="projects")
       private Collection<Employee> employees;
       ....
    }
    Now I want to add information that who assigned a particular project to the employee, so the modified table looks like this:

    Code:
    emp_proj: empId, projId, assignee.
    Since assignee is not the attribute of any of the two entities [i.e. employee or project] i can't access it.

    One solution to this is that I make another entity for table emp_proj and break the relationship into two OneToMany relationships employee -> emp_proj and project -> emp_project.

    Can anyone suggest a better solution by which i can access the attributes of the relationship?

    Thanks
    Amit Khanna

  2. #2
    Join Date
    Apr 2011
    Posts
    107

    Default

    Hello,

    You get the correct answer : transform your n-n relationship to two 1-n relationship.

    There is no better choice.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •