问题
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