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.  [ 2 posts ] 
Author Message
 Post subject: Simple report query - No table, no fields
PostPosted: Wed Nov 21, 2007 9:28 pm 
Newbie

Joined: Wed Jan 31, 2007 10:20 pm
Posts: 4
This is really basic and I have read the million warnings along the lines of "read the docs", "learn the basics", "search the forums" etc and I've still come up short after several hours now of trying to do what is seemingly a simple task. The problem is, as someone new to NHibernate, I'm still not sure of what terminology to search for hence it being hard to find what i'm after. So, in summary, apologies for the stupid post...

I'm using NHibernate for usual CRUD actions without problem and very much enjoying it. However, for one page on my asp.net site I simply want to show a very basic gridview with some summary, read-only data on it (a list of dates and count of requests per date). I'm using CreateSqlQuery to get the data I need (which through the debugger I can see is working) but I've no idea how to bind that IList to my gridview because I can't seem to map the objects returned in the IList to anything. Normally I would just use the standard dataReader but I'd prefer to stick with NHibernate.

This is what I'm returning:


Code:
public System.Collections.IList GetSummary()
        {
            IList requestsList;
            using(ISession session = factory.OpenSession())
            {
                requestsList = session.CreateSQLQuery("SELECT CONVERT(char(10), request_date, 120) as req_date, sum(request_id) as req_count FROM tbl_request GROUP BY CONVERT(char(10), request_date, 120)")
                    .AddScalar("req_date", NHibernateUtil.Date)
                    .AddScalar("req_count", NHibernateUtil.Int64).List();
            }
            return requestsList;
        }


If I try to just bind that IList to my gridview I see all the object array properties (IsReadOnly, IsFixedLength etc) in each row which is obviously not what i'm after.

If I try to iterate the object arrays in the IList and convert to anything I get "Cannot apply indexing with [] to an expression of type 'object'" restricting me reaching the values i'm after (admitidly this is more a limitation of my .net skills - but I dont feel this is the 'Nhibernate' way regardless).

As I see it, the problem is I don't have a class for each row of the data returned, which I could easily do but then I have no idea how I would map that class because there is nothing in the database to map it to that reflects what it represents (I.e. a date and a count of requests that occured on that date). Which is why i used a custom query in the first place.

Any help, even a link to the right place in the docs would be very much appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 10:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
The query you issued would have returned an IList of Object arrays (i.e. object[]). So writing requestsList[0][0] and requestsList[0][1] will give you the req_date and req_count from the first row. Search for "report query" or "reporting query" and you will find some information.

_________________
Karl Chu


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