错误信息如下:
Error is: "weblogic.application.ModuleException: weblogic.application.naming.EnvironmentException: Error processing persistence unit pro_persistence of module xxx.war: Error instantiating the Persistence Provider class org.hibernate.ejb.HibernatePersistence of the PersistenceUnit pro_persistence: java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence"
weblogic.application.ModuleException: weblogic.application.naming.EnvironmentException: Error processing persistence unit pro_persistence of module xxx.war: Error instantiating the Persistence Provider class org.hibernate.ejb.HibernatePersistence of the PersistenceUnit pro_persistence: java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence
at weblogic.application.internal.ExtensibleModuleWrapper.prepare(ExtensibleModuleWrapper.java:114)
at weblogic.application.internal.flow.ModuleListenerInvoker.prepare(ModuleListenerInvoker.java:100)
at weblogic.application.internal.flow.ModuleStateDriver$1.next(ModuleStateDriver.java:192)
at weblogic.application.internal.flow.ModuleStateDriver$1.next(ModuleStateDriver.java:187)
at weblogic.application.utils.StateMachineDriver$ParallelChange.run(StateMachineDriver.java:83)
Truncated. see log file for complete stacktrace
Caused By: weblogic.application.naming.EnvironmentException: Error processing persistence unit pro_persistence of module xxx.war: Error instantiating the Persistence Provider class org.hibernate.ejb.HibernatePersistence of the PersistenceUnit pro_persistence: java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence
at weblogic.persistence.BasePersistenceUnitInfo.getPersistenceProvider(BasePersistenceUnitInfo.java:604)
at weblogic.persistence.BasePersistenceUnitInfo.initializeEntityManagerFactory(BasePersistenceUnitInfo.java:613)
at weblogic.persistence.BasePersistenceUnitInfo.init(BasePersistenceUnitInfo.java:202)
at weblogic.persistence.BaseJPAIntegrationProvider.createPersistenceUnitInfo(BaseJPAIntegrationProvider.java:54)
at weblogic.persistence.AbstractPersistenceUnitRegistry.storeDescriptors(AbstractPersistenceUnitRegistry.java:422)
Truncated. see log file for complete stacktrace>
解决方法:
首先org.hibernate.ejb.HibernatePersistence在Hibernate里已经是@Deprecated的,所以不建议使用,
1. 如果你用的Hibernate是5.2之前的版本,那很有可能是你缺少相应的jar package, 所以只要在pom.xml里添加相应的dependency,如下:
<dependecny><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId> <version>{hibernate.version}</version> </dependency>
2. 上面提到过org.hibernate.ejb.HibernatePersistence在Hibernate里已经是@Deprecated了,所以建议使用org.hibernate.jpa.HibernatePersistenceProvider代替。更改项目中相应的persistence.xml文件,如下:
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="pro_persistence" transaction type="RESOURCE_LOCAL"><provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <mapping-file>...</mapping-file> <class>*.*</class> </persistence-unit> </persistence>
补充:
Hibernate从5.2开始已经是一个空白的jar package了,原来其中的Hibernate's EJB部分已经被一处,而Hibernate's JPA部分已经被移到了hibernate-core.jar package中了。 hibernate-entity-manger-5.2.1.jar抓图如下: