2

I am getting .ConstraintViolationException when I try to persist data using the POST REST API.

Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint Detail: Failing row contains (null, John Doe, How are you?, I am fine).

I am using @GeneratedValue(strategy = GenerationType.IDENTITY) to auto generate "id" from Hibernate and I am not sure If I am missing any configuration in application.properties. I am using Postgres db.

I tried using GenerationType.AUTO and I was getting hibernate_sequence missing error from postgres.

Thanks!

POST REST API input using Postman

            {   
                "personName": "John Doe",
                "question": "How are you?",
                "response": "I am fine"
            }

questionnaries.sql

            CREATE TABLE questionnaries( 
            id BIGINT PRIMARY KEY,
            personName VARCHAR(255) NOT NULL,
            question VARCHAR(255) NOT NULL,
            response VARCHAR(255) NOT NULL
            );

Questionnarie.java #

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "questionnaries")
public class Questionnarie {



    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;


    @Column(name = "personname")
    @NotNull
    private String personname;


    @Column(name = "question")
    @NotNull
    private String question;

    @Column(name = "response")
    @NotNull
    private String response;

    public Questionnarie() {}

    public Questionnarie(@NotNull String personname, @NotNull String question, @NotNull String response) {
        super();
        this.personname = personname;
        this.question = question;
        this.response = response;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPersonname() {
        return personname;
    }

    public void setPersonname(String personname) {
        this.personname = personname;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }}

application.properties

# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection

spring.datasource.jndi-name=java:jboss/datasources/test_data_source

# ===============================
# = JPA / HIBERNATE
# ===============================
# Show or not log for each sql query
spring.jpa.show-sql=true

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

2 Answers 2

3

That means your database supports sequences for primary key values. So in your case, you will have to create a Database sequence and then use @GeneratedValue(strategy=GenerationType.AUTO) or @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq") @SequenceGenerator(name="seq", sequenceName = "db_seq_name") to generate values for primary key fields.

Also make sure that you add SERIAL to your SQL, so that it looks like: id SERIAL PRIMARY KEY

See the PostgreSQL documentation for the serial data types.

Sign up to request clarification or add additional context in comments.

1 Comment

If I use @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq") then generator="seq" signifies that the name of the sequence is "seq" in the database? or will it be <tablename_colname_seq> ?
0

Change your script to:

CREATE TABLE questionnaries( 
 id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
 personName VARCHAR(255) NOT NULL,
 question VARCHAR(255) NOT NULL,
 response VARCHAR(255) NOT NULL
);

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Please add additional information to your answer that will help the reader understand why/how does it resolve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.