-->
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.  [ 6 posts ] 
Author Message
 Post subject: nhibernate not saving my collections
PostPosted: Thu Jun 09, 2005 7:22 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I have a somewhat complex design for my photos and I'm having problems getting nhibernate to save everything.

Here's basically what I am doing in code:

Code:
Plan p = new Plan();
//set a few properties

p.Save();  //this does the nhibernate session stuff for me.


This works, the plan goes in the db just fine.

Then I try this:
Code:
Photo p = new Photo();
//set a few properties

p.Photos.FeaturedPhoto = p;
p.Save();


and the featured photo reference doesn't get saved in the database (the photo does however)

The Photos property is a class that has:
FeaturedPhoto
GalleryPhotos
Elevations
Floorplans

and that object gets saved correctly, the photo references do not however.
Here are the relevant mapping files:


Plan
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:nhibernate-mapping-2.0 nhibernate-mapping-2.0.xsd">

   <class name="Foundation.Business.Plan, Foundation" table="Plans">
      <id name="ID" type="Int32" unsaved-value="0" column="id">      
         <generator class="identity" />
      </id>
      
      <property name="Name" type="String" length="100" column="name"></property>
      <property name="RefNumber" type="String" length="50" column="refNumber" not-null="false"></property>
      <property name="PlanTypeID" type="Int32" column="planType_id"></property>
      <property name="Description" type="String" length="5000" column="description" not-null="false"></property>
      <property name="SquareFootage" type="Int32" column="squareFootage"></property>
      <property name="BasePrice" type="Int32" column="basePrice" not-null="false"></property>
      <property name="Floors" type="Int32" column="floors"></property>
      <property name="Beds" type="Int32" column="beds"></property>
      <property name="Baths" type="float" length="8" column="baths"></property>
      <property name="GarageNumCars" type="Int32" column="garageNumCars"></property>
      <property name="LivingRooms" type="Int32" column="livingRooms"></property>
      <property name="DiningRooms" type="Int32" column="diningRooms"></property>
      <property name="MastBedFloor" type="Int32" column="masterBedFloor"></property>
      <property name="ContactUser_id" type="Int32" column="contactUser_id" not-null="false"></property>
      
      <many-to-one name="Photos" column="photoCollection_id" class="Foundation.Business.Collections.PlanPhotoCollection, Foundation" cascade="all"   />
            
      <bag name="Communities" table="Plans_Communities_Join">
         <key column="plan_id" />
         <many-to-many class="Foundation.Business.Community, Foundation" column="community_id"/>
      </bag>
      
      <bag name="Features" table="Plans_Features_Join">
         <key column="plan_id" />
         <many-to-many class="Foundation.Business.Feature, Foundation" column="feature_id"/>
      </bag>
      
      
   </class>

</hibernate-mapping>


PhotoCollection
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:nhibernate-mapping-2.0 nhibernate-mapping-2.0.xsd" >
   <class name="Foundation.Business.Collections.PhotoCollection, Foundation" table="PhotoCollections">
      <id name="ID" column="id" type="Int32" unsaved-value="0">
         <generator class="identity" />
      </id>         
            
      <many-to-one name="FeaturedPhoto" class="Foundation.Business.Photo, Foundation" cascade="all" column="featuredPhoto_id" />
      
      <bag name="GalleryPhotos" table="Gallery_Photos">
         <key column="photoCollection_id" />         
         <many-to-many class="Foundation.Business.Photo, Foundation" column="photo_id" />
      </bag>                              
      
      <joined-subclass name="Foundation.Business.Collections.PlanPhotoCollection, Foundation" table="PlanPhotos">
         <key column="photoCollection_id"/>         
         
         <bag name="Elevations" table="Plan_Elevations">
            <key column="planPhoto_id" />
            <many-to-many class="Foundation.Business.Photo, Foundation" column="elevation_id" />
         </bag>
         
         <bag name="Floorplans" table="Plan_Floorplans">
            <key column="planPhoto_id" />
            <many-to-many class="Foundation.Business.Photo, Foundation" column="floorplan_id" />
         </bag>         
      </joined-subclass>
      
      <joined-subclass name="Foundation.Business.Collections.CommunityPhotoCollection, Foundation" table="CommunityPhotos">
         <key column="photoCollection_id" />
         
         <many-to-one name="AreaMap" class="Foundation.Business.Photo, Foundation" column="areaMap_id" />
         <many-to-one name="PlotMap" class="Foundation.Business.Photo, Foundation" column="plotMap_id" />
      </joined-subclass>      
                  
   </class>

</hibernate-mapping>



Photo
Code:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:nhibernate-mapping-2.0 nhibernate-mapping-2.0.xsd" >

   <class name="Foundation.Business.Photo, Foundation" table="Photos">
      <id name="ID" type="Int32" column="id"  unsaved-value="0">
         <generator class="identity"></generator>
      </id>
      
      <property name="Title" type="String" length="50" column="title" />
      <property name="Description" type="String" length="255" column="description" />
      <property name="Path" type="String" length="255" column="path" />
      <property name="Filename" type="String" length="255" column="filename" />
      <property name="Filesize" type="Int32" column="fileSize" />
      <property name="Width" type="Int32" column="width" />
      <property name="Height" type="Int32" column="height" />
      <property name="TimesViewed" type="Int32" column="timesViewed" />
      <property name="PhotoType" column="photoType_id" />
      
      
      
   </class>


</hibernate-mapping>



Again, what happens above is the plan is saved, the photo is saved, the photoCollection table gets a record inserted, but the featured column is null.

Thanks in advance for any advice.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 3:53 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
update:

I have log4net writing to a logfile now, so I will post that here if it helps.

One thing I noticed is that now the featured photo is saved, but the 2 collections (elevations and floorplans) are not.


Here is the log file (snipped for relevancy) :
Code:
NHibernate.Impl.SessionFactoryImpl [] <> - Instantiated session factory
2005-06-10 14:37:40,625 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - opened session
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - flushing session
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushing entities and processing referenced collections
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - Processing unreferenced collections
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 unreachable collections.
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - scheduling collection removes/(re)creates/updates
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 for recreate (0), remove (0), and update (0)
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - executing flush
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - post flush
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - disconnecting session
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - transaction completion
2005-06-10 14:37:46,781 [3964] DEBUG NHibernate.Impl.SessionImpl [] <> - closing session
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - opened session
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - flushing session
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushing entities and processing referenced collections
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processing unreferenced collections
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 unreachable collections.
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - scheduling collection removes/(re)creates/updates
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 for recreate (0), remove (0), and update (0)
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - executing flush
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - post flush
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - disconnecting session
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - transaction completion
2005-06-10 14:38:10,421 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - closing session
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - opened session
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - flushing session
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushing entities and processing referenced collections
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processing unreferenced collections
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 unreachable collections.
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - scheduling collection removes/(re)creates/updates
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 for recreate (0), remove (0), and update (0)
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - executing flush
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - post flush
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - disconnecting session
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - transaction completion
2005-06-10 14:38:10,687 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - closing session
2005-06-10 14:38:28,140 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - opened session
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - flushing session
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushing entities and processing referenced collections
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processing unreferenced collections
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 unreachable collections.
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - scheduling collection removes/(re)creates/updates
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 for recreate (0), remove (0), and update (0)
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - executing flush
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - post flush
2005-06-10 14:38:28,171 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - disconnecting session
2005-06-10 14:38:28,187 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - transaction completion
2005-06-10 14:38:28,187 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - closing session
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - opened session
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - flushing session
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushing entities and processing referenced collections
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processing unreferenced collections
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 unreachable collections.
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - scheduling collection removes/(re)creates/updates
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 for recreate (0), remove (0), and update (0)
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - executing flush
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - post flush
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - disconnecting session
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - transaction completion
2005-06-10 14:38:29,718 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - closing session
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - opened session
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - flushing session
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushing entities and processing referenced collections
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processing unreferenced collections
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 unreachable collections.
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - scheduling collection removes/(re)creates/updates
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 for recreate (0), remove (0), and update (0)
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - executing flush
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - post flush
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - disconnecting session
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - transaction completion
2005-06-10 14:38:30,953 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - closing session
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - opened session
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - flushing session
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushing entities and processing referenced collections
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processing unreferenced collections
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 unreachable collections.
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - scheduling collection removes/(re)creates/updates
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Processed 0 for recreate (0), remove (0), and update (0)
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - executing flush
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - post flush
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - disconnecting session
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - transaction completion
2005-06-10 14:46:23,828 [2336] DEBUG NHibernate.Impl.SessionImpl [] <> - closing session



Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 11, 2005 1:43 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
i thought the default cascade on collections was cascade="none"... have you tried setting the cascade attribute on the collections in your PhotoCollection sub-classes?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 12, 2005 7:42 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Yes I have tried this...

now I am thinking the problem lies withing my NHibernate saving implementation.

Basically I use the standard process for saving an object:



    Get the current session (reconnect if disconnected)
    Begin transaction
    do some stuff
    Commit transaction
    Catch errors & rollback transaction
    Finally flush & disconnect session


When I thought about this in more depth I thought that this code would be nested for child collections... so basically i put in some logic that:

if I didn't start the transaction, then I will not commit or roll it back.. This way the parent operation would be the one to rollback or commit all of the transactions.

I realized yesterday that this code was causing most of the problems... so now I am guessing that nhibernate manages all of that for me? Is this true?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 2:41 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Yes, NH will manage the "nested" transactions properly. I have several objects that map child objects/collections. Any time the call to persist the child object fails at the DB layer the child object is not saved and the parent object changes are rolled back to (like version), etc.

this is my basic SaveOrUpdate wrapper:

Code:
public void SaveOrUpdate(object o)
{
   ITransaction t = this.dbSession.BeginTransaction();

   try
   {
      PropertyInfo lastUpdate = o.GetType().GetProperty("LastUpdate", BindingFlags.DeclaredOnly);

      if (lastUpdate != null)
         lastUpdate.SetValue(o, DateTime.Now, null);

      this.dbSession.SaveOrUpdate(o);
      t.Commit();
   }
   catch
   {
      t.Rollback();
      throw;
   }
}


I am going to replace that little reflection bit with an Auditing Interceptor to track more data pertaining to changes made in the DB, but otherwise I've not had any problems with my transactions and saving/updating children.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 3:58 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Thanks for the replies. Looks like I was trying to overdo it a little :)


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