如何在谷歌端点上进行正确的选择查询?

时间:2014-03-29 16:33:26

标签: google-app-engine jpa

我使用Google Cloud Endpoints创建了一个Android项目,我创建了一个模型类Poll.java,现在我想在PollEndpoint.java类中进行查询,以检索与特定作者的投票。

这是PollEndpoint.java中的查询代码

@ApiMethod(name = "getSpecificPoll", path="lastpoll")
public Poll getSpecificPoll(@Named("creator") String creator) {
    EntityManager mgr = getEntityManager();
    Poll specificPoll = null;

    try {

        Query query = mgr.createQuery("select from Poll where creator 
                                        ='"+creator+"'");
        specificPoll = (Poll) query.getSingleResult();
        } finally {
            mgr.close();
        }
        return specificPoll;
    }

客户端部分中的代码是:

 private class PollQuery extends AsyncTask<Void, Void, Poll> {
        @Override
        protected Poll doInBackground(Void... params) {

            Poll pollQuery = new Poll();

            Pollendpoint.Builder builderQuery = new Pollendpoint.Builder(
                      AndroidHttp.newCompatibleTransport(), new
                      JacksonFactory(),null);

            builderQuery = CloudEndpointUtils.updateBuilder(builderQuery);

            Pollendpoint endpointQuery = builderQuery.build();



                  try {
                    pollQuery =
                                        endpointQuery.getSpecificPoll("Bill").execute();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();

                }

                  if (pollQuery != null){
                  System.out.println(pollQuery.getKeyPoll().getId());
                  } else System.out.println("Null query");

                  return null;
            }

问题是服务器抛出异常:

javax.persistence.PersistenceException: FROM clause of query has class com.development.pollmeproject.Poll but no alias
    at org.datanucleus.api.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException(NucleusJPAHelper.java:302)

我认为查询语句不正确,我该如何编写正确的查询语句?

1 个答案:

答案 0 :(得分:0)

您提供的查询不是有效的JPQL。 JPQL的形式更多

SELECT p FROM Poll p WHERE p.creator = :creatorParam

错误消息确实告诉您,但我不确定您为什么不确定