Reputation: 233
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!
{
"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
Upvotes: 2
Views: 2334
Reputation: 1
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
);
Upvotes: 0
Reputation: 761
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.
Upvotes: 3