Firstly, I just want to say that I am new to NHibernate so this may be an elementary question.
I have a Merchant object which has a single RegionalOffice and an IList of MerchanLocations. It looks like this (fields and classes not included for simplicity):
Code:
public class IntEntity
{
private int m_Id = 0;
public virtual int Id { get { return m_Id; } }
}
public class Merchant : IntEntity
{
public virtual string AccountName { get; set; }
public virtual RegionalOffice RegionalOffice { get; set; }
public virtual IList<MerchantLocation> MerchantLocations { get; set; }
}
public class RegionalOffice : IntEntity
{
public virtual string OfficeCode { get; set; }
public virtual string Name { get; set; }
}
public class MerchantLocation : IntEntity
{
public virtual string Name { get; set; }
public virtual string StoreNumber { get; set; }
public virtual Address Address { get; set; }
public virtual string Phone { get; set; }
public virtual string Notes { get; set; }
}
My mapping file looks like the following (fields and classes not included for simplicity):
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="XX.YY"
default-lazy="false"
default-cascade="all">
<class name="Merchant">
<id name="Id" column="merchantid" access="nosetter.pascalcase-m-underscore">
<generator class="native" />
</id>
<property name="AccountName" length="256" not-null="true" />
<many-to-one name="RegionalOffice" column="regionalofficeid" cascade="all-delete-orphan" />
<bag name="MerchantLocations" cascade="all-delete-orphan">
<key column="merchantlocationid" />
<one-to-many class="MerchantLocation" not-found="ignore" />
</bag>
</class>
<class name="MerchantLocation">
<id name="Id" column="merchantlocationid" access="nosetter.pascalcase-m-underscore">
<generator class="native" />
</id>
<property name="Name" length="256" />
<property name ="StoreNumber" />
<many-to-one name ="Address" column="addressid" cascade="all-delete-orphan" />
<property name="Phone" length="10" />
</class>
<class name="RegionalOffice">
<id name="Id" column="regionalofficeid" access="nosetter.pascalcase-m-underscore">
<generator class="native" />
</id>
<property name="OfficeCode" length="50" not-null="true" unique="true" />
<property name="Name" length="256" not-null="true" />
</class>
</hibernate-mapping>
When I call Insert() on my Merchant, i receive the following exception:
{"object references an unsaved transient instance - save the transient instance before flushing: RegionalOffice"}
Ive done a bunch of research on why this error might exist and it seems to be the way Im using NHibernate in general. Currently my architechture is built on a stateful .Net based application server. Because of this i thought i could use NHibernate in a non-traditional way. The way that I have implemented it includes the following:
A singleton wrapper class for the ISessionFactory which has methods for providing IStatelessSessions. The stateless sessions are called in a using statement which calls our basic CRUD model. This means that I'm not really persisting objects which is what i think is causing this issue. I have turned off lazy loading because of this as well.
Do you guys think this is the reason for my exception? I also tried calling Insert on my RegionalOffice and it inserted correctly. If I assign that to my Merchant object and call Insert on my Merchant the RegionalOffcie shows up correctly in the DB, however my IList of MerchantLocations is null and there are no MerchantLocations inserted into the DB. If the exception is because of the way I have implemented the sessions, is there a reference or blog someone can point me to on how to implement a thread-safe ISession wrapper as the application server I have to work with will use multiple threads on an application server session? Also are there any best practices on implementing ISessions outside of using IIS or Apache etc, as they are stateless as this application server is stateful?
Any help would be appreciated! =)