java - JPA插入错误

标签 java mysql 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;
 }

部门:

@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]

我无法理解,因为我有这个错误。我是否可以用不同的方式插入交叉表? 有人可以帮助我吗? 谢谢

最佳答案

看起来像线 部门 dept=man.find(Department.class, session.getAttribute("idDep")); 返回 NULL。

但是在 db 表中,dept_no 列没有 null 约束。

关于java - JPA插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719493/

相关文章:

php - 查找重复项并在 sql 中合并它们的值

php - foreach在laravel中加入并选择

java - 如何将 SQL 表创建转换为 JPA hibernate 自动创建

java - Hibernate 无法添加或更新子行 : a foreign key constraint fails

JAVA - 实例化 Scala 案例类

java - 如何使用动态xpath进行谷歌搜索?

php - 根据我的数据库中是否有数据更改我的网站中的链接

google-app-engine - google app engine java datastore是否缓存JPA查询结果?

java - 单元测试在标记为 @Transactional 时通过,但在未标记为 @Transactional 时失败

java - jdk1.8.0 中的 NoSuchMethodException : for sun. misc.Launcher$AppClassLoader.addAppURL(java.net.URL)