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

/**
* 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. mybatis sql循环的使用

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...

  2. DEDE在图集列表中调出图集的所有图片[首页也适用]

    在include/common.func.php 中添加以下函数代码 代码如下:   // 在图集列表中调出图集的所有图片 function Getimgs($aid, $imgwith = 220, ...

  3. 关于Vue的各个UI框架(elementUI、mint-ui、VUX)

    elementUI 官网:http://element.eleme.io/ 使用步骤: 1.安装完vue-cli后,再安装 element-ui 命令行:npm i element-ui -D 相当于 ...

  4. PHP项目开发

    PHP项目开发 =================================== member:(用户表) userid username password name mobile emai a ...

  5. IE调试网页之三:使用 F12 工具控制台查看错误和状态 (Windows)

    IE调试网页之三:使用 F12 工具控制台查看错误和状态 (Windows)  等 请见博客园的  我的收藏

  6. python中的Queue(队列)详解

    一.Queue简介 python中的队列分类可分为两种: 1.线程Queue,也就是普通的Queue 2.进程Queue,在多线程与多进程会介绍. Queue的种类: FIFO:  Queue.Que ...

  7. 基础 - 32位操作系统最多只支持4G内存。

    32位操作系统最多只支持4G内存. CPU能不能直接访问硬盘的数据呢, 不能. 只能通过把硬盘的数据先放到内存里, 然后再从内存里访问硬盘的数据.我们平时玩游戏碰上读图loading 进度条的这个过程 ...

  8. 关于手残,搞废我的OLED屏幕的 追悼会

    2017-12-1913:36:41 昨天按照B站的资料利用esp12F做了一个天气站,可预报天气,惭愧的是模型做好了,照片还没拍就夭折了,可怜了我20块的屏幕,我心伤悲,莫知我哀呀! 本来调试已经成 ...

  9. 流API--缩减操作

    在Stream流操作中,比如说min(),max(),count()方法,这几个操作都会将一个流缩减成一个值,流API将这些操作称为特例缩减.另外,流API同时泛华了缩减这种概念,提供了reduce( ...

  10. git clone代码时候出现的报错

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px "Andale Mono"; color: #28fe14; backgr ...