2013-05-15 57 views
1

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

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> 

儿童。的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> 

的hibernate.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> 

你有什么想法为什么批量大小不起作用?

回答

5

您误解了batch-size的含义。

这意味着它会立刻读取5儿童系列,而不是它将加载该系列的5个元素。

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

+0

非常感谢您的解释! –