读书人

解决hibernate查询时自动更新有关问

发布时间: 2012-11-10 10:48:50 作者: rapoo

解决hibernate查询时,自动更新问题

对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;}

?

读书人网 >编程

热点推荐