1

Now I am using jpa with hibernate , when i was done getEntityManager.persist(objects) then i will ask for user confirmation like continue and rollback using user interface

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

        }
}

please help me how to do continue and rollback in jpa with hibernate when getEntityManager().persist() is done.

nag
  • 647
  • 6
  • 25
  • 43

1 Answers1

2

To commit with JPA:

entityManager.getTransaction().commit();

To rollback with JPA:

entityManager.getTransaction().rollback();

Call either of these methods after your call to persist to perform the desired action. In your case entityManager would be replaced with the call to retrieve the entityManager, getEntityManager()

Reference: http://www.objectdb.com/java/jpa/persistence/store

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • thank you,i have one more doubt in above code i done the migrateData() in getEntityManager.persist(temp); i need rollback commit all data in updateData() method bec i am call request updateData() method from UI can you please tell how can i write code this senario. – nag Oct 29 '12 at 16:58