Persist JPA object array

Asked

Viewed 467 times

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

Thus only the last Entity treated by for is inserted in the bank, how do I insert all of them? I tried some solutions using em.flush() and em.clear() at the end of the loop but did not succeed.

1 answer

1


Possibly the thing gets messy when you try to iterate the result of the testPlan.getParams call method() .

take off the testPlan.getParams() from within the for

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

I believe this will work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.