a different object with the same identifier value

xiaoxiao2026-06-19  33

今天在作excel导入的时候碰到的问题, excel里面的记录往数据库录入的过程中,如果存在就update,不存在就save于是在操作数据库前分别将2个不同性质的bean放到saveList和updateList依次调用 saveOrUpdateByList(List list)方法 public synchronized int saveOrUpdateByList(List listObj){ int count = 0; Session session = null; try { if(listObj != null && !listObj.isEmpty()){ session = getSession(); for(int i = 1;i<=listObj.size();i++){ session.saveOrUpdate(listObj.get(i-1)); count++; if(i%20 == 0){ session.flush(); session.clear(); } } } } catch (Exception e) { e.printStackTrace(); }finally{ if(session != null){ session.flush(); session.clear(); releaseSession(session); } } return count; } 老是报错a different object with the same identifier value was already associated with the session 查了下网站,说用merge()方法,就把方法中的 session.saveOrUpdate(listObj.get(i-1)); 改成 session.saveOrUpdate(session.merge(listObj.get(i-1))); 就好了, 解决总归解决,本来想上eye论坛探讨下问题出现的原因以及这样解决的办法,不过弱弱的理论还是让我放弃了,怕跟不散大家的思路呀...
转载请注明原文地址: https://www.6miu.com/read-5050431.html

最新回复(0)