Search code examples
hibernatejpajndiejb-3.0websphere-liberty

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


I'm creating an EJB application with JPA. I created EJBBean that uses the @PersistenceContext with the Entity Manager. I also provided the settings in the Persistence.xml file that has the Hibernate information. I'm trying to use transaction-type=JTA and trying to access the DataSource using the JNDI. I have also provided some information in the server.xml file of the Websphere Liberty

However, I'm getting an error message -

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.

Please refer to the Error Message mentioned after the Server.xml file for more information

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>

Exception Information

    [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

Solution

  • The exception message is indicating that Hibernate classes cannot be found. This is expected since Liberty does not provide Hibernate classes in the jpa-2.0 (OpenJPA) or jpa-2.1 (eclipselink) features.

    If you want to use Hibernate with Liberty, you will need to download the Hibernate jars yourself and either include them in your application or configure a shared library.

    Assuming you already have the Hibernate jars handy at the location C:/hibernate, you could add the following to your server.xml:

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

    And then provide the shared library to your application like this:

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

    Also, here is a useful sample to work off of:
    Liberty JPA Hibernate Sample