-->
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.  [ 7 posts ] 
Author Message
 Post subject: ICriteria fetch issue
PostPosted: Sun Mar 16, 2008 12:38 pm 
Newbie

Joined: Sat Sep 22, 2007 2:26 am
Posts: 4
I am a newbie at NHibernate. I am using version 1.2. I have a problem with using ICriteria.

I have two entity classes like this:

Code:
public class SubFirmObj
{ ... }

public class AnnouncementObj
{
   ...
   SubFirmObj SubFirm;
}


I can get AnnouncementObj data including their "SubFirms" with a "one-SQL-Select-Query" using "inner join fetch".

Code:
IQuery q = session.CreateQuery("select a from AnnouncementObj a inner join fetch a.SubFirm sf");
List<AnnouncementObj> list = (List<AnnouncementObj>)q.List<AnnouncementObj>;


But I cannot refactor my code to use ICriteria. Is it possible to fetch "SubFirms" with ICriteria.

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 16, 2008 1:25 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
Code:
IList<AnnouncementObj> list = session.CreateCritery(typeof(AnnouncementObj).SetFetchMode("SubFirm", FetchMode.Join).List<AnnouncementObj>();


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 16, 2008 1:49 pm 
Newbie

Joined: Sat Sep 22, 2007 2:26 am
Posts: 4
jta wrote:
Code:
IList<AnnouncementObj> list = session.CreateCritery(typeof(AnnouncementObj).SetFetchMode("SubFirm", FetchMode.Join).List<AnnouncementObj>();


Thanks for your reply.

I used some code like this code before, but I took this error:
The value "System.Object[]" is not of type "AnnouncementObj" and cannot be used in this generic collection.


ICriteria.List<T>() turns me not the type T (AnnouncementObj in this code), but it turns a list of System.Object array that includes AnnouncementObj and SubFirmObj.

It is strange. I am missing something, I think.

edit :

Oh sorry, I tried and saw that it worked for my question. Thanks a lot.
But I have a complex scenario. My code like this:

Code:
ICriteria c = session.CreateCriteria(typeof(AnnouncementObj));
//Add criteria expressions, bla bla...
c.SetProjection(Projections.Count("AnnouncementId"));
int rowCount = (int)c.List()[0];
c.SetProjection(null);

c.SetFetchMode("SubFirm", FetchMode.Join);
List<AnnouncementObj> = (List<AnnouncementObj>)c.List<AnnouncementObj>();


I need rowcount of my query for paging. If I comment this part, it works. But SetProjection (or maybe first "List") makes something wrong.

I think, I found the "strange" thing. Any ideas for this strange thing??


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 16, 2008 4:44 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
You want to get both the count of objects and the list??? If you fetch the list anyways why dont you just access the count property on the returned list?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 6:18 am 
Newbie

Joined: Sat Sep 22, 2007 2:26 am
Posts: 4
I get the paged data, for example first 10 object of the whole list. I also need the count of whole list.
I used two ICriteria objects and solved the problem. But as I said, it is strange. SetProjection causes something different, I think.
Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 6:21 am 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
Take a look at multicriteria then


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 7:07 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I suppose SetProjection(null) does not work. NHIbernate 2.0 will contain a CriteriaTransformer for that. Until then you can try something like that

http://forum.hibernate.org/viewtopic.php?t=984458&highlight=projectionlist

_________________
--Wolfgang


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