-->
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.  [ 3 posts ] 
Author Message
 Post subject: Simple query - doable in hibernate?
PostPosted: Thu Jun 17, 2004 12:45 am 
Newbie

Joined: Sun Apr 11, 2004 2:25 am
Posts: 9
Using Hibernate 2 and mysql 4.0.8

I'm going to use an example to show our problem.

Let's say we have a box class. The box class has a collection of balls (called balls). The relationship is one-to-many, and is mapped via a Balls_In_Box table:

(snippit from box config)

<bag name="balls" table="Balls_In_Box">
<key column="box_id"/>
<one-to-many class="Ball"/>
</bag>

So what would the query look like that would return all boxes that contain no balls? It seems to me like nothing works...we've tried:

Query q = s.createQuery("select bx from bx in class Box, ball in bx.balls.elements group by bx having count(ball) = 0");

list = q.list()

but this always returns empty.

We could do a simple "select from Box as bx where bx.balls.size = 0" if our mysql supported that...

Any ideas? :|

~Jordan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 17, 2004 2:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I think this works:

Code:
select box
from Box box
   left join box.balls ball
where ball is null



p.s. "ball in bx.balls.elements" is a very deprecated syntax!q


Top
 Profile  
 
 Post subject: :)
PostPosted: Thu Jun 17, 2004 4:03 am 
Newbie

Joined: Sun Apr 11, 2004 2:25 am
Posts: 9
Thanks that did it...but we were attempting to use the syntax on your tips and tricks page - maybe you should update it? I guess now's a good time to ask if one still needs to get the count of something using the
.iterate(string) thing. What would be nice is if

Query q = session.createQuery("from class where class.id = :id and class.param = :param).setInteger("id", id).setString("param", param);

count = ((Integer) s.iterate("select count(*) " + q.getQueryString()).next()).intValue();

worked, however it doesn't (since the getQueryString() returns the string with the : stuff included), and thus forcing one to re-write the select statement using concatination :|


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