Sunday, 15 September 2013

configure many to many for unique notes in middle table

configure many to many for unique notes in middle table

I have two entities: Student and Prepod. Its linked as many to many.
I have so mapping:
Student:
@Entity
@Table(name="Student")
public class Student {
private Long id;
private String name;
private Long age;
private Set<Prepod> prepods = new HashSet<Prepod>();
@ManyToMany(mappedBy="students" ,fetch = FetchType.EAGER)
public Set<Prepod> getPrepods() {
return prepods;
}
public void setPrepods(Set<Prepod> prepods) {
this.prepods = prepods;
}
public Student(){
name = null;
}
public Student(Student s){
name = s.getName();
}
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
@Column(name="id")
public Long getId() {
return id;
}
@Column(name="name")
public String getName(){
return name;
}
@Column(name="age")
public Long getAge(){
return age;
}
public void setId(Long i){
id = i;
}
public void setName(String s){
name = s;
}
public void setAge(Long age){
this.age = age;
}
}
Prepod:
@Entity(name = "prepod")
public class Prepod {
private Long id;
private String name;
Set<Student> students = new HashSet<Student>();
@ManyToMany(fetch = FetchType.EAGER)
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long i) {
id = i;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
And I get so database schema:
Thuse in table prepod_Student can be duplicate values.
How to change my mapping for resolving this problem?

No comments:

Post a Comment