-->
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.  [ 3 posts ] 
Author Message
 Post subject: Stateless sessions and NHibernate
PostPosted: Fri Mar 27, 2009 5:18 pm 
Newbie

Joined: Fri Mar 27, 2009 4:09 pm
Posts: 3
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! =)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 29, 2009 4:53 am 
Newbie

Joined: Thu Feb 05, 2009 11:46 am
Posts: 4
I think you need to make your merchant and merchant location bi-directional. Have a look here for a parent-child example


http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/example-parentchild.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 30, 2009 10:53 am 
Newbie

Joined: Fri Mar 27, 2009 4:09 pm
Posts: 3
Thanks! That fixed it. I made a bunch of different changes on Friday after i posted this which helped, and I was doing more research and actually came across that link. After thinking about it over the weekend I was going to implement that and you confirmed my belief.


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