如何使用Entitymanager(Hibernate)将我从List获取的数据插入数据库?

时间:2017-02-08 05:18:51

标签: java hibernate

我有一个List,我想添加将我从列表中获取的数据添加到数据库中。 我的代码是:

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

                }

我已将所有表格与POJOS映射,并且工作正常。我只想添加CommentText Comment type comment Id Email Id Creation Date { {1}}和Modification date。我有一个表PAGEINFO,还有一个名为PageInfo的POJO,包含这7个字段。求助

1 个答案:

答案 0 :(得分:1)

在事务中运行persist代码。

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