论坛成员 我在使用 Hibernate JPA 框架插入数据时遇到了一个问题。
我在任务和资源之间建立了一对多的关系。
我的任务模型如下所示的代码。
@Entity
@Table(name = "task")
public class Task {
private Integer id;
private Set<Employee> employee = new HashSet<Employee>(0);
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToMany(cascade = CascadeType.ALL, fetch =FetchType.EAGER)
@JoinTable(name = "task_employee", joinColumns = { @JoinColumn(name = "taskid") }, inverseJoinColumns = { @JoinColumn(name = "employeeid") })
public Set<Employee> getEmployee() {
return employee;
}
public void setEmployee(Set<Employee> employee) {
this.employee = employee;
}
}
我的员工模型是
@Entity
@Table(name = "employee")
public class Employee {
private Integer id;
private String firstname;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "firstname")
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
当
1.task id为1,employeeid为1,
2. Task id 为 1,employeeid 为 2,
3. Task id 为 2,employeeid 为 3,
但是当员工 id 像下面的插入一样重复时会发生错误。
4.任务id为1,employeeid为1
它给了我错误
79512 [http-8080-4] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000
79517 [http-8080-4] ERROR org.hibernate.util.JDBCExceptionReporter - Duplicate entry '2' for key 'employeeid'
79517 [http-8080-4] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
我的代码有什么问题?请帮我!!!