Evan Haas
Evan Haas

Reputation: 2574

How to select count in NHibernate HQL when joining tables

Why does NHibernate translate this HQL:

select count(*) from TeacherResource as tr
    inner join fetch tr.Product as pr
    where pr.CatalogTitle like '%ame%'

into this invalid SQL (including the where clause but omitting the table join):

select count(*) as col_0_0_
    from   TeacherResources teacherres0_
    where  product1_.CatalogTitle like '%ame%'

and how do I perform a count that will behave as expected?

Here is the relevant portion of the entity:

Public Class TeacherResource
    Public Overridable Property TeacherResourceId As Guid
    Public Overridable Property Product As BvCustomProduct
End Class

and mapping:

<class name="TeacherResource" table="TeacherResources">
    <id name="TeacherResourceId">
        <generator class="guid"/>
    </id>
    <many-to-one name="Product" column="ProductBvin"/>
</class>

Upvotes: 0

Views: 1324

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52735

You don't need an explicit join, nor a fetch for your query.

This should be enough:

select count(*)
from TeacherResource
Where Product.CatalogTitle like '%ame%'

Upvotes: 1

Related Questions