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.

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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...
You are right, but the question was more related to why only one insertion was done than on how to do batch updating.
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." (docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch15.html)
-1
session.merge(object);
session.flush();
session.clear();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.