performance - NHibernate insert entity with proxy associations without fetching -


let's there category , product tables. , product has assigned id , references category:

public class product {     public virtual int productid { get; set; }     public virtual category category { get; set; }     public virtual string name { get; set; }      public product() {}     public product(int productid, string name, category category) { ... } } 

when inserting 3 products this:

        var session = sessionfactory.opensession();          product product = new product(1, "new product", session.load<category>(1));         session.save(product);         session.flush();          product = new product(2, "new product 2", session.load<category>(2));         session.save(product);         session.flush();          product = new product(3, "new product 3", session.load<category>(3));         session.save(product);         session.flush(); 

it generate 3 inserts (it not fetch categories).

but when doing same set of actions without flushing after each insert, fetch proxy associations:

        var session = sessionfactory.opensession();          product product = new product(1, "new product", session.load<category>(1));         session.save(product);          product = new product(2, "new product 2", session.load<category>(2));         session.save(product);          product = new product(3, "new product 3", session.load<category>(3));         session.save(product);          session.flush(); 

the generated sql following: nhibernate fetching proxy associations on insert

this simplified example, need insert 1000 of entities , use batching without fetching related entities.

update: complete example can downloaded here

it looks flush initiates sorting of inserted data. documentation said should improve performance batch operations (http://www.nudoq.org/#!/packages/nhibernate/nhibernate/environment/f/orderinserts). avoid such behaviour add

x.setproperty("order_inserts", "false"); 

to nhibernate configuration.


Comments