gpt4 book ai didi

java - Objectify - 如何按 boolean 值过滤?

转载 作者:行者123 更新时间:2023-12-03 23:07:39 24 4
gpt4 key购买 nike

在过滤 boolean 值时,我使用 Objectify 为 google appengine 数据存储区撞墙了。这大致是我所拥有的:

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

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

最佳答案

我在谷歌搜索时发现了这个老问题,我想把它弄清楚。

您应该能够通过 boolean 字段进行查询,只要它们在进入数据存储时已被索引。这是使用 Objectify 和 App Engine 单元测试库的完整单元测试(要运行它,您必须在 the unit test jar described here 中进行链接)。以下测试通过。所以问题出在其他地方,我建议您使用单元测试来发现它。

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());
}
}

关于java - Objectify - 如何按 boolean 值过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3580899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com