-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Help with HQL
PostPosted: Fri Aug 27, 2004 12:02 pm 
Newbie

Joined: Thu Jun 03, 2004 2:10 am
Posts: 19
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!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 3:50 pm 
Regular
Regular

Joined: Fri Aug 29, 2003 12:48 pm
Posts: 63
Do you even want category to be a real entity object? If there's nothing more to it than what you show, wouldn't it be simpler to do many-to-many mappings from Industry to Product?

Code:
<class name="Industry">
  ...
  <set name="products" table="cat">
    <key column="industry_id"/>
    <many-to-many class="Product" column="product_id"/>
  </set>
</class>


Then you could use count(elements(Industry.products)) in your hql query.

Note I'm by no means a Hibernate expert, just trying to help out.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 4:53 pm 
Newbie

Joined: Thu Jun 03, 2004 2:10 am
Posts: 19
Categories are important in the system.

Do you know a way to solve the problem i stated in the message?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 5:14 pm 
Regular
Regular

Joined: Fri Aug 29, 2003 12:48 pm
Posts: 63
The elements() function provided by HQL might have some help for you. Or you could just use the SQL query you already have written. I very much appreciate everything Hibernate does for me, but note that this piece of the puzzle - queries involving collections - can be more painful than not when all is said and done.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 2:41 pm 
Newbie

Joined: Thu Jun 03, 2004 2:10 am
Posts: 19
Thanks for the reply.

I did solve the problem in the end, so I thought i'd post the solution for anyone else that might have a similar problem:

Code:
select
    cat.industry.id, count(distinct proj.id)
from
    Cat cat, Project proj join proj.categories pcat
where
    pcat.id = cat.id
    and proj.expirationDate > :expiration
group by
    cat.industry.id



Cheers.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.