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

时间:2017-01-09 04:12:15

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

我正在使用JPA创建EJB应用程序。我创建了使用 @PersistenceContext 实体管理器的EJBBean。我还在 Persistence.xml 文件中提供了具有 Hibernate 信息的设置。我正在尝试使用 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

2 个答案:

答案 0 :(得分:1)

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

如果你想在Liberty上使用Hibernate,你需要自己下载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

答案 1 :(得分:0)

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

问题1:

我采用了Andy所说的相同方法。

问题2:

  

[错误] CNTR0020E:EJB抛出了意外(未声明)异常   在bean上调用方法“testMessage”期间     “将beanId(SolarUniversityEAR#SolarUniversityEjb.jar#StudentEjb豆,   null)“。异常数据:java.lang.AbstractMethodError:     org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManag ER(Ljavax / persistenc E / SynchronizationTypë; Ljava / util的/地图;)LJ avax /持久/ ENT ityManager;   的&GT;在   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" />

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