InspiredBy
InspiredBy

Reputation: 4320

Determine if NHibernate entity has cascading records

I'm looping though and inserting into my DB several million records. Performance is a number one priority.

I want to take advantage of using Stateless sessions but as you may be aware they do not support cascading objects on more complex entities.

If there a general way I can determine whether an entity has cascading records or doesn't? if it does I'll use regular sessions and if doesn't - stateless ones.

Does it make sense to do something like this? If so what's the best way to do it?

protected virtual ISession Session
{
  get { return ....SessionFactory.GetCurrentSession(); }
}

protected virtual IStatelessSession StatelessSession
{
   get { return ....SessionFactory.OpenStatelessSession(); }
}

public virtual T SaveOrUpdate(T entity)
{ 
    if(entity.HasCascadingrecords) //<- Something like this
    {
        Session.Save(entity);
    }
    else
    {
         StatelessSession.Save(entity);
    }
}

Upvotes: 2

Views: 64

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

Let's follow these to get some more initial details:

And at the end, we can use snippet like the one below:

// get type of the Entity we need
var entityType = typeof(TEntity);

// get persister
var entityType = typeof(TEntity);
var factory = ... // get factory
var persister = factory.GetClassMetadata(entityType) as AbstractEntityPersister;

// check this setting
var hasCascades = persister.HasCascades;

// use it somehow
return hasCascades

Upvotes: 2

Related Questions