Eclipselink尝试插入表中的空值

时间:2018-11-09 18:25:29

标签: java postgresql jpa eclipselink

我目前正在尝试使用eclipselink创建多对多映射。请注意,对于此特定示例,未使用任何表来解析该关系(我知道这不是一个好习惯,但对于该特定示例是必要的。)

我已经创建了一个数据库架构以及要映射的表employee3和保险。雇员表包含一个名为insurance_id的列,该列是创建映射的主键的一部分。拥有employee_id的保险也是如此。

现在输入代码:

这是两个类的代码:

首批员工:

@Entity
@Table(name="employee3", schema = "many_to_many")
public class EmployeeManyToMany
{  
  protected EmployeeManyToMany()
  {

  }

  @Id
  private int id;

  private String firstName;
  private String lastName;

  @ManyToMany(cascade =  CascadeType.PERSIST)
  private Collection<InsuranceManyToMany> insurance;


  public EmployeeManyToMany(int id, String firstName, String lastName)
  {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    insurance = new ArrayList<InsuranceManyToMany>();
  }
.....

保险:

@Entity
@Table(name = "insurance", schema = "many_to_many")
public class InsuranceManyToMany
{
  @Id
  @Column(name = "id")
  private int insuranceId;
  private String company;

  @ManyToMany(mappedBy = "insurance", cascade = CascadeType.PERSIST)
  private Collection<EmployeeManyToMany> employee;

  protected InsuranceManyToMany()
  {
  }

  public void addEmployee(EmployeeManyToMany emp)
  {
     employee.add(emp);
  }

  public InsuranceManyToMany(String company, int insuranceId)
  {
     this.insuranceId = insuranceId;
     this.company = company;
     employee = new ArrayList<EmployeeManyToMany>();
  }
....

创建雇员对象并向其中添加保险列表之后,我尝试将其持久保存到数据库中。 导致以下错误:

javax.persistence.RollbackException: Exception [EclipseLink-4002]              
Eclipse Persistence Services - 2.7.3.v20180807-4be1041):    
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: null value 
in column "insurance_id" violates not-null constraint
Detail: Failing row contains (1, hans, test, null).
Error Code: 0
Call: INSERT INTO many_to_many.employee3 (ID, FIRSTNAME, LASTNAME) VALUES
(?, ?, ?)
bind => [3 parameters bound]
Query: InsertObjectQuery(swd_ws18_06_Tag3.EmployeeManyToMany@88d98e)

我不知道为什么会发生这种情况,因为值永远不会为空。

感谢您的帮助!

BR 西蒙

0 个答案:

没有答案