I'm back ... with similar problems ... :D
From previous discussion about the same subject, I have one tiny curiosity. I think I'll post some codes below.
Please assume that these codes below is executed after a successful configuration of NHibernate and opening a session.
Code:
lst = session.Find("SELECT q FROM Question q join fetch q.Asker_ID m");
this.dgTest.DataSource = lst;
this.dgTest.DataBind();
Question is a persitent object having a many-to-one relationship with Member (the foreign key is Asker_ID)
Note:
- lst is an IList
- dgTest is a DataGrid; the columns are auto-generated
The code resulted in showing ALL (sorry about the caps) properties of each Questions posted by the corresponding Member, except for the foreign key, i.e. Asker_ID.
Next, I modified the code so the DataGrid will only show me the Subject of each Questions.
Code:
lst = session.Find("SELECT q.Question_Subject FROM Question q join fetch q.Asker_ID m");
this.dgTest.DataSource = lst;
this.dgTest.DataBind();
It's a clear success. But ... here comes the first Question.
The second question won't be a problem in an auto-generate-column DataGrid. But what if you want to add a BoundColumn. How do you refer to the "q.Question_Subject" for the Data Field?
The obvious work-around would be selecting only "q", then add "Question_Subject" as the Data Field of the bound column.
Why am I asking this, because I believe it took more resource to get all the properties of a persistent object from the Database instead of just one column. Any comment on this?
Next, I modified the code again. This time I want the DataGrid to show me the ID of each Questions and the ID of the Member who posts the Question.
Here's my code:
Code:
lst = session.Find("SELECT q.ID, m.User_ID FROM Question q join fetch q.Asker_ID m");
this.dgTest.DataSource = lst;
this.dgTest.DataBind();
Now, my DataGrid seems to show me properties of an array. (I've talk about this in my previous post with the same subject).
After debugging, I found out the difference between the lst from those three codes above.
The 1st one resulted in an array of Question. The 2nd one resulted in an array of String (cmiiw). The 3rd one is an array of array of String (again, cmiiw).
It's obvious that I cannot use an array of array of something directly as a datasource for a DataGrid.
So, the second question is ... How do you convert an array of array of something into an array of something? Or maybe, Why does NHibernate returns an array of array of something if the "SELECT" clause select more than one item?
I hope I made my points clear enough.
Thank you.