The 休眠文档 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/给出了很好的例子。还有这个博客文章 http://blog.astradele.com/2005/09/24/hibernate-sessionevict-and-explicit-saving/会给你一些见解。我将从下面添加一些行。
可以随时重新加载对象及其所有集合,使用refresh()
方法。当使用数据库触发器来初始化对象的某些属性时,这非常有用。
sess.save(cat);
sess.flush(); //force the SQL INSERT
sess.refresh(cat); //re-read the state (after the trigger executes)
see here http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-loading了解更多示例。
每当你将一个对象传递给save(), update() or saveOrUpdate()
,并且每当您使用以下方式检索对象时load(), get(), list(), iterate() or scroll()
,该对象被添加到会话的内部缓存中。
When flush()
随后调用,该对象的状态将与数据库同步。如果您不希望发生这种同步,或者您正在处理大量对象并且需要有效地管理内存,则evict()
方法可用于从一级缓存中删除对象及其集合。
ScrollableResult cats = sess.createQuery("from Cat as cat").scroll(); //a huge result set
while ( cats.next() ) {
Cat cat = (Cat) cats.get(0);
doSomethingWithACat(cat);
sess.evict(cat); // (if gives the compile time error then use it: sess.evict(cat.getClass());
}
阅读完整示例here http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-sessioncache.
了解会话 APIhere http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/Session.html.