Hi
I'm having some problems regarding loading of objects in an ASP.NET webapplication. I have an online questionnaire system which allows users to create questions and submit answers. My problem is that when saving an answer collection and then loads it on the same request, the answer objects are not fully populated.
I have an AnswerCollection object which contains an IDictionary of Answer objects. Each Answer object has a reference to a Question object.
Let's say I have two methods. One for saving answer collections and one for loading answer collections. The saving works fine. Loading also works fine if I load the AnswerCollection in a new request. However, if I first save an AnswerCollection and then loads it in the same request, the Question-reference in the Answer-object is null. Why is my Question object null
Code:
public int SaveAnswerCollection(AnswerCollection answerCollection)
{
NHibernateSession.SaveOrUpdate(answerCollection);
NHibernateSession.Flush();
return answerCollection.Id;
}
public int GetAnswerCollection(int id)
{
return (AnswerCollection)NHibernateSession.Load(typeof(AnswerCollection), id);
}
Code:
AnswerCollection ac = new AnswerCollection();
ac.Question = someQuestion;
SaveAnswerCollection(ac);
AnswerCollection ac2 = GetAnswerCollection(ac.Id);
ac2.Question <-- Question is null!!
Hibernate version: NHibernate 1.2.0
Mapping documents:<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="AnswerCollection, model" table="AnswerCollection">
<id name="Id" type="integer" column="Id">
<generator class="identity"/>
</id>
<many-to-one name="QuestionCollection"
class="QuestionCollection, model"
column="QuestionCollection"/>
<map name="answers" cascade="all-delete-orphan" lazy="false">
<key column="answercollection" />
<index column="aorder" type="integer" />
<one-to-many class="Answer, model" />
</map>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="QuestionCollection, model" table="QuestionCollection">
<id name="Id" type="int" column="Id">
<generator class="identity"/>
</id>
<map name="QuestionCollectionQuestion" cascade="all-delete-orphan" lazy="false">
<key column="QuestionCollection" />
<index column="QuestionOrder" type="integer" />
<one-to-many class="QuestionCollectionQuestion, model" />
</map>
<property name="Mandatory" column="Mandatory" type="boolean" />
<property name="TimeStamp" column="TimeStamp" type="datetime" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="Answer, model" table="Answer">
<id name="id" type="int" column="Id">
<generator class="identity"/>
</id>
<many-to-one name="answercollection"
class="AnswerCollection, model"
column="answercollection"/>
<many-to-one name="Question"
class="Question, model"
column="aorder"/>
</class>
</hibernate-mapping>
Name and version of the database you are using:MS SQL Server 2005
[/code]