Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Batch Persist using EntityManager

843859Oct 25 2008 — edited Oct 25 2008
I am trying to do a batch persist of objects using EntityManager in Java SE and I am getting the error message: "cannont perist detached object". My ultimate goal is to quickly persist a batch of objects. If I persist the object immediately (instead of adding it to a list and doing a batch persist), the program takes about 10 minutes to load all the 9,000 objects into the database. I am trying to minimize time with the following code, but it fails. How can I do a batch persist using EntityManager? I

This is my code:

void persistList(List<ObjectToPersist> objList){

EntityManager em = emf.creatEntityManager();
for(ObjectToPersist obj : objList){
em.getTransaction().begin();
em.persist(obj);
em.getTransaction().commit();
}
}

if I do this, it doesn't fail but it still takes about 10 minutes to insert 9.000 objects:


void persistList(List<ObjectToPersist> objList){

EntityManager em = emf.creatEntityManager();
for(ObjectToPersist obj : objList){
em.persist(obj);
}
}

Thanks in advance.
Aurora

Edited by: aacain on Oct 25, 2008 9:48 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 22 2008
Added on Oct 25 2008
3 comments
491 views