【问题标题】:Bulk insert of component collection in Hibernate?在 Hibernate 中批量插入组件集合?
【发布时间】:2011-11-13 17:44:09
【问题描述】:

我有如下所列的映射。

当我更新分离的类别项目(不包含任何 Hibernate 类,因为它来自 dto 转换器)时,我注意到 Hibernate 将首先删除所有雇主工资实例(集合链接),然后插入所有雇主工资条目一对一:(...

我知道它必须删除然后插入所有条目,因为它已完全分离。

但是,我不明白,为什么 Hibernate 不通过批量插入插入所有条目?.. 即:将所有雇主工资条目全部插入一条 SQL 语句中?

如何告诉 Hibernate 使用批量插入? (如果可能的话)。 我尝试使用以下值,但没有发现任何区别:

hibernate.jdbc.batch_size=30

我的映射sn-p:

<class name="com.sample.CategoriesDefault" table="dec_cats" >
 <id name="id" column="id" type="string" length="40" access="property">
  <generator class="assigned" />
 </id>
 <component name="incomeInfoMember" class="com.sample.IncomeInfoDefault">
   <property name="hasWage" type="boolean" column="inMemWage"/>
    ...
   <component name="wage" class="com.sample.impl.WageDefault">
     <property name="hasEmployerWage" type="boolean" column="inMemEmpWage"/>
      ...
     <set name="employerWages" cascade="all-delete-orphan" lazy="false">
      <key column="idCats" not-null="true" />
      <one-to-many entity-name="mIWaEmp"/>
     </set>
   </component>
 </component>
</class>

【问题讨论】:

    标签: hibernate hibernate-mapping


    【解决方案1】:

    批量插入 - 用于快速插入的特定本机方式,通常用于本机外部工具,例如用于 MS SQL 的 bcp.exe。 你说的是Batch-insert,尝试使用像这个例子这样的代码:

    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    
    for ( int i=0; i<100000; i++ ) {
        Customer customer = new Customer(.....);
        session.save(customer);
        if ( i % 20 == 0 ) { //20, same as the JDBC batch size
            //flush a batch of inserts and release memory:
            session.flush();
            session.clear();
        }
    }
    
    tx.commit();
    session.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 2012-02-16
      • 2015-07-03
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多