简体   繁体   English

使用JPA + Google App Engine:在CLASSPATH中找不到META-INF / persistence.xml文件

[英]Using JPA+Google App Engine: No META-INF/persistence.xml files were found in the CLASSPATH

I am new to Google App Engine. 我是Google App Engine的新手。 I've been around errors in my project for the last three days. 在过去的三天内,我一直在解决我的项目中的错误。

I set up JPA 2.0 with my Google App Engine project. 我在Google App Engine项目中设置了JPA 2.0。 The client is an Android application. 客户端是一个Android应用程序。 When I am trying to insert an Entity, I am getting the following Warnings/Erros in my Google App Engine Logs. 尝试插入实体时,我的Google App Engine日志中出现以下警告/错误。 And I am not able to insert any Entities. 而且我无法插入任何实体。

I put the persistence.xml file in the CLASSPATH. 我将persistence.xml文件放在CLASSPATH中。 Also, I am using the option: "Discover annotated class automatically" in my project JPA configs. 另外,我在项目JPA配置中使用选项:“自动发现带注释的类”。 Please, let me know if I forgot to put any relevant code here. 请让我知道是否忘记了任何相关代码。

Log: 日志:

com.google.api.server.spi.SystemService invokeServiceMethod: Could not initialize class com.sample.EMF
java.lang.NoClassDefFoundError: Could not initialize class com.sample.EMF
    at com.sample.GroupEndpoint.getEntityManager(GroupEndpoint.java:159)
    at com.sample.GroupEndpoint.insertGroup(GroupEndpoint.java:96)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:45)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:359)
    at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:127)
    at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:85)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
    at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:188)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
    at java.lang.Thread.run(Thread.java:724)

My Entity class: 我的实体类:

package com.sample;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import com.google.appengine.api.datastore.Key;

@Entity
public class Group {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    private String name;

    @Basic
    @ManyToOne
    private User owner;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }

    public Key getKey() {
        return key;
    }
}

My Endpoint class: package com.sample; 我的端点类:包com.sample; import java.util.List; 导入java.util.List;

import javax.annotation.Nullable;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.response.CollectionResponse;
import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.datanucleus.query.JPACursorHelper;

@Api(name = "groupendpoint", namespace = @ApiNamespace(ownerDomain = "sample.com", ownerName = "sample.com", packagePath = ""))
public class GroupEndpoint {

    /**
     * This method lists all the entities inserted in datastore.
     * It uses HTTP GET method and paging support.
     *
     * @return A CollectionResponse class containing the list of all entities
     * persisted and a cursor to the next page.
     */
    @SuppressWarnings({ "unchecked", "unused" })
    @ApiMethod(name = "listGroup")
    public CollectionResponse<Group> listGroup(
            @Nullable @Named("cursor") String cursorString,
            @Nullable @Named("limit") Integer limit) {

        EntityManager mgr = null;
        Cursor cursor = null;
        List<Group> execute = null;

        try {
            mgr = getEntityManager();
            Query query = mgr.createQuery("select from Group as Group");
            if (cursorString != null && cursorString != "") {
                cursor = Cursor.fromWebSafeString(cursorString);
                query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
            }

            if (limit != null) {
                query.setFirstResult(0);
                query.setMaxResults(limit);
            }

            execute = (List<Group>) query.getResultList();
            cursor = JPACursorHelper.getCursor(execute);
            if (cursor != null)
                cursorString = cursor.toWebSafeString();

            // Tight loop for fetching all entities from datastore and accomodate
            // for lazy fetch.
            for (Group obj : execute)
                ;
        } finally {
            mgr.close();
        }

        return CollectionResponse.<Group> builder().setItems(execute)
                .setNextPageToken(cursorString).build();
    }

    /**
     * This method gets the entity having primary key id. It uses HTTP GET method.
     *
     * @param id the primary key of the java bean.
     * @return The entity with primary key id.
     */
    @ApiMethod(name = "getGroup")
    public Group getGroup(@Named("id") Long id) {
        EntityManager mgr = getEntityManager();
        Group group = null;
        try {
            group = mgr.find(Group.class, id);
        } finally {
            mgr.close();
        }
        return group;
    }

    /**
     * This inserts a new entity into App Engine datastore. If the entity already
     * exists in the datastore, an exception is thrown.
     * It uses HTTP POST method.
     *
     * @param group the entity to be inserted.
     * @return The inserted entity.
     */
    @ApiMethod(name = "insertGroup")
    public Group insertGroup(Group group) {
        EntityManager mgr = getEntityManager();
        try {
            mgr.persist(group);
        } finally {
            mgr.close();
        }
        return group;
    }

    /**
     * This method is used for updating an existing entity. If the entity does not
     * exist in the datastore, an exception is thrown.
     * It uses HTTP PUT method.
     *
     * @param group the entity to be updated.
     * @return The updated entity.
     */
    @ApiMethod(name = "updateGroup")
    public Group updateGroup(Group group) {
        EntityManager mgr = getEntityManager();
        try {
            if (!containsGroup(group)) {
                throw new EntityNotFoundException("Object does not exist");
            }
            mgr.persist(group);
        } finally {
            mgr.close();
        }
        return group;
    }

    /**
     * This method removes the entity with primary key id.
     * It uses HTTP DELETE method.
     *
     * @param id the primary key of the entity to be deleted.
     */
    @ApiMethod(name = "removeGroup")
    public void removeGroup(@Named("id") Long id) {
        EntityManager mgr = getEntityManager();
        try {
            Group group = mgr.find(Group.class, id);
            mgr.remove(group);
        } finally {
            mgr.close();
        }
    }

    private boolean containsGroup(Group group) {
        EntityManager mgr = getEntityManager();
        boolean contains = true;
        try {
            Group item = mgr.find(Group.class, group.getKey());
            if (item == null) {
                contains = false;
            }
        } finally {
            mgr.close();
        }
        return contains;
    }

    private static EntityManager getEntityManager() {
        return EMF.get().createEntityManager();
    }

}

EMF Class: package com.sample; EMF类:包com.sample; import javax.persistence.EntityManagerFactory; 导入javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; 导入javax.persistence.Persistence;

public final class EMF {
    private static EntityManagerFactory emfInstance;

    private EMF() {
    }

    public static EntityManagerFactory get() {
        if(emfInstance == null) {
            emfInstance = Persistence
                    .createEntityManagerFactory("transactions-optional");
        }
        return emfInstance;
    }
}

persistence.xml file: persistence.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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_1_0.xsd" version="1.0">

    <persistence-unit name="transactions-optional">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
            <property name="datanucleus.singletonEMFForName" value="true"/>
        </properties>

    </persistence-unit>

</persistence>

Update: I know, the endpoints and persistence.xml, EMF were generated by the Plugin. 更新:我知道,端点和persistence.xml,EMF是由插件生成的。 But when I set up JPA, these errors come to me and I don't know how to solve this. 但是,当我设置JPA时,这些错误传给了我,我不知道该如何解决。

I finally solved my problem. 我终于解决了我的问题。 Just to let you know what I did... 只是让你知道我做了什么...

  1. I followed step-by-step this tutorial on Google Developers: https://developers.google.com/appengine/docs/java/datastore/jpa/overview-dn2#Setting_Up_JPA_2_0 我在Google Developers上逐步遵循了本教程: https : //developers.google.com/appengine/docs/java/datastore/jpa/overview-dn2#Setting_Up_JPA_2_0

Specially regarding: persistence.xml and its location. 特别是关于:persistence.xml及其位置。 Also, I did not trust on the automatic tracing of the Annotated Entities, so I transformed my persistence.xml into: 另外,我也不相信带注释的实体的自动跟踪,因此我将persistence.xml转换为:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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_1_0.xsd"
    version="1.0">

    <persistence-unit name="transactions-optional">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
            <class>com.sample.DeviceInfo</class>
            <class>com.sample.Group</class>
            <class>com.sample.User</class>
            <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true" />
            <property name="datanucleus.NontransactionalWrite" value="true" />
            <property name="datanucleus.ConnectionURL" value="appengine" />
            <property name="datanucleus.singletonEMFForName" value="true" />
            <property name="javax.persistence.query.timeout" value="5000" />
            <property name="datanucleus.datastoreWriteTimeout" value="10000" />
        </properties>

    </persistence-unit>

</persistence>

And also fixed my classpath to point to the right folders. 并且还修复了我的类路径以指向正确的文件夹。

Thanks everybody who helped me! 谢谢所有帮助我的人!

创建Entity类时,适用于Eclipse的Google插件可以自动为您创建这些文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有TopLink的JPA:在类路径中未找到META-INF / persistence.xml - JPA with TopLink: No META-INF/persistence.xml was found in classpath 在类路径中找不到META-INF / persistence.xml - No META-INF/persistence.xml was found in classpath Kundera(JPA)-在类路径中找不到任何META-INF / persistence.xml文件 - Kundera (JPA) - Could not find any META-INF/persistence.xml file in the classpath 找不到 Gradle [META-INF/persistence.xml] - Gradle [META-INF/persistence.xml] was not found 原因:java.lang.IllegalStateException:没有从{classpath *:META-INF / persistence.xml}解析的持久性单元 - Caused by: java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml} 在类路径中找不到任何 META-INF/persistence.xml 文件 - Could not find any META-INF/persistence.xml file in the classpath 在类路径中找不到任何META-INF / persistence.xml文件 - Could not find any META-INF/persistence.xml file in the classpath Maven / Eclipse:在类路径中找不到任何META-INF / persistence.xml文件 - Maven/Eclipse: Could not find any META-INF/persistence.xml file in the classpath 在类路径中找不到任何 META-INF/persistence.xml 文件 Java - 动态 Web 项目 - Could not find any META-INF/persistence.xml file in the classpath Java - Dynamic Web Project Eclipse - 在类路径中找不到任何 META-INF/persistence.xml 文件 - Eclipse - Could not find any META-INF/persistence.xml file in the classpath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM