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: Fri Aug 15, 2008 6:50 am 
Newbie

Joined: Fri Aug 15, 2008 6:47 am
Posts: 6
Hey all,

Got a query about a query :). I have a structure like so:

Product Groups
Product Categories
Products

A Product group has many categories and a category many products. I am trying to select a list of groups where products match a specific criteria. This query works for me:

var groups = session.CreateCriteria(typeof (Model.ProductGroup))
.CreateCriteria("ProductCategories")
.CreateCriteria("Products", "p")
.Add(new GtExpression("Nitrogen", 10D))
.List <Model.ProductGroup>();


The issue is I would like the producs to be filtered by this criteria, at the moment all products will be returned below a groups categories, products collection. Am I trying to do something that isnt possible or is there a solution?



Any help would be appreciated.



Cheers
Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 8:09 am 
Newbie

Joined: Fri Aug 15, 2008 6:47 am
Posts: 6
Is this just me being stupid? I am starting to think what I am trying to do might not work?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 11:35 pm 
Newbie

Joined: Fri Aug 15, 2008 6:47 am
Posts: 6
Anybody got any ideas for me?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 12:43 am 
Beginner
Beginner

Joined: Fri Jan 12, 2007 1:08 am
Posts: 41
You need to navigating the relationships between your entities:

Code:
var groups = session.CreateCriteria(typeof (Model.ProductGroup), "pg")
  .CreateAlias("pg.ProductCategories", "pc")
  .CreateAlias("pc.Products", "p")
  .Add(new GtExpression("p.Nitrogen", 10D))
  .List <Model.ProductGroup>();

If you are using field mappings for your collections then you'll need to refer to the field because NHibernate does not know about the properties:

Code:
var groups = session.CreateCriteria(typeof (Model.ProductGroup), "pg")
  .CreateAlias("pg._productCategories", "pc")
  .CreateAlias("pc._products", "p")
  .Add(new GtExpression("p.Nitrogen", 10D))
  .List <Model.ProductGroup>();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 1:15 am 
Newbie

Joined: Fri Aug 15, 2008 6:47 am
Posts: 6
jagid wrote:
You need to navigating the relationships between your entities:

Code:
var groups = session.CreateCriteria(typeof (Model.ProductGroup), "pg")
  .CreateAlias("pg.ProductCategories", "pc")
  .CreateAlias("pc.Products", "p")
  .Add(new GtExpression("p.Nitrogen", 10D))
  .List <Model.ProductGroup>();

If you are using field mappings for your collections then you'll need to refer to the field because NHibernate does not know about the properties:

Code:
var groups = session.CreateCriteria(typeof (Model.ProductGroup), "pg")
  .CreateAlias("pg._productCategories", "pc")
  .CreateAlias("pc._products", "p")
  .Add(new GtExpression("p.Nitrogen", 10D))
  .List <Model.ProductGroup>();


Thanks, I have worked this out, the trick was create alias and joins:

.CreateAlias("ProductCategories", "pc", JoinType.InnerJoin)
.CreateAlias("pc.Products", "p", JoinType.LeftOuterJoin)

Then set the result transformer to be unique:

.SetResultTransformer(new DistinctRootEntityResultTransformer());

And it all works :). I am starting to like nHibernate now. Once I figure these things out.


Cheers
Stefan


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.