Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JPA+;Google应用程序引擎:在类路径中未找到META-INF/persistence.xml文件_Java_Android_Google App Engine_Jpa - Fatal编程技术网

Java 使用JPA+;Google应用程序引擎:在类路径中未找到META-INF/persistence.xml文件

Java 使用JPA+;Google应用程序引擎:在类路径中未找到META-INF/persistence.xml文件,java,android,google-app-engine,jpa,Java,Android,Google App Engine,Jpa,我是谷歌应用引擎的新手。在过去的三天里,我一直在研究项目中的错误 我用我的谷歌应用引擎项目建立了JPA2.0。客户端是一个Android应用程序。当我尝试插入一个实体时,我在我的Google应用程序引擎日志中收到以下警告/错误。我无法插入任何实体 我将persistence.xml文件放在类路径中。此外,我在项目JPA配置中使用了“自动发现带注释的类”选项。如果我忘了在这里输入任何相关代码,请告诉我 日志: 我的实体类: package com.sample; import javax.pers

我是谷歌应用引擎的新手。在过去的三天里,我一直在研究项目中的错误

我用我的谷歌应用引擎项目建立了JPA2.0。客户端是一个Android应用程序。当我尝试插入一个实体时,我在我的Google应用程序引擎日志中收到以下警告/错误。我无法插入任何实体

我将persistence.xml文件放在类路径中。此外,我在项目JPA配置中使用了“自动发现带注释的类”选项。如果我忘了在这里输入任何相关代码,请告诉我

日志:

我的实体类:

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;
    }
}
我的端点类: 包com.sample; 导入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();
    }

}
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>

org.datanucleus.api.jpa.PersistenceProviderImpl

更新:我知道,端点和persistence.xml、EMF都是由插件生成的。但是当我设置JPA时,这些错误出现了,我不知道如何解决这个问题。

当您创建实体类时,Eclipse的Google插件可以自动为您创建这些文件。

我终于解决了我的问题。只是想让你知道我做了什么

  • 我循序渐进地学习了关于谷歌开发者的教程:
  • 特别关注:persistence.xml及其位置。 此外,我不信任自动跟踪带注释的实体,因此我将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>
    
    
    org.datanucleus.api.jpa.PersistenceProviderImpl
    com.sample.DeviceInfo
    com.sample.Group
    com.sample.User
    真的
    
    并且还修复了我的类路径以指向正确的文件夹


    谢谢所有帮助我的人

    哪些文件?我知道,端点和persistence.xml、EMF都是由插件生成的。但是当我设置JPA时,这些错误出现了,我不知道如何解决。你确定要将你的应用部署到默认版本吗?可能您已经在应用程序引擎上更新了非默认版本,而默认版本没有EMF类。你也在开发服务器上调试过吗?是的,我调试过。我也在使用默认版本。开发服务器上是否也出现了相同的错误?作为补充说明,我更喜欢使用JDO而不是JPA-它有一组超集的功能,不会导致JPA中经常出现的与持久性相关的错误。下面的链接有助于迁移到Android Studio
    <?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>
    
    <?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>