获取 CWWJP0050E:无法定位持久性提供程序 org.hibernate.ejb.HibernatePersistence

Getting CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence

我正在使用 JPA 创建 EJB 应用程序。我创建了使用 @PersistenceContextEntity Manager 的 EJBBean。我还在 Persistence.xml 文件中提供了具有 Hibernate 信息的设置。我正在尝试使用 transaction-type=JTA 并尝试使用 JNDI 访问 DataSource。我还在 server.xmlWebsphere Liberty

文件中提供了一些信息

但是,我收到一条错误消息 -

javax.ejb.EJBException:CWNEN0030E:服务器无法获取 java:comp/env/SolarUniversityJpa 引用的对象实例。异常消息是:java.lang.RuntimeException:CWWJP0050E:无法定位持久性提供程序 org.hibernate.ejb.HibernatePersistence。;嵌套异常是:java.lang.RuntimeException:java.lang.RuntimeException:CWWJP0050E:无法定位持久性提供程序 org.hibernate.ejb.HibernatePersistence。

更多信息请参考Server.xml文件后提到的错误信息

Servlet

@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB
    StudentEjbBean studentEjbBean;

    public StudentServlet() {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        studentEjbBean.testJpa();
    }

}

EjbBean

@Stateless
@LocalBean
public class StudentEjbBean {

    @PersistenceContext(name="SolarUniversityJpa")
    EntityManager entityManager;

    public StudentEjbBean() {
    }

    public void testJpa()
    {
        StudentDao studentDao = new StudentDaoImpl();
        studentDao.SaveInformation(entityManager);
    }
}

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="SolarUniversityJpa"
        transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:global/env/jdbc/MySql</jta-data-source>
        <class>com.student.entity.StudentEntity</class>
        <properties>
            <property name="connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/SolarUniversity" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

Server.xml

<server description="new server">
    <!-- Enable features -->
    <featureManager>
        <feature>webProfile-7.0</feature>
        <feature>localConnector-1.0</feature>
    </featureManager>

    <!-- To access this server from a remote client add a host attribute to 
        the following element, e.g. host="*" -->
    <httpEndpoint httpPort="9080" httpsPort="9443"
        id="defaultHttpEndpoint" />

    <!-- Automatically expand WAR files and EAR files -->
    <applicationManager autoExpand="true" />

    <applicationMonitor updateTrigger="mbean" />

    <jdbcDriver id="mySqlEmbedded" libraryRef="mySqlLib" />
    <library id="mySqlLib" filesetRef="mySqlFileSet" />
    <fileset id="mySqlFileSet" dir="E:/Backups"
        includes="mysql-connector-java-5.1.34.jar" />

    <dataSource id="jdbc/samplejpadatasource" jndiName="jdbc/MySql"
        jdbcDriverRef="mySqlEmbedded">
        <properties user="root" password="root"
            URL="jdbc:mysql://localhost:3306/SolarUniversity">
        </properties>
    </dataSource>

    <enterpriseApplication id="SolarUniversityEAR"
        location="SolarUniversityEAR.ear" name="SolarUniversityEAR">
        <!-- <classloader commonLibraryRef="MySQLLib" /> -->
    </enterpriseApplication>
</server>

异常信息

    [ERROR   ] CNTR0019E: EJB threw an unexpected (non-declared) exception during invocation of method "testMessage". Exception data: javax.ejb.EJBException: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/SolarUniversityJpa reference.  The exception message was: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.; nested exception is: java.lang.RuntimeException: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.
        at com.ibm.ejs.container.util.ExceptionUtil.EJBException(ExceptionUtil.java:461)
        at [internal classes]
        at com.ejb.stateless.student.EJSLocalNSLStudentEjbBean_58c656c8.testMessage(EJSLocalNSLStudentEjbBean_58c656c8.java)
        at com.servlet.student.StudentServlet.doPost(StudentServlet.java:56)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1290)
        at [internal classes]
    Caused by (repeated) ... : java.lang.RuntimeException: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.
        at com.ibm.ws.jpa.management.JPAPUnitInfo.getEntityManagerFactory(JPAPUnitInfo.java:960)
        ... 7 more
    Caused by: java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence
        at com.ibm.ws.classloading.internal.AppClassLoader.findClassCommonLibraryClassLoaders(AppClassLoader.java:499)
        ... 1 more

    [ERROR   ] SRVE0777E: Exception thrown by application class 'com.ibm.ejs.container.util.ExceptionUtil.EJBException:461'
    javax.ejb.EJBException: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/SolarUniversityJpa reference.  The exception message was: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.; nested exception is: java.lang.RuntimeException: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.
        at com.ibm.ejs.container.util.ExceptionUtil.EJBException(ExceptionUtil.java:461)
        at [internal classes]
        at com.ejb.stateless.student.EJSLocalNSLStudentEjbBean_58c656c8.testMessage(EJSLocalNSLStudentEjbBean_58c656c8.java)
        at com.servlet.student.StudentServlet.doPost(StudentServlet.java:56)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1290)
        at [internal classes]
    Caused by (repeated) ... : java.lang.RuntimeException: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.
        at com.ibm.ws.jpa.management.JPAPUnitInfo.getEntityManagerFactory(JPAPUnitInfo.java:960)
        ... 7 more
    Caused by: java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence
        at com.ibm.ws.classloading.internal.AppClassLoader.findClassCommonLibraryClassLoaders(AppClassLoader.java:499)
        ... 1 more

    [ERROR   ] SRVE0315E: An exception occurred: java.lang.Throwable: javax.ejb.EJBException: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/SolarUniversityJpa reference.  The exception message was: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.; nested exception is: java.lang.RuntimeException: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.
        at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:5000)
        at [internal classes]
    Caused by: javax.ejb.EJBException: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/SolarUniversityJpa reference.  The exception message was: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.; nested exception is: java.lang.RuntimeException: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.
        at com.ibm.ejs.container.util.ExceptionUtil.EJBException(ExceptionUtil.java:461)
        at [internal classes]
        at com.ejb.stateless.student.EJSLocalNSLStudentEjbBean_58c656c8.testMessage(EJSLocalNSLStudentEjbBean_58c656c8.java)
        at com.servlet.student.StudentServlet.doPost(StudentServlet.java:56)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1290)
        ... 1 more
    Caused by (repeated) ... : java.lang.RuntimeException: java.lang.RuntimeException: CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence.
        at com.ibm.ws.jpa.management.JPAPUnitInfo.getEntityManagerFactory(JPAPUnitInfo.java:960)
        ... 7 more
    Caused by: java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence
        at com.ibm.ws.classloading.internal.AppClassLoader.findClassCommonLibraryClassLoaders(AppClassLoader.java:499)
        ... 1 more

异常消息表明找不到 Hibernate 类。这是预期的,因为 Liberty 在 jpa-2.0 (OpenJPA) 或 jpa-2.1 (eclipselink) 特性中不提供 Hibernate 类。

如果您想将 Hibernate 与 Liberty 一起使用,您将需要自己下载 Hibernate jar 并将它们包含在您的应用程序中或配置一个共享库。

假设您已经在 C:/hibernate 位置方便地使用了 Hibernate 罐子,您可以将以下内容添加到 server.xml:

<library id="HibernateLib">
  <fileset dir="C:/hibernate" includes="*.jar"/>
</library>

然后像这样向您的应用程序提供共享库:

<enterpriseApplication id="SolarUniversityEAR" location="SolarUniversityEAR.ear" name="SolarUniversityEAR">
     <classloader commonLibraryRef="HibernateLib"/>
</enterpriseApplication>

此外,这里有一个有用的示例:
Liberty JPA Hibernate Sample

感谢 Andy Guibert 提出的建议。这可能对其他人有帮助。这是我解决问题的方法。

对于问题 1:

我遵循了安迪所说的相同方法。

对于问题 2:

[ERROR ] CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "testMessage" on bean "BeanId(SolarUniversityEAR#SolarUniversityEjb.jar#StudentEjb‌​Bean, null)". Exception data: java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManag‌​er(Ljavax/persistenc‌​e/SynchronizationTyp‌​e;Ljava/util/Map;)Lj‌​avax/persistence/Ent‌​ityManager; > at com.ibm.ws.jpa.container.v21.internal.JPA21Runtime.createEnt‌​ityManagerInstance(J‌​PA21Runtime.java:104‌​)

我下载了支持Jpa 2.1的新版Hibernate。在此之后,我又遇到了一个问题

对于问题 3:

CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "testMessage" on bean "BeanId(SolarUniversityEAR#SolarUniversityEjb.jar#StudentEjbBean, null)". Exception data: java.lang.NullPointerException

为了解决这个问题,我在 Persistence.xml 中添加了以下代码(因为我使用的是 transaction-type="JTA")。这是特定于 WebSphere Liberty 的。资料来源:7.5.9. org.hibernate.service.jta.platform.spi.JtaPlatform

<property name="hibernate.transaction.jta.platform" 
value="org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform" />

最后,我能够在数据库中保存信息。