解决a different object with the same identifier value was already associated with the session错误

本人在使用该项目的时候,用同一个session先做了查询,然后在调用session.saveOrUpdate()后报错。报错代码类似如下:

Object ob = session.creatQuery("from.......").uniqueResult();

String a = ob.getSid();

......

session.saveOrUpdate(Object);

本人技术有限,只能使用笨方法解决,解决办法如下:

新建session用于查询,查完关闭。

Session session1 = sessionFactory.openSession();

Object ob = session1.creatQuery("from.......").uniqueResult();

String a = ob.getSid();

session1.close();

......

session.saveOrUpdate(Object); 调用另一个session保存,不使用一个session。这样就避免了出现那种问题。

以下大神的文章对我启发很大,分享给你们:



https://blog.csdn.net/shenzhen_mydream/article/details/6094662

你可能感兴趣的:(解决a different object with the same identifier value was already associated with the session错误)