2

在过滤布尔值时,我使用 Objectify 为谷歌 appengine 数据存储区碰壁了。这大致是我所拥有的:

class Task implements Serializable {
 ... 
 boolean failed;
 ...
}

无论我在搜索时做什么,我总是得到一个空响应,尽管 db 中有对象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()
4

2 回答 2

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());
  }
}
于 2010-11-06T16:58:52.923 回答
4

您还没有索引布尔失败属性。

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

所以要让它工作,添加

@Index boolean failed;

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

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

希望这可以帮助。

于 2014-11-12T10:23:56.820 回答