I'm trying to persist a ArrayList
of Entity
in JPA with the following for
:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(testCaseEntity);
em.persist(testTypeEntity);
for (TestPlanParamsSpringModel test : testPlan.getParams()) {
em.persist(test);
}
em.getTransaction().commit();
em.close();
emf.close();
In this way only the last Entity handled by for
is inserted in the database, how do I insert all of them? I've tried some solutions using em.flush()
and em.clear()
at the end of the loop but did not succeed.