使用jpa eclipselink批量插入

时间:2013-06-29 11:19:14

标签: jsf jpa bulkinsert

我正在使用jpa eclipselink for database mysql,我需要批量插入一个包含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();
            }
        }

任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:3)

您的捕获博客只会忽略抛出的任何异常。也许您应该记录它们,以便您实际看到它们。 :)

答案 1 :(得分:0)

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();
 }