-->
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.  [ 5 posts ] 
Author Message
 Post subject: SaveOrUpdateCopy
PostPosted: Mon Sep 29, 2008 5:38 am 
Newbie

Joined: Mon Sep 29, 2008 4:56 am
Posts: 3
Hi

I am migrating my project to nhibernate 2.0, In 2.0 my code is breaking when I save an object using differnt session

This is code from unit test case which works fine with older version of nhibernate but breaks in 2.0



Code:
ISession sessionOne = new SessionManager().GetNewSession();
            IEventLogger logger = EventLoggerFactory.CreateDefaultInstance();

            var currencyHelperOne = new CurrencyHelper(sessionOne, logger);

            var accountOne = new Account
            {
                Code = "TEST_AC_1",
                Currency = currencyHelperOne.GetByIsoCode(Currency.KnownIsoCodes.US_DOLLAR),
                Name = "TEST_AC_1",
                ShortName = "TEST_AC_1",
                Status = AccountStatus.KnownValues.ACTIVE(sessionOne),
                Type = AccountType.KnownValues.MAP_FUND(sessionOne)
            };

            sessionOne.Save(accountOne);                 
           
            sessionOne.Flush();
            sessionOne.Evict(accountOne);
         
            ISession sessionTwo = new SessionManager().GetNewSession();
            Account accountTwo = (Account)sessionTwo.SaveOrUpdateCopy(accountOne);


here the line

Account accountTwo = (Account)sessionTwo.SaveOrUpdateCopy

throws an exception

Quote:
Error 23 TestCase 'MerrillLynch.AIPlatform.Testing.DomainModel.NHibernateSessionMobilityTest.SessionInteractionTest3'
failed: NHibernate.StaleObjectStateException : Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [MerrillLynch.AIPlatform.Business.Holdings.Account#242]
at NHibernate.Event.Default.DefaultMergeEventListener.EntityIsDetached(MergeEvent event, IDictionary copyCache)
at NHibernate.Event.Default.DefaultMergeEventListener.OnMerge(MergeEvent event, IDictionary copyCache)
at NHibernate.Event.Default.DefaultMergeEventListener.OnMerge(MergeEvent event)
at NHibernate.Impl.SessionImpl.FireSaveOrUpdateCopy(MergeEvent event)
at NHibernate.Impl.SessionImpl.SaveOrUpdateCopy(Object obj)
at MerrillLynch.AIPlatform.Testing.DomainModel.NHibernateSessionMobilityTest.SessionInteractionTest3() in C:\Working\AIP_NHibernate_Migration\nhibernate_2.0_migration\MerrillLynch.AIPlatform.DomainModel.Test\NHibernateSessionMobilityTest.cs:line 43 C:\Working\AIP_NHibernate_Migration\nhibernate_2.0_migration\MerrillLynch.AIPlatform.DomainModel.Test\NHibernateSessionMobilityTest.cs 43


here is the mapping file for account class

Code:
[<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MerrillLynch.AIPlatform.DomainModel" namespace="MerrillLynch.AIPlatform.Business.Holdings">
  <class name="Account" table="account">
    <id name="Id" column="id" type="Int32">
      <generator class="identity" />
    </id>
    <version name="Timestamp" column="timestamp"
      type="MerrillLynch.AIPlatform.Utilities.SqlTimestampType, MerrillLynch.AIPlatform.Utilities.Common"
      generated="always" />
    <property name="Code" column="code" type="String" length="30" not-null="true" />
    <property name="Name" column="name" type="String" length="100" not-null="true" />
    <property name="ShortName" column="short_name" type="String" length="30" not-null="true" />
    <property name="FirstTradingDate" column="first_trading_date" type="DateTime" not-null="false" />
    <property name="LastTradingDate" column="last_trading_date" type="DateTime" not-null="false" />
    <property name="LastUpdated" column="last_updated" type="DateTime" not-null="true" />
    <property name="LastUpdatedBy" column="last_updated_by" type="String" length="100" not-null="true" />
    <many-to-one name="Parent" class="Account" column="parent_account_id" not-null="false" />
    <many-to-one name="Status" class="AccountStatus" column="account_status_id" not-null="true" />
    <many-to-one name="Type" class="AccountType" column="account_type_id" not-null="true" />
    <many-to-one name="Currency" class="MerrillLynch.AIPlatform.Business.Common.Currency" column="currency_id" not-null="true" />
    <many-to-one name="ComplianceReport" class="MerrillLynch.AIPlatform.Business.Risk.ComplianceReport" column="compliance_report_id" not-null="false" />
    <set name="MutableGroupings" table="account_account_grouping" inverse="false" lazy="true">
      <key column="account_id" />
      <many-to-many class="AccountGrouping" column="account_grouping_id" />
    </set>
    <set name="MutableRoles" table="account_role" inverse="false" lazy="true">
      <key column="account_id" />
      <many-to-many class="MerrillLynch.AIPlatform.Business.Common.Role" column="role_id" />
    </set>
    <set name="RiskExtractTemplates" inverse="false" lazy="true">
      <key column="account_id" />
      <one-to-many class="MerrillLynch.AIPlatform.Business.Risk.RiskExtractTemplate" />
    </set>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2008 9:52 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
brijeshsnm,

The first advice I would give to you is, don't use saveOrUpdateCopy at all.

But if you must, bear in mind that this method only accepts transient or persistent objects, and the one in your example is "detached".

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2008 4:34 am 
Newbie

Joined: Mon Sep 29, 2008 4:56 am
Posts: 3
Thanks diaz,

even if I do somethinh like this which make my accountOne persistent object to be saved with sessionTwo I still get same error

Code:
ISession sessionOne = new SessionManager().GetNewSession();
            IEventLogger logger = EventLoggerFactory.CreateDefaultInstance();

            var currencyHelperOne = new CurrencyHelper(sessionOne, logger);

            var accountOne = new Account
            {
                Code = "TEST_AC_1",
                Currency = currencyHelperOne.GetByIsoCode(Currency.KnownIsoCodes.US_DOLLAR),
                Name = "TEST_AC_1",
                ShortName = "TEST_AC_1",
                Status = AccountStatus.KnownValues.ACTIVE(sessionOne),
                Type = AccountType.KnownValues.MAP_FUND(sessionOne)
            };

            sessionOne.Save(accountOne);                 
               
            ISession sessionTwo = new SessionManager().GetNewSession();
            Account accountTwo = (Account)sessionTwo.SaveOrUpdateCopy(accountOne);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2008 10:14 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
First of all, please read my article, here

http://hibernar.org/articulos_en/persis ... ethods.php

because it seems from your code that you don't clearly understand what "state" (as in persistent, detached or transient) means.

Second, don't use saveOrUpdate. It is deprecated and confusing.

Third, why do you need 2 sessions in that code? The probability of you really needing 2 live sessions anywhere is very, very slim.

Fourth, you are trying to persist an object that already still persistent (from session 1) into another session. Why? Do they point to different databases?
Are you done with session 1? Can you close it, in order to make that accountOne object detached?

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2008 11:09 am 
Newbie

Joined: Mon Sep 29, 2008 4:56 am
Posts: 3
Awsm!! great article. Understood what mistake I am doing, now. Thanks!


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