-->
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: Automatically maintain association key
PostPosted: Mon Jul 09, 2007 1:09 pm 
Beginner
Beginner

Joined: Fri Apr 27, 2007 11:50 am
Posts: 23
Hi Guys,

I have a unidirectional one-to-many collection mapped out, and have the .NET code to add items to the collection, but i'm having problems when it comes to saving the parent object with it's chidlren.

I want the fact that the child objects belong to the parent object to automaticaly set the foreign key when they are saved, but it's throwing an error because it is trying to insert NULL.

My mapping files are as follows

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Think.ProofIT.Core.Domain.PageSet, Think.ProofIT.Core" table="[PageSet]" lazy="false">
         
      <id name="ID" column="[PageSetId]" unsaved-value="0">
         <generator class="native" />
      </id>

      <property name="Title" column="[Title]" />
      <property name="IsActive" column="[IsActive]" />

      <list name="Pages" table="[Page]" cascade="all" lazy="false">
         <key column="PageSetId" not-null="true" />
         <index column="[Order]" />
         <one-to-many class="Think.ProofIT.Core.Domain.Page, Think.ProofIT.Core" />
      </list>

   </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Think.ProofIT.Core.Domain.Page, Think.ProofIT.Core" table="[Page]" lazy="false">

      <id name="ID" column="[PageId]" unsaved-value="0">
         <generator class="native" />
      </id>

      <property name="FileName" column="[FileName]" />
      <property name="ThumbnailFileName" column="[ThumbnailFileName]" />
      
   </class>
</hibernate-mapping>


Any help is appreciated

Matt


Top
 Profile  
 
 Post subject: Sorted
PostPosted: Tue Jul 10, 2007 5:33 am 
Beginner
Beginner

Joined: Fri Apr 27, 2007 11:50 am
Posts: 23
I have managed to work out was i was doing wrong.

The documentation for One-to-many associations states:

Quote:
Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, NHibernate
may cause constraint violations when it creates or updates the association.


My problem was that i had set the key column to be NOT NULL.

Setting the column to be NULLABLE it worked perfectly.

I presume that does some insert and then updates it straight away.

NB: The documentation also recomends using a bidirectional association but i do belive you can only use a set or bag collection, which you can't use the <index> attribute.


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.