views:

402

answers:

2

Hello,

I think this is an easy question, but my googling is weak on this.

I had the problem described in the following link with regard to a generated ID and cascading:

https://www.hibernate.org/hib_docs/nhibernate/html/example-parentchild.html (towards the bottom)

I fixed it using their suggested method of an Interceptor. Everything appears to be working, so I am happy.

That said, I have no idea what the significance of the return value is from methods such as:

    public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types)
    {
        if (entity is Persistent) ((Persistent)entity).OnLoad();
        return false;
    }

    public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
    {
        if (entity is Persistent) ((Persistent)entity).OnSave();
        return false;
    }

In both cases false is returned.

When I google about NHibernate Interceptors I see plenty of examples of how to write one. Some instead return true (http://www.lostechies.com/blogs/rhouston/archive/2008/03/27/creating-a-timestamp-interceptor-in-nhibernate.aspx). I have no idea what the difference is here. My code is working, but Interceptors seem useful to me so I'd like to have a better understanding.

A: 

Huey,

Read this post, i'm not a .net programmer, but this post is very usefull:

http://knol.google.com/k/fabio-maulo/nhibernate-chapter-11/1nr4enxv3dpeq/14#

Chapter 11. Interceptors and events It is often useful for the application to react to certain events that occur inside NHibernate. This allows implementation of certain kinds of generic functionality, and extension of NHibernate functionality.

11.1. Interceptors The IInterceptor interface provides callbacks from the session to the application allowing the application to inspect and/or manipulate properties of a persistent object before it is saved, updated, deleted or loaded. One possible use for this is to track auditing information. For example, the following IInterceptor automatically sets the createTimestamp when an IAuditable is created and updates the lastUpdateTimestamp property when an IAuditable is updated.

Hope it enlight you a little more.

Kamia
I have actually read this before but maybe I need to read more between the lines. In the example they provide, it looks like it returns true if it modifies the entity and false if it does not. Maybe that is all there is to it. I guess I should have looked closer at the code and not expect a 'the return value means (blank)' instruction.
eyston
- this is an old post but just in case someone finds it useful - I had to modify the return value in the interceptor to return True if IsSaved is false, which will then instruct NH to save the instance as new (with insert).
TheMar
+2  A: 

I believe the return value should indicate if the state parameter has been changed in the interceptor method. You're right - it's a tough one to google at the moment - the NHibernate site moved recently and google doesn't seem to find as much useful info as it used to.

Steve Willcock