2017-06-23 109 views
0

我正在學習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] 

我不明白,因爲我有這樣的錯誤。我是否以不同的方式插入交叉表? 有人可以幫助我嗎? 謝謝

回答

0

看起來像行 Department dept = man.find(Department.class,session.getAttribute(「idDep」)); 返回NULL。

但是在db表中dept_no列沒有null約束。

+0

我checke D它,find()返回正確的實體,事實上我不能低估,因爲它不起作用 – VincScorsone

0

不確定這是否是問題的真正原因,但Department在不同於DepEmp實體的事務中被檢索。這通常會導致問題,所以它可能值得入手訂購您的代碼是這樣的:

  1. HttpSession session = request.getSession(); 
    Object deptId = session.getAttribute("idDep"); 
    
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("EmployeeJPA"); 
    EntityManager entityManager = entityManagerFactory.createEntityManager(); 
    entityManager.getTransaction().begin(); 
    
    Employee emp = new Employee(); 
    // fill employee's fields 
    entityManager.persist(emp); 
    
    Department dept = entityManager.find(Department.class, deptId); 
    
    DeptEmp depEm = new DeptEmp(); 
    depEm.setEmployee(emp); 
    depEm.setDepartment(dept); 
    entityManager.persist(depEm); 
    
    entityManager.getTransaction().commit(); 
    entityManager.close(); 
    entityManagerFactory.close(); 
    

    您需要執行應該是非常相同的事務上下文中的這三個動作堅持新員工

  2. 查找部
  3. 堅持員工,部門關係
+0

我已經嘗試過它,但不起作用 – VincScorsone