NHibernate插入实体与代理关联而不提取

时间:2015-07-07 11:59:09

标签: performance nhibernate proxy

我们说有分类和产品表。产品已分配ID 并引用类别:

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) { ... }
}

插入三个这样的产品时:

        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();

它只会生成三个插入(它不会获取类别)。

但是当在每次插入后执行相同的操作集而不刷新时,它将获取代理关联:

        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();

生成的SQL如下: NHibernate fetching proxy associations on insert

这是一个非常简化的示例,实际上我需要插入大约1000个实体,并希望在不获取所有相关实体的情况下使用批处理。

更新:完整示例可以下载here

0 个答案:

没有答案