-->
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: Rowcount projection, criteria, and joins
PostPosted: Wed Sep 23, 2009 8:08 am 
Newbie

Joined: Wed Sep 23, 2009 7:54 am
Posts: 1
Hi!

I have three entities:

User { School school; }
School { City city; }
City { Integer id; }

I want to get count of users with the city id of 1.

My criteria:
Criteria crit = session.createCriteria(User.class, "u");
crit.createAlias("school.city", "cty", Criteria.INNER_JOIN);
crit.add(Restrictions.eq("cty.id", cityId));
crit.setProjection(Projections.count("id"));

Problem:
Hibernate: select count(this_.id) as y0_ from users this_ where this_.type=? and cty1_.id=? and this_.active=?
Unknown column 'cty1_.id' in 'where clause'

The expected query would be:
select count(u.id) from users u, schools s where u.active=1 and u.school_id = s.id and s.city_id = 1

Please help, because I tried I guess almost everything :) Of course, the same query without the projection works fine.
Thank you very much.


Top
 Profile  
 
 Post subject: Re: Rowcount projection, criteria, and joins
PostPosted: Tue May 25, 2010 5:21 pm 
Newbie

Joined: Tue May 25, 2010 5:16 pm
Posts: 3
Hi All,

Someone knows how to resolve this?

I'm having the same problem, hibernate ignore my join and I don't know how to do this.

Thanks


Top
 Profile  
 
 Post subject: Re: Rowcount projection, criteria, and joins
PostPosted: Wed May 26, 2010 10:09 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
You need another alias:
Code:
Criteria crit = session.createCriteria(User.class, "u");
crit.createAlias("school", "school", Criteria.INNER_JOIN);
crit.createAlias("school.city", "cty", Criteria.INNER_JOIN);
crit.add(Restrictions.eq("cty.id", cityId));
crit.setProjection(Projections.count("id"));


you can also use the chaining syntax to make your code more readable:

Code:
Criteria crit = session.createCriteria(User.class, "u")
.createAlias("school", "school", Criteria.INNER_JOIN)
.createAlias("school.city", "cty", Criteria.INNER_JOIN)
.add(Restrictions.eq("cty.id", cityId))
.setProjection(Projections.count("id"));


Top
 Profile  
 
 Post subject: Re: Rowcount projection, criteria, and joins
PostPosted: Wed May 26, 2010 1:42 pm 
Newbie

Joined: Tue May 25, 2010 5:16 pm
Posts: 3
Tks a lot man.

Works so good for me!


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.