1

I have a List and I want to add to add the data which I get from the list into the database. My code is:

List<EventPayload> mylist=eventpojo.getEventPayload();

            for(EventPayload array : mylist)
            {
                System.out.println("Comment Text :"+array.getCommentText());
                System.out.println("Comment Type :"+array.getCommentType());
                System.out.println("Comment Id :"+array.getCommentId());
                System.out.println("Email id :"+array.getComment_email());
                String email1=array.getComment_email();
                EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jcg-JPA");
                EntityManager em = entityManagerFactory.createEntityManager();


                List email=em.createQuery("SELECT usrinfo.user_id FROM UserInfo usrinfo WHERE usrinfo.email_id = :id").setParameter("id", email1).getResultList();
                String userid=email.get(0).toString();
                System.out.println("User id:"+userid);
                Date date = new Date();
                PageInfo pageinfo=new PageInfo();
                pageinfo.setComment_id(array.getCommentId());
                pageinfo.setComment_text(array.getCommentText());
                pageinfo.setComment_type(array.getCommentType());
                String date1=sdf.format(date);
                pageinfo.setCreation_date(sdf.parse(date1));
                pageinfo.setModification_date(sdf.parse(date1));
                pageinfo.setRetrospective_id(eventpojo.getEventRetrospectiveId());
                int user_id = Integer.parseInt(userid);
                pageinfo.setUser_id(user_id);
                em.persist(pageinfo);

                }

I have all the tables mapped with the POJOS and it is working fine.I just want to add the CommentText Comment type comment Id Email Id Creation Date Modification date and RetrospectiveID.I have a table PAGEINFO and also a POJO named PageInfo with these 7 fields.Kindly help

11
  • What is the problem statement here? Is your code not working? Commented Feb 8, 2017 at 6:11
  • data is not being inserted into the database.. Commented Feb 8, 2017 at 6:15
  • Can you try moving .given below statement outside of for loop. EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jcg-JPA"); EntityManager em = entityManagerFactory.createEntityManager(); Commented Feb 8, 2017 at 6:18
  • When I try to initialize the entity manager out of for loop I cant access String email outside the for loop Commented Feb 8, 2017 at 6:20
  • @SumitKumar I tried it did'nt work.. Commented Feb 8, 2017 at 6:28

1 Answer 1

1

Run the persist code inside transaction.

List<EventPayload> mylist=eventpojo.getEventPayload();
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jcg-JPA");

        for(EventPayload array : mylist)
        {
            System.out.println("Comment Text :"+array.getCommentText());
            System.out.println("Comment Type :"+array.getCommentType());
            System.out.println("Comment Id :"+array.getCommentId());
            System.out.println("Email id :"+array.getComment_email());
            String email1=array.getComment_email();

            EntityManager em = entityManagerFactory.createEntityManager();


            List email=em.createQuery("SELECT usrinfo.user_id FROM UserInfo usrinfo WHERE usrinfo.email_id = :id").setParameter("id", email1).getResultList();
            String userid=email.get(0).toString();
            System.out.println("User id:"+userid);
            Date date = new Date();
            PageInfo pageinfo=new PageInfo();
            pageinfo.setComment_id(array.getCommentId());
            pageinfo.setComment_text(array.getCommentText());
            pageinfo.setComment_type(array.getCommentType());
            String date1=sdf.format(date);
            pageinfo.setCreation_date(sdf.parse(date1));
            pageinfo.setModification_date(sdf.parse(date1));
            pageinfo.setRetrospective_id(eventpojo.getEventRetrospectiveId());
            int user_id = Integer.parseInt(userid);
            pageinfo.setUser_id(user_id);
            EntityTransaction  trans= entityManager.getTransaction();


            trans.begin();

            em.persist(pageinfo);

            trans.commit();
            em.close();

            }

        entityManagerFactory.close();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.