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]