2

我正在为数据库 mysql 使用 jpa eclipselink,我需要批量插入一个包含 6000 多个对象的列表。但是数据库中只插入了 215 行,没有抛出异常。

这是我的代码

private EntityManagerFactory emf = null;

    private static final String PERSISTENCE_UNIT_NAME = "Cation";

    private static EntityManagerFactory factory;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        EntityManager em = factory.createEntityManager();
        try {
            em.getTransaction().begin();
            for (int i = 0; i < sgmlList.size(); i++) {
                // Getting the object from the list by using loop
                SGML sgml = sgmlList.get(i);
                em.persist(sgml);
            }
            em.getTransaction().commit();
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage("SGML imported successfully"));
        } catch (Exception ex) {

        } finally {
            if (em != null) {
                em.close();
            }
        }

谁能帮我解决这个问题。

4

2 回答 2

3

您的 catch 博客会简单地忽略任何抛出的异常。也许您应该记录它们以便您真正看到它们。:)

于 2013-06-29T11:39:43.380 回答
1

JPA批量插入是这样的..

 public void bulkinsertEditores(List<Editore> editories) {
try {
EntityManagerFactory emf = EntityManagerFactoryClass.getEntityManagerFactoryInstance();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
int ip = 0;
for (Editore ed : editories) {
ip = ip + 1;
em.persist(ed);
if ((ip % 20) == 0) {
  em.flush();
  em.clear();
  }

}

  em.getTransaction().commit();
 } catch (Exception e) {
 e.printStackTrace();
 }
于 2013-09-10T17:05:43.790 回答