org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session :
如代码所致,当持久化上下文中存在托管的的实体和脱管的实体相同id时,更新已经脱管(游离)的实体就会报错。
Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".
持久化上下文:
作用:
1、hibernate可以进行自动的脏检查和事务延迟写入。
2、hibernate可以用持久化上下文作为一级高速缓存
3、hibernate可以保证Java对象同一性范围
4、hibernate可以把持久化上下文扩展到跨整个回话。
org.hibernate.event.def.DefaultLoadEventListener
440L(试图找到在session级别的缓存的实体)
Object entity = loadFromSessionCache( event, keyToLoad, options );
然后还会反复寻找各种缓存机制,如果没有,就会调用
return loadFromDatasource(event, persister, keyToLoad, options);
- /**
- * 如代码所致,当持久化上下文中存在托管的的实体和脱管的实体相同id时,更新已经脱管(游离)的实体就会报错。
- */
- privatestaticvoidmerge1() {
- Configuration configuration =newConfiguration().configure();
- SessionFactory sessionFactory = configuration.buildSessionFactory();
- Session session1 = sessionFactory.openSession();
- Transaction tr1 = session1.beginTransaction();
- TestBean testBean1 = (TestBean) session1.get(TestBean.class,1);
- tr1.commit();
- session1.close();
- testBean1.setPassword("234");
- Session session2 = sessionFactory.openSession();
- Transaction tr2 = session2.beginTransaction();
- TestBean testBean2 = (TestBean) session2.get(TestBean.class,1);
- testBean2.setName("hello2");
- testBean2.setPassword("456");
- //执行update会报错:
- //org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
- session2.update(testBean1);
- tr2.commit();
- session2.close();
- sessionFactory.close();
- }
- /**
- * 调用merge会合并当前托管的相同id的实体,以merge传入实体为准
- */
- privatestaticvoidmerge2() {
- Configuration configuration =newConfiguration().configure();
- SessionFactory sessionFactory = configuration.buildSessionFactory();
- Session session1 = sessionFactory.openSession();
- Transaction tr1 = session1.beginTransaction();
- TestBean testBean1 = (TestBean) session1.get(TestBean.class,1);
- tr1.commit();
- session1.close();
- testBean1.setPassword("234");
- Session session2 = sessionFactory.openSession();
- Transaction tr2 = session2.beginTransaction();
- TestBean testBean2 = (TestBean) session2.get(TestBean.class,1);
- testBean2.setName("hello2");
- testBean2.setPassword("456");
- TestBean testBean3 = (TestBean)session2.merge(testBean1);
- System.out.println(testBean1);
- System.out.println(testBean2);
- System.out.println(testBean3);
- //打印如下(testBean2的hello2的name属性改变被覆盖了):
- //TestBean [id=1, name=hello, password=234]
- //TestBean [id=1, name=hello, password=234]
- //TestBean [id=1, name=hello, password=234]
- //Hibernate: update Test_Bean set name=?, password=? where id=?
- tr2.commit();
- session2.close();
- sessionFactory.close();
- }
- /**
- * 不同session就没有问题
- */
- privatestaticvoidmerge3() {
- Configuration configuration =newConfiguration().configure();
- SessionFactory sessionFactory = configuration.buildSessionFactory();
- Session session1 = sessionFactory.openSession();
- Transaction tr1 = session1.beginTransaction();
- TestBean testBean1 = (TestBean) session1.get(TestBean.class,1);
- tr1.commit();
- session1.close();
- testBean1.setPassword("1234");
- Session session2 = sessionFactory.openSession();
- Transaction tr2 = session2.beginTransaction();
- Session session3 = sessionFactory.openSession();
- Transaction tr3 = session3.beginTransaction();
- TestBean testBean2 = (TestBean) session2.get(TestBean.class,1);
- testBean2.setName("hello2");
- testBean2.setPassword("456");
- session3.update(testBean1);
- tr2.commit();
- session2.close();
- tr3.commit();
- session3.close();
- sessionFactory.close();
- }
如代码所致,当持久化上下文中存在托管的的实体和脱管的实体相同id时,更新已经脱管(游离)的实体就会报错。
Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".
持久化上下文:
作用:
1、hibernate可以进行自动的脏检查和事务延迟写入。
2、hibernate可以用持久化上下文作为一级高速缓存
3、hibernate可以保证Java对象同一性范围
4、hibernate可以把持久化上下文扩展到跨整个回话。
org.hibernate.event.def.DefaultLoadEventListener
440L(试图找到在session级别的缓存的实体)
Object entity = loadFromSessionCache( event, keyToLoad, options );
然后还会反复寻找各种缓存机制,如果没有,就会调用
return loadFromDatasource(event, persister, keyToLoad, options);