在 Hibernate文档
提供了很好的例证。另外,此博客文章还将为您提供一些见识。我将从下面添加一些行。
使用该方法可以随时重新加载对象及其所有集合
refresh()。当数据库触发器用于初始化对象的某些属性时,这很有用。
sess.save(cat);sess.flush(); //force the SQL INSERTsess.refresh(cat); //re-read the state (after the trigger executes)
有关更多示例,请参见此处。
每当将对象传递给时
save(), update() or saveOrUpdate(),以及每次使用检索对象时
load(), get(),list(), iterate() or scroll(),都会将该对象添加到Session的内部缓存中。
当
flush()随后调用时,对象的状态将与数据库进行同步。如果您不希望发生这种同步,或者正在处理大量对象,并且需要有效地管理内存,则
evict()可以使用该方法从一级缓存中删除对象及其集合。
ScrollableResult cats = sess.createQuery("from Cat as cat").scroll(); //a huge result setwhile ( 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()); }从这里阅读完整的示例。
在此处阅读有关会话API的信息。



