-->
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.  [ 11 posts ] 
Author Message
 Post subject: Problem with joins
PostPosted: Thu Jan 26, 2006 10:57 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
Using nHibernate 1.01 in Visual Studio 2003 .net 1.1

I am using nHibernate in my application, but i just wanted to asked if there was a more effiect way of accessing the data than i have been doing.


Example of what im going:

I have four objects that are utilizing NHibernate Fair, Stand and Exhibitor and User

There are multiple Fairs and each Fair contains multiple Stands and each Stand contains one Exhibitor and each Exhibitor contains one user.

And what i want to do is given a User Id find all the Fairs they have a stand at.

So i have written this piece of code to do it:

IList l = fairdao.getFairs(); //calls a named query that get all fairs
IList attendedFairs = new ArrayList();

foreach(Fair f in l)
{
IList stands = f.Stands;

foreach(Stand s in stands)
{
Exhibitor ex = s.Exhibitor;

if(ex.User.Id==1)
{
attendedFairs.Add(f);
break;

}

}

}


While this does the job, it just appears to me the i could utilize hibernates functionality better. Could you give me afew pointers. Thanks

Iv tried this:

<query name="Fair.GetAttending">from Fair fair inner join fair.Stands stands where stands.Exhibitor.VcfUser.Id = ?</query>

But this returns a nasty list of fairs and stands.....how do i get this to only returns fairs?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 11:10 am 
Newbie

Joined: Wed Jan 25, 2006 3:09 am
Posts: 10
hi bosh,

ah.. the Fair[] collection.. yeah that sounds familiar... this happens when
the query returns more than one type of object, sorry my fault... :/

add: 'select fair' to the query and your results will only contain the 'Fairs'.

Code:
select fair from Fair fair inner join fair.Stands stands where stands.Exhibitor.User.Id = :userid


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 1:02 pm 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
How about the opposite of this?

Get all fairs
where for all stands the Exhibitor.User.Id is not qual to ?

I was going to try:

select fair from Fair fair inner join fair.Stands stands where stands.Exhibitor.User.Id != ?

But this will bring back any fair where any one of the users is not the id....i want all the users to not have the id


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 8:37 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
Hi guys.

Can you shed any light on this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 9:27 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
use the elements( ) keyword


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 10:06 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
Im struggling.....this dont seem to work..

select fair from Fair fair inner join fair.Stands stands where elements(stands.Exhibitor.VcfUser.Id) != 21


produces this error:

NHibernate.QueryException: dereferenced: Id [select fair from vcf.code.entites.Fair fair inner join fair.Stands stands where elements(stands.Exhibitor.VcfUser.Id) != 21]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 10:27 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
Elements receives an association path ending with a collection (not a property as you used it with). AFAIK this situation of yours is solved in a more SQL like way:

from parent
where exists( select child.id from child where child.parentid = parent.id and
child.someprop.id != 21)

another way would be using ICriteria (doesn't really apply to you case but is realated).

criteria = CreateCriteria(Parent)
subc = criteria.CreateAlias("ChildList", "c"))
.Add( Expression.Eq( "c.x.y.z", 1 ) )

criteria.SetResultTransformer( CriteriaUtil.DistinctRootEntity ).List();

Dragos


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 10:32 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
dragos, could you elaborate on the 'setresulttransformer' ? I've never seen it mentioned in any documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 11:22 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
Thanks for the help....im probs being dumb, coz i dont fully see how this resolves my problem...

I tried this initially, and it returned nothing:


Code:
from Fair fair
where not exists( select stand from Stand stand where stand.Fair.Code = fair.id and stand.Exhibitor.VcfUser.id = 21)



Was as this works:

Code:
from Fair fair
where exists( select stand from Stand stand where stand.Fair.Code = fair.id and stand.Exhibitor.VcfUser.id != 21)



What is the difference? usrly they work out as the same thing?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 12:30 pm 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
OK, so you want all fairs where a specific exhibitor did not participate:

The following should bring you all fairs for which no stand exists having the user x.

from Fair fair
where not exists( select stand from Stand stand where stand.Fair.Code = fair.id and stand.Exhibitor.VcfUser.id = 21)

The following will bring you fairs where there is at least another participant other than your exhibitor:

from Fair fair
where exists( select stand from Stand stand where stand.Fair.Code = fair.id and stand.Exhibitor.VcfUser.id != 21)

About setresulttransformer, it'a a method on ICriteria that transforms the result set. By design, ICriteria generated SQL so works with cartesian products. The result transformers get the query result as input and modify information according to some rule.

For example
select f from Fair inner f join f.stands
returns an IList with the same fair appearing multiple times. Using DistinctRootEntity you force NHibernate to remove duplicates fairs from the result retrieving something like distinct fairs.

Hope it helps


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 12:40 pm 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
dado2003 wrote:
About setresulttransformer, it'a a method on ICriteria that transforms the result set. By design, ICriteria generated SQL so works with cartesian products. The result transformers get the query result as input and modify information according to some rule.

For example
select f from Fair inner f join f.stands
returns an IList with the same fair appearing multiple times. Using DistinctRootEntity you force NHibernate to remove duplicates fairs from the result retrieving something like distinct fairs.

Hope it helps


haha, thanks for that information. I had built my own helper functions for that!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.