Persist array of JPA objects

1

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.

    
asked by anonymous 30.10.2015 / 13:15

1 answer

1

Possibly it gets in the way when you try to iterate through the result of the call of the testPlan.getParams () method.

remove testPlan.getParams() from within for

...
List<TestPlanParamsSpringModel> params = testPlan.getParams();
for (TestPlanParamsSpringModel test : params) {
    em.persist(test);
}
...

I believe that this will work.

    
30.10.2015 / 19:38