caching appears to be configured correctly.
something things to consider:
1. caching works when you save the state because caching is configured
from state to city, not city to state. when you save the city directly
it will not be associated with the state's 2nd level cache until it's
loaded from the database again.
2. if state is resolved from the database there is no need to call
Utility.Save(). NH will do this automatically when the session is
flushed. could could simply be
using(var tx = session.BeginTransaction())
{
   try
   {
      Session.Get<State>(id).Add(new City{Name= "name"});
      tx.Commit();
   }
   catch
   {
      tx.Rollback();
      throw;
   }
}
3. I assume you are using a transaction. If not you must/should.
especially with 2nd level cache. I won't go into the details but all
NH reads/writes must be done within a transaction.
4. using Identity as a POID is not ideal with NH. it works, even when
it shouldn't. By that I mean it breaks the concept of unit of work.
google POID generators for more information.


On Jan 13, 7:17 am, Manoj <[email protected]> wrote:
> Below is mapping and code.
>
>   <class name="State" table="State" lazy="true">
>     <cache usage="read-write"/>
>     <id name="ID" type="System.Int32" column="Id">
>         <generator class="identity"/>
>     </id>
>     <version name="Version" column="Version" type="System.Int32"
> access="property"/>
>     <property name="StateName" column="StateName" type="System.String"
> not-null="true" length="50"/>
>     <bag name="CityList" table="City" inverse="true" lazy="true"
> cascade="save-update">
>         <cache usage="read-write"/>
>         <key column="Id"/>
>         <one-to-many class="City"/>
>     </bag>
>   </class>
>
>   <class name="City" table="City" lazy="true">
>     <cache usage="read-write"/>
>     <id name="ID" type="System.Int32" column="Id">
>         <generator class="identity"/>
>     </id>
>     <version name="Version" column="Version" type="System.Int32"
> access="property"/>
>     <property name="CityName" column="CityName" type="System.String"
> not-null="true" length="50"/>
>     <many-to-one name="State" column="Id" class="State" cascade="none"
> update="true" insert="true" not-found="ignore" not-null="false"/>
>   </class>
>
>   //Working successfully - in below code, state shows newly added city
> correctly after saving. no need of any refresh
>   City newcity = new City();
>   newcity.CityName = "test";
>   newcity.State = st;
>   st.City.Add(newcity);
>   Utility.Save(st);        //we are executing save on state object
>
>   //Not Working - in below code, state does not show newly added city
> until we execute refresh on state
>   City newcity = new City();
>   newcity.CityName = "test";
>   newcity.State = st;
>   //st.City.Add(newcity);        //here we are not adding newcity in
> state list (this line is different from above successful code)
>   Utility.Save(newcity);        //we are executing save on city object
> (this line is different from above successful code)

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to