-->
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: how to test for existence of related object
PostPosted: Sat Mar 25, 2006 4:05 am 
Newbie

Joined: Sat Mar 25, 2006 3:50 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Imagine I have two mapped objects, X and Y, and two join tables A and B that are also represented as mapped objects.

A
aid (pk)
xid (mapped to X)
yid (mapped to Y)

B
bid (pk)
xid (mapped to X)
yid (mapped to Y)

The basic HQL query I want is:
from A where x.id=?

Simple enough. The part I'm having trouble with is that I'd also like to test for the existence of (x,y) in B. So, along with A, I'd like to return true/1 or false/0 where true/1 indicates that there is a matching record B with B.x=A.x and B.y=A.y.

I can limit the result set to one or the other in the WHERE clause using a subselect, but I'd like to have it as a computed value so I can return both types of objects, and even sort/paginate on the value.

One approach I tried was to do a subselect in the from clause, something like:
from A a, (select count(*) from B b where a.x=b.x and a.y=b.y)=0 ...
but that doesn't appear to be valid HQL.

I've also tried adding a mapping in A to B, but I'm not sure how to get a one-to-one relationship out of it (constraining both x and y, though neither are a primary key in A or B).

Thanks for any suggestions.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 1:34 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you don't mind dealing with an array of Objects being returned from your query, this should work:
Code:
select a, count(b)
from A a, B b
where a.x.id = :xval and b.x.id = :xval
  and a.y.id = :yval and b.y.id = :yval
The returned array will contain your A object in slot 0, and the count (as an Integer object) in slot 1.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 1:45 am 
Newbie

Joined: Sat Mar 25, 2006 3:50 am
Posts: 2
Thanks for the response. That definitely gets me partway there. I'll give it a shot.

I was hoping to be able to sort/paginate based on the count() as well. For example, the results may be shown in a UI paginated 5 at a time, with the count() value as a possible sort key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 5:37 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That syntax can be extended to include order by. Check out section 14.15, "Tips and Tricks", of the ref docs.


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.