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.  [ 4 posts ] 
Author Message
 Post subject: bi-directional one-to-many
PostPosted: Wed Jul 02, 2008 9:27 pm 
Beginner
Beginner

Joined: Fri May 30, 2008 3:57 pm
Posts: 26
NHibernate 1.2
.Net 3.5
SQL Server 2005


I can't seem to get a bi-directional one-to-many working properly, and I'm not sure why. Here is my configuration files with the irrelevant bits pulled out for brevity.


parent:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Services.ProcedureDS.NH__Procedure, ProcedureDS"
         table="Procedures">
   
    <id name="id" column="id" type="Int32" >
      <generator class="identity" />
    </id>

    <set name="layouts" lazy="false" table="ProcedureLayouts" inverse="true" cascade="all">
      <key column="procedure_id"/>

      <one-to-many not-found="exception" class="Services.ProcedureDS.NH__ProcedureLayout, ProcedureDS"/>
    </set>
  </class>
</hibernate-mapping>



children:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Services.ProcedureDS.NH__ProcedureLayout, ProcedureDS"
         table="ProcedureLayouts">
   
    <id name="id" column="id" type="Int32" >
      <generator class="identity" />
    </id>

    <property name="version" column="version" type="Byte" />
    <many-to-one name="parentProcedure" column="procedure_id" class="Services.ProcedureDS.NH__Procedure, ProcedureDS" />
   
  </class>
</hibernate-mapping>


I've been staring at section 6.8 of the documentation for the last couple of hours and I don't understand why this isn't working.




Can someone please point out what I'm doing wrong?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 3:06 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The mapping looks good. What does not work ? Post the classes and the code where you try to save objects.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 11:17 am 
Beginner
Beginner

Joined: Fri May 30, 2008 3:57 pm
Posts: 26
Thank you for the reply Wolli, I tracked it down.

Apparently NHibernate requires you to explicitly set the parent of the child, which I didn't expect. Up until now I've been dealing with m:n relationships, and adding the child to the collection was enough, so the change in behavior was unexpected (for me atleast).

I'm still getting to know the quirks of NHibernate, but from what I've gathered, the inverse=true tells NHibernate that the child is going to manage the relationship (insofar as inserts/updates are concerned). Normally the parent would manage the relationship, but in the case of a non-null parent id in the child, you run into a chicken and egg problem, hence the need for the child to manage it.

In a m:n relationship you don't have this problem because the the relationship exists outside of the both the parent and child. ie, both can be persisted without the relationship getting in the way.

Please let me know if I'm still misunderstanding the issue's surrounding 1:n vs m:n collections.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 11:21 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
If you need more information on the Parent-Child handling, have a look here:

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/example-parentchild.html

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.