如何在getEntityManager.persist(object)之后提交和回滚数据

时间:2012-10-29 16:33:39

标签: java hibernate jpa fluent-nhibernate hibernate-entitymanager

现在我正在使用jpa和hibernate,当我完成getEntityManager.persist(objects)然后我会要求用户确认,例如使用用户界面继续和回滚

private List<TempCustomers> tempCustomer =new ArrayList<TempCustomers>();

 @Begin(join = true)
    public String migrateData() {

    log.info("Mobee Migrate Customer Size  :"+doTempCustomers.size());




            for(DoTempCustomers tempCustomers:doTempCustomers){
            try {

                TempCustomers temp=new TempCustomers();
                BeanUtils.copyProperties(temp, tempCustomers);


                tempCustomer.add(temp);
                getEntityManager().persist(temp);

            }catch (Exception e) {
                // TODO: handle exception
                return "null";
            }

             }
            log.info("Size........."+tempCustomer.size());
            return "null";
    }

@Begin(join = true)
    public String updatedData(){
         log.info("Size of Customers :"+tempCustomer.size());
         log.info("Decision ..."+decision);

        try{    
       if(decision.equals("Continue")){
        for(TempCustomers tempCust:tempCustomer){

            TempCustomers temp=new TempCustomers();
            BeanUtils.copyProperties(temp, tempCust);   

            log.info("updated Sucessfully");

           getEntityManager().getTransaction().commit();

       }}else{
           getEntityManager().getTransaction().rollback();

        }
        }
        catch(Exception e){

        }
}

当getEntityManager()。persist()完成时,请帮助我如何在hpa中使用hiber继续和回滚。

1 个答案:

答案 0 :(得分:2)

提交JPA:

entityManager.getTransaction().commit();

使用JPA回滚:

entityManager.getTransaction().rollback();

在呼叫保持执行所需操作后,请调用其中一种方法。在您的情况下,entityManager将替换为检索entityManager的调用,getEntityManager()

参考: http://www.objectdb.com/java/jpa/persistence/store