Objectify-如何按布尔值过滤? - java

在对布尔值进行过滤时,我已经在Google Appengine数据存储中使用Objectify碰壁了。这大致就是我所拥有的:

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

参考方案

我在谷歌搜索时发现了这个旧问题,并希望将其清除。

只要布尔字段在进入数据存储区时已被索引,您就应该能够通过它们查询。这是使用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-搜索字符串数组中的字符串 - java

在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…

Java Scanner读取文件的奇怪行为 - java

因此,在使用Scanner类从文件读取内容时,我遇到了一个有趣的问题。基本上,我试图从目录中读取解析应用程序生成的多个输出文件,以计算一些准确性指标。基本上,我的代码只是遍历目录中的每个文件,并使用扫描仪将其打开以处理内容。无论出于何种原因,扫描程序都不会读取其中的一些文件(所有UTF-8编码)。即使文件不是空的,scanner.hasNextLine()在…

Java Globbing模式以匹配目录和文件 - java

我正在使用递归函数遍历根目录下的文件。我只想提取*.txt文件,但不想排除目录。现在,我的代码如下所示:val stream = Files.newDirectoryStream(head, "*.txt") 但是这样做将不会匹配任何目录,并且返回的iterator()是False。我使用的是Mac,所以我不想包含的噪音文件是.DS_ST…

直接读取Zip文件中的文件-Java - java

我的情况是我有一个包含一些文件(txt,png,...)的zip文件,我想直接按它们的名称读取它,我已经测试了以下代码,但没有结果(NullPointerExcepion):InputStream in = Main.class.getResourceAsStream("/resouces/zipfile/test.txt"); Buff…

Java RegEx中的单词边界\ b - java

我在使用\b作为Java Regex中的单词定界符时遇到困难。对于text = "/* sql statement */ INSERT INTO someTable"; Pattern.compile("(?i)\binsert\b");找不到匹配项Pattern insPtrn = Pattern.compile(&…