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: Implementing reference counting with NHibernate
PostPosted: Fri Feb 01, 2008 8:11 am 
Newbie

Joined: Fri Feb 01, 2008 8:02 am
Posts: 2
I have the following database scheme:

Code:
  ______________        __________        ______________
|              |      |          |      |              |
|  ParentOne   |      |  Child   |      |  ParentTwo   |
|______________|      |__________|      |______________|
|              |      |          |      |              |
| *ParentOneId | <--> | *ChildId | <--> | *ParentTwoId |
|  Name        |      |  Name    |      |  Name        |
|  ChildId     |      |__________|      |  ChildId     |
|______________|                        |______________|


That is, a Child can belong to one or more ParentOnes and/or ParentTwos. If there are no references left from ParentOne or ParentTwo to a Child, it should be deleted.

Anyone have any ideas on how to accomplish this?

---

In a first attempt, I have:
  • set cascade to all-delete-orphan
  • implemented ILifecycle.OnDelete() in the Child class where I veto if there are still references left to the child

I can't seem to get this working though. Besides, I don't like the ILifecycle stuff as it brings NHibernate into my domain objects.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 4:10 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Look at my posts near the end of this topic:
http://forum.hibernate.org/viewtopic.php?t=956783

My proposal is essentially to implement IInterceptor.Instantiate and keep a WeakReference dictionary of every entity created by the process. When you need to delete something, you would then have to check this entire WeakReference cache, using Reflection, to see if the entity you want to delete is referenced elsewhere. This approach would be necessary if you have entities detached from (closed) sessions that reference the entity you want to delete since there NHibernate doesn't know anything about detached entities. You would probably need to force garbage collection before going through the WeakReference cache, and of course you should remove references where IsAlive is false.

If you don't work with detached entities at all, then in theory NHibernate should be able to tell you if the object you want to delete has another parent and should provide a way to remove the reference from the other parent. Since it doesn't, and since it doesn't even allow you to examine the session cache, you may need to resort to the WeakReference approach anyway ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 3:16 am 
Newbie

Joined: Fri Feb 01, 2008 8:02 am
Posts: 2
Thanks for the help. It sounds like a lot of work, but I guess it's what I'll have to do.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 3:38 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
For those who are interested, I've implemented this approach to keep WeakReferences and automatically dissociate entities scheduled for deletion from others in still memory using reflection.

It's slow when there are thousands of entities in the session (in my case only one was explicitly put in the session but thousands got pulled in due to cascading rules), and I'll try to optimize the performance, but it appears to be working.


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.