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: [2.0] left join create separate queries
PostPosted: Fri Jul 04, 2008 10:21 am 
Newbie

Joined: Wed Jul 02, 2008 7:54 am
Posts: 8
Location: Brussels
Hello,

I would like to know how is it possible to prevent the fact to create a new query for each left join in my query?

Code:
sess.CreateQuery(String.Format(@"select cust from TrasysGR.MDP.DomainModel.Customer cust
                        left outer join cust.ClassB
                        left outer join cust.ClassC
                        inner join fetch cust.Addresses ad
                        where cust.Id = {0}
                        and ad.Valid = 't'
                        ", id) ).List<Customer>()


For that NHibernate Create 3 queries. One for Address, one for ClassB and another one for ClassC.

That's unuseful. I want an only query.

And if I try to put left outer join, NHibernate says to me that not possible to put severals fetch in one query... Why?

Sorry to ask so many questions but I'm a beginner

Thank you[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 11:12 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try "fetch" on all joins:

Code:
sess.CreateQuery(String.Format(@"select cust from TrasysGR.MDP.DomainModel.Customer cust
                        left outer join fetch cust.ClassB
                        left outer join fetch cust.ClassC
                        inner join fetch cust.Addresses ad
                        where cust.Id = {0}
                        and ad.Valid = 't'
                        ", id) ).List<Customer>()


But I found this in the docs:

Quote:
Note that, in the current implementation, only one collection role may be fetched in a query (everything else
would be non-performant).


So I assume, it's not possible to get this in a single select.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 11:17 am 
Newbie

Joined: Wed Jul 02, 2008 7:54 am
Posts: 8
Location: Brussels
Oki I understand now why there're saverals queries.

And I decided to use criterias because I couldn't use fetch in my CreateQuery

Code:
Customer customerWithData = sess.CreateCriteria(typeof(Customer))
                        .SetFetchMode("ClassB", FetchMode.Eager)
                        .SetFetchMode("ClassC", FetchMode.Eager)
                        .SetFetchMode("Addresses", FetchMode.Eager)
                        // .CreateAlias("Addresses", "add")
                        .Add(Expression.Eq("Id", id))
                        //.Add(Expression.Eq("add.Valid", 't'))
                        .List<Customer>()[0];


But I don't know to load only a part of a collection. For exemple I want to only load valids addresses. I tried to use an alias and use the alias with the expression like I let in comment.

So for the moment I load every adresse. If someone can help it'll be great

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 11:25 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have a look at

Quote:
http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/querycriteria.html#querycriteria-associations


especially the end of that paragraph:

Quote:
Note that the kittens collections held by the Cat instances returned by the previous two queries are not pre-filtered by the criteria! If you wish to retrieve just the kittens that match the criteria, you must use SetResultTransformer(CriteriaUtil.AliasToEntityMap).


Try and search the forum for AliasToEntityMap, I can't help you there.

_________________
--Wolfgang


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.