7

JPA 経由で Cloud SQL に接続しようとすると、次のエラーが生成されます。

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)

私のEMFクラスは

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

    private EMF() {
    }

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

EMF初期化部分は

 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");
        }
}

私のサーブレットは:

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 は

<?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
4

1 に答える 1

2

finalで forEntityManagerFactoryを使用する必要がありますかEMF。に使っSingleton Design PatternてみてくださいEMFEntityManagerFactoryクラスはスレッドセーフです。

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();

ここでEntityManagerFactoryは、Web アプリケーションで使用するためのより良い方法を示します。

于 2012-10-25T06:42:26.633 に答える