Thanks for your answer Fabio - I think that I worked it out.

I wrote a very simple program to test this behaviour and I've found
the solution - I'm just posting back to give some closure to my
thread, I hate it when people don't post solutions!

My simple test includes a "Parent" with a string Value, and a "Child"
reference. The "Child" has a string Value and a collection of
"ChildOfChild" objects. "ChildOfChild" has a string value. All 3
classes have a "Parent" link that is a Guid. As I suspected it is easy
to get all the Save and Updates to cascade, I can now populate all
three levels of the data structure and save it all by a single
ISession.Save(Parent).

It appears that in this scenario, whether you have a Class as a
property, or you have a Collection as a property, you use a "many-to-
one" mapping in the parent, with cascade=true. To let Hibernate know
that you intend it to treat the Child as a part of the parent, you
have to use "one-to-one" mapping in the child.

I guess that if the one-to-one is missing, the child won't save when
the parent saves.

It would be good if someone in the know can tell me if all this is
correct, or if there are any exceptions or gotchas that I need to look
out for.


My mapping for this simple DB structure is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="NHTest"
                   namespace="NHTest">

  <class name="Parent" table="parent">
    <id name="Guid" type="Guid" column="Guid">
      <generator class="guid.comb" />
    </id>
    <many-to-one name="Child" cascade="all"/>
    <property name="Value" />
  </class>

  <class name="Child" table="child">
    <id name="Guid" type="Guid" column="Guid">
      <generator class="guid.comb" />
    </id>
    <property name="Value" />
    <one-to-one name="Parent" class="Parent" />
    <set name="Collection" cascade="all">
      <key column="Parent" />
      <one-to-many class="ChildOfChild" />
    </set>
  </class>

  <class name="ChildOfChild" table="childofchild">
    <id name="Guid" type="Guid" column="Guid">
      <generator class="guid.comb" />
    </id>
    <property name="Value" />
    <one-to-one name="Parent" class="Child" />
  </class>

</hibernate-mapping>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to