如何使用ID删除批量调用中的多个对象?
我试过这个
EntityManager em = ...
em.getTransaction().begin();
try
{
for (Visitor obj : map.keySet())
{
Visitor fake = em.getReference(Visitor.class, obj.getId());
em.remove(fake);
}
em.getTransaction().commit();
}
catch (Exception ex)
{
ex.printStackTrace();
}
我在日志文件中看到了DELETE语句但它们抛出了它们
<1148538 fatal store error> org.apache.openjpa.persistence.RollbackException: Optimistic locking errors were detected when flushing to the data store. The following objects may have been concurrently modified in another transaction: [com.reporting.data.Visitor-53043]1148538>
at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:593)
at com.reporting.ui.DBUtil.saveAnswers(DBUtil.java:311)
我有单线程.
更新:
我也试过了
for (Visitor obj : map.keySet())
em.remove(obj);
但它很慢,因为在每次迭代时它都会将SELECT发送到服务器.我假设OpenJPA将它重新附加到上下文.