没有怎么深入研究过Hibernate与Spring,最近在使用SSH时,使用了OpenSessionInViewFilter的方式:
- <filter>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <filter-class>
- org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
- </filter-class>
- <init-param>
- <param-name>singleSession</param-name>
- <param-value>false</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
一个Dao类继承了HibernateDaoSupport,有一个方法如下:
- public List executeQuery( String hql, String[] param, int start, int pageSize)
- {
- // TODO Auto-generated method stub
- List list = new ArrayList();
- if(hql==null || hql.equals(""))
- {
- return list;
- }
- Query query = session.createQuery(hql);
- if(hql.indexOf("?")>0 && param!=null)
- {
- for(int i=1;i<=param.length;i++)
- {
- query.setParameter(i, param[i]);
- }
- }else if(hql.indexOf("?")<0 && param!=null)
- {
- return list;
- }
- if(start>=0 && pageSize>0)
- {
- query.setFirstResult(start);
- query.setMaxResults(pageSize);
- }
- return query.list();
- }
结果在Action调用通过Service调用此方法,最后在浏览器里狂刷,才几下子...浏览器就停在那些了.没研究出什么原理,最后使用HibernateCallback回调解决了.
谁发现相关的文章给我发个连接啊~~~~
- public List executeQuery(final String hql, final String[] param, final int start, final int pageSize)
- {
- return (List)getHibernateTemplate().execute(new HibernateCallback()//这里使用了匿名内部类
- {
- public Object doInHibernate(Session session)//Spring进行事务维护 省去每次创建session和关闭session
- throws HibernateException
- {
- // TODO Auto-generated method stub
- List list = new ArrayList();
- if(hql==null || hql.equals(""))
- {
- return list;
- }
- Query query = session.createQuery(hql);
- if(hql.indexOf("?")>0 && param!=null)
- {
- for(int i=1;i<=param.length;i++)
- {
- query.setParameter(i, param[i]);
- }
- }else if(hql.indexOf("?")<0 && param!=null)
- {
- return list;
- }
- if(start>=0 && pageSize>0)
- {
- query.setFirstResult(start);
- query.setMaxResults(pageSize);
- }
- return query.list();
- }
- }, true);
- }