As the subject says, I've a got problem when I try to save an entity with @ManyToMany annotation. The main problem is that the save operation is not saving the link Table(but makes an insert in both entities<cascade=CascadeType.All>
.
To figure out the the problem, there are the beans with them annotations.
Assignment.java:
package test.bean.school;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class Assignment {
private int id;
private
String description;
private Set<Student> students = new HashSet<Student>();
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name="Assignment_Student",
joinColumns={@JoinColumn(name="students_id")},
inverseJoinColumns={@JoinColumn(name="assignments_id")})
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public int hashCode(){
return this.description.hashCode();
}
public boolean equals(Object object){
if (object instanceof Assignment){
return this.description.equals(((Assignment)object).getDescription());
}
return false;
}
}
Student.java :
package test.bean.school;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class Student {
private int id;
private String name;
private Set<Assignment> assignments = new HashSet<Assignment>();
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER , mappedBy="students")
@JoinTable( name="Assignment_Student",
joinColumns={@JoinColumn(name="assignments_id")},
inverseJoinColumns={@JoinColumn(name="students_id")} )
public Set<Assignment> getAssignments() {
return assignments;
}
public void setAssignments(Set<Assignment> assignments) {
this.assignments = assignments;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int hashCode(){
return this.name.hashCode();
}
public boolean equals(Object object){
if (object instanceof Student){
return this.name.equals(((Student)object).getName());
}
return false;
}
}
... compiles fine and without exceptions at runtime, but, when I try to get the beans from the database, the set attribute in each bean is empty.
Please, help me!!!