-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL for related objects
PostPosted: Mon Jun 13, 2005 12:24 pm 
Hi,

I have an object VehicleJob which has a collection of Vehicle instances. Each Vehicle instance can be related to an Invoice object (but may also be null)

I need a query which gets all VehicleJob instances that have one or more Vehicles where Invoice is null.

I've tried the following but I am starting to get confused!

Code:
IQuery q = session.CreateQuery("from VehicleJob as j where exists (select elements(j.Vehicles) where j.Vehicles.Invoice is null)");


Can anyone help with the HQL?

Many thanks.


Top
  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 2:47 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
what about using my favorite interface ICriteria?

not tested but try something like:

Code:
IList vJobs = session.CreateCriteria(typeof(VehicleJob)
  .add(Expression.IsNull(Vehicle.Invoice))
  .list();


HTH,

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 4:12 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Hmm, how about this?
Code:
select job
from VehicleJob as job
    join job.Vehicles as vehicle
    left join vehicle.Invoice as invoice
where invoice is null

You may need to distinct-ify the results yourself though.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 8:45 am 
Sergey and Devonl,

Thanks very much for the help. I went with sergey's suggestion in the end and it worked brilliantly. Now starting to get my head around how HQL works.


Thanks.


Top
  
 
 Post subject: Re: HQL for related objects
PostPosted: Mon Mar 26, 2007 12:02 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 10:32 am
Posts: 34
Hi - (late coming, I know ...): Is anything wrong with the OP's HQL? IMHO, it should work -

SteveO wrote:
Hi,

I have an object VehicleJob which has a collection of Vehicle instances. Each Vehicle instance can be related to an Invoice object (but may also be null)

I need a query which gets all VehicleJob instances that have one or more Vehicles where Invoice is null.

I've tried the following but I am starting to get confused!

Code:
IQuery q = session.CreateQuery("from VehicleJob as j where exists (select elements(j.Vehicles) where j.Vehicles.Invoice is null)");


....


Regards
Harald M.


Top
 Profile  
 
 Post subject: Re: HQL for related objects
PostPosted: Mon Mar 26, 2007 12:29 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 10:32 am
Posts: 34
harald_m_mueller wrote:
Hi - (late coming, I know ...): Is anything wrong with the OP's HQL? IMHO, it should work -
Steve0 wrote:
IQuery q = session.CreateQuery("from VehicleJob as j where exists (select elements(j.Vehicles) where j.Vehicles.Invoice is null)");
...

...


Answering my own question: The following should work:

Code:
IQuery q = session.CreateQuery("from VehicleJob as j where exists (select elements(j.Vehicles) as v where v.Invoice is null)");


j.Vehicles in the original HQL cannot be continued with .Invoice, because it is a collection!

Harald M.


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