Search code examples
mysqlselectcounthql

HQL: Unknown column in where clause


I am trying to count the rows of an hql query. I get the warning that the column TypeOfPermission does not exist. That strikes me as odd as a previous query does work. The line below gives me back 2 rows (as expected)

from ClientIdentity c left join fetch c.Permissions p where p.TypeOfPermission = :permissionType;

However using the following count query, the query fails as the column TypeOfPermission is now no longer recognised.

countQuery = "select count(*) from ClientIdentity c left join fetch c.Permissions p where p.TypeOfPermission = :permissionType";
long count = countQuery.UniqueResult<long>();

Solution

  • When using "Count(*)", "fetch" is not needed.

    countQuery = "select count(*) from ClientIdentity c left join c.Permissions p where p.TypeOfPermission = :permissionType";
    long count = countQuery.UniqueResult<long>();