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!
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.
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
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<>();
}
来源:https://stackoverflow.com/questions/8582452/jpa-manytomany-how-can-jointable-have-a-property