对hibernate的session操作时时,默认的FlushModel时auto的,对于查询,不需要Flush。hibernate建议在session的操作前,设置 flush mode 为MANUAL 的(还有NRVER,但hibernate是deprecated的)。
For a logically "read only" session, it is reasonable to set the session's flush mode to FlushMode.MANUAL at the start of the session (in order to achieve some extra performance).
dao查询代码:
@SuppressWarnings("unchecked")
public List searchByHql(final String hql) {
System.out.println("searchByHql:"+hql);
HibernateCallback cb = new HibernateCallback() {
public Object doInHibernate(Session session) {
session.setFlushMode(FlushMode.MANUAL);
Query q = session.createQuery(hql);
return q.list();
}
};
List list = (List) getHibernateTemplate().execute(cb);
return list;
}
本文探讨了在Hibernate中如何通过调整FlushMode来提升只读查询的性能。介绍了将默认的auto模式改为manual模式的原因,并提供了一个具体的代码示例。
321

被折叠的 条评论
为什么被折叠?



