当前位置: 首页 > 知识库问答 >
问题:

Hibernate无法实例化ID生成器

东方明亮
2023-03-14

环境:Spring Boot 2.2.6
启动程序:spring-boot-starter-data-jpa,*-thymeleaf,*-web,*-Tomcat,*-test
其他依赖项:mariadb-java-client 2.6.0,spring-boot-devtools(管理),lombok(管理)
DB mariadb:10.4
Hibernate方言:mariadb103
使用内部容器进行测试与Tomcat

关键错误是Hibernate无法实例化id生成器,以下是控制台日志:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-04-26 13:53:52.895  INFO 23238 --- [  restartedMain] c.m.c.ContractPlusApplication            : Starting ContractPlusApplication on mark-Inspiron-5759 with PID 23238 (/home/mark/eclipse-workspace/contract-plus.zip_expanded/contract-plus/target/classes started by mark in /home/mark/eclipse-workspace/contract-plus.zip_expanded/contract-plus)
2020-04-26 13:53:52.897  INFO 23238 --- [  restartedMain] c.m.c.ContractPlusApplication            : No active profile set, falling back to default profiles: default
2020-04-26 13:53:52.939  INFO 23238 --- [  restartedMain] o.s.b.devtools.restart.ChangeableUrls    : The Class-Path manifest attribute in /home/mark/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/jaxb-runtime-2.3.2.jar referenced one or more files that do not exist: file:/home/mark/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/jakarta.xml.bind-api-2.3.2.jar,file:/home/mark/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/txw2-2.3.2.jar,file:/home/mark/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/istack-commons-runtime-3.0.8.jar,file:/home/mark/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/stax-ex-1.8.1.jar,file:/home/mark/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/FastInfoset-1.2.16.jar,file:/home/mark/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/jakarta.activation-api-1.2.1.jar
2020-04-26 13:53:52.939  INFO 23238 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-04-26 13:53:52.939  INFO 23238 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-04-26 13:53:53.535  INFO 23238 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-04-26 13:53:53.584  INFO 23238 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 43ms. Found 1 JPA repository interfaces.
2020-04-26 13:53:54.033  INFO 23238 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-04-26 13:53:54.040  INFO 23238 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-04-26 13:53:54.040  INFO 23238 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-26 13:53:54.042  INFO 23238 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : An older version [1.2.21] of the APR based Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.23]
2020-04-26 13:53:54.042  INFO 23238 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : Loaded APR based Apache Tomcat Native library [1.2.21] using APR version [1.6.3].
2020-04-26 13:53:54.042  INFO 23238 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2020-04-26 13:53:54.042  INFO 23238 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2020-04-26 13:53:54.045  INFO 23238 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1  11 Sep 2018]
2020-04-26 13:53:54.087  INFO 23238 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-04-26 13:53:54.088  INFO 23238 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1149 ms
2020-04-26 13:53:54.190  INFO 23238 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-04-26 13:53:54.225  INFO 23238 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-04-26 13:53:54.258  INFO 23238 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-04-26 13:53:54.305  INFO 23238 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-04-26 13:53:54.450  INFO 23238 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-04-26 13:53:54.515  INFO 23238 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MariaDB103Dialect
2020-04-26 13:53:54.711  WARN 23238 --- [  restartedMain] ConfigServletWebServerApplicationContext : 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=com.mmurphy.contractplus.core.entities.Client]
2020-04-26 13:53:54.712  INFO 23238 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-04-26 13:53:54.714  INFO 23238 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2020-04-26 13:53:54.716  INFO 23238 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-04-26 13:53:54.726  INFO 23238 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-26 13:53:54.735 ERROR 23238 --- [  restartedMain] 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=com.mmurphy.contractplus.core.entities.Client]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at com.mmurphy.contractplus.ContractPlusApplication.main(ContractPlusApplication.java:10) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_252]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_252]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_252]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_252]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.6.RELEASE.jar:2.2.6.RELEASE]
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=com.mmurphy.contractplus.core.entities.Client]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    ... 21 common frames omitted
Caused by: org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.mmurphy.contractplus.core.entities.Client]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:124) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:344) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.internal.SessionFactoryImpl.lambda$new$1(SessionFactoryImpl.java:286) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) ~[na:1.8.0_252]
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_252]
    at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1628) ~[na:1.8.0_252]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[na:1.8.0_252]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[na:1.8.0_252]
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) ~[na:1.8.0_252]
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) ~[na:1.8.0_252]
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_252]
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:485) ~[na:1.8.0_252]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:285) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1237) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    ... 25 common frames omitted
Caused by: org.hibernate.MappingException: The increment size of the [client_sequence] sequence is set to [50] in the entity mapping while the associated database sequence increment size is [1].
    at org.hibernate.id.enhanced.SequenceStyleGenerator.configure(SequenceStyleGenerator.java:261) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:118) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    ... 42 common frames omitted

这是我的实体

import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import lombok.Data;


@Data
@Entity(name="clients")
public class Client {

    @Id
    @SequenceGenerator(name="client_generator", sequenceName="client_sequence")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="client_generator")
    private long id;

    private String client_name;

    @Enumerated(EnumType.STRING)
    private ClientStatus status;

    @CreatedDate 
    private Timestamp created_date;

    @LastModifiedDate 
    private Timestamp change_date;

    private Timestamp hold_date;

    private Timestamp deleted_date;

}

明显的重复说Maria不支持序列已经过时了,因为MariaDB103方言的声明变化之一是序列支持。

我也看到一些关于自定义生成器的类似问题,但我无法从这些问题中得到答案。我能够使它工作使用身份和自动编号,但我想尝试序列,但无法弄清楚秘密咒语。这里缺什么?

共有1个答案

阳英朗
2023-03-14

Hibernate 5.4尝试通过数据库序列增量大小验证@SequenceGeneratorAllocationSize

Caused by: org.hibernate.MappingException: The increment size of the [client_sequence] sequence is set to [50] in the entity mapping while the associated database sequence increment size is [1].

您应该按照以下方式更正您的映射:

@Data
@Entity(name="clients")
public class Client {

  @Id
  @SequenceGenerator(name="client_generator", sequenceName="client_sequence", allocationSize=1)
  @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="client_generator")
  private long id;

  // ...
}
 类似资料:
  • 我在尝试将HiberNate与SAP HANA In-Memory数据库连接时遇到了一些性能问题,该数据库不支持AUTO_INCREMENT(http://scn.sap.com/thread/3238906)。 因此,我将HiberNate设置为使用序列生成ID。 但是当我插入大量记录(例如,40000)时,Hibernate首先会生成ID。它看起来像: 只有在生成所有 ID 后,它才会开始实际

  • 我有一个使用两个数据源(db2和sql-server)的Hibernate的Spring项目。 我一加上 在创建sql-server时,我会得到一个,因为不支持序列。但是,该实体唯一被使用的地方是在使用db2的DAO中,而db2使用了适当的方言。 我错过了什么?

  • 在尝试创建具有自动生成ID的表时,我遇到了一个异常: commandAcceptanceException:“执行DDL时出错”create table seat(id bigint not null,description varchar(255),num integer not null,PROCY decimal(19,2),row char(255)not null,primary key

  • 问题内容: 我有一个要映射到一个类的SQL查询。我正在使用Hibernate的createSQLQuery,然后将Result Transformer用于要映射到的类。但是Hibernate抛出此错误: 据我所知,这意味着我的构造函数是错误的。它设置为public,并且为空。不知道我还要在这里做什么? 从pgadmin运行时,SQL查询工作正常。在这里,我使用addScalar设置了所有字段,因为

  • 欢迎(org.springframework.ui.model)到{[/],methods=[GET]}:已经映射了“page controller”bean方法public java.lang.string com.jeeniso.controllers.PageController.PlainPage()。2018-02-18 14:28:27.667信息12088---[restartedM

  • 我试图使用Postgis 2.2和Postgreql 9.5与JPA,Postgis 9.5方言。我已经在pom.xml的要求,按这里http://www.hibernatespatial.org/documentation/documentation/和类型导入正确,但是当我试图运行程序使用几何类型我得到这个错误: 我显然遗漏了一些配置,有人能指出是什么吗?