-->
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.  [ 10 posts ] 
Author Message
 Post subject: Group by statement in nhibernate
PostPosted: Wed Jun 24, 2009 8:23 pm 
Newbie

Joined: Wed Jun 24, 2009 8:13 pm
Posts: 12
Code:
public IList<SearchQueries> GetTopSearches()
        {
            return NHibernateSession.CreateCriteria(typeof(SearchQueries))
                .SetProjection(Projections.ProjectionList()
                .Add(Projections.RowCount(), "catCountQuery")
                .Add(Projections.GroupProperty("Query,")))
                .AddOrder(Order.Desc("catCountQuery"))
                .List<SearchQueries>();
        }



but i get error

The value "System.Object[]" is not of type "WebCrawler.Core.Entities.SearchQueries" and cannot be used in this generic collection.
Parameter name: value

i want to use group by Query statement so i use .Add(Projections.GroupProperty("Query,"))).

I want SQL like:
Select COUNT(Query) AS catCountQuery FROM SearchQueries GROUP BY Query DESC

What ia m doing wrong, thx

_________________
http://www.dostavahrane.si


Top
 Profile  
 
 Post subject: Re: Group by statement in nhibernate
PostPosted: Thu Jun 25, 2009 1:20 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
.List<SearchQueries>();

expects that the query returns entities of this type. Your query returns an int (or maybe a long, not sure). Try this instead:

.List<int>();

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Group by statement in nhibernate
PostPosted: Thu Jun 25, 2009 3:46 am 
Newbie

Joined: Wed Jun 24, 2009 8:13 pm
Posts: 12
Hi,

thx for your answer but i think that must be SearchQueries becouse now i get

Error 1 Cannot implicitly convert type 'System.Collections.Generic.IList<int>' to 'System.Collections.Generic.IList<WebCrawler.Core.Entities.SearchQueries>'. An explicit conversion exists (are you missing a cast?) D:\PROJEKTI\crawler\WebCrawlerSuite\WebCrawler.Data\SearchQueriesDao.cs 29 17 WebCrawler.Data

_________________
http://www.dostavahrane.si


Top
 Profile  
 
 Post subject: Re: Group by statement in nhibernate
PostPosted: Thu Jun 25, 2009 4:04 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Aarg, sorry, you don't only get a single value, you get an array:

NHibernateSession.CreateCriteria(typeof(SearchQueries))
.SetProjection(Projections.ProjectionList()
.Add(Projections.RowCount(), "catCountQuery")
.Add(Projections.GroupProperty("Query,")))
.AddOrder(Order.Desc("catCountQuery"))

will return an array { catCountQuery, Query }. You can't cast that into an IList<SearchQueries>.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Group by statement in nhibernate
PostPosted: Thu Jun 25, 2009 4:10 am 
Newbie

Joined: Wed Jun 24, 2009 8:13 pm
Posts: 12
hmm so how must whole method look like now?

I have IList becouse there is many results not only one...

_________________
http://www.dostavahrane.si


Top
 Profile  
 
 Post subject: Re: Group by statement in nhibernate
PostPosted: Thu Jun 25, 2009 4:23 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Depends on what you want to get ? Your query returns a count per query, so you can either build a class for that or return a list of object[].

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Group by statement in nhibernate
PostPosted: Thu Jun 25, 2009 5:00 am 
Newbie

Joined: Wed Jun 24, 2009 8:13 pm
Posts: 12
yes i want to make list of objects but you said that i can not use .List<SearchQueries>(); in my method. Now i am little confused what to do...

_________________
http://www.dostavahrane.si


Top
 Profile  
 
 Post subject: Re: Group by statement in nhibernate
PostPosted: Thu Jun 25, 2009 5:08 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You want the SearchQueries plus their execution count ??

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Group by statement in nhibernate
PostPosted: Thu Jun 25, 2009 8:12 am 
Newbie

Joined: Wed Jun 24, 2009 8:13 pm
Posts: 12
yes but if this is to complicated just group by statement.

I want this
Select COUNT(Query) AS catCountQuery FROM SearchQueries GROUP BY Query DESC

but if is to complicated then

Select * FROM SearchQueries GROUP BY Query DESC

but i don't know what is wrong with my method that does not working...

_________________
http://www.dostavahrane.si


Top
 Profile  
 
 Post subject: Re: Group by statement in nhibernate
PostPosted: Thu Jun 25, 2009 10:32 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
That statement is not the problem:

Select COUNT(Query) AS catCountQuery FROM SearchQueries GROUP BY Query DESC

but you only get the count here, no objects. If you want the object plus the count, the statement will be much more complex (even the sql). And regardless how you write the statement, you will never get a result like List<SearchQueries> because the object and the count are treated as two "objects".

You can try this HBM query, but I'm not sure if a subselect in the select part is supported:

session.CreateQuery("select s1, (select count(*) from SearchQueries s2 where s2.Query = s1.Query) as catCountQuery from SearchQueries s1").List();

_________________
--Wolfgang


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