2010-08-27 69 views
2

在布爾值過濾時,我使用Objectify爲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() 

回答

6

我發現這個古老的問題,而谷歌搜索,我想清除它。

只要它們在進入數據存儲時被索引,就應該能夠通過布爾字段進行查詢。這是一個使用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()); 
    } 
} 
+0

小調:IDE建議創建缺少的FakeEntity#hashCode() – mjn 2011-07-01 09:08:35

4

您還沒有Indexed boolean failed屬性。

如果一個字段沒有被索引,過濾器將不會在物化數據存儲中工作。

因此使其工作,加

@Index boolean failed; 

現在您的過濾器將工作。

請注意,雖然索引,已保存的值不能被過濾。因此,要麼創建新記錄並保存或讀取所有數據存儲實體並再次保存。

希望這會有所幫助。