如题所示,对于这个问题,官网文档已给出答案,详见:

/**
* Obtain a Hibernate Session, either from the current transaction or
* a new one. The latter is only allowed if the
* {@link org.springframework.orm.hibernate3.HibernateTemplate#setAllowCreate "allowCreate"}
* setting of this bean's {@link #setHibernateTemplate HibernateTemplate} is "true".
* <p><b>Note that this is not meant to be invoked from HibernateTemplate code
* but rather just in plain Hibernate code.</b> Either rely on a thread-bound
* Session or use it in combination with {@link #releaseSession}.
* <p>In general, it is recommended to use HibernateTemplate, either with
* the provided convenience operations or with a custom HibernateCallback
* that provides you with a Session to work on. HibernateTemplate will care
* for all resource management and for proper exception conversion.
* @return the Hibernate Session
* @throws DataAccessResourceFailureException if the Session couldn't be created
* @throws IllegalStateException if no thread-bound Session found and allowCreate=false
* @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean)
*/
org.springframework.orm.hibernate3.support.HibernateDaoSupport#getSession()

Spring推荐我们使用HibernateTemplate,如有必要,则通过HibernateCallback来调用原生Session。

推荐原因,HibernateTemplate可以帮助我们进行资源管理和适当的异常转换!

getHibernateTemplate() VS getSession()的更多相关文章

  1. getHibernateTemplate()和getSession()的区别

    自动生成hibernate配置文件的时候,会在dao层用到getSession()方法来操作数据库记录,但是他还有个方法getHibernateTemplate(),这两个方法究竟有什么区别呢? 1. ...

  2. hibernate总记录数查询和分页查询

    //参考代码 //第一种方法: String hql = "select count(*) from User as user"; Integer count = (Integer ...

  3. Hibernate使用count(*)取得表中记录总数

    /** * @TODO:查询某一年度的所有计划数量 */ public int findCountByYear(String currYear) { String hqlString = " ...

  4. Spring 管理数据源

    Spring 管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不 ...

  5. 学习SSH框架

    1.SSH框架的认知 在做相关的java的网页的开发制作时,良好的层次分解是十分有比要的,所以我们在云涌第三方的框架之下来简化还有名了我们相关的网站的开发. SSH框架实则为Struct + spri ...

  6. MyEclipse的快捷使用(含关联源码和Doc的方式)

    删除行代码 :在Eclipse中将光标移至待删除的行上,然后按Ctrl+d 组合键 快速导入包 :在Eclipse中将光标移至相应的类上面,按Ctrl+Shift+M 组合键 批量行注释 :Ctrl+ ...

  7. 再论hibernatetemplate

    自动生成hibernate配置文件的时候,会在dao层用到getSession()方法来操作数据库记录,但是他还有个方法getHibernateTemplate(),这两个方法究竟有什么区别呢? 1. ...

  8. No value for key [org.hibernate.impl.SessionFactoryImpl 异常解决

    使用Hibernate+Spring进行CRUD操作时.出现例如以下类似异常信息: java.lang.IllegalStateException: No value for key [org.hib ...

  9. getHibernateTemplate()的用法

    getHibernateTemplate() spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修改和删除等操作,此方法是在配置了spring以后,hi ...

随机推荐

  1. myEclipse配置SVN

    方法三:直接解压      下载SVN插件:site-1.6.10.zip      解压后将其全部文件拷贝至:D:\Program Files\Genuitec\MyEclipse 8.5\drop ...

  2. solr6.5的安装与配置

    环境介绍 solr 6.5 tomcat8 jdk1.8 win7系统 一.下载solr安装包 下载地址:http://www.apache.org/dyn/closer.lua/lucene/sol ...

  3. Docker版本升级至17.03

    2017/3/3,Docker官方发表了一篇博客,Docker版本从1.13.*直接跳入17.03,该版本的意思是17年3月.同时,还声明了Docker以后会以CE(Community Edition ...

  4. 无序列表li横向排列的间隙问题

    今天在写页面的时候,无意中遇到这样一个问题,就是无序列表li横向排列即做成导航栏时,列表项间有间隙. 如: 将列表项li变成列表块后(即将li标签设置为,display:inline-block后), ...

  5. Linux的ls命令在Windows中的应用

    Linux的ls命令在Windows中的应用 注:ls是Linux中的命令.其作用是列出当前目录下的文件与文件夹.效果等同于Wndows中的dir指令. 如下图 下面是详细步骤 步骤一.在桌面新建一个 ...

  6. Oracle_基本函数查询综合

    Oracle_基本函数查询综合 --[1]查询出每各月倒数第三天受雇的所有员工 select;   --[2]找出早于30年前受雇的员工 select>; select; select;     ...

  7. 【转载】keil5中加入STM32F10X_HD,USE_STDPERIPH_DRIVER的原因

    初学STM32,在RealView MDK 环境中使用STM32固件库建立工程时,初学者可能会遇到编译不通过的问题.出现如下警告或错误提示: warning: #223-D: function &qu ...

  8. 将本地的项目导入到github仓库总结lxw

    关键步骤: 第一:git clone https://github.com/lxw18231857001/demo-.git           #把github上面的仓库克隆到本地 本地项目文件夹下 ...

  9. ThinkPhp 添加模型类

    ----------------------------------------------- <?phpnamespace app\common\model;use traits\model\ ...

  10. ios开发-第二天

    1.#import可保证无论头文件出现多少次,只包含一次,而#include相反. 2.如果用户不提供文件路径的话,那么argc的值为1,可以用来做是否错误的判断. 3.面向对象和面向过程的区别 面向 ...