现在我将 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() 完成后使用 hibernate 在 jpa 中继续和回滚。
最佳答案
使用 JPA 提交:
entityManager.getTransaction().commit();
使用 JPA 回滚:
entityManager.getTransaction().rollback();
在调用持久执行所需操作后调用这两个方法之一。在您的情况下,entityManager
将被替换为检索entityManager 的调用,getEntityManager()
关于java - getEntityManager.persist(object) 之后如何提交和回滚数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13125809/