如何使用Hibernate插入多行到数据库? [英] How to insert multiple rows into database using hibernate?

查看:82
本文介绍了如何使用Hibernate插入多行到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在循环列表并将其插入到数据库中,但是它正在逐一更新记录。最终,我只在数据库的最后一条记录中看到。 >输入名称:Linux,windows,mac

  Session session =(Session)HibernateUtil.getSessionFactory()。openSession(); 
String [] items = pi.getNewLicenseName()。split(,);
for(String item:items)
{
feature.setName(item);
session.save(feature);
}
session.getTransaction()。commit();
HibernateUtil.shutdown();

hibernate.cfg.xml:

 <冬眠-结构> 

< session-factory>


< property name =connection.driver_class> com.microsoft.sqlserver.jdbc.SQLServerDriver< / property>
< property name =connection.url> jdbc:sqlserver:// ******< / property>
< property name =connection.username> *****< / property>
< property name =connection.password> *****< / property>

< property name =dialect> org.hibernate.dialect.SQLServerDialect< / property>


< property name =cache.provider_class> org.hibernate.cache.internal.NoCacheProvider< / property>

< property name =hibernate.current_session_context_class>线程< / property>
< property name =hibernate.current_session_context_class> org.hibernate.context.internal.ThreadLocalSessionContext< / property>


< property name =show_sql> true< / property>
< property name =hibernate.hbm2ddl.auto>更新< / property>

<! - 给注释的实体类命名 - >

< mapping class =com.DAO.Feature/>

< / session-factory>

这里有三次获取循环并插入到数据库中,但以某种方式覆盖了值。因为我

  Hibernate:插入FEATURE(NAME)的值(?)
休眠:更新FEATURE set NAME =? FEATURE_ID =?

请帮助我将多行插入到数据库中。

在Hibernate文档中批处理。



设置属性

  hibernate.jdbc.batch_size 20 

然后使用此代码

 会话会话= sessionFactory.openSession(); 
Transaction tx = session.beginTransaction();

(int i = 0; i <100000; i ++){
Customer customer = new Customer(.....);
session.save(customer);
if(i%20 == 0){// 20,与JDBC批量大小相同
//刷新一批插入并释放内存:
session.flush();
session.clear();
}
}

tx.commit();
session.close();

确保您考虑到您的id生成策略的含义,例如在这里讨论



更新2015-09- 23



我终于找到时间坐下来,在 http://frightanic.com/software-development/jpa-batch-inserts/


I'm looping list and inserting into database , but its getting updating records one by one.finally i'm seeing in database last record of the list only.

input name : Linux,windows,mac

Session session = (Session) HibernateUtil.getSessionFactory().openSession();
String[] items = pi.getNewLicenseName().split(",");
for (String item : items)
{
feature.setName(item);
session.save(feature);
}
 session.getTransaction().commit();
 HibernateUtil.shutdown();

hibernate.cfg.xml:

<hibernate-configuration>

    <session-factory>


    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.url">jdbc:sqlserver://******</property>
    <property name="connection.username">*****</property>
    <property name="connection.password">*****</property>

    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>


        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>


        <property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>

        <!-- Names the annotated entity class -->

         <mapping class="com.DAO.Feature"/>

    </session-factory>

Here three times get the loop and inserting into database.But somehow overwriting the values.Because i'm see the sql insert and update running in console.

Hibernate: insert into FEATURE (NAME) values (?)
Hibernate: update FEATURE set NAME=? where FEATURE_ID=?

Please help me to insert the multiple rows into database.

解决方案

There's a very nice chapter about batch processing in the Hibernate docs.

Set the property

hibernate.jdbc.batch_size 20

Then use this code

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

Make sure you consider the implications for your id-generation strategy e.g. discussed here.

Update 2015-09-23

I finally found the time to sit down and write a detailed article at http://frightanic.com/software-development/jpa-batch-inserts/.

这篇关于如何使用Hibernate插入多行到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆