简体   繁体   English

Google App Engine查询无法在Java中运行

[英]Google App Engine Query Not Working in Java

Riddle me this Stackoverflow: 这让我感到困惑:

I have a query that I am sending to GAE. 我有一个查询要发送给GAE。 The query (When in String format) looks like this: SELECT * FROM USER WHERE USER_ID = 5884677008 查询(字符串格式时)如下所示:SELECT * FROM USER WHERE USER_ID = 5884677008

If I go to the GAE console and type it in via a manual GQL query, it returns the item just fine. 如果我转到GAE控制台并通过手动GQL查询将其键入,它将返回该项目。 If I browse via the GUI and scroll to it, I can see it just fine. 如果我通过GUI浏览并滚动到它,则可以看到它。 But when I call it from the Java code, it returns nothing every time. 但是,当我从Java代码中调用它时,每次都不会返回任何内容。 code: 码:

I have already confirmed the query is correct as I printed it out as a String just so I can test it. 我已经确认查询是正确的,因为我将其打印为字符串,以便可以对其进行测试。

Anyone have any idea what is going on with this? 任何人都知道这是怎么回事吗?

    q = new Query(entityName); //entityName = "User", confirmed
    q.setFilter(filter); //filter = "USER_ID = 5884677008", confirmed
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    PreparedQuery pq = datastore.prepare(q);
    /*
    This always is empty here. Calling either pq.countEntities()); or
    pq.toString()); returns size 0 or a String of nothing.
    */

Thanks! 谢谢! -Sil -Sil

Edit: I Do have an index built, but it did not seem to help with the problem. 编辑:我确实有建立索引,但它似乎没有帮助解决该问题。

From the docs , you don't necessarily need to do toString. 文档开始 ,您不一定需要执行toString。 Have you tried asIterable or asSingleEntity on pq? 您是否在pq上尝试过asIterable或asSingleEntity? Something like: 就像是:

PreparedQuery pq = datastore.prepare(q);

for (Entity result : pq.asIterable()) {
  String test = (String) result.getProperty("prop1");
}

That's if you have multiple entries. 那就是如果您有多个条目。 In the event you only have one: 如果您只有一个:

PreparedQuery pq = datastore.prepare(q);

Entity result = pq.asSingleEntity();
String test = (String) result.getProperty("prop1");

Basically, if you don't call asIterable or asSingleEntity, the query is JUST prepared and doesn't run 基本上,如果您不调用asIterable或asSingleEntity,则查询只是准备好的并且不会运行

Took quite a bit of testing, but found the issue. 进行了大量测试,但发现了问题。

The problem revolved around the filter being set. 问题与设置的过滤器有关。 If I removed the filter, it worked fine (but returned everything). 如果我删除了过滤器,它可以正常工作(但返回的一切)。 Turns out, what was being passed as a filter was a String version of the user_id as opposed to the Long version of it. 事实证明,作为过滤器传递的是user_id的String版本,而不是Long版本。 There was really no way to tell as the exact SQL query DID NOT read ( SELECT * FROM USER WHERE USER_ID = "5884677008" ) when I printed it, which would have been a dead giveaway. 当我打印它时,确实没有办法告诉我未读到的确切SQL查询(SELECT * FROM USER WHERE USER_ID =“ 5884677008”),这简直是无济于事。

I changed the passed filter parameter (which I had stored in a hashmap of (String, Object) btw) from a String to a Long and that solved the issue. 我将传递的过滤器参数(我已经存储在(字符串,对象)btw的哈希图中的参数)从字符串更改为Long,这解决了这个问题。

One thing to point out though, as @Patrice brought up (And as I excluded from my code while posting to save space), to actually iterate through the list of results, you do need to call a method against it (Either .asIterable() or .asSingleEntity() ). 需要指出的是,当@Patrice调出(并且为了节省空间而在发布时从我的代码中排除)时,实际上要遍历结果列表,您确实需要针对它调用一个方法(.asIterable( )或.asSingleEntity())。

You actually can check against the number of returned entities / results by calling pq.countEntities() and it will return the correct number even before you call a formatting method against the pq, but as @tx802 pointed out, it is deprecated, and despite the fact that it worked for me, someone in the future using this post as a reference may not have it work for them. 实际上,您可以通过调用pq.countEntities()来检查返回的实体/结果的数量,即使在您对pq调用格式化方法之前,它也将返回正确的数字,但是正如@ tx802所指出的那样,它对我有用的事实,将来有人将此帖子作为参考,可能对他们没有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM