Spring Boot org.hibernate.exception.ConstraintViolationException

时间:2018-05-09 23:51:34

标签: java postgresql hibernate spring-boot jpa

当我尝试使用POST REST API保存数据时,我得到.ConstraintViolationException。

引起:org.postgresql.util.PSQLException:错误:列“id”中的空值违反了非空约束   细节:失败的行包含(null,John Doe,你好吗?我很好)。

我正在使用@GeneratedValue(strategy = GenerationType.IDENTITY)从Hibernate自动生成“id”,我不确定我是否缺少application.properties中的任何配置。我正在使用Postgres db。

我尝试使用GenerationType.AUTO,我从postgres获得了hibernate_sequence错误。

谢谢!

使用Postman

POST REST API输入
            {   
                "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

1 个答案:

答案 0 :(得分:2)

这意味着您的数据库支持主键值的序列。因此,在您的情况下,您必须创建数据库序列,然后使用@GeneratedValue(strategy=GenerationType.AUTO)@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq") @SequenceGenerator(name="seq", sequenceName = "db_seq_name")为主键字段生成值。

还要确保将SERIAL添加到SQL中,使其看起来像:id SERIAL PRIMARY KEY

请参阅the serial data types的PostgreSQL文档。