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.  [ 5 posts ] 
Author Message
 Post subject: Query question
PostPosted: Thu Sep 04, 2008 1:51 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
I have a parent entity with a collection of details.

Parent
Code:
public class GeneratingUnit : DomainObject<int>
    {
        private string name;
        private string fullName;
        private Counterparty counterparty;
        private decimal productionCapacity;
        private IList<GeneratingUnitProduction> production =
                         new List<GeneratingUnitProduction>();
...
}



Child
Code:
public class GeneratingUnitProduction : DomainObject<int>
    {
        private GeneratingUnit generatingUnit;
        private DateTime flowDate;
   ...
}


I want to write a NHibernate query that will return me all the GeneratingUnits that do not have any production for a given flowDate.

Can someone demonstrate an example of this query...?

A equivalent query in SQL would like something like:
Code:
SELECT *
  FROM Generating_Unit
where generating_unit_id NOT IN
       (
        SELECT generating_unit_id
          FROM generating_unit_production
         WHERE Flow_Date = :flowDate
       )


Thanks in advance...
Greg


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 8:58 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
I found a solution to my query problem...

Here it is:

Code:
DetachedCriteria subQuery =
                DetachedCriteria.For(typeof (GeneratingUnit)).SetProjection(Projections.Id()).CreateCriteria(
                    "Production").Add(Restrictions.Eq("FlowDate", flowDate));
            return
                new List<GeneratingUnit>(
                    Session.CreateCriteria(typeof (GeneratingUnit))
                    .Add(Subqueries.PropertyNotIn("Id", subQuery))
                    .List<GeneratingUnit>());


If anyone has a more elegant solution please let me know.

Greg


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 8:59 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
I found a solution to my query problem...

Here it is:

Code:
DetachedCriteria subQuery =
                DetachedCriteria.For(typeof (GeneratingUnit)).SetProjection(Projections.Id()).CreateCriteria(
                    "Production").Add(Restrictions.Eq("FlowDate", flowDate));
            return
                new List<GeneratingUnit>(
                    Session.CreateCriteria(typeof (GeneratingUnit))
                    .Add(Subqueries.PropertyNotIn("Id", subQuery))
                    .List<GeneratingUnit>());


If anyone has a more elegant solution please let me know.

Greg


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 9:02 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
I found a solution to my query problem...

Here it is:

Code:
DetachedCriteria subQuery =
    DetachedCriteria.For(typeof (GeneratingUnit))
          .SetProjection(Projections.Id())
          .CreateCriteria("Production")
              .Add(Restrictions.Eq("FlowDate", flowDate));

return new List<GeneratingUnit>(
    Session.CreateCriteria(typeof (GeneratingUnit))
          .Add(Subqueries.PropertyNotIn("Id", subQuery))
         .List<GeneratingUnit>());


If anyone has a more elegant solution please let me know.

Greg


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 9:06 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
sorry for the duplicate posts... Firebox borked out on me :(


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