-->
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: Criteria query for subclasses in collection
PostPosted: Mon Sep 27, 2010 11:49 am 
Newbie

Joined: Mon Sep 27, 2010 11:11 am
Posts: 2
I'm trying to build a Criteria query, but I'm having some difficulty.

In my system, a patient Case contains a Diagnostic, and each Diagnostic contains one or more MaladyDiagnostic. A MaladyDiagnostic can be any of several subclasses (let's call them MaladyDiagnosticA, MaladyDiagnosticB, and MaladyDiagnosticC, all of which extend MaladyDiagnostic).

The gist of my query is that I want to find all Cases that have a MaladyDiagnosticA in their Diagnostic's collection of MaladyDiagnostics.

Here's my (failed) attempt:

Code:
subQuery = DetachedCriteria.forClass(MaladyDiagnosticA.class);
subQuery.setProjection(Projections.id());

subQuery2 = DetachedCriteria.forClass(Diagnostic.class);
subQuery2.createAlias("maladyDiagnoses", "m");
subQuery2.add(Property.forName("m.id").in(subQuery));
subQuery2.setProjection(Projections.id());

c = session.createCriteria(Case.class);
c.createCriteria("diagnostic");
c.add(Property.forName("id").in(subQuery2));


In addition to probably being horribly inefficient, the above doesn't work. I always get zero rows returned, even though there are Cases in the test system that have a Diagnostic that contains a MaladyDiagnosisA. I'm quite new to Hibernate's Criteria API, and simply haven't been able to coax it to work. Any help would be greatly appreciated!


Top
 Profile  
 
 Post subject: Re: Criteria query for subclasses in collection
PostPosted: Mon Sep 27, 2010 12:53 pm 
Newbie

Joined: Mon Sep 27, 2010 4:23 am
Posts: 2
Projections.id() return a IdentifierProjection instance and I think you need a list of your identifier type. Why don't you try with subQuery.setProjection(Projections.property("<id_property>")); Then you need catch them into a list, not into a query. And you can have problems if the list is empty.

I hope you have lucky, and so sorry for my english level ;)


Top
 Profile  
 
 Post subject: Re: Criteria query for subclasses in collection
PostPosted: Tue Sep 28, 2010 11:22 am 
Newbie

Joined: Mon Sep 27, 2010 11:11 am
Posts: 2
Embarrassingly enough, it turns out my test data wasn't correct after all, so all my queries were returning 0 results. Fixing my test data solved my problems. Sorry for using up your time!


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.