Specify entity / SQL insertion order in NHibernate transaction without multiple flushes

25 Views Asked by At

I have a recursive structure where NHibernate entities in my system (called "Templates") can be comprised of simpler & simpler templates. When cloning an entire template structure, I currently recreate the tree in its entirety in C#, within a transaction, and then instruct NHibernate to persist the top-most template. And then the configuration details manage the persistence of all of the "descendant" templates.

public class Template
{
    public virtual Template ParentTemplate { get; set; }
    public virtual ISet<Template> ChildTemplates { get; set; }
    // < Data >
}
<many-to-one name="ParentTemplate" column="ParentTemplateId" />
<set name="ChildTemplates"  cascade="none" lazy="true" inverse="true" >
  <key column="ParentTemplateId"/>
  <one-to-many class="Template, Domain" />
</set>

The issue I'm running into relates to certain queries & triggers in my DB. If possible, I'd like to instruct NHibernate to insert these cloned templates in a particular order. Namely, I'd like to make it so that when a "child" template is inserted, its "parent" template is already a record in the DB. I.e. I want it to insert the new cloned templates, "from the top down."

I realise that a simple way to manage this would be to add all templates when they're created to the NHibernate session, and then "flush" at least once at each "level" before adding in child associations. Though I was a bit concerned with performance here. So I wanted to know whether it was possible to specify this behaviour in NHibernate itself; that way I wouldn't require multiple DB calls to achieve the same outcome.

0

There are 0 best solutions below