9

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.

user2848031
  • 187
  • 12
  • 36
  • 69

3 Answers3

36

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();
    }
}
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 https://frightanic.com/software-development/jpa-batch-inserts/.

diginoise
  • 7,352
  • 2
  • 31
  • 39
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
6

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:

for (String item : items)
{
  Feature feature = new Feature();
  feature.setName(item);
  session.save(feature);
}
  • 2
    There's a reason why Hibernate calls your proposed solution a "naive approach" (see the link to the docs in my answer). You really shouldn't do it like that... – Marcel Stör Dec 08 '13 at 21:08
  • You are right, but the question was more related to why only one insertion was done than on how to do batch updating. – Arie van Wijngaarden Dec 09 '13 at 12:00
  • 1
    For those wondering why it's a "naive approach", you could encounter a OutOfMemoryException for an exceptionally large array: "This would fall over with an OutOfMemoryException somewhere around the 50,000th row. That is because Hibernate caches all the newly inserted `Feature` instances in the session-level cache. In this chapter we will show you how to avoid this problem." (http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch15.html) – Shawn Apr 11 '19 at 13:24
-1
session.merge(object);
session.flush();
session.clear();
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53