简体   繁体   中英

Spring Boot org.hibernate.exception.ConstraintViolationException

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

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 .

将您的脚本更改为: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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM