How to persist an Array List of type Entity in JPA

user1855852 picture user1855852 · Nov 27, 2012 · Viewed 47.8k times · Source

How to persist an Array List of type Entity in JPA ?

For example, there is an entity called "Table". I am creating an array list ArrayList<Table> table = new ArrayList<Table>(); Trying to persist it using entityManager.persist(table); and it did not work. Any solution for this ?

Answer

Ravinath picture Ravinath · Feb 22, 2014
 EntityManagerFactory emf = Persistence.createEntityManagerFactory("TDEMSPU");
        em = emf.createEntityManager();


            em.getTransaction().begin(); 

        List<Enquiry> tempEnqList = tempEnqList();
        for (Iterator<Enquiry> it = tempEnqList.iterator(); it.hasNext();) {
            Enquiry enquiry = it.next();

            em.persist(enquiry);
            em.flush();
            em.clear();
        }

         em.getTransaction().commit();