App Engine关键策略失败

时间:2014-05-02 13:28:31

标签: android google-app-engine

我使用以下POJO创建App Engine端点。

package com.incident.incidentreporter;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.persistence.Entity;
import javax.persistence.Id;

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

@Entity
public class Incidents {
@Id
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private Date incidentdate;
@Persistent
private String incidentdetails;
@Persistent
private double lat;
@Persistent
private double lngtitude;
@Persistent
private String reporter;
@Persistent
private Blob incidentimage;
    public Incidents() {
        // TODO Auto-generated constructor stub
    }
    public Incidents(Long id, Date incidentdate, String incidentdetails,
            double lat, double lngtitude, String reporter, Blob incidentimage) {
        super();
        this.id = id;
        this.incidentdate = incidentdate;
        this.incidentdetails = incidentdetails;
        this.lat = lat;
        this.lngtitude = lngtitude;
        this.reporter = reporter;
        this.incidentimage = incidentimage;
    }
    /**
     * @return the incidentdate
     */
    public Date getIncidentdate() {
        return incidentdate;
    }
    /**
     * @param incidentdate the incidentdate to set
     */
    public void setIncidentdate(Date incidentdate) {
        this.incidentdate = incidentdate;
    }
    /**
     * @return the incidentdetails
     */
    public String getIncidentdetails() {
        return incidentdetails;
    }
    /**
     * @param incidentdetails the incidentdetails to set
     */
    public void setIncidentdetails(String incidentdetails) {
        this.incidentdetails = incidentdetails;
    }
    /**
     * @return the lat
     */
    public double getLat() {
        return lat;
    }
    /**
     * @param lat the lat to set
     */
    public void setLat(double lat) {
        this.lat = lat;
    }
    /**
     * @return the lngtitude
     */
    public double getLngtitude() {
        return lngtitude;
    }
    /**
     * @param lngtitude the lngtitude to set
     */
    public void setLngtitude(double lngtitude) {
        this.lngtitude = lngtitude;
    }
    /**
     * @return the reporter
     */
    public String getReporter() {
        return reporter;
    }
    /**
     * @param reporter the reporter to set
     */
    public void setReporter(String reporter) {
        this.reporter = reporter;
    }
    /**
     * @return the incidentimage
     */
    public Blob getIncidentimage() {
        return incidentimage;
    }
    /**
     * @param incidentimage the incidentimage to set
     */
    public void setIncidentimage(Blob incidentimage) {
        this.incidentimage = incidentimage;
    }
    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }



}

自动生成的端点代码如下所示。

package com.incident.incidentreporter;

import com.incident.incidentreporter.EMF;

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;

import java.util.List;

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

@Api(name = "incidentsendpoint", namespace = @ApiNamespace(ownerDomain = "incident.com", ownerName = "incident.com", packagePath = "incidentreporter"))
public class IncidentsEndpoint {

    /**
     * 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 = "listIncidents")
    public CollectionResponse<Incidents> listIncidents(
            @Nullable @Named("cursor") String cursorString,
            @Nullable @Named("limit") Integer limit) {

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

        try {
            mgr = getEntityManager();
            Query query = mgr.createQuery("select from Incidents as Incidents");
            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<Incidents>) 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 (Incidents obj : execute)
                ;
        } finally {
            mgr.close();
        }

        return CollectionResponse.<Incidents> 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 = "getIncidents")
    public Incidents getIncidents(@Named("id") Long id) {
        EntityManager mgr = getEntityManager();
        Incidents incidents = null;
        try {
            incidents = mgr.find(Incidents.class, id);
        } finally {
            mgr.close();
        }
        return incidents;
    }

    /**
     * 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 incidents the entity to be inserted.
     * @return The inserted entity.
     */
    @ApiMethod(name = "insertIncidents")
    public Incidents insertIncidents(Incidents incidents) {
        EntityManager mgr = getEntityManager();
        try {

            mgr.persist(incidents);
        } finally {
            mgr.close();
        }
        return incidents;
    }

    /**
     * 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 incidents the entity to be updated.
     * @return The updated entity.
     */
    @ApiMethod(name = "updateIncidents")
    public Incidents updateIncidents(Incidents incidents) {
        EntityManager mgr = getEntityManager();
        try {
            if (!containsIncidents(incidents)) {
                throw new EntityNotFoundException("Object does not exist");
            }
            mgr.persist(incidents);
        } finally {
            mgr.close();
        }
        return incidents;
    }

    /**
     * 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 = "removeIncidents")
    public void removeIncidents(@Named("id") Long id) {
        EntityManager mgr = getEntityManager();
        try {
            Incidents incidents = mgr.find(Incidents.class, id);
            mgr.remove(incidents);
        } finally {
            mgr.close();
        }
    }

    private boolean containsIncidents(Incidents incidents) {
        EntityManager mgr = getEntityManager();
        boolean contains = true;
        try {
            Incidents item = mgr.find(Incidents.class, incidents.getId());
            if (item == null) {
                contains = false;
            }
        } finally {
            mgr.close();
        }
        return contains;
    }

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

}

我成功生成了客户端端点库,并将应用程序部署到Google App引擎。

我认为关键字段会自动生成。

我现在面临的问题是,如果我没有设置id字段,插入事件的代码就会失败。

我的代码中是否存在导致此问题的错误?

由于此字段是Long数据类型且必须是唯一的,是否需要添加代码才能生成它?

我正在使用Android客户端应用。

请告知。

罗纳德

1 个答案:

答案 0 :(得分:0)

这部分

Query query = mgr.createQuery("select from Incidents as Incidents");

对我来说非常可疑。它缺少select子句中的列列表,我相信您应该查看使用列的所有后勤工作流程。这可能足以解决您的问题,但是,我相信这是您应该开始研究问题的地方。