Hibernate version: 2.1.6
I was wondering if anyone could help me out with HQL. I have three mapped objects:
Code:
<class name="Industry" table="industry" mutable="false">
<id name="industryId"
type="long"
column="industry_id"
unsaved-value="-1">
<generator class="assigned"/>
</id>
.....
<set name="categories" inverse="true" lazy="false">
<key column="industry_id" />
<one-to-many class="Cat" />
</set>
</class>
Code:
<class name="Cat" table="cat" mutable="false">
<id name="catId"
type="long"
column="cat_id"
unsaved-value="-1">
<generator class="assigned"/>
</id>
.....
<many-to-one name="industry"
class="Industry"
column="industry_id">
<meta attribute="use-in-tostring">true</meta>
</many-to-one>
<set name="projects" lazy="true" table="project_cat">
<key column="cat_id" />
<many-to-many class="Project" column="project_id" />
</set>
</class>
Code:
<class name="Project" table="project">
<id name="projectId"
type="long"
column="project_id"
unsaved-value="-1">
<generator class="native"/>
</id>
....
<property name="expirationDate"
column="expiration_date"
type="timestamp"
not-null="false"
index="idx_project_expiration_date">
</property>
<set name="categories" lazy="true" inverse="true" table="project_cat">
<key column="project_id" />
<many-to-many class="Cat" column="cat_id" />
</set>
</class>
The relationship is that an industry has categories in it. Each project is listed under one or more of those categories - so that each project can technically be in more than one industry.
My goal is to return a list that shows each industry id (industryId) and the number of projects in that industry (count(industryId)). The main catch is that a project should not be counted twice if it's in more than one category in that industry.
In SQL, the statement is pretty straight-forward:
Code:
SELECT c.INDUSTRY_ID, COUNT(c.INDUSTRY_ID)
FROM
(SELECT DISTINCT c.INDUSTRY_ID, p.PROJECT_ID
FROM project_cat pc, cat c, project p
WHERE pc.cat_id = c.cat_id
AND p.PROJECT_ID = pc.project_id
AND p.EXPIRATION_DATE > current_date)
c GROUP BY c.INDUSTRY_ID
I have tried for a while now to get the same result using HQL and i haven't really gotten anywhere :(.
Any help would be appreciated!