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.  [ 4 posts ] 
Author Message
 Post subject: HQL "contains" query?
PostPosted: Tue May 15, 2007 5:14 am 
Newbie

Joined: Thu Mar 22, 2007 6:50 am
Posts: 6
Hi all,

I'm suppose this is a simple question, but I couldn't find anything on the Net (maybe I was looking with the wrong search terms)...

What I want to do is create an HQL query that retuns the count of a class that has another class appearing in the Set of one if it's properties. Let me try with an example :)

Let's say I have a class "recipe" and a class "ingredient". ingredient is a normalized entity. So, recipe has a property ingredients that is a set of ingredient items.

Code:
<class name="Recipe" table="recipe">
...
<set name="ingredients" table="recipe_ingredient" cascade="none">
<key column="recipe_id" />
<many-to-many column="ingredient_id" class="Ingredient" />
</set>
...
</class>


Code:
<class name="Ingredient" table="ingredient">
...
</class>


I'd like to count the recipe items that contain a specific ingredient (passed in with ingredient id), something like:

Code:
select count(x) from Recipe as x where x.ingredients contains (select y from Ingredient where y.id = ?)


Any help appreciated!
Best regards,
James


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 5:40 am 
Beginner
Beginner

Joined: Mon Nov 06, 2006 2:40 am
Posts: 29
Location: New Delhi, India
fire query on Ingredient table

select count(y.recipe_id) from Ingredient as y where y.id=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 5:44 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
For such cases, I think, usingSQLQuery is easier and better than HQL.
session.createSQLQuery("select count(ingredient_id) from recipe_ingredient where ingredient_id = :id").setParameter("id", 23).uniqueResult() ;
It's simple.

------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 6:09 am 
Newbie

Joined: Thu Mar 22, 2007 6:50 am
Posts: 6
@Sagi:

I cannot query the ingredient table as ingredient is normalized. My tables look like this:

Code:
CREATE TABLE recipe (
   id INTEGER NOT NULL PRIMARY KEY,
   title VARCHAR(1024) DEFAULT NULL
);
CREATE TABLE ingredient (
   id INTEGER NOT NULL PRIMARY KEY,
   name VARCHAR(1024) NOT NULL
);

CREATE TABLE recipe_ingredient (
   recipe_id INTEGER NOT NULL,
   ingredient_id INTEGER NOT NULL
);


@pramodkp:

I'd rather stick to HQL if possible as my whole data access strategy is built on GenericDao. Of course, if what I want to do is not possible at all in HQL I'll resolve to something else.

Many thanks for your initial replies...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.