Java 如何使用休眠将多行插入数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20458401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:04:57  来源:igfitidea点击:

How to insert multiple rows into database using hibernate?

javahibernate

提问by user2848031

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

输入名称: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.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.

这里 3 次得到循环并插入到数据库中。但是以某种方式覆盖了值。因为我看到 sql 插入和更新在控制台中运行。

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

Please help me to insert the multiple rows into database.

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

采纳答案by Marcel St?r

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

在 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.

确保您考虑对您的 id-generation 策略的影响,例如这里讨论的

Update 2015-09-23

更新 2015-09-23

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

我终于有时间坐下来在https://frightanic.com/software-development/jpa-batch-inserts/写一篇详细的文章。

回答by Arie van Wijngaarden

With the save()method in a session, Hibernate couples the object to a row and this relation remains while the session remains active. Therefore, if you use the same object, you actually update the existing row. The solution is to construct a new object for every row. In this case:

使用save()会话中的方法,Hibernate 将对象耦合到一行,并且在会话保持活动状态时这种关系仍然存在。因此,如果您使用相同的对象,实际上是在更新现有行。解决方案是为每一行构造一个新对象。在这种情况下:

for (String item : items)
{
  Feature feature = new Feature();
  feature.setName(item);
  session.save(feature);
}

回答by samir vora

session.merge(object);
session.flush();
session.clear();