转:getHibernateTemplate()更替getSession() hibernate3 默认支持延迟加载lazy SQL语句

转:getHibernateTemplate()更换getSession() hibernate3 默认支持延迟加载lazy SQL语句

转至:http://quyulu.blog.163.com/blog/static/3191360520101114115837861/

 

在项目中遇到数据库连接开了没有关闭导致在客户那边部署后网站运行了没多久就死了。连接不上数据库了。。

这是一篇搜索到的文章。。手动关闭session、仅供参考

 

一.getHibernateTemplate()更换getSession(),手动关闭session

 

问题:操作CLOB字段时出错

javax.servlet.ServletException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started

 

//***********新增********************// public boolean newDataObj(Object obj) { boolean ret=true; Session session=this.getHibernateTemplate().getSessionFactory().openSession(); try{ Transaction tx=session.beginTransaction(); session.save(obj); tx.commit(); } catch(Exception e){ e.printStackTrace(); logger.error(e); ret=false; } finally { if (session!= null && session.isOpen()) { session.close(); session = null; } } return ret; } //***********更新********************// public boolean updateDataObj(Object obj) { boolean ret=true; Session session=this.getHibernateTemplate().getSessionFactory().openSession(); try{ Transaction tx=session.beginTransaction(); session.update(obj); tx.commit(); } catch(Exception e) { e.printStackTrace(); logger.error(e); ret=false; } finally { if (session!= null && session.isOpen()) { session.close(); session = null; } } return ret; } //***********删除********************// public boolean deleteDateObj(Object obj) { boolean ret=true; Session session=this.getHibernateTemplate().getSessionFactory().openSession(); try { Transaction tx=session.beginTransaction(); session.delete(obj); tx.commit(); } catch (Exception e) { e.printStackTrace(); logger.error(e); ret=false; } finally { if (session != null && session.isOpen()) { session.close(); session = null; } } return ret; } //***********删除********************// public List<TDepartment> queryDep(String defaultDisplay) { List depList = null; Session session=this.getHibernateTemplate().getSessionFactory().openSession(); String hql = "from TDepartment tDepartment where tDepartment.isused='1' "; if(defaultDisplay!=null && !defaultDisplay.equals("")) hql += " and tDepartment.defaultDisplay='"+defaultDisplay+"' "; hql += " order by tDepartment.departmentId asc "; try { Query query = session.createQuery(hql); depList = query.list(); session.close(); } catch (HibernateException e) { e.printStackTrace(); } finally { if (session!= null && session.isOpen()) { session.close(); session = null; } } return depList; }


 

 

 

 

 

 

:

1.使用getSession()方法你只要继承sessionFactory,而使用getHibernateTemplate()方法必须继承

HibernateDaoSupport当然包括sessionFactory,这点区别都不是特别重要的,下面这些区别就很重要了

 


     2.getSession()

 

方法是没有经过spring包装的,spring会把最原始的session给你,在使用完之后必须自己调用相应close方法,

而且也不会对声明式事务进行相应的管理,一旦没有及时关闭连接,就会导致数据库连接池的连接数溢出,

 

 

getHibernateTemplate()

方法是经过spring封装的,例如添加相应的声明式事务管理,由spring管理相应的连接。


   
在实际的使用过程中发现的确getHibernateTemplate()getSession()方法要好很多,但是有些方法在

getHibernateTemplate()并没有提供,这时我们用HibernateCallback 回调的方法管理数据库.

 

getSession() 获得的是原始的sessionFactory,每次你必须自己维护session如结束后你必须关闭session。如果是hibernate中进行数据库操作,你获得是原始的hibernate styleexcepttion

 

hibernate templatespring包装过的,它会帮你管理session,并且它会将hibernate exceptions转换成其他的分类后的错误。这点getSession是肯定不行了。例如你用orclemysql返回的错误在getSession中就是不一样的,而在hibernate template中就是一样的。

 

实际使用中发现,对于基本的操作Hibernate template处理的的确比getSession要好,但到了复杂查询的时候如分页时需要调用getHibernateTemplate().execute(HibernateCallBack).要产生很多innerClass,调试非常不便。而getSession就相当简单多了。

 

=======================================================

 

二. 延迟加载

问题:无法加载关联项

错误页面提示

could not initialize proxy - no Session

控制台

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

 

 

        <many-to-one

        name="fkTDepartment"

        column="FK_T_DEPARTMENT"

        class="com.commonfile.hibernate.TDepartment"

        lazy="false"

 

:

hibernate中:hibernate3 默认支持延迟加载(lazy="proxy"我们可以把proxy看作是true),hibernate2 默认立即加载 (lazy="false")。

hibernate3中,所有的实体设置文件(user.hbm.xml)中的lazy属性都被默认设成了true,就是当这个类没有被调用时,延时加载,导致了以上情况的发生,在配置文件中将lzay属性设为false

 

 

===============================================================================

 

.SQL语句 查询出错

select new TUser(tUser.id,tUser.userName,tUser.department,tUser.fkTStation,tUser.unitFlag) from TUser  tUser where tUser.id != 'f' and tUser.isused=1 and tUser.id not in (00309,00324,00338,00313,00311,00337) order by tUser.department.codepath, tUser.fkTStation.sequence,tUser.unitFlag, nlssort(tUser.userName,'NLS_SORT=SCHINESE_PINYIN_M')

 

: hibernate3不支持没有单引号”的条件查询