-->
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: IInterceptor and session
PostPosted: Thu Jan 24, 2008 9:54 am 
Newbie

Joined: Tue Dec 11, 2007 3:36 pm
Posts: 7
I have a many-to-one object that when deleted in a session should set the parent entity's many-to-one property to null. The many-to-one object does not have a reference to the parent entity.

I have seen one way to do this is to use ILifecycle.OnDelete to get the related parent entities and set their many-to-one properties to null...however ILifecycle has been deprecated and replaced by IInterceptor. Unfortunately, IInterceptor has no way to access the session, so I can't go off and search for the affected parent entities!

What is the recommended way to handle this problem?


Top
 Profile  
 
 Post subject: Access as normal
PostPosted: Thu Jan 24, 2008 5:03 pm 
Newbie

Joined: Fri Jan 18, 2008 7:45 pm
Posts: 18
Location: Eugene, OR
I'm working on updates to the Interceptor that I'll be posting to the JIRA as soon as it is back online that relate specifically to deleting objects.

To answer your basic question, in your custom interceptor (which should be inherited from EmptyInterceptor instead of implementing the IInterceptor interface if possible) simply access your session as usual. For example:

Code:
public override void OnDelete(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
    if(entity is IDeletedMyParents)
    {
        entity.ParentObject.ForeignKey = null;
        NHibernateHelper.GetCurrentSession().Save(entity.ParentObject);
    }
}


However, it seems like you're probably working against your own schema here, and really should update that instead. You probably don't have your cascade attributes set correctly in the schema...

_________________
Woil / Will Shaver / http://primedigit.com/


Top
 Profile  
 
 Post subject: Re: Access as normal
PostPosted: Sat Jan 26, 2008 3:14 pm 
Newbie

Joined: Tue Dec 11, 2007 3:36 pm
Posts: 7
Woil wrote:
I'm working on updates to the Interceptor that I'll be posting to the JIRA as soon as it is back online that relate specifically to deleting objects.

To answer your basic question, in your custom interceptor (which should be inherited from EmptyInterceptor instead of implementing the IInterceptor interface if possible) simply access your session as usual. For example:

Code:
public override void OnDelete(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
    if(entity is IDeletedMyParents)
    {
        entity.ParentObject.ForeignKey = null;
        NHibernateHelper.GetCurrentSession().Save(entity.ParentObject);
    }
}


However, it seems like you're probably working against your own schema here, and really should update that instead. You probably don't have your cascade attributes set correctly in the schema...


I'm a bit confused about your solution...the entity that is being deleted is the parent object, so that is what is passed into OnDelete. Your solution makes it look like the child entity is passed in during the OnDelete.

As for the cascade, I'm assuming you are talking about a cascade from the parent to the children on the database schema? I'm not a big fan of database triggers or cascades as I see them as business rules that should be in the application...I'm one of those "databases should just be a data store" guys :) So, I need to implement this cascade in the .NET code, and I'm looking for a way to do it...I had no idea you could call NHibernateHelper to get the current session...that may be what I am looking for, but on the other hand I've seen it written that messing with the session using an interceptor is not a good idea...and thoughts on that?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 3:20 pm 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
Could you post an example of the mapping or db schema for what you're trying to accomplish?

It sounds like you have a child with a many-to-one to a parent. When you delete the parent you want the child to be automatically deleted? If that's the case, then what about just setting the cascade in the parent's mapping file for the child bag to all or all-delete-orphan?


Top
 Profile  
 
 Post subject: Re: Access as normal
PostPosted: Sat Jan 26, 2008 3:46 pm 
Newbie

Joined: Fri Jan 18, 2008 7:45 pm
Posts: 18
Location: Eugene, OR
mikeg22 wrote:
I'm a bit confused about your solution...the entity that is being deleted is the parent object, so that is what is passed into OnDelete. Your solution makes it look like the child entity is passed in during the OnDelete.


The OnDelete method fires for every object just prior to deletion. Parents and children alike.

mikeg22 wrote:
As for the cascade ... So, I need to implement this cascade in the .NET code, and I'm looking for a way to do it...


I'm not talking about a database delete, I'm talking about one in your mapping.

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html_single/#manipulatingdata-graphs

mikeg22 wrote:
I had no idea you could call NHibernateHelper to get the current session...that may be what I am looking for, but on the other hand I've seen it written that messing with the session using an interceptor is not a good idea...and thoughts on that?


The NHibernateHelper class is from the introduction to NHibernate in the docs. I'm calling it in my interceptor and it does work without any problems for me. I haven't been using NHibernate for terribly long time, but I have looked through the source for this area. All updates/inserts/deletes are queued so it doesn't matter where you call them from.

_________________
Woil / Will Shaver / http://primedigit.com/


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 6:16 pm 
Newbie

Joined: Tue Dec 11, 2007 3:36 pm
Posts: 7
benhyrman wrote:
Could you post an example of the mapping or db schema for what you're trying to accomplish?

It sounds like you have a child with a many-to-one to a parent. When you delete the parent you want the child to be automatically deleted? If that's the case, then what about just setting the cascade in the parent's mapping file for the child bag to all or all-delete-orphan?


The situation is that when I delete the parent, I want the children foreign keys to be set to null...you can tell this isn't a real parent-child relationship, but more of a foreign key relationship, like Address (M:1) State for example.
When the state is deleted, the addresses with that state shouldn't be deleted, but their state_id should be reset to null.


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.