另:可以设定HibernateTemplate的AllowCreate为True,并在finally中关闭Session。也可以将true作为参数传递到super.getSession(..)方法中取得Session。这样也可以,就是麻烦点。
参见:
http://springframework.org/docs/api/org/springframework/orm/hibernate3/HibernateTemplate.html
http://www.mxjava.com/blog/article.asp?id=246
参考资料:http://blog.sina.com.cn/s/blog_50e4caf70100a1nx.html于是,如果我们一定要书写hql语句可以参考如下形式
public PageUtil getLog(final Long userid,final Integer page) {
String counthql="select count(mod) from Blog mod where mod.userinfo.userid=?";
Integer count=(Integer)getHibernateTemplate().find(counthql, userid).get(0);
final PageUtil pu=new PageUtil();// 分页包装类
pu.setCount(count);
pu.setPage(page);
pu.setMaxPagesbyCount(count);
List li= getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
String hql="from Blog mod left join fetch mod.userinfo where mod.userinfo.userid=? order by mod.logid desc";
Query query=session.createQuery(hql);
query.setParameter(0, userid);
query.setMaxResults(pu.getMaxResults());
query.setFirstResult(pu.getFirstResult());
return query.list();
}
});
pu.setResults(li);
return pu ;
}只有这样做了以后才能确保数据库的连接能够尽早被释放,项目不至于崩溃。
参见:
http://springframework.org/docs/api/org/springframework/orm/hibernate3/HibernateTemplate.html
http://www.mxjava.com/blog/article.asp?id=246
参考资料:http://blog.sina.com.cn/s/blog_50e4caf70100a1nx.html于是,如果我们一定要书写hql语句可以参考如下形式
public PageUtil getLog(final Long userid,final Integer page) {
String counthql="select count(mod) from Blog mod where mod.userinfo.userid=?";
Integer count=(Integer)getHibernateTemplate().find(counthql, userid).get(0);
final PageUtil pu=new PageUtil();// 分页包装类
pu.setCount(count);
pu.setPage(page);
pu.setMaxPagesbyCount(count);
List li= getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
String hql="from Blog mod left join fetch mod.userinfo where mod.userinfo.userid=? order by mod.logid desc";
Query query=session.createQuery(hql);
query.setParameter(0, userid);
query.setMaxResults(pu.getMaxResults());
query.setFirstResult(pu.getFirstResult());
return query.list();
}
});
pu.setResults(li);
return pu ;
}只有这样做了以后才能确保数据库的连接能够尽早被释放,项目不至于崩溃。
解决方案 »
- 数据库取出来的值存到数组,或者list里面,
- 偶买嘎的!继续struts2+spring整合问题!!虚心求教!
- struts结合jfreechart做柱状图,柱状图每根柱子要求有链接,就是能下钻,网上查资料在servlet下的非常多,在Struts2里边就不知道怎么弄了
- JDBC问题,为什么我的TYPE_SCROLL_SENSITIVE和TYPE_SCROLL_INSENSITIVE不起效果?
- tomcat 路径中超过1各空格无法访问的问题
- jstl使用spring mvc的一个问题
- jsp中表单提交
- 无法执行JSP文件了,请教啊
- JBOSS出错,急!!!
- 从EJB QL 传来的警告:the argument of SELECT single_valued_path_expression cannot be an identification_variable
- jdbc和hibernate的区别.
- 按字典顺序比较两个字符串”你”和”我”的大小关系(java实现)
解决办法2 hibernate.connection.release_mode=auto
解决办法3 getSession(false);不妨可以再测试一下
起码hibernate3中没有这个值,
只有after_statement
after_transaction
on_close
三个。