craig
craig

Reputation: 26272

Persistence.createEntityManagerFactory hangs during execution

When I attempt to contact http://localhost:8080/jax/users, I get an error that reads:

javax.servlet.ServletException: java.lang.NoClassDefFoundError: Could not initialize class services.DaoHelper

When debugging, the code hangs on the first line of the DaoHelper's contructor:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("databaseU");

I am able to browse the database in Netbeans' database services panel.

What am I missing? Perhaps a missing element in the persistence.xml file?

trace:

java.lang.NoClassDefFoundError: Could not initialize class services.DaoHelper
    services.UserResource.<init>(UserResource.java:23)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    com.sun.jersey.server.spi.component.ResourceComponentConstructor._construct(ResourceComponentConstructor.java:191)
    com.sun.jersey.server.spi.component.ResourceComponentConstructor.construct(ResourceComponentConstructor.java:179)
    com.sun.jersey.server.impl.resource.PerRequestFactory$PerRequest._getInstance(PerRequestFactory.java:182)
    com.sun.jersey.server.impl.resource.PerRequestFactory$AbstractPerRequest.getInstance(PerRequestFactory.java:144)
    com.sun.jersey.server.impl.application.WebApplicationContext.getResource(WebApplicationContext.java:238)
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

services.UserResource:

@Path("/users")
public class UserResource {

    //per Chris
    //private EntityManager mgr = DaoHelper.getInstance().getEntityManager();

    @GET
    @Produces("application/xml")
    public Collection<User> index(){

        //per Chris
        DaoHelper helper = DaoHelper.getInstance();
        EntityManager mgr = helper.getEntityManager();

        TypedQuery<User> query =  mgr.createQuery("SELECT u FROM User u", User.class);
        return query.getResultList();

    }

}

services.DaoHelper:

public class DaoHelper {

    private static DaoHelper instance = new DaoHelper();

    //per Chris
    //private final EntityManager mgr;

    private DaoHelper(){
        //per Chris
        //EntityManagerFactory factory = Persistence.createEntityManagerFactory("databasePU");
        //mgr = factory.createEntityManager();
    }

    public EntityManager getEntityManager(){
        //per Chris
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("databasePU");
        return factory.createEntityManager();
        //return mgr;
    }

    public static DaoHelper getInstance(){
        return instance;
    }

}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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="databasePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>entities.User</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/my_db"/>
      <property name="javax.persistence.jdbc.password" value="password"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="user"/>
      <!-- logging -->
      <property name="eclipselink.logging.file" value="output.log"/>
      <property name="eclipselink.logging.logger" value="JavaLogger"/>
      <property name="eclipselink.logging.level" value="FINE"/>
      <!-- /logging -->
    </properties>
  </persistence-unit>
</persistence>

** edit 0 **

I have based my project on this sample: [Create rich data-centric web applications using JAX-RS, JPA, and Dojo][1]

** edit 1 **

javax.persistence.PersistenceException: Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Configuration error.  Class [com.mysql.jdbc.Driver] not found.
    org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:501)
    org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
    org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
    org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)
    org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)
    services.DaoHelper.getEntityManager(DaoHelper.java:28)
    services.UserResource.index(UserResource.java:29)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  [1]: http://www.ibm.com/developerworks/web/library/wa-datawebapp/

Upvotes: 0

Views: 3046

Answers (1)

ayengin
ayengin

Reputation: 1596

JPA is not implementation It is a specification so you need to add one of the implementation of jpa(hibernate ,openjpa,eclipselink ..) to your class path.In your case eclipselink( <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>). .In addition,your code trying to use same entitymanager instance but this instances not thread safe so look this how to use entitymanager and entitymanagerfactory.

Upvotes: 1

Related Questions