【问题标题】:Getting CWWJP0050E: Unable to locate persistence provider org.hibernate.ejb.HibernatePersistence获取 CWWJP0050E: 无法找到持久性提供程序 org.hibernate.ejb.HibernatePersistence
【发布时间】:2017-05-23 07:07:23
【问题描述】:

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

server.xml 文件中提供了一些信息

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

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 5.2,则您的 persistence.xmlorg.hibernate.jpa.HibernatePersistenceProvider 中的提供程序。它正在寻找的旧 ejb 类不再可用。

标签: hibernate jpa jndi ejb-3.0 websphere-liberty


【解决方案1】:

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

对于问题 1:

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

对于问题 2:

[ERROR ] CNTR0020E: EJB 引发了意外的(未声明的)异常 在 bean 上调用方法“testMessage”期间 "BeanId(SolarUniversityEAR#SolarUniversityEjb.jar#StudentEjb‌​Bean, null)"。异常数据:java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager‌​er(Ljavax/persistenc‌​e/SynchronizationTyp‌​e;Ljava/util/Map;)Lj‌​avax/persistence/EntityManager; >在 com.ibm.ws.jpa.container.v21.internal.JPA21Runtime.createEnt‌​ityManagerInstance(J‌​PA21Runtime.java:104‌​)

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

对于问题 3:

CNTR0020E: EJB 在执行期间抛出了意外的(未声明的)异常 在 bean 上调用方法“testMessage” "BeanId(SolarUniversityEAR#SolarUniversityEjb.jar#StudentEjbBean, null)"。异常数据: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" />

最后,我可以将信息保存在数据库中。

【讨论】:

    【解决方案2】:

    异常消息表明找不到 Hibernate 类。这是意料之中的,因为 Liberty 没有在 jpa-2.0 (OpenJPA) 或 jpa-2.1 (eclipselink) 功能中提供 Hibernate 类。

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

    假设您已经在C:/hibernate 位置有方便的 Hibernate jar,您可以将以下内容添加到您的 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

    【讨论】:

    • 这绝对完美!谢谢你的帮助:-)
    • 我现在收到新异常: [错误] CNTR0020E: EJB 在 bean "BeanId( SolarUniversityEAR#SolarUniversityEjb.jar#StudentEjbBean,null)"。异常数据:java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(Ljavax/persistence/SynchronizationType;Ljava/util/Map;)Ljavax/persistence/EntityManager;在 com.ibm.ws.jpa.container.v21.internal.JPA21Runtime.createEntityManagerInstance(JPA21Runtime.java:104)
    • 这是使用 JPA 2.1 之前的 HIbernate 版本的常见错误。确保您使用的是与 JPA 2.1 兼容的休眠版本。
    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 2013-09-30
    • 2013-06-20
    相关资源
    最近更新 更多