c# - nHibernate 批量大小不起作用

标签 c# database nhibernate

我在 nHibernate(C# - VS 2012) 中遇到批量大小问题。 我在集合和配置中设置了“批量大小”,但它不起作用。

using (var s = OpenSession())
        {
            using (var t = s.BeginTransaction())
            {                   
                Parent parent = s.CreateCriteria(typeof(Parent)).List<Parent>().First();
                Console.Write(parent.Children[0]);
                t.Commit();
            }
        }

nHibernate 分析器显示它一次获取所有子项(例如 1000 个子项),但它应该只需要 5 个。

Parent.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="L6.Model" assembly="L6">
  <class name="Parent">
    <id name="ParentId">
      <generator class="native" />
    </id>
    <bag name="Children" batch-size="5">
      <key column="ID_Parent"/>
      <one-to-many class="Child"/>
    </bag>
  </class>
</hibernate-mapping>

child .hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="L6.Model" assembly="L6">
  <class name="Child">
    <id name="ChildId">
      <generator class="native" />
    </id>
    <many-to-one name="Parent" column="ID_Parent" class="Parent" />
  </class>
</hibernate-mapping>

休眠.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it 
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.dll provider for SQL Server -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">
            Server=.;initial catalog=Lista6;Integrated Security=SSPI
        </property>
    <property name="adonet.batch_size">5</property>
        <property name="show_sql">true</property>
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
        <property name="command_timeout">60</property>
        <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <property name="generate_statistics">true</property>
    <mapping file="Child.hbm.xml" />
    <mapping file="Parent.hbm.xml" />
  </session-factory>
</hibernate-configuration>  

您知道为什么批量大小不起作用吗?

最佳答案

你误解了 batch-size 的意思。

这意味着它将一次读取5 个子集合,而不是加载集合中的 5 个元素。

adonet.batch_size 另一方面,意味着插入/更新/删除语句将以该大小的组发送,以减少往返次数。

关于c# - nHibernate 批量大小不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16576609/

相关文章:

c# - 减少 PostSharp 编译时间开销

c# - 如何使用 QueryOver 从内连接中按列排序?

c# - 引用类时 NHibernate 映射问题(延迟加载问题?)

mysql - 如何正确删除数据库并重新导入?

c# - 将其转换为 SQL 的最佳方法是什么

mysql - MySQL 中的 BLOB 和 TEXT 数据类型有什么区别?

linq - Linq到NHibernate项目状态?有贡献吗?铅?

c# - 如何在 C# 中设置 Windows 默认打印机?

c# - 抛出 HttpResponseException 会导致未处理的异常

c# - 如何通过控制台应用程序在 Windows 2008 Server Web SP2 上的 IIS7 中设置站点?