0

I have created a sequence in the database (PostgreSQL) and I have the following annotations in the domain model. I am using JPA. However, when I invoke the web service (REST) without an id in the object that I want to create it throws a constraint error. However shouldn't it use a value from the sequence below?

@Id
@SequenceGenerator(name = "user_seq", sequenceName = "user_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "user_seq")
private int id;

Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint

0

1 Answer 1

1

When value comes from database sequence, GenerationType.SEQUENCE should be used as a strategy. GenerationType.IDENTITY is used with identity columns.

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

2 Comments

Awesome, thanks for spotting that. I have written SEQUENCE in the other classes. Is IDENTITY the correct type to use if you use auto incrementing column values as you can in MySQL for instance? I don't understand the explanation in the API.
Welcome. Yes, identity is correct choice for MySQL auto increment.

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.