当我想将学生添加到另一项运动时,我有一个问题,而不是保存额外的运动,它会覆盖现有的运动。
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="STUDENT_ID")
private int studentId;
@Column(name="\"ACTIVE\"")
private boolean active;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_SPORTS",
joinColumns = { @JoinColumn(name = "STUDENT_ID") },
inverseJoinColumns = { @JoinColumn(name = "SPORTS_ID") })
private List<Sports> sports= new ArrayList<Sports>();
public Student () {
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId= studentId;
}
我的输出应该是Student_sports中的以下内容:
Student_id sports_id
5 5
5 6
但是在Student_sports表中输出是这样的:
Student_id sports_id
5 6
答案 0 :(得分:0)
此处有unique = true
:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="STUDENT_ID", unique = true, nullable = false, length = 5) // <= You can also add some details more like this
private int studentId;
@Column(name="\"ACTIVE\"")
private boolean active;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_SPORTS",
joinColumns = { @JoinColumn(name = "STUDENT_ID") },
inverseJoinColumns = { @JoinColumn(name = "SPORTS_ID") })
private List<Sports> sports= new ArrayList<Sports>();
public Student () {
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId= studentId;
}
请你试试吗?