我正在使用 jpa 项目开发 jsf 现在我遇到批量插入问题,因为列表大小超过 6000,这意味着它应该在表中插入超过 6000 条记录,但它只插入 215 条记录。
我的代码在这里
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();
}
}
我的持久性 xml 是
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="Cation" transaction-type="RESOURCE_LOCAL">
<class>com.cation.bean.Users</class>
<class>com.cation.bean.BatchInfo</class>
<class>com.cation.bean.SGML</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.1.4:3306/cation"/>
<property name="javax.persistence.jdbc.user" value="rix"/>
<property name="javax.persistence.jdbc.password" value="rix123"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
当它将对象保留在列表中时,我也尝试过此代码
if (i % 200 == 0) {
em.flush();
em.clear();
}
但随后也会出现同样的问题。任何人都可以帮助解决这个问题。