-->
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: Fetchmode.Join not working for me
PostPosted: Fri Apr 14, 2006 3:13 pm 
Newbie

Joined: Fri Apr 14, 2006 3:04 pm
Posts: 10
Newbie to NHibernate...hopefully someone can help me out here?

I'm trying to generate some results using the criteria API instead of HQL.

Basically, I'm trying to generate this SQL:
SELECT t1.something, t1.something2
FROM Table1 t1 LEFT OUTER JOIN Table2 t2 ON t1.SomeID = t2.SomeID
WHERE t2.something IS NULL

Using this...

IList results = null;

ISession session = factory.OpenSession();
results = session.CreateCriteria(typeof(Contact))
.SetFetchMode("ContactDetails", FetchMode.Join)
.CreateCriteria("ContactDetails", "CD")
.Add(new NullExpression("ContactDetailName"))
.SetResultTransformer(CriteriaUtil.AliasToEntityMap)
.List();

It produces the query I want, but with an INNER JOIN instead of a LEFT JOIN (which does not help me). I've tried a couple of different things but with no success. Help?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 15, 2006 7:25 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Try SetFetchMode("CD", FetchMode.Join) - you have to use the same alias that you use in CreateCriteria.


Top
 Profile  
 
 Post subject: Thanks...but still not working
PostPosted: Mon Apr 17, 2006 12:40 am 
Newbie

Joined: Fri Apr 14, 2006 3:04 pm
Posts: 10
Hi Sergey,

Thanks for the reply, but unfortunately it still doesn't work using the alias. I also tried using the overload of CreateCriteria that doesn't set an alias, and using the full association path in SetFetchMode, and that didn't work either - any other ideas?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 4:19 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
Hey there,

Bellow are my comments on your problem:

1. My opinion is that there is now way you can apply fetch join to an association that you also intend to filter, in your case:

.SetFetchMode("ContactDetails", FetchMode.Join)

and

.CreateCriteria("ContactDetails", "CD")
.Add(new NullExpression("ContactDetailName"))

(although I maybe mistaken on this one, but I found this by trial and error)

2. With criteria there is now way you can generate left join's

3. You can do it either via HQL or qith a custom SQL expression in criteria:

session.CreateCriteria(typeof(Contact))
.SetFetchMode("ContactDetails", FetchMode.Join)
.Add( Expression.Sql("(select count(details.id) from details where details.contactid = {alias}.contactid) = 0 ") )

_________________
Dragos


Top
 Profile  
 
 Post subject: RE: Fetchmode.Join not working for me
PostPosted: Mon Apr 17, 2006 1:29 pm 
Newbie

Joined: Fri Apr 14, 2006 3:04 pm
Posts: 10
Thanks Dragos.

I got it to work, not exactly as you have shown but along the same lines...

I will have to admit that I'm a bit disappointed that I had to use a SQL expression to accomplish what I was trying to do, as my original intent was to abstract away the db details. With this solution, intimate knowledge of the db is still required. Hum. Hopefully as I get into NHibernate more...other solutions will present themselves to me.

CC


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 10:30 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
You're right and wrong at the same time.

First of all this could have been done in HQL (I like it better than criteria).

Second, with a little bit of work you can still abstract the database. Use the NH configuration and the factory objects you can get all the needed info for building the SQL (tables, dialect) and you can wrap the needed logic for some widely used types of sql expressions in classes that inherit from SQLExpression or Expression or create a SQLExpression factory :).

When there's a will, there's a way...

_________________
Dragos


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.