合并将更新新的会话对象更新?

时间:2013-03-11 18:30:16

标签: hibernate

合并会将名称更新为Manik或Ankit吗?

Student s1 = null;
s1 = (Student)session1.get(Student.class, new Integer(101));
session1.close();
s1.setName("Manik");
Session session2 = factory.openSession();
Student s2 = null;
Object o1 = session2.get(Student.class, new Integer(101));
s2 = (Student)o1;
s2.setName("Ankit");
Transaction tx=session2.beginTransaction();
session2.merge(s1);

3 个答案:

答案 0 :(得分:1)

它应该将名称更新为“Manik”(仅供参考OP的原始Q:Persistence context cache the id and SQL query?)。

在这里讨论奇怪的可变对象缓存(我一直认为恕我直言,将可变对象存储在缓存中是愚蠢的想法):http://apmblog.compuware.com/2009/02/16/understanding-caching-in-hibernate-part-one-the-session-cache/

合并应将s2替换为s1重新附加s1上下文/会话。刷新或关闭会话后,它会将其保存到数据库中。如果您已保存s2然后合并s1我认为但不确定您是否会在保存时获得机会锁定异常,尤其是在跨线程共享会话时。

确定的最佳方法是编写单元测试。

答案 1 :(得分:0)

这将使用名称Manik更新。我们将使用merge来更新值,而不考虑会话状态。

答案 2 :(得分:0)

由于这一点,我们会更新为Manik:Use merge() if you want to merge your modifications at any time without consideration of the state of the session.来自hibernate documentation

相关问题