在jdbc中合并函数

时间:2015-11-28 14:52:46

标签: jdbc

public static void merge() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("LifeCycle");
        EntityManager em = null;
        try {
            em = emf.createEntityManager();
            System.out.println("--------------------------------------------------------");

            EmployeeEntity employee=new EmployeeEntity();
            employee.setEmpId(1002);
            //employee.setEmpId(1111);
            employee.setEmpLocation("Mysore");
            employee.setEmpName("Mahadeva");
            System.out.println("Before  merge   :  The EmployeeEntity instance is in MANAGED state  :" + em.contains(employee));
            em.merge(employee);
            System.out.println("After   merge   :  The EmployeeEntity instance is in MANAGED state  :" + em.contains(employee));

            em.getTransaction().begin();
            em.getTransaction().commit();
            System.out.println("After  Commit   :  The EmployeeEntity instance is in MANAGED state  :" + em.contains(employee));
            System.out.println("--------------------------------------------------------");
        } catch (Exception exception) {
            System.out.println(exception.getMessage());
        } finally {
            if(em!=null){
                em.close();
            }
            if (emf != null) {
                emf.close();

            }
        }
    }

对于所有三个系统我输出为假,为什么?我不明白,即使我在begin()和commit之间编写syso,它也会将false作为输出。 提前谢谢你。

1 个答案:

答案 0 :(得分:0)

合并适用于实体的克隆 首先将克隆的属性复制到工作单元中,然后管理该克隆的实体,而不是您传递的原始实体。
您的示例中您期望的是em.persist(entity)操作。坚持注册实体而不进行克隆  试试em.persist(entity),它会给你预期的结果 为RMI对象提供了合并操作,从而提供了这种行为。