I have a hql query like this:
return (long)Session.CreateQuery (" select count(*) " +
" from Files p " +
" join fetch p.Application a " +
" where a.Name = :AppName")
.SetParameter("AppName", application)
.UniqueResult();
Hibernate creates sql query like this:
select field1, field2, field3, count(*)
That’s why it cannot be executed.
Why select count(*) makes hibernate to list all fields and how to prevent it?
Thanks
In my opinion, the problem comes with the use of ‘
FETCH‘, which is normally used to get the dependencies of a given entity from the database. So Hibernate queries the fields of Application to create the objects.If you only want to count the files associated to an application, change ‘
JOIN FETCH‘ for ‘INNER JOIN‘ (or simply ‘JOIN‘). The only queried field should becount(*)then.