【问题标题】:Cloud Console Datastore filtersCloud Console 数据存储区过滤器
【发布时间】:2014-06-17 03:22:24
【问题描述】:

我最近从 JDO 切换到了 Objectify,以简化我的一些后端(我是 App Engine 和服务器端东西的初学者)。

我有一个实体 AppVersion,它曾经在 Cloud Console 中看起来像这样:

当我切换到 objectify 时,它不再具有按 minVersionRequired 过滤的选项,看起来像这样:

实体代码(之前)

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class AppVersion {

@Id
private String applicationName;
private int minVersionRequired;

public String getApplicationName() {
    return applicationName;
}
public int getMinVersionRequired() {
    return minVersionRequired;
}
public void setApplicationName(String applicationName) {
    this.applicationName = applicationName;
}
public void setminVersionRequired(int minVersionRequired) {
    this.minVersionRequired = minVersionRequired;
}
}

实体代码(之后)

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

@Entity
public class AppVersion {

@Id
private String applicationName;
private int minVersionRequired;

public String getApplicationName() {
    return applicationName;
}
public int getMinVersionRequired() {
    return minVersionRequired;
}
public void setApplicationName(String applicationName) {
    this.applicationName = applicationName;
}
public void setminVersionRequired(int minVersionRequired) {
    this.minVersionRequired = minVersionRequired;
}
}

端点代码(之前)请注意,这是在 Eclipse 中自动生成的

import com.companionfree.zooperthemeviewer.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 = "appversionendpoint", namespace = @ApiNamespace(ownerDomain = "company.com", ownerName = "company.com", packagePath = "app"))
public class AppVersionEndpoint {

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

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

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

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

/**
 * 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 appversion the entity to be inserted.
 * @return The inserted entity.
 */
@ApiMethod(name = "insertAppVersion")
public AppVersion insertAppVersion(AppVersion appversion) {
    EntityManager mgr = getEntityManager();
    try {
        if (containsAppVersion(appversion)) {
            throw new EntityExistsException("Object already exists");
        }
        mgr.persist(appversion);
    } finally {
        mgr.close();
    }
    return appversion;
}

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

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

private boolean containsAppVersion(AppVersion appversion) {
    EntityManager mgr = getEntityManager();
    boolean contains = true;
    try {
        AppVersion item = mgr.find(AppVersion.class,
                appversion.getApplicationName());
        if (item == null) {
            contains = false;
        }
    } finally {
        mgr.close();
    }
    return contains;
}

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

}

端点代码(之后)注意这是我在 Android Studio 中创建的

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 java.util.List;

import javax.inject.Named;

import static com.company.backend.OfyService.ofy;

@Api(name = "appversionendpoint", version = "v1", namespace =
@ApiNamespace(ownerDomain = "backend.company.com",
    ownerName = "backend.company.com", packagePath = ""))
public class AppVersionEndpoint {

@ApiMethod(name = "listAppVersion")
public CollectionResponse<AppVersion> listAppVersion() {
    List<AppVersion> execute;
   execute =  ofy().load().type(AppVersion.class).list();
return CollectionResponse.<AppVersion> builder().setItems(execute).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 (null if DNE).
 */
@ApiMethod(name = "getAppVersion")
public AppVersion getAppVersion(@Named("id") String id) {
    return ofy().load().type(AppVersion.class).id(id).now();

}

/**
 * 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 appversion the entity to be inserted.
 * @return The inserted entity.
 */
@ApiMethod(name = "insertAppVersion")
public AppVersion insertAppVersion(AppVersion appversion) {
    AppVersion exist = getAppVersion(appversion.getApplicationName());
    AppVersion result;
    if (exist == null) {
        ofy().save().entity(appversion).now();
        result = getAppVersion(appversion.getApplicationName());
    } else {
        throw new IllegalArgumentException(appversion.getApplicationName() + " exists already");
    }
    return result;
}
}

我更希望它像原来一样可过滤,但我不知道为什么它会有所不同。谁能给我填一下?

【问题讨论】:

    标签: android google-app-engine google-cloud-datastore


    【解决方案1】:

    这里发生了两件事:首先,默认情况下,Objectify 假定您不想为类的属性建立索引(这使您的数据存储索引保持精简和平均)。其次,我相信新的 Datastore 控制台的过滤器 UI 仅显示具有与其关联的索引的属性(因为您无法过滤未索引的属性)。

    因此,如果您希望能够按 minVersionRequired 进行查询或排序,只需在 POJO 中的该字段中添加 @Index 注释,Objectify 将在基础实体中使用 setIndexedProperty() 方法低级 Datastore API 中的类。

    如果您想默认为您的类中的所有属性建立索引,您可以将@Index 注释放在该类上,然后将@Unindex 任何您明确不想被索引的地方。

    【讨论】:

    • 似乎没有用。也许需要一些时间?我应该不需要重新添加所有实体,对吧?
    • 嘿,你知道什么...几个小时后我回来检查并发现更改已生效。谢谢!
    猜你喜欢
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 2018-07-06
    • 2016-06-06
    相关资源
    最近更新 更多