GAE datastore querying integer fields

Posted by ParanoidAndroid on Stack Overflow See other posts from Stack Overflow or by ParanoidAndroid
Published on 2012-12-12T19:11:50Z Indexed on 2012/12/13 17:03 UTC
Read the original article Hit count: 304

I notice strange behavior when querying the GAE datastore. Under certain circumstances Filter does not work for integer fields. The following java code reproduces the problem:

log.info("start experiment");

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

int val = 777;

// create and store the first entity.
Entity testEntity1 = new Entity(KeyFactory.createKey("Test", "entity1"));
Object value = new Integer(val);
testEntity1.setProperty("field", value);
datastore.put(testEntity1);

// create the second entity by using BeanUtils.
Test test2 = new Test(); // just a regular bean with an int field
test2.setField(val);

Entity testEntity2 = new Entity(KeyFactory.createKey("Test", "entity2"));

Map<String, Object> description = BeanUtilsBean.getInstance().describe(test2);
for(Entry<String,Object> entry:description.entrySet()){
    testEntity2.setProperty(entry.getKey(), entry.getValue());
}

datastore.put(testEntity2);


// now try to retrieve the entities from the database...

Filter equalFilter = new FilterPredicate("field", FilterOperator.EQUAL, val);

Query q = new Query("Test").setFilter(equalFilter);

Iterator<Entity> iter = datastore.prepare(q).asIterator();

while (iter.hasNext()) {
    log.info("found entity: " + iter.next().getKey());
}

log.info("experiment finished");

the log looks like this:

INFO: start experiment
INFO: found entity: Test("entity1")
INFO: experiment finished

For some reason it only finds the first entity even though both entities are actually stored in the datastore and both 'field' values are 777 (I see it in the Datastore Viewer)! Why does it matter how the entity is created? I would like to use BeanUtils, because it is convenient.

The same problem occurs on the local devserver and when deployed to GAE.

© Stack Overflow or respective owner

Related posts about java

Related posts about google-app-engine