Hi,
I have been using NHibernate for a while now and am perplexed by one-to-many relationships.
For example, if I have the following scrapbook class which contains a one-to-many bag of ScrapBookArticle objects.
Here are the mapping files:
Scrapbook
Code:
<?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="ScrapbookArticles" inverse="false" lazy="true" generic="true" cascade="all">
<key column="ScrapbookUid"/>
<one-to-many class ="waitrose.core.domain.ScrapBookArticle,waitrose.core"/>
</bag>
</class>
</hibernate-mapping>
ScrapBookArticleCode:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="waitrose.core.domain.ScrapBookArticle,waitrose.core" table="ScrapbookArticles" lazy="true">
<id name="Uid" column="Uid" type="System.Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="guid"/>
</id>
<property column="ArticleUrl" type="String" name="ArticleUrl" length="2048" />
<property column="DateAdded" type="DateTime" name="DateAdded" />
<property column="Title" type="String" name="Title" length="512" />
<property column="Description" type="String" name="Description" length="2048" />
</class>
</hibernate-mapping>
In the ScrapbookArticles table, I have a ScrapbookUid foreign key column that I must set to allow nulls. This is because NHibernate first does an insert and then an update on this table.
For may next project I will not have the luxury of being able to change the DB schema and am worried that I will not be able to use NHibernate.
Can anyone please demistify whether I can have my foreign-key as not null? I do not see how I could use NHibernate otherwise.
I did find the following article that seemed to allude otherwise:
http://forum.hibernate.org/viewtopic.php?t=957582&highlight=foriegn+foreign+key+nullable
This must be a requirement that other people would like.
Thanks in advance
Paul