hibernate merge 的作用 (如何解决 NonUniqueObjectException)

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session : 
  1.       
  2. /** 
  3.      * 如代码所致,当持久化上下文中存在托管的的实体和脱管的实体相同id时,更新已经脱管(游离)的实体就会报错。 
  4.      */  
  5.     private static void merge1() {  
  6.         Configuration configuration = new Configuration().configure();  
  7.         SessionFactory sessionFactory = configuration.buildSessionFactory();  
  8.         Session session1 = sessionFactory.openSession();  
  9.         Transaction tr1 = session1.beginTransaction();  
  10.           
  11.         TestBean testBean1 = (TestBean) session1.get(TestBean.class1);  
  12.           
  13.         tr1.commit();  
  14.         session1.close();  
  15.           
  16.         testBean1.setPassword("234");  
  17.           
  18.           
  19.         Session session2 = sessionFactory.openSession();  
  20.         Transaction tr2 = session2.beginTransaction();  
  21.           
  22.         TestBean testBean2 = (TestBean) session2.get(TestBean.class1);  
  23.         testBean2.setName("hello2");  
  24.         testBean2.setPassword("456");  
  25.           
  26.         //执行update会报错:  
  27.         //org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session  
  28.         session2.update(testBean1);  
  29.           
  30.         tr2.commit();  
  31.         session2.close();  
  32.           
  33.         sessionFactory.close();  
  34.     }  
  35.       
  36.       
  37.     /** 
  38.      * 调用merge会合并当前托管的相同id的实体,以merge传入实体为准 
  39.      */  
  40.     private static void merge2() {  
  41.         Configuration configuration = new Configuration().configure();  
  42.         SessionFactory sessionFactory = configuration.buildSessionFactory();  
  43.         Session session1 = sessionFactory.openSession();  
  44.         Transaction tr1 = session1.beginTransaction();  
  45.           
  46.         TestBean testBean1 = (TestBean) session1.get(TestBean.class1);  
  47.           
  48.         tr1.commit();  
  49.         session1.close();  
  50.           
  51.         testBean1.setPassword("234");  
  52.           
  53.           
  54.         Session session2 = sessionFactory.openSession();  
  55.         Transaction tr2 = session2.beginTransaction();  
  56.           
  57.         TestBean testBean2 = (TestBean) session2.get(TestBean.class1);  
  58.         testBean2.setName("hello2");  
  59.         testBean2.setPassword("456");  
  60.           
  61.         TestBean testBean3 = (TestBean)session2.merge(testBean1);  
  62.           
  63.         System.out.println(testBean1);  
  64.         System.out.println(testBean2);  
  65.         System.out.println(testBean3);  
  66.           
  67. //      打印如下(testBean2的hello2的name属性改变被覆盖了):  
  68. //      TestBean [id=1, name=hello, password=234]  
  69. //      TestBean [id=1, name=hello, password=234]  
  70. //      TestBean [id=1, name=hello, password=234]  
  71.           
  72. //      Hibernate: update Test_Bean set name=?, password=? where id=?  
  73.           
  74.         tr2.commit();  
  75.         session2.close();  
  76.           
  77.         sessionFactory.close();  
  78.     }  
  79.       
  80.       
  81.     /** 
  82.      * 不同session就没有问题 
  83.      */  
  84.     private static void merge3() {  
  85.         Configuration configuration = new Configuration().configure();  
  86.         SessionFactory sessionFactory = configuration.buildSessionFactory();  
  87.         Session session1 = sessionFactory.openSession();  
  88.         Transaction tr1 = session1.beginTransaction();  
  89.           
  90.         TestBean testBean1 = (TestBean) session1.get(TestBean.class1);  
  91.           
  92.         tr1.commit();  
  93.         session1.close();  
  94.           
  95.         testBean1.setPassword("1234");  
  96.           
  97.           
  98.         Session session2 = sessionFactory.openSession();  
  99.         Transaction tr2 = session2.beginTransaction();  
  100.           
  101.         Session session3 = sessionFactory.openSession();  
  102.         Transaction tr3 = session3.beginTransaction();  
  103.           
  104.         TestBean testBean2 = (TestBean) session2.get(TestBean.class1);  
  105.         testBean2.setName("hello2");  
  106.         testBean2.setPassword("456");  
  107.           
  108.         session3.update(testBean1);  
  109.           
  110.         tr2.commit();  
  111.         session2.close();  
  112.         tr3.commit();  
  113.         session3.close();  
  114.           
  115.         sessionFactory.close();  
  116.     }  

如代码所致,当持久化上下文中存在托管的的实体和脱管的实体相同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); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值