0

我在 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 查询正在运行,但我不明白我在此查询中做错了什么......

在此先感谢您的帮助

4

1 回答 1

1

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

  • limitJPQL 不支持。在 Query 对象上使用setFirstResult()andsetMaxResults()
  • 给定查询中使用的名称(spot_review, user_iduser, ...),您似乎正在查询表和列而不是实体和字段。JPQL 不使用表名和列名。绝不。始终映射实体和字段/属性。
  • select all,AFAIK,在 JPQL 中不存在
  • group by在 select 子句(sum、count、avg 等)中没有任何聚合函数的子句没有意义

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

在参考文档中了解 HQL 。

于 2012-06-08T20:46:10.000 回答