问题描述
在更新到Spring Boot 2.2和关联的Hibernate 5.4.x时,我们遇到了问题.
We run into a problem when updating to Spring Boot 2.2 and the associated Hibernate 5.4.x.
我们有以下序列生成器
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hilo_sequence_generator")
@GenericGenerator(
name = "hilo_sequence_generator",
strategy = "...",
parameters = {
@Parameter(name = "sequence_name", value = "XXX_SEQUENCE"),
@Parameter(name = "initial_value", value = "1"),
@Parameter(name = "increment_size", value = "10"),
@Parameter(name = "optimizer", value = "hilo")
})
private Long id;
随着SB 2.2/HB 5.4的更新,我们收到以下初始化错误
With the update to SB 2.2 / HB 5.4 we get the following initialization error
org.hibernate.MappingException: The increment size of the sequence is set to [10] in the entity mapping while the associated database sequence increment size is [1]
DB序列增量大小为1:
The DB sequence increment size is 1:
Sequence "...xxx_sequence"
Type | Start | Minimum | Maximum | Increment | Cycles? | Cache
--------+-------+---------+---------------------+-----------+---------+-------
bigint | 1 | 1 | 9223372036854775807 | 1 | no | 1
但是,据我了解,hilo算法的(软)增量大小与数据库的(硬)增量大小无关.
However, to my understanding the (soft) increment size of the hilo algorithm has nothing to do with the (hard) increment size of the DB.
这是HB的错误还是我们可以以某种方式解决该问题?
Is this an error in HB or can we circumvent the problem somehow?
WRT解决了Jens的问题
WRT to the questions of Jens:
-
序列是由Liquibase生成的:
Sequence is generated by Liquibase:
<changeSet author="GerdAschemann (generated)" id="1540300079577-1">
<createSequence sequenceName="rbl_sequence"/>
</changeSet>
它在应用程序上下文设置/Bean初始化期间发生:
It happens during Application context setup/Bean initialization:
2019-12-12 18:29:52.841 INFO 20076 --- [ main] o.h.j.i.u.LogHelper : {} HHH000204: Processing PersistenceUnitInfo [name: default]
2019-12-12 18:29:52.971 INFO 20076 --- [ main] o.h.Version : {} HHH000412: Hibernate Core {5.4.9.Final}
2019-12-12 18:29:53.127 INFO 20076 --- [ main] o.h.a.c.Version : {} HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2019-12-12 18:29:53.320 INFO 20076 --- [ main] o.h.d.Dialect : {} HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
2019-12-12 18:29:53.962 ERROR 20076 --- [ main] o.s.b.w.e.t.TomcatStarter : {} Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'batchDeleteEndpointConfiguration' defined in file [R:\codebase\reisebegleitung\app\app-guide\target\classes\de\db\vendo\rbl\guide\adapter\maintenanceEndpoint\BatchDeleteEndpointConfiguration.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'maintenanceApplication' defined in file [R:\codebase\reisebegleitung\app\app-guide\target\classes\de\db\vendo\rbl\guide\application\MaintenanceApplication.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reisekettenRepositoryImpl' defined in file [R:\codebase\reisebegleitung\app\app-guide\target\classes\de\db\vendo\rbl\guide\adapter\domainJpa\ReisekettenRepositoryImpl.class]: Unsatisfied dependency expressed through constructor parameter 4; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reisekettenJpaRepository': Cannot create inner bean '(inner bean)#5b430541' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5b430541': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate id generator [entity-name=de.db.vendo.rbl.guide.adapter.domainJpa.entity.SubscriptionRetryEntity]
2019-12-12 18:29:53.994 INFO 20076 --- [ main] o.a.c.c.StandardService : {} Stopping service [Tomcat]
禁用验证不会更改任何内容
Disabling validation does not change anything
向GH添加了一个示例项目: https://github.com/ascheman/hibernate-so-59305427
Added a sample project to GH: https://github.com/ascheman/hibernate-so-59305427
堆栈跟踪:
2019-12-13 11:58:33.245 INFO 16796 --- [ main] n.a.d.s.b.h.HibernateProblemApplication : No active profile set, falling back to default profiles: default
2019-12-13 11:58:33.773 INFO 16796 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2019-12-13 11:58:33.858 INFO 16796 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 76ms. Found 1 JPA repository interfaces.
2019-12-13 11:58:34.194 INFO 16796 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-12-13 11:58:34.282 INFO 16796 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-12-13 11:58:35.149 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : SELECT COUNT(*) FROM PUBLIC.DATABASECHANGELOGLOCK
2019-12-13 11:58:35.157 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : CREATE TABLE PUBLIC.DATABASECHANGELOGLOCK (ID INT NOT NULL, LOCKED BOOLEAN NOT NULL, LOCKGRANTED TIMESTAMP, LOCKEDBY VARCHAR(255), CONSTRAINT PK_DATABASECHANGELOGLOCK PRIMARY KEY (ID))
2019-12-13 11:58:35.162 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : SELECT COUNT(*) FROM PUBLIC.DATABASECHANGELOGLOCK
2019-12-13 11:58:35.165 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : DELETE FROM PUBLIC.DATABASECHANGELOGLOCK
2019-12-13 11:58:35.166 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : INSERT INTO PUBLIC.DATABASECHANGELOGLOCK (ID, LOCKED) VALUES (1, FALSE)
2019-12-13 11:58:35.169 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : SELECT LOCKED FROM PUBLIC.DATABASECHANGELOGLOCK WHERE ID=1
2019-12-13 11:58:35.176 INFO 16796 --- [ main] l.lockservice.StandardLockService : Successfully acquired change log lock
2019-12-13 11:58:36.825 INFO 16796 --- [ main] l.c.StandardChangeLogHistoryService : Creating database history table with name: PUBLIC.DATABASECHANGELOG
2019-12-13 11:58:36.826 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : CREATE TABLE PUBLIC.DATABASECHANGELOG (ID VARCHAR(255) NOT NULL, AUTHOR VARCHAR(255) NOT NULL, FILENAME VARCHAR(255) NOT NULL, DATEEXECUTED TIMESTAMP NOT NULL, ORDEREXECUTED INT NOT NULL, EXECTYPE VARCHAR(10) NOT NULL, MD5SUM VARCHAR(35), DESCRIPTION VARCHAR(255), COMMENTS VARCHAR(255), TAG VARCHAR(255), LIQUIBASE VARCHAR(20), CONTEXTS VARCHAR(255), LABELS VARCHAR(255), DEPLOYMENT_ID VARCHAR(10))
2019-12-13 11:58:36.827 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : SELECT COUNT(*) FROM PUBLIC.DATABASECHANGELOG
2019-12-13 11:58:36.827 INFO 16796 --- [ main] l.c.StandardChangeLogHistoryService : Reading from PUBLIC.DATABASECHANGELOG
2019-12-13 11:58:36.827 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : SELECT * FROM PUBLIC.DATABASECHANGELOG ORDER BY DATEEXECUTED ASC, ORDEREXECUTED ASC
2019-12-13 11:58:36.828 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : SELECT COUNT(*) FROM PUBLIC.DATABASECHANGELOGLOCK
2019-12-13 11:58:36.833 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : CREATE SEQUENCE PUBLIC.xxx_sequence
2019-12-13 11:58:36.835 INFO 16796 --- [ main] liquibase.changelog.ChangeSet : Sequence xxx_sequence created
2019-12-13 11:58:36.835 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : CREATE TABLE PUBLIC.xxx_entity (id BIGINT NOT NULL, CONSTRAINT kunde_pkey PRIMARY KEY (id))
2019-12-13 11:58:36.836 INFO 16796 --- [ main] liquibase.changelog.ChangeSet : Table xxx_entity created
2019-12-13 11:58:36.836 INFO 16796 --- [ main] liquibase.changelog.ChangeSet : ChangeSet classpath:/db/db.changelog.xml::1::GerdAschemann ran successfully in 3ms
2019-12-13 11:58:36.837 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : SELECT MAX(ORDEREXECUTED) FROM PUBLIC.DATABASECHANGELOG
2019-12-13 11:58:36.840 INFO 16796 --- [ main] liquibase.executor.jvm.JdbcExecutor : INSERT INTO PUBLIC.DATABASECHANGELOG (ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, MD5SUM, DESCRIPTION, COMMENTS, EXECTYPE, CONTEXTS, LABELS, LIQUIBASE, DEPLOYMENT_ID) VALUES ('1', 'GerdAschemann', 'classpath:/db/db.changelog.xml', NOW(), 1, '8:c491fb651a899536b626ee5370713ff7', 'createSequence sequenceName=xxx_sequence; createTable tableName=xxx_entity', '', 'EXECUTED', NULL, NULL, '3.8.2', '6234716828')
2019-12-13 11:58:36.850 INFO 16796 --- [ main] l.lockservice.StandardLockService : Successfully released change log lock
2019-12-13 11:58:36.947 INFO 16796 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2019-12-13 11:58:37.007 INFO 16796 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.4.9.Final}
2019-12-13 11:58:37.152 INFO 16796 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2019-12-13 11:58:37.251 INFO 16796 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2019-12-13 11:58:37.456 WARN 16796 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate id generator [entity-name=net.aschemann.demo.spring.boot.hibernateproblem.XxxEntity]
2019-12-13 11:58:37.456 INFO 16796 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2019-12-13 11:58:37.458 INFO 16796 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2019-12-13 11:58:37.465 INFO 16796 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-12-13 11:58:37.475 ERROR 16796 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate id generator [entity-name=net.aschemann.demo.spring.boot.hibernateproblem.XxxEntity]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at net.aschemann.demo.spring.boot.hibernateproblem.HibernateProblemApplication.main(HibernateProblemApplication.java:10) [classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate id generator [entity-name=net.aschemann.demo.spring.boot.hibernateproblem.XxxEntity]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
... 15 common frames omitted
Caused by: org.hibernate.MappingException: Could not instantiate id generator [entity-name=net.aschemann.demo.spring.boot.hibernateproblem.XxxEntity]
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:124) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:344) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
at org.hibernate.internal.SessionFactoryImpl.lambda$new$1(SessionFactoryImpl.java:292) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184) ~[na:1.8.0_192-ea]
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_192-ea]
at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1628) ~[na:1.8.0_192-ea]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_192-ea]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_192-ea]
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) ~[na:1.8.0_192-ea]
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) ~[na:1.8.0_192-ea]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_192-ea]
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) ~[na:1.8.0_192-ea]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:291) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1237) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
... 19 common frames omitted
Caused by: org.hibernate.MappingException: The increment size of the [XXX_SEQUENCE] sequence is set to [10] in the entity mapping while the associated database sequence increment size is [1].
at org.hibernate.id.enhanced.SequenceStyleGenerator.configure(SequenceStyleGenerator.java:257) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:118) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
... 36 common frames omitted
Process finished with exit code 1
推荐答案
我遇到了同样的问题,您可以在id字段中使用以下注释来解决它:
I experienced the same issue, you can fix it with the following annotation in the id field:
@SequenceGenerator(name = "hilo_sequence_generator", allocationSize = 1)
这篇关于org.hibernate.MappingException:在实体映射中,序列的增量大小设置为[10],而...大小为[1]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!