这是我的代码
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();
}
}
任何人都可以帮我解决这个问题。
答案 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();
}