![](/img/trans.png)
[英]nested exception is org.hibernate.exception.ConstraintViolationException
[英]Spring Boot org.hibernate.exception.ConstraintViolationException
當我嘗試使用 POST REST API 保留數據時,我收到 .ConstraintViolationException。
引起:org.postgresql.util.PSQLException:錯誤:“id”列中的空值違反了非空約束詳細信息:失敗的行包含(空,John Doe,你好嗎?,我很好)。
我正在使用 @GeneratedValue(strategy = GenerationType.IDENTITY) 從 Hibernate 自動生成“id”,我不確定是否缺少 application.properties 中的任何配置。 我正在使用 Postgres 數據庫。
我嘗試使用 GenerationType.AUTO 並且我從 postgres 收到 hibernate_sequence missing 錯誤。
謝謝!
{
"personName": "John Doe",
"question": "How are you?",
"response": "I am fine"
}
CREATE TABLE questionnaries(
id BIGINT PRIMARY KEY,
personName VARCHAR(255) NOT NULL,
question VARCHAR(255) NOT NULL,
response VARCHAR(255) NOT NULL
);
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;
}}
# ===============================
# = 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
這意味着您的數據庫支持主鍵值的序列。 因此,在您的情況下,您將必須創建一個數據庫序列,然后使用@GeneratedValue(strategy=GenerationType.AUTO)
或@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
@SequenceGenerator(name="seq", sequenceName = "db_seq_name")
為主鍵字段生成值。
還要確保將SERIAL添加到SQL中,使其看起來像: id SERIAL PRIMARY KEY
有關串行數據類型,請參見PostgreSQL文檔。
將您的腳本更改為: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 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.