2013-03-11 111 views
2

合并将更新Name到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); 

回答

0

这将更新为名称Manik.We将使用合并来更新值,而不考虑会话状态。

0

这将更新为Manik因为:Use merge() if you want to merge your modifications at any time without consideration of the state of the session.hibernate documentation

1

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

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

合并应s1s1重新连接到上下文/会话替换s2。一旦你刷新或关闭会话,它会将其保存到数据库。如果您保存了s2,然后合并s1我认为但不确定您可能会在保存时出现Opportunistic Lock异常,特别是如果您跨线程共享会话。

确切知道的最好方法是编写单元测试。