Skip to content
Advertisement

Hibernate not allowing to increment by 5 in sequence

I have following db sequence

CREATE SEQUENCE phonebook_id_seq INCREMENT BY 5;

select nextval('phonebook_id_seq'); // 1,6,11,....

Java

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "phoneSequenceGenerator")
@SequenceGenerator(name = "phoneSequenceGenerator", sequenceName = "phonebook_id_seq")
private Long id;

However with Hibernate I am getting following error which says hibernate is expecting increment by 50. Why is so?

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: sequence [phonebook_id_seq] defined inconsistent increment-size; found [5] but expecting [50]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:396)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:336)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1692)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1630)
    ... 41 common frames omitted
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: sequence [phonebook_id_seq] defined inconsistent increment-size; found [5] but expecting [50]
    at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateSequence(AbstractSchemaValidator.java:191)
    at org.hibernate.tool.schema.internal.AbstractSchemaValidator.performValidation(AbstractSchemaValidator.java:100)
    at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:68)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:191)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:313)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:452)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:360)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:384)
    ... 45 common frames omitted

Edit1 Added java code.

Advertisement

Answer

The error message is quite clear. You have enabled (or not disabled) schema validation, so when the application starts Hibernate is comparing the database with what it expects from annotations and/or configuration. The phonebook_id_seq sequence is defined with an increment of 5 in the database but the annotation says 50 (or says nothing and has a default value of 50). Change the annotation to match the database or change the sequence or disable validation.

I’m guessing that validation is enabled with spring.jpa.hibernate.ddl-auto=validate based on the tags, but without seeing your code it is hard to tell for sure.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement