2013-03-11 68 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異常,特別是如果您跨線程共享會話。

確切知道的最好方法是編寫單元測試。