Hi,
I am very confused about the sequence of events NHibernate does things. I have the following relationship between a scrapbook that contains a map of usercookbooks. I have the following mapping files:
Scrapbook
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="waitrose.core.domain.Scrapbook,waitrose.core" table="Scrapbooks" lazy="true">
<id name="Uid" column="Uid" type="System.Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="guid"/>
</id>
<bag name="Recipes" inverse="false" lazy="true" table="ScrapbookRecipes" generic="true" cascade="save-update">
<key column="ScrapbookUid"/>
<many-to-many class="waitrose.core.domain.Recipe,waitrose.core" column="RecipeUid"/>
</bag>
<map name="CookBooks" inverse="false" table="UserCookBooks" generic="true" cascade="all">
<key column="ScrapbookUid"/>
<index column="[Name]" type="String"/>
<one-to-many class="waitrose.core.domain.UserCookBook,waitrose.core"/>
</map>
</class>
</hibernate-mapping>
UserCookbook:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="waitrose.core.domain.UserCookBook,waitrose.core" table="UserCookBooks" lazy="true">
<id name="Uid" column="Uid" type="System.Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="guid"/>
</id>
<property column="Name" type="String" name="Name" not-null="true" length="100" />
</class>
</hibernate-mapping>
When I try and delete a cookbook with the following code:
_user = _userService.GetSiteUserByUid(_userUid);
Assert.IsNotNull(_user.ScrapBook.CookBooks[COOK_BOOK_NAME_ONE]);
_user.ScrapBook.CookBooks.Remove(COOK_BOOK_NAME_ONE);
_userService.SaveOrUpdate(_user);
I have logged the SQL that NHibernate is running and it first performs an update like so:
UPDATE UserCookBooks SET ScrapbookUid = null, [Name] = null WHERE ScrapbookUid = @p0; @p0 = 'a62f2faa-c40c-4f36-855d-a6dd9ec6f132'
And then performs a delete
DELETE FROM UserCookBooks WHERE Uid = @p0; @p0 = 'bcf142c2-a666-4158-a55b-c51988000f1f'
The problem I have is that I do not want to make the ScrapbookUid or the Name field null fields in the database and if they are the update fails.
Can anyone tell me what I am doing wrong?
Cheers
Paul
|