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: Cascading Save with composite-id parent and child classes
PostPosted: Fri Feb 22, 2008 5:57 am 
Newbie

Joined: Fri Feb 22, 2008 5:39 am
Posts: 14
Hello,

I'm new to NHibernate and I have some problems with cascading saves.

My both calsses "Parent" and "Child" has composite-id. When I save the parent class i would like to save all child objects. So what's the problem ?

I looked into the log file and saw that for the "Parent" class NHibernate performs INSERT query which is correct and for the "Child" class it performs UPDATE query which is not correct because there is no rows in the database to update.

I read that NHiberante doesn't know to save cascading objects with composite-ids. It performs INSERT or UPDATE for the child object depending on unsave-value parameter in coposite-id element of child class. So I can't use it becuase some child elements needs to be updated and some new elements needs to be inserted.

Do you know how can i workaround that issue ? I can't alter tables in my database to create an unique id.

Thanks in advance for your help.

P.S. I use NHibernate v2.0.50727

_________________
Thomas JASKULA - NODEVO
1, avenue du Général de Gaulle
60500 - Chantilly
+33 (0)3 44 26 36 72


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 11:42 am 
Newbie

Joined: Fri Feb 22, 2008 5:39 am
Posts: 14
Hi

After few tests I noticed that it's not a composite-id key problem but even single <id> element with "assigned" generator doesn't work.

It works fine only with "native" generator.

I would like to know how to make work cascading saves with assigned keys ?

thanks for you help.

_________________
Thomas JASKULA - NODEVO
1, avenue du Général de Gaulle
60500 - Chantilly
+33 (0)3 44 26 36 72


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 3:53 pm 
Beginner
Beginner

Joined: Sat Jul 21, 2007 3:56 pm
Posts: 27
Hi durtal,

I guess the problem with the assigned id's is that NHibernate cannot find out by itself whether an entity is transient or persistent.

You can solve this by helping NHibernate a bit.

Therefor you can create an interceptor:

Code:
using NHibernate;
using ObjectFramework.Repository;

namespace ObjectFramework.NHImpl.Interceptor
{
   public class IsUnsavedInterceptor:EmptyInterceptor
   {
      public override bool? IsUnsaved(object entity)
      {
         if (entity is IPersistence)
         {
            return ((IPersistence)entity).IsNew();
         }
         else
         {
            return false;
         }
      }
      
   }
}


... an interface that your entity in question should implement:

Code:
namespace ObjectFramework.Repository
{
   public interface IPersistence
   {
      bool IsNew();
      void SetNew();
   }
}


Code:
using ObjectFramework.Repository;

namespace ObjectFramework.Binding
{
   public class EntityBaseWithAssignedId:EntityBase,IPersistence
   {
      private bool isNew=false;

      bool IPersistence.IsNew()
      {
         return isNew;
      }

      void IPersistence.SetNew()
      {
         isNew=true;
      }
   }
}


... and finally call the following line for each new entity you create:
Code:
((IPersistence) newEntity).SetNew();


Maybe there are easier ways to get the job done, but at least this works for me.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 4:45 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
I seem to recall that for assigned identifiers you can help NHibernate understand whether or not the entity is new by simply providing a Version mapping.

Cheers,

Symon.

_________________
Symon Rottem
http://blog.symbiotic-development.com


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.