2

Im working on a Spring (not Boot!) project, with Hibernate and PostgreSQL database. I also use Flyway for migration. I use Flyway to generate the schema of the database, and insert initial data to it with given SQL scripts in my resources folder. For this reason, I excluded the hibernate.hbm2ddl.auto property from my hibernate.properties file. On the startup, the schema is created and the data is inserted to the database, but my problem is, that this way Hibernate doesn't generate its sequence, and I cant save data from the application:

org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist

What can I do with this?

3
  • You should provide the schema. But the error message is saying hibernate_sequence does not exist. So you have to create the sequence to DB and try. Commented Jul 4, 2017 at 11:15
  • I see. So its completely okay to create an SQL script manually, that creates the hibernate_sequence? Commented Jul 4, 2017 at 11:17
  • Yes it is. Cause hibernate can not produce schema. Commented Jul 4, 2017 at 11:20

2 Answers 2

6

As you didn't provide any code not sure what is wrong there, assume missing @SequenceGenerator annotation, I'll provide the one is working for me.

@Entity
@Table(name = "your_table")
@SequenceGenerator(name = "your_table_id_seq", sequenceName = "your_table_id_seq", allocationSize = 1)
public class YourTable implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "your_table_id_seq")
private Long id;

. . .

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

1 Comment

Thank you, I am looking forward to implement this solution too!
3

You need to create a sequence like this:

CREATE SEQUENCE hibernate_sequence START 1;

2 Comments

Now its better, but still have errors. The sequence is created, but now I cant insert data from the app, because org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "user_pkey" Detail: Key (id)=(1) already exists. The order is the following: create hibernate_sequence, create tables, insert data. Seems like inserting the data not incrementing the value in hibernate_sequence?
Add DEFAULT nextval(‘hibernate_sequence’) to id in your schema

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.