Again I'm pretty sure that I'm missing the obvious, but I can't make it work:
User has many Blogs
Blogs has many Users
I manage to save just fine, and cascades works, the problem is that NHibernate isn't saving to the association table.
The user class mapping:
Code:
<class
lazy='true'
name='NHibernate.Generics.Tests.User, NHibernate.Generics.Tests'
table='Users'>
<id
column='user_id'
name='UserId'>
<generator
class='native' />
</id>
<property
column='user_name'
name='UserName'
type='string' />
<set
name='Blogs'
cascade='save-update'
lazy='true'
inverse='false'
access='NHibernate.Generics.GenericAccessor, NHibernate.Generics'
table='UsersBlogs'>
<key
column='user_id' />
<many-to-many
class='NHibernate.Generics.Tests.Blog, NHibernate.Generics.Tests'
column='blog_id' />
</set>
</class>
The Blog class mapping:
Code:
<class
lazy='true'
name='NHibernate.Generics.Tests.Blog, NHibernate.Generics.Tests'
table='Blogs'>
<id
name='BlogID'
column='blog_id'
unsaved-value='0'>
<generator
class='native' />
</id>
<property
name='BlogName'
column='blog_name' />
<set
name='Posts'
access='NHibernate.Generics.GenericAccessor, NHibernate.Generics'
lazy='true'
cascade='all-delete-orphan'
inverse='true'>
<key
column='post_blogid' />
<one-to-many
class='NHibernate.Generics.Tests.Post, NHibernate.Generics.Tests' />
</set>
<set
name='Users'
access='NHibernate.Generics.GenericAccessor, NHibernate.Generics'
cascade='save-update'
inverse='true'
table='UsersBlogs'>
<key
column='blog_id' />
<many-to-many
class='NHibernate.Generics.Tests.User, NHibernate.Generics.Tests'
column='user_id' />
</set>
</class>
The code that fails:
Code:
session.Save(user1);
session.Save(user2);
//blog1 & blog2 are saved by cascades
session.Dispose();
seesion = factory.OpenSession();
User newUser = (User)session.Load(typeof(User), user1.Id);
Assert.AreEqual(2, user1.Blogs.Count);
The code cause the following to happen:
-User1 saved
-User2 saved
-Blog1 saved
-Blog2 saved
No association is saved :-(