-->
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.  [ 6 posts ] 
Author Message
 Post subject: System.Object[]' does not contain a property
PostPosted: Thu Mar 29, 2007 8:38 pm 
Beginner
Beginner

Joined: Tue Feb 13, 2007 9:29 pm
Posts: 21
1.2 beta, sql server 2000.

Hi,

I'm building a forum and the requirement is for each discussion, there is a one-to-many relationships with comments. Here I'm trying to query a list of dicussion titles and the number of comments to each discussions

Code:

IQuery q = session.CreateQuery(
                      "select discussions.DiscussionID, discussions.DiscussionTitle , count(comments) "+
                      "from JumptreeForum_Discussions as discussions "+
                      "left outer join discussions.Comments as comments "+
                      "group by discussions.DiscussionID, discussions.DiscussionTitle" 



very simple. Now I'm trying to bind it to a repeater

Code:

<asp:Repeater ID="Test" runat="server">
           <ItemTemplate>
            <%#Eval("DiscussionID")%>
           </ItemTemplate>
       </asp:Repeater>




but i'm getting 'System.Object[]' does not contain a property with the name 'DiscussionID'.

here is the sql it generated

Code:

select jumptreefo0_.DiscussionID as x0_0_, jumptreefo0_.DiscussionTitle as x1_0_, count(comments1_.DiscussionCommentID) as x2_0_ from JumptreeForum_Discussions jumptreefo0_ left outer join JumptreeForum_DiscussionComments comments1_ on jumptreefo0_.DiscussionID=comments1_.DiscussionID group by  jumptreefo0_.DiscussionID , jumptreefo0_.DiscussionTitle



looks like hte column is changed to "x0_0_", so how can I properly bind the result to my Repeater?

*btw, used the genereated sql mannully to query the database, result all came out fine.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 9:13 pm 
Beginner
Beginner

Joined: Tue Feb 13, 2007 9:29 pm
Posts: 21
and btw, I did do the following, because i knew it worked.. i just don't know how to retreive the column values.

Code:

IList result = q.List();

Test.DataSource = result;
Test.DataBind();



Thanks again, somebody please help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 11:26 pm 
Beginner
Beginner

Joined: Tue Feb 13, 2007 9:29 pm
Posts: 21
Okay. I got it working, but VERY INEFFICIENT. There got be a better way to do this.

So just for the sake of to make it work at least, I modified the query to just return a list of "dicussions objects", instead of scalar values like in the previous post and bind it to a repeater

Code:

IList<JumptreeForum_Discussions> discussions = session.CreateQuery("from JumptreeForum_Discussions discussion").List<JumptreeForum_Discussions>();



as you can see, this way, each row of the repeater will be a "JumptreeForum_Discussion" object, no more array of objects (System.Object[])

and then my repeater simply do

Code:
<asp:Repeater ID="Test" runat="server">
           <ItemTemplate>
            <%#Eval("DiscussionID")%>/ <%#Eval("Comments.Count") %> <br />
           </ItemTemplate>
</asp:Repeater>


where "Comments" is a IList Comments that I have in my JumptreeForum_Discussions class. By doing the "Comments.Count", I'm actually lazy loading the comments collection for that dicussion, get everything back and do a count on the returned collection.

This is inefficent for sure. In my previous query, I got everything with 1 sql query that I can see in my log file.

In the current query, I have 20 discussions and each of them have, comments, because I'm lazy loading the "comments" when I want to do "Count" on them, I see 20 queries performed.

I mean I guess it's not that big of a deal if say I set the paging site to 50 or 100. But still, I must not be doing it right.

Somebody got any good suggestions?? I'm a newbie, Please help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 2:40 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
You have a couple of options here.
First understand that when you do an "select..." in HQL it is giving you back an array of Objects that you neeed to cast to their actual type before consuming them ... When you include the 'select' clause in HQL it think in terms of values and not objects.
For example you would Convert.Int32 your discussions.ID field you get back from object[0]. Remember that the Eval method in databinding is using reflection for a property name in the datasource....that name is obviously not present in a simple array of objects.
When you shifted to simply "from Jumptre..." it was returning OBJECTS back and not an object array (object[]) so at that point you were dealing with JumptreeForum_Discussions objects...reflection would find your property name and bind it accordingly in the Eval method so it worked.

If you need to optimize this, you can create a kind of Data Transfer Object that has just the properties you want to pull out of the db and use
Code:
"select new DTO(discussions.Id,select discussions.DiscussionID, discussions.DiscussionTitle , ..."

You'll need to inform NHibernate of this custom object using an <imports /> mapping.
NHibernate is smart enough to new the custom object you have and load it with the data.

Another option is to use Projections in the ICriteria space with a ResultTransformer. With these you can pull 'project' just the properties you need from an object and transform the results into a custom object like the above HQL.
Do a search in the online docs for 'Projections' and that should get you pointed in the right direction. Also look at the unit tests for Projections (under Expression) for usage examples.
Hope this helps...

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 11:35 am 
Beginner
Beginner

Joined: Tue Feb 13, 2007 9:29 pm
Posts: 21
Hi mnichols,

That's really helpful, I finally understood, it totally makes sense.

I like the DataTransfer object idea, sounds simple enough, I guess in that case, I'll just have to create DataTransfer object whenever I need only several columns from this and that tables.

Projection is a bit harder for me to undersatnd, I'll definitly study on it.

Thanks again for your extremely helpful suggestions, truely appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 1:19 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Glad to help
MIKE

_________________
If this helped...please remember to rate it!


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