10

I have a question for designing the ManyToMany in EJB, how can a jointable has a property?
Here is an example, the students and courses are ManyToMany, every student have many courses, and many students choose one course.

    @Entity
    public class Student implements Serializable { 
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        Long id;
        String name;
        private Collection<Course> courses; 

        @ManyToMany(mappedBy = "students",cascade=CascadeType.ALL)      
        public Collection<Course> getCourses() {
            return this.courses;
        }

        public void setCourses(Collection<Course> courses) {
            this.courses = courses;
        }

    }


    @Entity
    public class Course implements Serializable { 
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        Long id;
        String name; 
        private Collection<Student> students; 

        @ManyToMany(cascade=CascadeType.ALL)
        @JoinTable(name = "Student_Course",
        joinColumns = {@JoinColumn(name = "Course_ID", referencedColumnName = "id")}, 
        inverseJoinColumns = {@JoinColumn(name = "Student_ID", referencedColumnName = "id")})  

        public Collection<Student> getStudents() {
            return this.students;
        }

        public void setStudents(Collection<Student> students) {
            this.students = students;
        }
    }

However if I have a property in the JoinTable, for example each student has one score for one course. How can I make it in EJB with ManyToMany?
Many thanks for your attention!

seaguest
  • 2,510
  • 5
  • 27
  • 45
  • 1
    If your relation have property, then you should model it as entity. This question is nearly identical: http://stackoverflow.com/questions/7602386/mapping-value-in-junction-table-to-entity/7603036#7603036 Even name of the entity representing relation (CourseAssignment) fits quite well to your case. – Mikko Maunu Dec 21 '11 at 08:16
  • 3
    It is not possible, you cannot add property to relationship. If you need to access property in the join table, then that property belongs to some entity and as a result you need third entity. – Mikko Maunu Dec 21 '11 at 09:16
  • sorry , I corrected that. I have just another question, when I used a @Embeddable class as a PK for jointable, it doesn't work(compile failed),someone said that JPA2.0 do not support that! please look at this [jpa-eclipselink-manytomany-with-dat](http://stackoverflow.com/questions/4013397/jpa-eclipselink-manytomany-with-data), is there any way to make it work? – seaguest Dec 21 '11 at 10:47
  • 1
    Thank Mikko Maunu very much! And for the ManyToMany, here is a good solution: [Mapping a many-to-many join table with extra column using JPA](http://giannigar.wordpress.com/2009/09/04/mapping-a-many-to-many-join-table-with-extra-column-using-jpa/) – seaguest Dec 21 '11 at 09:26

3 Answers3

4

It is not possible, you cannot add property to relationship. If you need to access property in the join table, then that property belongs to some entity and as a result you need third entity.

Yogu
  • 9,165
  • 5
  • 37
  • 58
3

It is possible.

You just need to replace many-to-many mapping with the explicit combination of one-to-many and many-to-one mappings via a 3rd entity, that would represent the association between the two primary entities (student and course in your example).

Please read details here

Alex Vayda
  • 6,154
  • 5
  • 34
  • 50
0

Entities with Many to Many Relationships (Merchant and Service). This can be achieved using a third entity as follow:-

@Entity
@Table(name = "merchant")
public class Merchant implements java.io.Serializable {

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.merchant",targetEntity = MerchantService.class)
    private Set<MerchantService> merchantServices = new HashSet<>();
}

@Entity
@Table(name = "merchant_service")
@AssociationOverrides({
        @AssociationOverride(name = "pk.merchant",
            joinColumns = @JoinColumn(name = "merchant_id")),
        @AssociationOverride(name = "pk.service",
            joinColumns = @JoinColumn(name = "service_id")) })
public class MerchantService implements java.io.Serializable {

    @EmbeddedId
    private MerchantServiceId pk = new MerchantServiceId();

    private boolean isActive;

    public MerchantServiceId getPk() {
        return pk;
    }

    public void setPk(MerchantServiceId pk) {
        this.pk = pk;
    }

    @Transient
    public Service getService() {
        return getPk().getService();
    }


    @Transient
    public Merchant getMerchant() {
        return getPk().getMerchant();
    }


    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

}

@Embeddable
public class MerchantServiceId implements java.io.Serializable {

    private Merchant merchant;
    private Service service;

    @ManyToOne
    public Merchant getMerchant() {
        return merchant;
    }

    @ManyToOne
    public Service getService() {
        return service;
    }

}

@Entity
@Table(name = "service")
public class Service implements java.io.Serializable {

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.service",targetEntity = MerchantService.class)
    private Set<MerchantService> merchantServices = new HashSet<>();

}