-->
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: foreign key, null fields and one-to-many
PostPosted: Wed Jun 06, 2007 10:59 am 
Regular
Regular

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


ScrapBookArticle

Code:
<?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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 11:14 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Your situation is described here.

http://www.hibernate.org/hib_docs/v3/re ... hild-bidir

The key thing to note is that the many side of the one-many relationship should be the owner. This would be the side that has the foreign key. Your mapping file of ScrapBookArticles doesn't seem to have the foreign key.

On the other side, you'd set inverse=true. Anyway this is all described in more detail in the link above.

Hope it helps.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


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.