Hibernate Books

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: Bag not saving items in collection
PostPosted: Tue Sep 13, 2011 2:40 pm 
Newbie

Joined: Mon Apr 25, 2011 5:13 pm
Posts: 5
I have a Class called Intersection that derives from Device. My Intersection class has an IList collection of a class called Zone called Zones. I am trying to map my Intersection and Zone classes using NHibernate.

The problem I am running into is when I save my Intersection using session.Save(intersection), the zones that are contained within my Zones List are not being saved.

I can save the Zone by itself before saving the Intersection. This fills out the Zone entry in the Zone table with all the information for the zone with a NULL value inserted into the Intersection_Id column. However, after doing this, when I save the Intersection that contains the zone in the list, they are not being linked to each other. Also, if I save my intersection and then try saving the zone using a:
Code:
foreach (Zone z in intersection.Zones) session.Save(z);

The zone is not being saved.

Any time I try to save the zone through the Intersection, either calling session.Save(intersection) or the foreach code above, the SQL generated does not include any zone information at all.

I'm not really sure why they aren't mapping at this point.

Here is my mapping for the Device/Intersection:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="EMTRAC.Devices.Device, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Device`" lazy="false">
    <id name="PK" type="System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="PK" />
      <generator class="identity" />
    </id>
    <many-to-one class="EMTRAC.Connections.Connection, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="LocalConnection" lazy="false" cascade="all">
      <column name="LocalConnection_id" />
    </many-to-one>
    <many-to-one class="EMTRAC.Connections.Connection, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Connection" lazy="false" cascade="all">
      <column name="Connection_id" />
    </many-to-one>
    <many-to-one class="EMTRAC.Packets.Packet, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Configuration" lazy="false" cascade="all">
      <column name="Configuration_id" />
    </many-to-one>
    <joined-subclass name="EMTRAC.Intersections.Intersection, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" lazy="false">
      <key>
        <column name="Device_id" />
      </key>
      <bag name="Zones" cascade="all-delete-orphan">
        <key>
          <column name="Intersection_id" />
        </key>
        <one-to-many class="EMTRAC.Zones.Zone, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </bag>
      <many-to-one class="EMTRAC.Intersections.Streets, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Streets" lazy="false" cascade="all">
        <column name="Streets_id" />
      </many-to-one>
      <many-to-one class="EMTRAC.Positions.Position, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Position" lazy="false" cascade="all">
        <column name="Position" />
      </many-to-one>
    </joined-subclass>
</hibernate-mapping>


And here is my mapping for the Zones:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="EMTRAC.Zones.Zone, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Zone`" lazy="false">
    <id name="ID" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="PK" />
      <generator class="identity" />
    </id>
    <property name="Active" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Active" />
    </property>
    <property name="Dir" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Dir" />
    </property>
    <property name="IntID" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="IntID" />
    </property>
    <property name="Width" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Width" />
    </property>
    <many-to-one class="EMTRAC.Headings.Heading, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Heading" cascade="all-delete-orphan">
      <column name="Heading_id" />
    </many-to-one>
    <many-to-one class="EMTRAC.Positions.Position, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Start" cascade="all-delete-orphan">
      <column name="Start_id" />
    </many-to-one>
    <many-to-one class="EMTRAC.Positions.Position, EMTRAC_v3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Finish" cascade="all-delete-orphan">
      <column name="Finish_id" />
    </many-to-one>
  </class>
</hibernate-mapping>


Can anyone point me in the right direction? I have cascading set so it should populate the children contained in the Zones list. What's going on?


Top
 Profile  
 
 Post subject: Re: Bag not saving items in collection
PostPosted: Tue Sep 13, 2011 3:56 pm 
Newbie

Joined: Mon Apr 25, 2011 5:13 pm
Posts: 5
Saving the Zone first, then saving the Intersection, then calling Session.Flush() for the ISession saves and links everything correctly.

Why does this work and why did this not work with saving only the Intersection?


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.