我知道HibernateSession
使用的是一级缓存这一事实,一旦我们从 中检索实体,随后对具有相同标识符的同一实体的 get 调用将从DB 而不是 DB中获取,直到Open为止。session
session
session
话虽如此,我对hibernate如何将一级缓存与数据库同步有疑问?考虑以下场景
//Lets say I have created the session
Session s1 = sessionFactory.getSession();
User u1 = s1.get(User.class, 1); //Getting User with ID=1
//s1 is not yet closed
//Lets say I create some other session
Session s2 = sessionFactory.getSession();
User u2 = s2.get(User.class, 1); //Getting User with ID=1
u2.setName("Abc"); // Changed a field
s2.save(u2); // Saved the changes to DB
s2.close(); //Closed the 2nd session
//Now when I once again retrieve User with ID=1 from s1, will I get updated User?
User u3 = s1.get(User.class, 1);// Here as per my understanding cache is used
所以我的问题是
- 由于
u3
是从一级缓存中获取的,是否u3
有更新值? - 如果有人在会话打开时直接更新数据库并修改用户对象,是否
session
与数据库同步?
预先感谢您在此线程上的时间和精力