-->
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.  [ 7 posts ] 
Author Message
 Post subject: problems with IInterceptor
PostPosted: Sat Jul 23, 2005 2:01 pm 
Hi

When i saves a object the first time I need to update a property and i thought the IInterceptor.OnSave would help me with this but..

In the onSave method I have.

Code:
if ( entity.GetType() == typeof(BLL.Supplier))
         {
            BLL.Supplier supplier = (BLL.Supplier) entity;
            supplier.SupplierId = BLL.SupplierIdStrategy.GetNextValue();
            return true;
         }


the value is updated and i return true, but it seems like NHibernate doesnt care that I have modified the state of the object, because it tries to insert null. The GetNextValue just return a new guid as a string, so there is nothing strange there. I have even tried to modify the state, by using the propertyNames array but no sucess ther either..


Top
  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 4:12 pm 
Beginner
Beginner

Joined: Wed Jun 29, 2005 10:40 am
Posts: 30
Location: denver, co
Try the ILifeCycle interface instead. I've never used interceptors, but have had great success with ILifeCycle.

ILifeCycle is an interface that the BLL.Supplier class should implement. It gives you an opportunity to modify the state of a persistent object at key points (load, save, update, etc), and allow you to silently veto operations like save or update.

There is a brief article here on the interface. It explains it well.


Also, make sure that in your mapping file, the SupplierId is mapped with a generator of "assigned", with an appropriate unsaved-value (for guids, this should be "00000000-0000-0000-0000-000000000000").


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 2:35 am 
Beginner
Beginner

Joined: Thu May 12, 2005 2:14 am
Posts: 33
Location: Sweden, Sk
Hi

Thanks for your reply tylord.

I havent time to test ILifecycle today but it looks like what Im looking for. Read about the ILifeCycle interface for Hibernate and the docs says that onUpdate isnt called when you do a flush. You must pass the object to update method to trigger the onUpdate, do you know if its the same in NHibernate?

And the onSave method. Is that only called when i saves the object for the first time?

Thx again tylord!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 12:24 pm 
Beginner
Beginner

Joined: Wed Jun 29, 2005 10:40 am
Posts: 30
Location: denver, co
It does appear that ILifeCycle in NHibernate behaves the same as Hibernate in that it only triggers OnUpdate on an explicit call to session.Update(). I wasn't aware of this - but some quick tests do show that. I suppose I've dodged this issue in the past since my OnUpdate methods generally didn't contain much.

OnSave will fire only when the object is inserted.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 9:23 am 
Beginner
Beginner

Joined: Thu May 12, 2005 2:14 am
Posts: 33
Location: Sweden, Sk
Hi again

Thx for your help tylord! :thumbup

As I currently need to modify the object during the first save ILifeCycle will work perfect for me. But what is a bit annoying is that I dont understand the meaning of the IInterceptors and how to work with them.

Are there somebody who help me understand when and how to use the Interceptors?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 2:52 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 3:22 pm
Posts: 38
Location: Menlo Park, CA
In my interceptor, I change the state[] array, not the object.

Here's my code for setting String.Empty to null (a hack for working with Oracle).

Code:
         bool modified = false;
         for (int i = 0; i < types.Length; i++)
         {
            if ( (types[i] is NHibernate.Type.AnsiStringType || state[i] is string)
               && state[i] != null
               && state[i].ToString() == "")
            {
               state[i] = null;
               modified = true;
            }
         }
         return modified;


2) Is there a reason you're comparing types instead of using the "is" operator?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 20, 2005 12:16 pm 
Beginner
Beginner

Joined: Thu May 12, 2005 2:14 am
Posts: 33
Location: Sweden, Sk
Hi

Sorry for the late answer, rponton. (the first week after vacation can be a nightmare).

The short answer is no. Tbh, I dont know if there is any difference.. Is there?


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