Hi,
Good to know that you solved your problem! and even better that you updated
forum about it. :)
I have a few questions with EntityBase.Equals() from the code below.
Why do you consider Version for Equals()? NHibernate should be able to take
care of that if you have Version property mapped. Am I missing something?
Also As per my understanding, while writing back to DB, NHibernate calls
Equals() to check if an object is already in the Session. Then if it finds
the object, it will check the Version property from the mapping and decide
whether to Update or Add or No changes required on the DB. Right?
On Sunday, December 29, 2013 10:50:03 PM UTC+5:30, Daniel Ricardo Castro
Alvarado wrote:
>
> Oh, sorry.
> I forgot to add the classes:
>
> EntityBase, just to avoid adding Version and Id in each entity
>
> abstract public class EntityBase
> {
> public virtual Guid Id { get; protected set; }
> public virtual int Version { get; set; }
>
> // Two entities are considered equal if their Ids are the same
> public override bool Equals(object obj)
> {
> if (obj == null)
> return false;
>
> var other = obj as EntityBase;
> if (other == null)
> return false;
>
> if (Id.Equals(Guid.Empty))
> return ReferenceEquals(this, obj);
> return Id.Equals(other.Id) && Version == other.Version;
> }
>
> public override int GetHashCode()
> {
> return Id.GetHashCode();
> }
> }
>
>
>
> public abstract class RegistryBase : EntityBase
> {
> // -- additional properties omitted --
>
> public virtual short Priority { get; protected set; }
> }
>
> public abstract class Case : RegistryBase
> {
> // -- additional properties ommited --
>
> public virtual IList<Specimen> Specimens { get; set; }
> }
>
>
>
> 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]>
>>
>>> (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].
>>> 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.
>>>
>>
>>
--
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.