views:

188

answers:

2

I'm loading a lot of data into the database with NHibernate. As I'm inserting, I want to check for duplicates.

I call saveorupdate in each loop without flushing the session.

Is there a way to query this session for duplicates without flushing?

A: 

Maybe you should try integrating nhibernate with Spring.NET, to allow Spring.NET to handle the sessions by creating DAO support. Hopefully this will help! http://www.springframework.net/docs/1.1-RC2/reference/html/dao.html

Chung Pow
A: 

you can write custom interceptor which gives you onsave method with following signature public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types)

then you can have duplicate check logic, inside this method. (I.e. by keeping dictionary of all ids already present for that type)

Alternatively, you can directly hookup your duplicate checking code in custom implementation of ISaveEventListner. you need to register your custom event listner at the time of configuring nhibernate. here is snippet for ref

Configuration cfg = new Configuration();
ISaveEventListener[] stack = new ISaveEventListener[] { new MySaveListener(), new DefaultSaveEventListener() };
cfg.EventListeners.SaveEventListeners = stack;
Since I've moved past this problem, I don't have a way to verify if this is correct. If someone can verify it, then I will accept the answer.
Aaron Smith