Hello.
I start from afar. My project’s stack is
Spring Boot 3.1.5
Liquibase 4.20.0
Hibernate 6.2.13
PostgreSQL (ver. 12.13)
JDBC Driver (ver. 42.6.0, JDBC4.2)
I am trying to create table “users” with integer id by using liquibase script.
application.properties:
spring.jpa.database=postgresql
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.url=~~~
spring.datasource.username=~~~
spring.datasource.password=~~~
Liquibase:
~~~
changes:
- createTable:
tableName: users
columns:
- column:
name: id
type: SERIAL
autoIncrement: true
constraints:
nullable: false
primaryKey: true
~~~
User.class:
~~~
@Entity
@Table(name = "users")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
~~~
Liquibase script creates table without exceptions and at the same time with it a sequence named “users_id_seq” (integer type) is being created.
Content of this sequence:
create sequence users_id_seq;
alter sequence users_id_seq owner to postgres;
alter sequence users_id_seq owned by users.id;
But when I try to run my application, I got an exception
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing sequence [users_seq]
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateSequence(AbstractSchemaValidator.java:182) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.performValidation(AbstractSchemaValidator.java:113) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:75) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:293) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.lambda$process$5(SchemaManagementToolCoordinator.java:143) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at java.base/java.util.HashMap.forEach(HashMap.java:1420) ~[na:na]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:140) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at org.hibernate.boot.internal.SessionFactoryObserverForSchemaExport.sessionFactoryCreated(SessionFactoryObserverForSchemaExport.java:37) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:293) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1458) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:75) ~[spring-orm-6.0.13.jar:6.0.13]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[spring-orm-6.0.13.jar:6.0.13]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.0.13.jar:6.0.13]
... 20 common frames omitted
I’ve tried to edit User.class like this:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "userseq")
@SequenceGenerator(name = "userseq", schema = "public", sequenceName = "users_id_seq")
private Integer id;
but got exception Schema-validation: missing sequence [public.users_id_seq].
The only solution I’ve come to is to create a new sequence together with the table I need like this:
- createSequence:
sequenceName: users_id_seq
schemaName: information_schema
incrementBy: 1
startValue: 1
dataType: integer
and edit annotations in User.class like this:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "userseq")
@SequenceGenerator(name = "userseq", sequenceName = "users_id_seq", allocationSize = 1)
private Integer id;
But this solution seems ugly for me: now I’ve got two sequences in two different schemas for one same purpose.
I’ve checked this behaviour on Hibernate version 6.2.5 (it repeated) and 5.6.15 (it did not, everything worked), so it seems like some new changes in workflow.
- Am I doing something wrong? How to get sequence validated without exceptions?
- org.hibernate.dialect.PostgreSQLDialect#getQuerySequencesString has
return "select * from information_schema.sequences"
hardcoded. Is this the source of the problem? - Why even direct declaration of schema and sequence names does not work?
And additional question, which does not directly relate to this particular problem: - If set option
spring.jpa.hibernate.ddl-auto=create
, thien Hibernate create the sequence by its own, but this sequence will have type bigint (for integer type id) and increment size of 50. Why bigint and why so big increment?
Thanks up front.