@NamedQuery INNER JOIN不起作用

时间:2014-05-30 12:45:05

标签: java mysql hibernate jpa inner-join

我有以下查询,我需要在联合表中找到元素的数量。

@NamedQueries({ @NamedQuery(name = "A.count", query = "SELECT COUNT (p.pid) FROM Pet p INNER JOIN Interaction ir ON ir.modelId = p.modelId WHERE p.modelId = :modelId")})

但是对于上面的设置,我在eclipse中得到以下3个错误。即使在服务器中部署它也会引发异常

  • 在FROM子句
  • 中未定义标识变量“Interaction”
  • 标识变量的名称不得与同一持久性单元中的任何实体具有相同的名称
  • 加入关联路径不是有效表达式

部署时的例外情况:

15:43:37,915 ERROR [org.hibernate.hql.internal.ast.ErrorCounter] (ServerService Thread Pool -- 153)  Path expected for join!:  Path expected for join!
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:378) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]

1 个答案:

答案 0 :(得分:1)

查询中的语法不正确。试试这个:

SELECT COUNT (p.pid) FROM Pet INNER JOIN model.pets as p 
INNER JOIN Interaction.model as model WHERE model.id = :modelId

另请看一下:

https://docs.jboss.org/hibernate/core/3.5/reference/en/html/queryhql.html#queryhql-joins