什么可能导致GAE查询中的“java.lang.IllegalArgumentException:Type mismatch”?

时间:2014-01-02 11:22:22

标签: java google-app-engine google-cloud-datastore

我有两个代码段,我试图在Google App Engine SDK v1.8.8 for Java的开发服务器上运行。

第一个:

return datastore.get(KeyFactory.createKey(Kinds.Provider,provider_id));

按预期工作,并从数据存储中返回提供者实体。

但是当我尝试将其更改为投影查询时,它只返回

Filter filter=new FilterPredicate(Entity.KEY_RESERVED_PROPERTY,
                                  Query.FilterOperator.EQUAL,
                                  KeyFactory.createKey(Kinds.Provider,provider_id));
Query q = new Query(Kinds.Provider)
            .setFilter(filter)
    .addProjection(new PropertyProjection("address", String.class))
        .addProjection(new PropertyProjection("last_modified", String.class));;
    }
PreparedQuery pq = datastore.prepare(q);
log.info("query:" + q.toString());

Entity result = pq.asSingleEntity();

我收到以下例外情况:

java.lang.IllegalArgumentException: Type mismatch.
    at com.google.appengine.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:96)
    at com.google.appengine.api.datastore.RawValue.asType(RawValue.java:61)
    at com.google.appengine.api.datastore.PropertyProjection.getValue(PropertyProjection.java:65)
    at com.google.appengine.api.datastore.EntityTranslator.createFromPb(EntityTranslator.java:29)
    at com.google.appengine.api.datastore.QueryResultsSourceImpl.processQueryResult(QueryResultsSourceImpl.java:199)
    at com.google.appengine.api.datastore.QueryResultsSourceImpl.loadMoreEntities(QueryResultsSourceImpl.java:106)
    at com.google.appengine.api.datastore.QueryResultIteratorImpl.ensureLoaded(QueryResultIteratorImpl.java:155)
    at com.google.appengine.api.datastore.QueryResultIteratorImpl.nextList(QueryResultIteratorImpl.java:110)
    at com.google.appengine.api.datastore.LazyList.forceResolveToIndex(LazyList.java:93)
    at com.google.appengine.api.datastore.LazyList.resolveToIndex(LazyList.java:73)
    at com.google.appengine.api.datastore.LazyList.resolveToIndex(LazyList.java:56)
    at com.google.appengine.api.datastore.LazyList.isEmpty(LazyList.java:260)
    at com.google.appengine.api.datastore.PreparedQueryImpl.asSingleEntity(PreparedQueryImpl.java:74)

记录查询.toString()yeilds:

SELECT last_modified, address FROM Provider WHERE __key__ = Provider(99)

有关此异常可能原因的任何想法?

1 个答案:

答案 0 :(得分:3)

好的,找到它。

数据库last_modified中的

实际上是Long所以它应该是

.addProjection(new PropertyProjection("last_modified", Long.class));

感谢您的倾听,我希望将来可以帮助某人...