在数据存储App引擎上执行DISTINCT查询

时间:2014-04-18 10:34:49

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

我想从应用引擎的数据存储中获取不同的数据。我使用下面的代码从数据库中获取不同的实体,但它会在应用引擎服务器上抛出Class name distinct could not be resolved异常。

端点类中的代码:

public CollectionResponse<UserMaster> searchUser(
        @Named("strPrefix1") String strPrefix1,
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

    EntityManager mgr = null;
    Cursor cursor = null;
    List<UserMaster> execute = null;
    try {
        mgr = getEntityManager();

    //here if i remove "distinct" and use "*" selector then it works properly but i want to 
    //fetch distinct records and there for i need "distinct".

        Query query = mgr
                .createQuery("select distinct from UserMaster f where f.userName >= :strPrefix1 and f.userName < :strPrefix2");

        query.setParameter("strPrefix1", strPrefix1);
        query.setParameter("strPrefix2", strPrefix1 + "\ufffd");

        if (cursorString != null && cursorString != "") {
            cursor = Cursor.fromWebSafeString(cursorString);
            query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }
        if (limit != null) {
            query.setFirstResult(0);
            query.setMaxResults(limit);
        }
        execute = (List<UserMaster>) query.getResultList();
        cursor = JPACursorHelper.getCursor(execute);
        if (cursor != null)
            cursorString = cursor.toWebSafeString();
        for (UserMaster obj : execute)
            ;

    } finally {
        mgr.close();
    }
    return CollectionResponse.<UserMaster> builder().setItems(execute)
            .setNextPageToken(cursorString).build();
}

例外:

 Class name distinct could not be resolved
org.datanucleus.exceptions.NucleusUserException: Class name distinct could not be resolved
    at org.datanucleus.query.expression.PrimaryExpression.bind(PrimaryExpression.java:192)
    at org.datanucleus.query.compiler.JavaQueryCompiler.compileResult(JavaQueryCompiler.java:657)
    at org.datanucleus.query.compiler.JPQLCompiler.compile(JPQLCompiler.java:83)
    at org.datanucleus.store.query.AbstractJPQLQuery.compileInternal(AbstractJPQLQuery.java:271)
    at org.datanucleus.store.query.Query.setImplicitParameter(Query.java:799)
    at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:437)
    at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:57)
    at com.sampleregistrationapp.UserFeedMasterEndpoint.searchUser(UserFeedMasterEndpoint.java:620)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:45)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:359)
    at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:127)
    at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:85)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)

1 个答案:

答案 0 :(得分:2)

此时它甚至与App Engine无关:​​您的JPQL查询错误,应该是::

select distinct f from UserMaster f where f.userName >= :strPrefix1 and f.userName < :strPrefix2

在您的查询中,JPA将distinct关键字视为您搜索的类名。

编辑:我努力为JPA&amp; amp;找到关于此的文档JDO。对于低级别数据存储区API, distinct 查询仅适用于投影查询,即仅返回实体的某些索引属性的查询,而不是整个实体。即使使用正确的语法,您的查询也很有可能无法正常工作。

https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Query#setDistinct(boolean) =&gt;见setDistinct(boolean distinct)