-->
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.  [ 2 posts ] 
Author Message
 Post subject: How does one intercept a many-to-many persistence?
PostPosted: Tue Jul 24, 2007 4:59 am 
Beginner
Beginner

Joined: Thu May 31, 2007 1:19 pm
Posts: 23
Hi

I wish to detect when my many-to-many relationship is being persisted. I am using a bag for this.

NHibernate does not call into my Interceptor to tell me when these bags are being saved (it calls in for Pre, Post flush and FindDirty)

Any help would be much appreciated,
Mark.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 6:32 am 
Beginner
Beginner

Joined: Thu May 31, 2007 1:19 pm
Posts: 23
Looking at the source there doesn't seem to be any support for intercepting persisting of entity collections.

I think something like this will work, but there's probably a more efficient way of doing it - please feel free to comment :)

public void PreFlush(System.Collections.ICollection entities)
{
foreach (object entry in entities)
{
Type objType = entry.GetType();
NHibernate.Metadata.IClassMetadata meta = session.SessionFactory.GetClassMetadata(objType);
for (int i = 0; i < meta.PropertyNames.Length; i++)
{
IType propertyType = meta.PropertyTypes[i];
if (propertyType.IsCollectionType)
{
object propertyValue = meta.GetPropertyValue(entry, meta.PropertyNames[i]);
IPersistentCollection persistentCollection = propertyValue as IPersistentCollection;
if (persistentCollection != null && persistentCollection.IsDirty)
{
ICollection snapshot = persistentCollection.CollectionSnapshot.Snapshot;
if (snapshot != null)
{
// Build list of items common to snapshot and current entries
ArrayList commonItems = new ArrayList();
foreach (object snapshotItem in snapshot)
{
foreach (object item in persistentCollection.Entries())
{
if (snapshotItem.Equals(item))
{
commonItems.Add(item);
}
}
}
// Handle deletes
foreach (object snapshotItem in snapshot)
{
if (!commonItems.Contains(snapshotItem))
{

}
}
// Handle inserts
foreach (object item in persistentCollection.Entries())
{
if (!commonItems.Contains(item))
{

}
}
}
}
}
}
}
}


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