Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GAE + JPA :java.lang.NoClassDefFoundError: Could not initialize class EMF

When i tried to connect my Cloud SQL via JPA the following error is generated :

2012-10-25 10:07:38.439
Error for /jpatest
java.lang.NoClassDefFoundError: Could not initialize class com.my.jpa.EMF
    at com.my.jpa.ContactService.createContact(ContactService.java:20)
    at com.my.jpa.JPATestServlet.doGet(JPATestServlet.java:14)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)

2012-10-25 10:07:38.440
Uncaught exception from servlet
java.lang.NoClassDefFoundError: Could not initialize class com.my.jpa.EMF
    at com.my.jpa.ContactService.createContact(ContactService.java:20)
    at com.my.jpa.JPATestServlet.doGet(JPATestServlet.java:14)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)

My EMF Class is

public final class EMF {
    private static final EntityManagerFactory emfInstance = Persistence
            .createEntityManagerFactory("JPATest");

    private EMF() {
    }

    public static EntityManagerFactory get() {
        return emfInstance;
    }
}

EMF initialising portion is

 public class ContactService {
        private static Logger logger = Logger.getLogger(ContactService.class
                .getName());

        public void createContact(Contact c) {
            logger.info("Entering createContact: [" + c.getFirstName() + ","
                    + c.getLastName() + "]");
            EntityManager mgr = EMF.get().createEntityManager();
            try {
                mgr.getTransaction().begin();
                mgr.persist(c);
                mgr.getTransaction().commit();
            } finally {
                mgr.close();
            }
            logger.info("Exiting createContact");
        }
}

My Servlet is :

public class JPATestServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {        
        ContactService service = new ContactService();
        service.createContact(new Contact("Manu", "Mohan", "686019", "TVM"));
        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, world");
    }
}

web.xml is

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <servlet>
        <servlet-name>JPATest</servlet-name>
        <servlet-class>com.my.jpa.JPATestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>JPATest</servlet-name>
        <url-pattern>/jpatest</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

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="JPATest">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.my.jpa.Contact</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.google.cloud.sql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:google:rdbms://instance-name/stock" />
            <property name="javax.persistence.jdbc.user" value="" />
            <property name="javax.persistence.jdbc.password" value="" />
            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="database" />
        </properties>
    </persistence-unit>
</persistence
like image 246
Master Mind Avatar asked Oct 25 '12 05:10

Master Mind


1 Answers

Do you need to use final for EntityManagerFactory in EMF. Try to use Singleton Design Pattern for EMF. EntityManagerFactory class is thread-safe.

EMF.java

public final class EMF {
    private EntityManagerFactory emfInstance;

    private static EMF emf;

    private EMF() {
    }

    public EntityManagerFactory get() {
        if(emfInstance == null) {
            emfInstance = Persistence.createEntityManagerFactory("JPATest");
        }
        return emfInstance;
    }

    public static EMF getInstance() {
        if(emf == null) {
            emf = new EMF();
        }
        return emf;
    }
}

// usages
EntityManagerFactory emf = Emf.getInstance().get();

Here better way to use EntityManagerFactory in web applicaiton.

like image 116
Zaw Than oo Avatar answered Nov 09 '22 10:11

Zaw Than oo