-->
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: ICriteria, additional restriction in JOIN clause
PostPosted: Tue Oct 21, 2008 8:46 am 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
I've a simple question on how to get to something, but I do not see how I can do it, so therefore, I open this topic. :)

What I have, is 2 classes which map to 2 tables in my DB. These 2 tables have a one to many relationship:

Table : Prestation (PrestationId, Code, ...)
Table : PrestationName (PrestationNameId, PrestationId, Language, Description)

As you can see, a Prestation can have zero to multiple names. Each name is in a certain language.

My objects are very similar:
I have a Prestation class which has a set of PrestationNames.

Now, I want to retrieve an overview of all the Prestations I have, and I want to display them with only one name, which should be in a language that I define.

I do this using Projections & ResultTransformers:
Code:
ICriteria crit = session.CreateCriteria(typeof(Prestation));

crit.CreateAlias ("Names", "n", JoinType.LeftOuterJoin).
    Add(Restrictions.Eq("n.Language", somelanguage));

crit.SetProjection ( .... );

crit.SetResultTransformer(Transformers.AliasToBean ( ... ));

return crit.List<PrestationView>();

Now, this all works fine in most of the situations. The above ICriteria results in the follwing query:
Code:
select ....
FROM Prestation
LEFT OUTER JOIN PrestationName ON Prestation.Id = PrestationName.PrestationId
WHERE PrestationName.Language = 1

However, this gives a little problem when I have a Prestation which does not have a Name for languagecode=1: this prestation is not present in my result-set, but I do want to have it present, be it with an empty name.

So, in fact, the SQL query that I want NHIbernate to generate for me, would be :
Code:
select ...
FROM Prestation
LEFT OUTER JOIN PrestationName n ON Prestation.Id  = n.PrestationId AND n.Language = 1

(Note that I filter in the JOIN clause, instead of in the WHERE clause).

Is there any way or possibility to do this using an ICriteria query ? And if so, how can i do this ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2008 10:41 am 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
Is this not possible, or does nobody have an idea how i could ressolve this ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2008 11:10 am 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
I was just browsing thru the Hibernate reference manual, and I saw that you have the possiblity to specify extra join conditions in HQL using the 'with' keyword.
So, I was going to try this, but NHibernate throws an exception saying that he doesn't know the with keyword ... ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 8:51 am 
Newbie

Joined: Sun Sep 21, 2008 4:10 pm
Posts: 3
Indeed, supplying additional conditions in join would make NHibernate much more usable


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.