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.  [ 2 posts ] 
Author Message
 Post subject: Child Collections Not Saving
PostPosted: Thu Jun 14, 2007 3:41 pm 
Newbie

Joined: Wed May 02, 2007 9:32 am
Posts: 17
Hi,

I have the following mapping file

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Orders.Core" assembly="Orders.Core" >
   <class name="Author" table="tmpJoeAuthors" lazy="false">
      <id name="AuthorId" column="AuthorId" unsaved-value="0">
         <generator class="native" />
      </id>

      <property name="FirstName" column="FirstName" />
      <property name="LastName" column="LastName" />

      <set name="Books" inverse="true" table="tmpJoeBooks" cascade="all">
         <key column="AuthorId" foreign-key="AuthorId" />
         <one-to-many class="Book"/>
      </set>
   </class>


   <class name="Book" table="tmpJoeBooks" lazy="false">
      <id name="TitleId" unsaved-value="0">
         <generator class="native"/>
      </id>
      <many-to-one name="WrittenBy" class="Author" column="AuthorId" not-null="true"/>
      <property name="Title" />
      <property name="PublishDate" />
   </class>
   
</hibernate-mapping>



The code for my Author and Book objects is as follows.

Code:

    public class Author
    {
        private int _AuthorId;
        public int AuthorId
        {
            get { return _AuthorId; }
            set { _AuthorId = value; }
        }

        private string _FirstName;
        public string FirstName
        {
            get { return _FirstName; }
            set { _FirstName = value; }
        }

        private string _LastName;
        public string LastName
        {
            get { return _LastName; }
            set { _LastName = value; }
        }

        private Iesi.Collections.ISet _Books = new Iesi.Collections.HashedSet();
        public Iesi.Collections.ISet Books
        {
            get
            {
                return _Books;
            }
            set
            {
               _Books = value;
            }
        }

        public Author()
        {
        }
    }

    public class Book
    {
        private int _TitleId = 0;
        public int TitleId
        {
            get { return _TitleId; }
            set { _TitleId = value; }
        }

        private Author _WrittenBy;
        public Author WrittenBy
        {
            get { return _WrittenBy; }
            set { _WrittenBy = value; }
        }


        private string _Title;
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }

        private DateTime _PublishDate;
        public DateTime PublishDate
        {
            get { return _PublishDate; }
            set { _PublishDate = value; }
        }
        public Book()
        {
           
        }
    }






I have populated my authors table, and am running the following test

Code:
        [TestMethod]
        public void AssignBooksToExistingAuthorTest()
        {
            ProjectBase.Data.NHibernateSessionManager.Instance.BeginTransactionOn(TestGlobals.SessionFactoryConfigPath);

            DAL.GenericDao<Core.Author, int> repository = new Orders.DAL.GenericDao<Core.Author, int>(TestGlobals.SessionFactoryConfigPath);
            Core.Author au = repository.GetById(2, false);
            Assert.IsNotNull(au);

            Core.Book book = new Core.Book();
            book.WrittenBy = au;
            book.Title = "Carrie";
            book.PublishDate = new DateTime(1970, 1, 1);
            au.Books.Add(book);

            repository.SaveOrUpdate(au);

            ProjectBase.Data.NHibernateSessionManager.Instance.CommitTransactionOn(TestGlobals.SessionFactoryConfigPath);
        }


(ProjectBase is from Billy McCafferty's enterprise sample on CodePlex).

I receive the following error when committing the transaction.
Quote:
Error 1 TestCase 'Orders.Test.DataTests.TestAuthorDEBUG.AssignBooksToExistingAuthorTest'
failed: NHibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)
at NHibernate.Impl.SessionImpl.CheckId(Object obj, IEntityPersister persister, Object id)
at NHibernate.Impl.SessionImpl.FlushEntity(Object obj, EntityEntry entry)
at NHibernate.Impl.SessionImpl.FlushEntities()
at NHibernate.Impl.SessionImpl.FlushEverything()
at NHibernate.Impl.SessionImpl.Flush()
at ProjectBase.Data.NHibernateSessionManager.CloseSessionOn(String sessionFactoryConfigPath)
at ProjectBase.Data.NHibernateSessionManager.RollbackTransactionOn(String sessionFactoryConfigPath)
at ProjectBase.Data.NHibernateSessionManager.CommitTransactionOn(String sessionFactoryConfigPath)
at Orders.Test.DataTests.TestAuthorDEBUG.AssignBooksToExistingAuthorTest() in C:\Projects\SN\Orders.Test\DataTests\TestAuthorDEBUG.cs:line 53 C:\Projects\SN\Orders.Test\DataTests\TestAuthorDEBUG.cs 53


If I remove the add to the collection, and just update the Author object it works OK, so I suspect its something that Ive screwed up in my mapping file.

Any ideas would be greatly appreciated.

Thanks
Joe[/code]


Top
 Profile  
 
 Post subject: oops
PostPosted: Fri Jun 15, 2007 11:29 am 
Newbie

Joined: Wed May 02, 2007 9:32 am
Posts: 17
Boy am I dumb...

This was a watered down sample to illustrate my problem. Turns out the PK field on the Books wasnt set to identity. That fixed my non-problem problem.

The problem in the "real-world" was even more trivial, and thus way more embarassing. I was manually setting the ID on an entity which was causing me a stale state exception.



(Posting this in the remote chance that it helps someone else).
StaleStateException, Stale State Exception


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