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

标签 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() 完成后使用 hibernate 在 jpa 中继续和回滚。

最佳答案

使用 JPA 提交:

entityManager.getTransaction().commit();

使用 JPA 回滚:

entityManager.getTransaction().rollback();

在调用持久执行所需操作后调用这两个方法之一。在您的情况下,entityManager 将被替换为检索entityManager 的调用,getEntityManager()

引用: http://www.objectdb.com/java/jpa/persistence/store

关于java - getEntityManager.persist(object) 之后如何提交和回滚数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13125809/

相关文章:

java - mvn install 忽略 hibernate 映射文件

JPA什么时候使用实体类型的Path而不是Join?

java - JNLP 文件关联 : How do I open the file which was double-clicked on?

java - ModelMapper 将数组属性 (get(0)) 展平为字符串?

java - 启动 OW2 Orchestra 时出现 SQL 语法错误

java - 如何通过 Hibernate 对数据库列的任何读/写访问调用 Java 方法

java - 线程 "main"java.lang.reflect.InvocationTargetException 中的异常

java - 需要使用java应用程序合并Avro文件

java - EntityManager 无法在独立应用程序中初始化

java - 如何在 hibernate/JPA 中禁用自动更新