Problem solved ... many thanks to devonl, KPixel, and sergey for all the their time and effort in helping me with the problem.
Problem arose when I need a listing of Questions asked and Members who asked them. I'm planning to put that listing inside a datagrid.
One the O/R mapping, Member has a one-to-many relationship with Questions (i.e. each Member can asked more than one Question, but each Question can only be asked by one specific Member).
Here's a part of my mapping file:
Code:
Member.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Ametis.Lexica.Objects.Member, Ametis.Lexica.Objects" table="Member">
<id name="User_ID" column="User_ID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Last_Logon" column="Last_Logon" type="DateTime" />
<property name="Points" column="Points" type="Int32" />
<set name="Questions" inverse="true" cascade="all">
<key column="User_ID" />
<one-to-many class="Ametis.Lexica.Objects.Question, Ametis.Lexica.Objects" />
</set>
</class>
</hibernate-mapping>
Code:
Question
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Ametis.Lexica.Objects.Question, Ametis.Lexica.Objects" table="Question">
<id name="ID" column="Question_ID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Question_Subject" column="Question_Subject" type="String" />
<property name="Question_Text" column="Question_Text" type="String" />
<property name="Points" column="Points" type="Int32" />
<property name="Post_Date" column="Post_Date" type="DateTime" />
<property name="Closed" column="Closed" type="Boolean" />
<many-to-one name="Asker_ID" column="User_ID" class="Ametis.Lexica.Objects.Member, Ametis.Lexica.Objects" />
</class>
</hibernate-mapping>
Here's my query:
Code:
SELECT q FROM Question q join fetch q.Asker_ID m
I've clarified the result of the query when Debugging my application.
Here's my DataGrid:
Code:
<asp:DataGrid id="dgTest" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Penanya">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Asker_ID.User_ID") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
In the end, I manage to (almost) achieve what I need. My DataGrid shows me a listing of all the Question's properties and the corresponding Member's User_ID.
Alright ... 8)
Again, thanks to devonl, especially about using the DataBinder. Thanks to KPixel for reminding about the array conversion. Thanks to sergey for noticing my mistake on the join.
Thanks a bunch.
Case closed? :D