JPA 2.0: How to improve performance on bulk insertion through JPA

放肆的年华 提交于 2019-12-06 09:11:53

问题


Example:

I have three tables: location, department, employee

now lets say location and department are master tables which already has whole data. Now I need to insert 1000 Employees list through JPA. I have relationship with Location and department in Employee Table as well.

so now to insert the entry in Employee, following I am doing:

for loop...1000
 Employee e = new Employee();
 e.setId(12);
 e.setEmpname("ABC");
 Location l = null;
 l = em.find(Location.class, 234);
 e.setLocation(l);
  Department d = null;
 d = em.find(Department.class, 111);
 e.setDepartment(d);
 em.persist(e);
loop ends...

It's taking some time to load the data into DB. Is it the only way to insert the data through JPA, as it is slowing down the performance. I don't want to use native queries. Please suggest if anybody have better approach to make it more efficient.


回答1:


JPA 2.0 doesn't provide specific support for batch inserts. Keeping within the JPA idiom, you can do this:

EntityManager em = ...;
EntityTransaction tx = em.getTransaction();
tx.begin();

for (int i = 0; i < 100000; i++) {
    Employee e = new Employee();
    // setup entity
    em.persist(e);
    if ((i > 0) && (i % 20 == 0)) { // Flush in batches of 20 to keep caches from bogging.
        em.flush();
        em.clear();
    }
}

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

Or, you can use em.createNativeQuery() and fire off a native SQL batch insert.

There are several other possibilities depending on the specific database and ORM you are using. For instance there are tricks with EclipseLink (http://java-persistence-performance.blogspot.com/2011/06/how-to-improve-jpa-performance-by-1825.html) or parametrization (http://java-persistence-performance.blogspot.com/2013/05/batch-writing-and-dynamic-vs.html).

A Hibernate specific walk-through can be found here: http://korhner.github.io/hibernate/hibernate-performance-traps-part-2/



来源:https://stackoverflow.com/questions/32066898/jpa-2-0-how-to-improve-performance-on-bulk-insertion-through-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!