我正在學習JPA,並且遇到問題。 我有這些實體:JPA插入錯誤
員工:
@Entity
@Table(name="employees")
@NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e")
public class Employee implements Serializable {
@Id
@Column(name="emp_no")
private int empNo;
@Temporal(TemporalType.DATE)
@Column(name="birth_date")
private Date birthDate;
@Column(name="first_name")
private String firstName;
private String gender;
@Temporal(TemporalType.DATE)
@Column(name="hire_date")
private Date hireDate;
@Column(name="last_name")
private String lastName;
//bi-directional many-to-one association to DeptEmp
@OneToMany(mappedBy="employee")
private List<DeptEmp> deptEmps;
public Employee() {
}
public int getEmpNo() {
return this.empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public Date getBirthDate() {
return this.birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getGender() {
return this.gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getHireDate() {
return this.hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<DeptEmp> getDeptEmps() {
return this.deptEmps;
}
public void setDeptEmps(List<DeptEmp> deptEmps) {
this.deptEmps = deptEmps;
}
public DeptEmp addDeptEmp(DeptEmp deptEmp) {
getDeptEmps().add(deptEmp);
deptEmp.setEmployee(this);
return deptEmp;
}
public DeptEmp removeDeptEmp(DeptEmp deptEmp) {
getDeptEmps().remove(deptEmp);
deptEmp.setEmployee(null);
return deptEmp;
}
部
@Entity
@Table(name="departments")
@NamedQuery(name="Department.findAll", query="SELECT d FROM Department d")
public class Department implements Serializable {
@Id
@Column(name="dept_no")
private String deptNo;
@Column(name="dept_name")
private String deptName;
//bi-directional many-to-one association to DeptEmp
@OneToMany(mappedBy="department")
private List<DeptEmp> deptEmps;
public Department() {
}
public String getDeptNo() {
return this.deptNo;
}
public void setDeptNo(String deptNo) {
this.deptNo = deptNo;
}
public String getDeptName() {
return this.deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public List<DeptEmp> getDeptEmps() {
return this.deptEmps;
}
public void setDeptEmps(List<DeptEmp> deptEmps) {
this.deptEmps = deptEmps;
}
public DeptEmp addDeptEmp(DeptEmp deptEmp) {
getDeptEmps().add(deptEmp);
deptEmp.setDepartment(this);
return deptEmp;
}
public DeptEmp removeDeptEmp(DeptEmp deptEmp) {
getDeptEmps().remove(deptEmp);
deptEmp.setDepartment(null);
return deptEmp;
}
的deptemp:
@Entity
@Table(name="dept_emp")
@NamedQuery(name="DeptEmp.findAll", query="SELECT d FROM DeptEmp d")
public class DeptEmp implements Serializable {
@EmbeddedId
private DeptEmpPK id;
@Temporal(TemporalType.DATE)
@Column(name="from_date")
private Date fromDate;
@Temporal(TemporalType.DATE)
@Column(name="to_date")
private Date toDate;
//bi-directional many-to-one association to Department
@ManyToOne
@JoinColumn(name="dept_no", nullable=false)
private Department department;
//bi-directional many-to-one association to Employee
@ManyToOne
@JoinColumn(name="emp_no", nullable=false)
private Employee employee;
public DeptEmp() {
}
public DeptEmpPK getId() {
return this.id;
}
public void setId(DeptEmpPK id) {
this.id = id;
}
public Date getFromDate() {
return this.fromDate;
}
public void setFromDate(Date fromDate) {
this.fromDate = fromDate;
}
public Date getToDate() {
return this.toDate;
}
public void setToDate(Date toDate) {
this.toDate = toDate;
}
public Department getDepartment() {
return this.department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Employee getEmployee() {
return this.employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
這其中,我試圖插入一個員工,並在記錄代碼十字表dept_emp:
EntityManagerFactory fact=Persistence.createEntityManagerFactory("EmployeeJPA");
EntityManager man=fact.createEntityManager();
man.getTransaction().begin();
Employee emp=new Employee();
emp.setFirstName(request.getParameter("name"));
emp.setLastName(request.getParameter("surname"));
emp.setEmpNo(Integer.parseInt(request.getParameter("id")));
emp.setBirthDate(Date.valueOf(request.getParameter("birth")));
emp.setBirthDate(Date.valueOf(request.getParameter("hire")));
emp.setGender(request.getParameter("gender"));
HttpSession session=request.getSession();
Department dept=man.find(Department.class, session.getAttribute("idDep"));
man.persist(emp);
man.getTransaction().commit();
DeptEmp depEm=new DeptEmp();
depEm.setEmployee(emp);
depEm.setDepartment(dept);
man.getTransaction().begin();
man.persist(depEm);
man.getTransaction().commit();
man.close();
fact.close();
這是錯誤:
GRAVE: Servlet.service() for servlet [servlet.AddEmployeeServlet] in
context with path [/EmployeeJPA] threw exception
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse
Persistence Services - 2.5.0.v20130507-3faac2b):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'dept_no' cannot be null Error Code: 1048 Call: INSERT INTO dept_emp (from_date, to_date, dept_no, emp_no) VALUES (?, ?, ?, ?)
bind => [null, null, null, null]
我不明白,因爲我有這樣的錯誤。我是否以不同的方式插入交叉表? 有人可以幫助我嗎? 謝謝
我checke D它,find()返回正確的實體,事實上我不能低估,因爲它不起作用 – VincScorsone