执行JPQL查询时出错

时间:2012-06-08 20:34:51

标签: jpa jpql

我是jpql中的新手我正在尝试执行此查询:

    String queryString="" +
            "SELECT u,sr " +
            "FROM spot_review sr, user u, spot s " +
            "WHERE " +
                "sr.user_iduser IN (SELECT ALL fu.user_iduserto FROM favorite_user fu WHERE fu.user_iduserfrom=:"+User.STRING_ID_USER+") and " +
                "sr.user_iduser = u.id_user and " +
                "sr.spot_idspot = s.id_spot and " +
                "sr.datereview IN (SELECT MAX(sr2.datereview) FROM spot_review sr2 GROUP BY sr2.user_iduser) " +
            "GROUP BY u.id_user " +
            "ORDER BY sr.user_iduser " +
            "LIMIT :"+ServiceConstants.STRING_PAGE_NUMBER+",:"+ServiceConstants.STRING_ROWS_PER_PAGE;

    Query query = entityManager.createQuery(queryString);

但是我收到了这个错误:

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: . near line 1, column 87 [SELECT u,sr FROM spot_review sr, user u, spot s WHERE sr.user_iduser IN (SELECT ALL fu.user_iduserto FROM favorite_user fu WHERE fu.user_iduserfrom=:idUser) and sr.user_iduser = u.id_user and sr.spot_idspot = s.id_spot and sr.datereview IN (SELECT MAX(sr2.datereview) FROM spot_review sr2 GROUP BY sr2.user_iduser) GROUP BY u.id_user ORDER BY sr.user_iduser LIMIT :pageNumber,:rowsPerPage]
    org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
    org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
    org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
    org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
    org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
    org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
    org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
    org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
    org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
    org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:272)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    $Proxy26.createQuery(Unknown Source)
    com.windy.server.dao.jpa.JpaDaoUser.getBestFavoriteUsersOBuser(JpaDaoUser.java:35)
    com.windy.server.service.impl.WindersMainServiceImpl.getBestFavoriteUsersOBuser(WindersMainServiceImpl.java:23)
    com.windy.server.controllers.WindersMainController.bestFavoriteUsersOBuserMobile(WindersMainController.java:93)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)

sql查询正在运行,但我没有在这个查询中记录我做错了什么......

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

这不是有效的JPQL查询,原因如下:

    JPQL不支持
  • limit。在查询对象
  • 上使用setFirstResult()setMaxResults()
  • 根据您的查询中使用的名称(spot_reviewuser_iduser,...),您似乎在查询表和列而不是实体和字段。 JPQL不使用表名和列名。决不。始终映射实体和字段/属性。
  • select all,AFAIK,在JPQL中不存在
  • 一个group by子句,在select子句中没有任何聚合函数(sum,count,avg等)没有意义

JPQL(或HQL)和SQL具有类似的结构,但它们不是同一种语言。

了解reference documentation中的HQL。