java - GAE 数据存储查询整数字段

标签 java google-app-engine google-cloud-datastore apache-commons-beanutils

我在查询 GAE 数据存储时注意到奇怪的行为。在某些情况下,过滤器不适用于整数字段。以下 java 代码重现了该问题:

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");

日志如下所示:

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

出于某种原因,它只找到第一个实体,即使这两个实体实际上都存储在数据存储中并且两个“字段”值都是 777(我在数据存储查看器中看到它)!为什么实体的创建方式如此重要?我想使用BeanUtils,因为它很方便。

在本地开发服务器上以及部署到 GAE 时也会出现同样的问题。

最佳答案

好的,我知道发生了什么事。 “问题”是由于某种原因 BeanUtils 将整数转换为字符串。字符串在数据存储查看器中看起来完全相同,但它当然不一样。这几乎愚弄了我。我应该研究 apache BeanUtils 手册什么的。

关于java - GAE 数据存储查询整数字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13846915/

相关文章:

google-app-engine - 在 Appengine 上将 Google Cloud Storage 与 Golang 结合使用

python - 从 Python 中删除 GAE NBD 实体并刷新 Memchach 后,数据存储查看器中仍然可以看到它们

java - 并行合并排序时出现内存不足错误

java - 没有找到适合 getText(String) 的方法

java - 有没有办法将 Java VM args 移动到 Spring Boot 配置文件?

google-app-engine - 如何使用 Java API 将超过 32Mb 的文件上传到谷歌云存储

java - 使用大量已实现的接口(interface)

google-app-engine - 通过 cron 作业运行的 Google 应用程序引擎 servlet 在一段时间后停止工作

python-2.7 - 在 App Engine (python) 之外使用 Datastore?

python - 最初是在 Python 中创建的,如何在 Go 中使用数据存储 GAE?