ConstraintViolationException JPA

时间:2014-11-11 10:27:09

标签: java servlets jpa

我尝试使用带有@SequenceGenerator的JPA实体向数据库添加一些数据。但看起来这个创造者并没有按预期做的事情,我不知道为什么。

Hibernate: call next value for seq1
Hibernate: insert into Customer (FirstName, Surname, code, customerType, id) values (?, ?, ?, ?, ?)
13:20:36,078 ERROR SqlExceptionHelper:147 - integrity constraint violation: NOT NULL check constraint; SYS_CT_10093 table: CUSTOMER column: FIRST_NAME

继承我的模特课:

@Entity
public class Customer {

@Id
@SequenceGenerator(name = "my_seq", sequenceName = "seq1", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_seq")
private Long id;

public String FirstName;
public String Surname;
public String code;
public enum Customertype{Private, Corporate};
@Enumerated(EnumType.STRING)
public Customertype customerType;

我调用我的保存方法的Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    new SqlExecutor().execute("/schema.sql");

    Customer customer = new Customer();
    customer.setFirstName("Peeter");
    new CustomerDAO().save(customer);

2 个答案:

答案 0 :(得分:2)

这与发电机无关。错误消息非常清楚:

  

NOT NULL检查约束; SYS_CT_10093表:CUSTOMER列:FIRST_NAME

因此,您尝试在customer表中插入一行,该行包含一个名为FIRST_NAME的非空列,并且没有提供任何值。因此,您会遇到约束违规异常。

SQL语句是

insert into Customer (FirstName, Surname, code, customerType, id)

所以,您看,FIRST_NAME没有插入任何值。

这也表明您的表有一个名为FirstName的列,另一个名为FIRST_NAME。这真的不是一个好主意。

答案 1 :(得分:0)

将具有指定列名的注释@Column添加到字段FirstName:

@Column(name="FIRST_NAME")
public String FirstName;

有关详情,请尝试查看http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes