Juuri Peeter
Juuri Peeter

Reputation: 131

ConstraintViolationException JPA

I'm trying to add some data to database using JPA entity with @SequenceGenerator. But appears that this genereator doesnt do what it is expected to do and i have no clue why.

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

Heres my model class :

@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 where i call my save method

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);

Upvotes: 1

Views: 2010

Answers (2)

Anton Kapralov
Anton Kapralov

Reputation: 110

Add annotation @Column with specified column name to field FirstName:

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

For more info try to look at http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691685

That has nothing to do with the generator. The error message is extremely clear:

NOT NULL check constraint; SYS_CT_10093 table: CUSTOMER column: FIRST_NAME

So, you're trying to insert a row in the customer table, which has a not null column named FIRST_NAME, and for which no value is provided. So you get a constraint violation exception.

The SQL statement is

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

so, you see, there is no value inserted for FIRST_NAME.

That also shows that your table has a column named FirstName, and another one named FIRST_NAME. That's really not a good idea.

Upvotes: 2

Related Questions