关于hibernate文档中合并的一些要点

时间:2015-02-12 09:38:12

标签: hibernate jpa

浏览Hibernate文档,其中包含JBoss网站上提到的4个基本要点 的合并()

  

(1)如果持久性实例目前具有相同标识符       与会话相关联,将给定对象的状态复制到       持久性实例

     

(2)如果当前没有与会话相关联的持久性实例,       尝试从数据库加载,或创建新的持久性实例

     

(3) 持久性实例 已退回

     

(4) 给定实例未与会话关联,它仍然存在       的分离

考虑一下代码段:

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();

        Employee employee = (Employee) session.get(Employee.class, 5);
        tx.commit();
        session.close(); // end of first session, employee is detached

        employee.getEmployeeId(); // The database identity is "5"
        employee.setFirstName("my new first description - Nauhkez");

        Session session2 = HibernateUtil.getSessionFactory().openSession();
        Transaction tx2 = session2.beginTransaction();

        Employee employee2 = (Employee) session2.get(Employee.class, 5);
        Employee employee3 = (Employee) session2.merge(employee); // Success!

        System.out.println("Checking referential equality: "+(employee == employee3));

        tx2.commit();
        session2.close();

员工是一个持久性或分离的对象?

Employee employee3 = (Employee) session2.merge(employee); // Success!

0 个答案:

没有答案