Objectify - 如何按布尔过滤?

And*_*ers 2 java google-app-engine objectify google-cloud-datastore

在过滤布尔值时,我使用Objectify为google appengine数据存储区打了一堵墙.这大致是我的意思:

class Task implements Serializable {
 ... 
 boolean failed;
 ...
}
Run Code Online (Sandbox Code Playgroud)

无论我在搜索时做什么,我总是得到一个空的响应,尽管数据库中有对象 failed = false

例子:

ofy().query(Task.class).filter("failed",false).list()
ofy().query(Task.class).filter("failed",Boolean.FALSE).list()
ofy().query(Task.class).filter("failed",0).list()
ofy().query(Task.class).filter("failed","false").list()
ofy().query(Task.class).filter("failed","FALSE").list()
Run Code Online (Sandbox Code Playgroud)

yon*_*ran 6

谷歌搜索时我发现了这个老问题,我想清除它.

您应该能够通过布尔字段进行查询,只要它们在进入数据存储区时被编入索引即可.这是使用Objectify和App Engine单元测试库的完整单元测试(要运行它,您必须链接到此处描述的单元测试jar).以下测试通过.所以问题出在其他地方,我建议您使用单元测试来发现它.

import static org.junit.Assert.*;

import javax.persistence.Id;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.datastore.QueryResultIterator;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

class FakeEntity {
  @Id public Long id;
  public boolean boolProp;
  public boolean equals(Object other) {
    return other != null &&
           other instanceof FakeEntity &&
           ((FakeEntity)other).id == this.id &&
           ((FakeEntity)other).boolProp == this.boolProp; 
  }
}

public class FakeEntityTest {
  private final LocalServiceTestHelper helper =
    new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
  @Before
  public void setUp() {
    helper.setUp();
  }
  @After
  public void tearDown() {
    helper.tearDown();
  }

  @Test
  public void testBoolQuery() {
    ObjectifyFactory objectifyFactory = ObjectifyService.factory();
    objectifyFactory.register(FakeEntity.class);
    Objectify objectify = objectifyFactory.begin();
    FakeEntity entityFalse = new FakeEntity();
    FakeEntity entityTrue = new FakeEntity();
    entityTrue.boolProp = true;
    objectifyFactory.begin().put(entityFalse);
    objectifyFactory.begin().put(entityTrue);

    assertArrayEquals(
        new FakeEntity[] {entityFalse},
        objectify.query(FakeEntity.class)
        .filter("boolProp", false).list().toArray());
    assertArrayEquals(
        new FakeEntity[] {entityTrue},
        objectify.query(FakeEntity.class)
        .filter("boolProp", true).list().toArray());
    assertArrayEquals(
        new FakeEntity[] {entityTrue},
        objectify.query(FakeEntity.class)
        .filter("boolProp", true).list().toArray());
    assertArrayEquals(
        new FakeEntity[] {entityTrue},
        objectify.query(FakeEntity.class)
        .filter("boolProp", Boolean.TRUE).list().toArray());
    // Filtering on integers and strings WON'T work:
    assertArrayEquals(
        new FakeEntity[] {},
        objectify.query(FakeEntity.class)
        .filter("boolProp", "true").list().toArray());
    assertArrayEquals(
        new FakeEntity[] {},
        objectify.query(FakeEntity.class)
        .filter("boolProp", 0).list().toArray());
  }
}
Run Code Online (Sandbox Code Playgroud)


Bha*_*thi 5

您尚未对布尔失败属性建立索引。

如果字段未建立索引,过滤器将无法在 objectify 数据存储中工作。

因此,要使其正常工作,请添加

@Index boolean failed;
Run Code Online (Sandbox Code Playgroud)

现在你的过滤器就可以工作了。

请注意,虽然已建立索引,但无法过滤已保存的值。因此,要么创建新记录并保存,要么读取所有数据存储实体并再次保存。

希望这可以帮助。