简体   繁体   中英

Hibernate sequence not generated

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?

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;

. . .

您需要创建一个像这样的序列:

CREATE SEQUENCE hibernate_sequence START 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM