-->
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: Complex Criteria query
PostPosted: Thu May 31, 2007 11:52 am 
Newbie

Joined: Thu May 31, 2007 10:51 am
Posts: 8
Location: Edinburgh, UK
Hi there,

I am really struggling to create a Criteria based query in NHibernate.

In simplistic model of my domain I have:

Order (maps to table Orders) object

Order has a one-to-many relationship with OrderItem (maps to OrderItems table) and is mapped as a bag property: Items.

Each OrderItems has a one-to-many relationship with Seller (maps to Sellers) and a mapped proerty of Owner

I need to write a query that says "Give me all the Orders that contain items sold by :SellerID"

Depending on user inputs I want to then add further criteria such as a date range.

This is what I have:

Code:
ICriteria crit = session.CreateCriteria(typeof(Order));

crit.CreateCriteria("Items").CreateCriteria("Owner").Add(Expression.IdEq(sellerID));

...


However this query is highly inefficient, and often timeout my sql server db locally.

Can anyone help?

Many thanks for your help in answering what is surely a simple question.

Regards, Richard


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 12:40 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
You may need some indexes on your DB, however you might also want to try breaking some of your query out into a subquery to prevent large joins across some tables.

I find that sometimes joins across large tables in our DB (some with >40 million records) that the query optimizer doesn't come up with good plans for joins and executing subqueries is more efficient.

Anyway, the answer to your question is going to be DB dependent I suspect.

Cheers,

Symon.


Top
 Profile  
 
 Post subject: Complex Criteria query
PostPosted: Thu May 31, 2007 1:13 pm 
Newbie

Joined: Thu May 31, 2007 10:51 am
Posts: 8
Location: Edinburgh, UK
Hi,

Thanks for your comments merge_s.rottem I will look into your selections.

Looking a this query further I have realised that the query is requesting duplicate Orders - ie is getting an Order for every OrderItem.

The SQL equivalent for what I want is

Code:
select distinct * from Orders where ID (Select OrderID from OrderItems where SellerId =:sellerID)


The extra criteria comes in because NHibernate uses the SellerID db column to map the Seller object to the Owner property of OrderItem.

Regards


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.