Thanks for your answer.

I wanted to use inverse=true, but if I remind correctly, I was having 
problems with my optimistic concurrency settings. I want the Case "Version" 
property is incremented every time I add, update or remove one specimen.

I can show the mappings, but there are a lot of extra properties (I have 
omitted them). Case is a subclass of RegistryBase, maped with 
table-per-class strategy (one table for RegistryBase, one for each 
subclass).

Mapping for RegistryBase:
            Id(x => x.Id)
                .UnsavedValue(Guid.Empty);

            Version(x => x.Version);

            // -- additional properties omitted --
            
            // example of additional association (perhaps relevant)
            References(x => x.Pathologist)
                .Cascade.Merge();

            // One property mapped to a function in the database
            Map(x => x.Priority)
                .Formula("getcasepriority(Id)") 
                .ReadOnly()
                .Generated.Always();

Mapping for Case:
            KeyColumn("Id");
            
            HasMany(x => x.Specimens)
                .Cascade.AllDeleteOrphan()
                .BatchSize(Math.Max(1, 
Properties.Settings.Default.CollectionBatchSize));

            // extra properties omitted

Mapping for Specimen:
            Id(x => x.Id)
                .UnsavedValue(Guid.Empty);

            References(x => x.Case);
            // -- additional properties omitted --
            
            // No version here, I want the optimistic concurrency is 
handled with the parent Case

I'm using Fluent NHibernate, by the way. I can post the omitted properties 
if needed, though they're just strings and perhaps references to other 
unrelated entities.

Thanks again

El sábado, 28 de diciembre de 2013 17:07:25 UTC-5, Gunnar Liljas escribió:
>
> In general I would suggest inverse=true for bidirectional mappings.
>
>
> I have been unable to get the same error, though. Could you show both 
> classes and their mappings?
>
> /G
>
>
>
>
>
>
> 2013/12/28 Daniel Ricardo Castro Alvarado <[email protected] <javascript:>>
>
>> (I'm sending my post again because the previous one was not showing after 
>> some days)
>>
>> Hello everyone,
>>
>> I'm making a test method for an application and I'm having trouble 
>> getting it to work correctly.
>>
>> In my model, one Case has many Specimen (but one Specimen is associated 
>> with only one Case).
>> I'm trying to add one Case with some Specimens to the database and then 
>> removing the Specimens (but not deleting the Case), but somehow I get 
>> StaleStateException and I can't figure why.
>>
>> Case.Specimens is mapped as a One to many collection with inverse="false" 
>> and cascade="all-delete-orphan". The relationship is bidirectional (i.e. 
>> each Specimen has a reference to the Case), though I don't think that's 
>> relevant.
>>
>>         using (var session = 
>> DatabaseSetUpFixture.SessionFactory.OpenSession())
>>             using (var tx = session.BeginTransaction())
>>             {
>>                 Case original = AddSampleCase(session);
>>                 
>>                 original.Specimens = new List<Specimen>() { 
>>                     CreateSampleSpecimen()
>>                 };
>>
>>                 session.Flush(); // Save the changes
>>                 session.Clear(); // Evict all entities from the session
>>
>>                 Case newVersion = session.Get<Case>(original.Id); // Get 
>> the DB version
>>                 newVersion.Specimens.Clear(); // Remove all the specimens
>>
>>                 session.Update(newVersion);
>>                 session.Flush(); // StaleStateException here :S
>>
>>                 // ...
>>                 
>>                 tx.Rollback(); // Leave the test DB untouched
>>             }
>>         }
>>
>> I need to use the same session so I can rollback all the changes made (as 
>> this is a test method).
>> If I comment "newVersion.Specimens.Clear();" the test ends normally (but 
>> I need that line, of course), and I don't understand why (even if I change 
>> some attributes of the Case)
>>
>> The stack trace is included below:
>>
>> en 
>> NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32
>>  
>> rowCount, IDbCommand statement) en 
>> p:\nhibernate-core\src\NHibernate\AdoNet\Expectations.cs:línea 29
>> en NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation 
>> expectation) en 
>> p:\nhibernate-core\src\NHibernate\AdoNet\NonBatchingBatcher.cs:línea 41
>> en NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, 
>> Object version, Int32 j, Object obj, SqlCommandInfo sql, 
>> ISessionImplementor session, Object[] loadedState) en 
>> p:\nhibernate-core\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:línea
>>  
>> 2919
>> en NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, 
>> Object version, Object obj, ISessionImplementor session) en 
>> p:\nhibernate-core\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:línea
>>  
>> 3102
>> en NHibernate.Action.EntityDeleteAction.Execute() en 
>> p:\nhibernate-core\src\NHibernate\Action\EntityDeleteAction.cs:línea 70
>> en NHibernate.Engine.ActionQueue.Execute(IExecutable executable) en 
>> p:\nhibernate-core\src\NHibernate\Engine\ActionQueue.cs:línea 136
>> en NHibernate.Engine.ActionQueue.ExecuteActions(IList list) en 
>> p:\nhibernate-core\src\NHibernate\Engine\ActionQueue.cs:línea 126
>> en NHibernate.Engine.ActionQueue.ExecuteActions() en 
>> p:\nhibernate-core\src\NHibernate\Engine\ActionQueue.cs:línea 174
>> en 
>> NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource
>>  
>> session) en 
>> p:\nhibernate-core\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs:línea
>>  
>> 249
>> en NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent 
>> event) en 
>> p:\nhibernate-core\src\NHibernate\Event\Default\DefaultFlushEventListener.cs:línea
>>  
>> 19
>> en NHibernate.Impl.SessionImpl.Flush() en 
>> p:\nhibernate-core\src\NHibernate\Impl\SessionImpl.cs:línea 1509
>>
>> So, what is causing StaleStateException?
>>
>> Thank you very much
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "nhusers" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]<javascript:>
>> .
>> Visit this group at http://groups.google.com/group/nhusers.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to