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: StaleStateException when saving child list item
PostPosted: Sat Sep 01, 2007 4:00 pm 
Newbie

Joined: Sat Sep 01, 2007 3:35 pm
Posts: 2
NHibernate version:
1.2.0

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="AgCg.ContentManagement.Domain.ContentBase, AgCg.ContentManagement" table="Content">
<id name="id" access="field" type="Guid" column="Id">
<generator class="assigned" />
</id>
<discriminator column="ClassName" />
<property name="title" access="field" column="Title" />
<property name="text" access="field" column="Text" />
<property name="dateCreated" access="field" column="DateCreated" />
<property name="lastModified" access="field" column="LastModified" />
<property name="hitCount" access="field" column="HitCount" />

<subclass name="AgCg.ContentManagement.Domain.Menu, AgCg.ContentManagement">
<list access="field" name="children" cascade="all-delete-orphan" table="ContentRel">
<key column="ParentId" />
<index column="Position" />
<many-to-many class="AgCg.ContentManagement.Domain.ContentBase, AgCg.ContentManagement" column="ChildId"/>
</list>
</subclass>

<subclass name="AgCg.ContentManagement.Domain.Story, AgCg.ContentManagement" />

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
[Test]
public void TestStoryMapping()
{
User jim = new User("jim bob");
Menu menu = Menu.CreateTopLevelMenu("some menu", "some text", jim);

ISession session = factory.OpenSession();
Story story = menu.CreateChildStory("some story", "some story text", jim) as Story;
session.Save(menu);
session.Flush();

session.Delete(menu);
session.Flush();
session.Dispose();
}

Full stack trace of any exception that occurs:
Demo.Tests.Integration.MappingFixture.TestStoryMapping : NHibernate.StaleStateException : Unexpected row count: 0; expected: 1

at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement)
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledUpdate.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()
at NHibernate.Impl.SessionImpl.Flush()
at Demo.Tests.Integration.MappingFixture.TestStoryMapping() in c:\SVN\AgCg\Demo.Tests.Integration\Source\MappingFixture.cs:line 79


Name and version of the database you are using:
MySQL 5.0

The generated SQL (show_sql=true):

NHibernate: INSERT INTO Content (Title, Text, DateCreated, LastModified, HitCount, ClassName, Id) VALUES (?p0, ?p1, ?p2, ?p3, ?p4, 'AgCg.ContentManagement.Domain.Menu', ?p5); ?p0 = 'some menu', ?p1 = 'some text', ?p2 = '9/1/2007 2:47:19 PM', ?p3 = '9/1/2007 2:47:19 PM', ?p4 = '0', ?p5 = '765c96f1-b3ac-4fa8-8121-4c1be6d21ade'
NHibernate: UPDATE Content SET Title = ?p0, Text = ?p1, DateCreated = ?p2, LastModified = ?p3, HitCount = ?p4 WHERE Id = ?p5; ?p0 = 'some story', ?p1 = 'some story text', ?p2 = '9/1/2007 2:47:19 PM', ?p3 = '9/1/2007 2:47:19 PM', ?p4 = '0', ?p5 = '5bee058c-8663-4ff1-b51c-d0de0f25b553'


If I save a story object by itself it works fine, same deal with a menu object, but if I try to save a menu containing a story, it fails.

I noticed in the sql above that there is an UPDATE issued for the story, when I am expecting an INSERT.

I am using an assigned generator for the id on the base class (for both story and menu).

I realize this post is similar to http://forum.hibernate.org/viewtopic.ph ... eexception, but I feel that I have a different case in that I can't provide an unsaved-value attribute since my domain objects generate the guids I'm using as ids.

Thanks for your help.

Robert


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 6:21 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Nhibernate seems to have problems when you create multiple transient entities that reference each other, and then try to save just the root entity. To avoid these problems, call session.Save() on each transient entity after you create it, before you attach it to others.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 8:05 pm 
Newbie

Joined: Sat Sep 01, 2007 3:35 pm
Posts: 2
I set the "unsaved-value" attribute to "any" and that seems to have fixed my problem.


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.