Spring新手遇初始化失败:找不到org.hibernate.ejb.HibernatePersistence类
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-entitymanagerdependencies if they're present, unless you're intentionally using Hibernate 4 (but I'd recommend upgrading if possible). - Run
mvn clean install -Uto 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

