如何使用子查询在hql中使用内连接

时间:2018-06-17 03:18:48

标签: hql

我是HQL的新手。 我有一张桌子 Product Table 我有一个像

这样的查询
Query query = getEntityManager().createQuery(
                "Select f from Product f where f.Type=1 and f.EmpId='" + empId + "' and f.Status IN ("+allowedStatus+")");

empId=Abc and allowedStatus = (1,2)

点击此查询后,我将获得状态为2的所有行。

我的问题是我只想查找版本最大的行(对于所有ProductId)以及状态为2的行。

我的O / P看起来像: Final Product Table

我在SQL中编写了一个查询:

SELECT f.*
FROM Product f
INNER JOIN
 (Select ProductID, Max(ProductVersion) AS Version , EmpId from Product f 
 WHERE f.Type=1 and f.empId='abc' and f.Status IN (1,2) 
 GROUP BY ProductID, empID) AS groupedtt 
ON f.ProductID = groupedtt.Product
AND f.ProductVersion = groupedtt.ProductVersion

但它在下面显示错误: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 72 [Select f from Product f inner join (Select f.ProductId, max(f.Version) As version from Product f where f.Type=1 and f.empid='abc' and f.Status IN (1,2) group by ProductId) AS groupeIt on f.ProductId = groupeIt.ProductId and f.Version = groupeIt.Version] at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1679)

1 个答案:

答案 0 :(得分:0)

使用HQL无法执行某种子查询。其中之一是SELECT之后的JOIN,就像您在这里尝试做的那样:

INNER JOIN
    (Select ProductID, Max(ProductVersion) AS Version , EmpId from Product f