Spring ORM 和 Hibernate 6.x 迁移:SpringHibernateJpaPersistenceProvider 未实现 jakarta.persistence.spi.PersistenceProvider

问题描述 投票:0回答:1

我在将 Spring 6.1.12 应用程序迁移到使用 Hibernate 6.5.2.Final 和 jakarta.persistence-api 版本 3.1.0 时遇到问题。

我在应用程序启动过程中遇到的错误如下:

Error creating bean with name 'entityManagerFactory' defined in file:  Class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider  does not implement the requested interface jakarta.persistence.spi.PersistenceProvider

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

环境: 春季 ORM:6.1.12 休眠:6.5.2.最终版 持久性 API:3.1.0(雅加达 EE) 错误详细信息: 似乎 org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider 被引用,但它没有实现 jakarta.persistence.spi.PersistenceProvider,这在 Jakarta EE 9+ 环境中是预期的。

我尝试过的: 确保所有依赖项都使用 jakarta.* 包,但问题仍然存在。 仔细检查我使用的是与 Jakarta EE 兼容的正确 Hibernate 和 Spring 版本。

如何正确配置 Spring ORM 以将 Hibernate 6.x 与 jakarta.persistence API 结合使用? 是否有其他方法可以为此设置配置 LocalContainerEntityManagerFactoryBean? 与 Hibernate 6.x 和 Jakarta EE 9+ 一起使用的正确 persistenceProvider 类是什么?

主要pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>server</artifactId>
        <groupId>com.example</groupId>
        <version>3.2.8</version>
    </parent>

    <artifactId>app</artifactId>
    <packaging>war</packaging>

    <name>app</name>

    <build>
        <finalName>${project.artifactId}-${project.version}-r${buildNumber}</finalName>
        <defaultGoal>install</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
                <configuration>
                    <packagingExcludes>
                        scripts/vendor/yui/**/*-coverage.js,
                        scripts/vendor/yui/**/*-debug.js
                    </packagingExcludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>pom.properties</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>2.17.5</version>
                <configuration>
                    <java>
                        <eclipse>
                            <version>4.19.0</version>
                        </eclipse>
                    </java>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>apply</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Additional plugins here -->
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>ApplicationResources_de.properties</exclude>
                    <exclude>ApplicationResources_fr.properties</exclude>
                    <exclude>ApplicationResources_ko.properties</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.10.9</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>${commons.fileupload.version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
        </dependency>

        <dependency>
            <groupId>net.sf.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>2.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>

        <!-- Additional dependencies here -->
    </dependencies>

    <profiles>
        <profile>
            <id>dev</id>
            <dependencies>
                <dependency>
                    <groupId>com.zaxxer</groupId>
                    <artifactId>HikariCP</artifactId>
                    <version>${hikari.version}</version>
                </dependency>
            </dependencies>
        </profile>

        <profile>
            <id>test</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-test</artifactId>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

    <properties>
        <aspectj.version>1.9.21.1</aspectj.version>
        <commons.fileupload.version>1.5</commons.fileupload.version>
        <servlet.version>6.1.0</servlet.version>
    </properties>
</project

使用模型 pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <artifactId>server-parent</artifactId>
    <groupId>com.example</groupId>
    <version>3.2.8</version>
  </parent>

  <artifactId>app-model</artifactId>
  <packaging>jar</packaging>
  <name>app-model</name>

  <dependencies>
    <dependency>
      <groupId>org.hibernate.orm</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>${hibernate.version}</version>
      <exclusions>
        <exclusion>
          <groupId>jakarta.persistence</groupId>
          <artifactId>jakarta.persistence-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.hibernate.orm</groupId>
      <artifactId>hibernate-jpamodelgen</artifactId>
      <version>${hibernate.version}</version>
    </dependency>

    <dependency>
      <groupId>jakarta.persistence</groupId>
      <artifactId>jakarta.persistence-api</artifactId>
      <version>3.1.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- Formatting plugin -->
      <plugin>
        <groupId>com.diffplug.spotless</groupId>
        <artifactId>spotless-maven-plugin</artifactId>
        <version>2.17.5</version>
        <configuration>
          <java>
            <eclipse>
              <version>4.19.0</version>
            </eclipse>
          </java>
        </configuration>
        <executions>
          <execution>
            <goals>
<!--              <goal>check</goal>-->
              <goal>apply</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- JHusky plugin -->
      <plugin>
        <groupId>io.github.dathin</groupId>
        <artifactId>jhusky</artifactId>
        <version>1.0.9</version>
        <executions>
          <execution>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

堆栈跟踪

[INFO] 15:32:00 (main) HikariDataSource - Start completed.
[INFO] 15:32:00 (main) XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: 
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file 
    [path-to-webapp/WEB-INF/classes/conf/ac-dataAccess.xml]: 
    Class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider does not implement the requested interface jakarta.persistence.spi.PersistenceProvider
[INFO] 15:32:00 (main) HikariDataSource - Shutdown initiated...
[INFO] 15:32:00 (main) HikariDataSource - Shutdown completed.
[INFO] 15:32:00 (main) ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file 
    [path-to-webapp/WEB-INF/classes/conf/ac-dataAccess.xml]: 
    Class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider does not implement the requested interface jakarta.persistence.spi.PersistenceProvider
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1806)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:954)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:396)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:288)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:102)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3997)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4424)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:164)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:599)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:571)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:654)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1130)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1933)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123)
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1041)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:425)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1629)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:303)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:109)
    at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:385)
    at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:332)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:776)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:772)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:164)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1203)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1193)
    at java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:145)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:749)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:203)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:164)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:415)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:164)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:874)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:164)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:757)
    at java.lang.reflect.Method.invoke(Method.java:568)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:345)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:473)
Caused by: java.lang.IncompatibleClassChangeError: Class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider does not implement the requested interface jakarta.persistence.spi.PersistenceProvider
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:390)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1802)
    ... 52 more
spring hibernate maven
1个回答
0
投票

我已经从其他项目的其他依赖项中排除了 hibernate 依赖项。这对我来说已经解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.