You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring新手遇初始化失败:找不到org.hibernate.ejb.HibernatePersistence类

Fixing "Cannot find class [org.hibernate.ejb.HibernatePersistence]" Error

Hey there! Let's tackle this error you're facing. The root cause is super clear: the class org.hibernate.ejb.HibernatePersistence was removed in Hibernate 5 and later versions—it got replaced with org.hibernate.jpa.HibernatePersistenceProvider. Here's what you need to do step by step to fix this:

1. Fix Your Hibernate Dependencies in pom.xml

First, make sure you're using a consistent, up-to-date Hibernate version. If you're on Hibernate 5.2 or newer, the hibernate-entitymanager artifact was merged into hibernate-core, so you don't need to include it separately anymore.

Example pom.xml Dependency Snippet:

<!-- Use a stable Hibernate 5.x version (adjust as needed) -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.6.15.Final</version>
</dependency>

<!-- If you need JPA API (usually included via transitive dependencies, but add explicitly if needed) -->
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>
  • Remove any old hibernate-entitymanager dependencies if they're present, unless you're intentionally using Hibernate 4 (but I'd recommend upgrading if possible).
  • Run mvn clean install -U to refresh your dependencies and clear any cached old artifacts.

2. Update persistence.xml Provider Configuration

Your persistence unit is still pointing to the old Hibernate persistence class. Replace it with the new one:

Before:

<provider>org.hibernate.ejb.HibernatePersistence</provider>

After:

<persistence-unit name="yourPersistenceUnitName" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <!-- Your entity classes, data source, and Hibernate properties go here -->
</persistence-unit>

3. Adjust Spring's Entity Manager Factory Configuration (applicationContext.xml)

If you're using LocalContainerEntityManagerFactoryBean in your Spring config, double-check that you're referencing the correct persistence provider class (if you've explicitly set it):

Updated Bean Configuration:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="yourPersistenceUnitName"/>
    <!-- Only add this line if you're explicitly setting the provider; it might be auto-detected otherwise -->
    <property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"/>
    <property name="dataSource" ref="yourDataSourceBean"/>
    <!-- Other JPA vendor/adapter settings -->
</bean>

Why Your Previous Fixes Might Have Failed

Chances are you mixed up Hibernate versions (e.g., using Hibernate 5 dependencies but keeping the old Hibernate 4 configuration) or only changed part of the required settings. Making sure all three areas (dependencies, persistence.xml, Spring config) are aligned will resolve the class-not-found error.

内容的提问来源于stack exchange,提问作者AaronLbk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:13:25