-->
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 for subclass attributes
PostPosted: Tue Nov 25, 2003 7:33 pm 
Newbie

Joined: Thu Oct 30, 2003 5:38 pm
Posts: 9
Location: CA
I have a small class hierarchy and am having trouble creating HQL to derive specific queries. Maybe someone can shed some light on my problem.

Here's the class hierarchy:

abstract class: ModelObject
- timestamp : Calendar

abstract class Event extends ModelObject
- sourceId : String

class Alarm extends Event .... class Incident extends Event
- acknowledged : Boolean .... - confirmed : Boolean

For instance when I have 4 instances of Incident and 0 instances of Alarm in the DB I can't seem to get the proper result set when I query using both subclass attributes.

Here are some query strings that I'm using with the session.createQuery() as well as psuedo-syntax on the setParameter() calls and my results:

queryString: "select distinct event from Event event where event.timestamp< :nowTimestamp"
hibernateQuery.setParameter( "nowTimestamp", java.util.GregorianCalendar)
INFO: 4 results from query ==> this is correct

queryString: select distinct event from Event event, Incident incident where incident.confirmed = :false"
hibernateQuery.setParameter( "false", Boolean(false) )
INFO: 4 results from query ==> this is correct

queryString: "select distinct event from Event event, Alarm alarm, Incident incident where (alarm=event and alarm.acknowledged=:false) or (incident=event and incident.confirmed = :false)"
hibernateQuery.setParameter( "false", Boolean(false) )
No results from query ==> this is NOT CORRECT (or at least not what I want)

queryString: "select distinct event from Event event, Alarm alarm, Incident incident where (event.timestamp < :nowTimestamp and (alarm=event and alarm.acknowledged = :false) or (incident=event and incident.confirmed = :false))"
hibernateQuery.setParameter( "nowTimestamp", java.util.GregorianCalendar)
hibernateQuery.setParameter( "false", Boolean(false) )
INFO: No results from query ==> this is NOT CORRECT

I've tried a number of other query strings, but have not come up with anything that works as I want under these minimal unit test conditions.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 2:11 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is expected since SQL generated will do a cartesian product. Castesian product with an empty table will return nothing.

Even if you had stuffs in Alarm, this is highly unefficient since the castesian product can be huge (nb of line of event * nb of line of incident * nb of line of alarm).

Sub query and in would solve your pb I guess

from event as event where event in (<sub querywith restrictions>) or (other sub query with restiction)

Code:
from Event event, Alarm alarm where event in (from Incident incident where (incident=event and incident.confirmed = :false) or event in (from Alarm alarm where (alarm=event and alarm.acknowledged=:false)"

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 4:33 pm 
Newbie

Joined: Thu Oct 30, 2003 5:38 pm
Posts: 9
Location: CA
Your answer makes sense to me, but when I tried it I still get no result set.
My new query string was:

"select distinct event from Event event, Alarm alarm, Incident incident where event in ((from Incident incident where (incident=event and incident.confirmed = :false))) or event in ((from Alarm alarm where (alarm=event and alarm.acknowledged = :false)))"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 4:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
"select distinct event from Event event where event in ((from Incident incident where (incident=event and incident.confirmed = :false))) or event in ((from Alarm alarm where (alarm=event and alarm.acknowledged = :false)))"


No cartesian product anymore.

_________________
Emmanuel


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.