-->
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: map and null fields
PostPosted: Wed Jun 06, 2007 10:49 am 
Regular
Regular

Joined: Fri May 05, 2006 11:54 am
Posts: 51
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 2:51 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
You have to make your collections inverse and specify on the many-to-one side that it is not-null="true". There are many forum topics on this. Search and you should find some more detailed examples.


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.