getHibernateTemplate() VS getSession()
如题所示,对于这个问题,官网文档已给出答案,详见:
/**
* 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()的更多相关文章
- getHibernateTemplate()和getSession()的区别
自动生成hibernate配置文件的时候,会在dao层用到getSession()方法来操作数据库记录,但是他还有个方法getHibernateTemplate(),这两个方法究竟有什么区别呢? 1. ...
- hibernate总记录数查询和分页查询
//参考代码 //第一种方法: String hql = "select count(*) from User as user"; Integer count = (Integer ...
- Hibernate使用count(*)取得表中记录总数
/** * @TODO:查询某一年度的所有计划数量 */ public int findCountByYear(String currYear) { String hqlString = " ...
- Spring 管理数据源
Spring 管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不 ...
- 学习SSH框架
1.SSH框架的认知 在做相关的java的网页的开发制作时,良好的层次分解是十分有比要的,所以我们在云涌第三方的框架之下来简化还有名了我们相关的网站的开发. SSH框架实则为Struct + spri ...
- MyEclipse的快捷使用(含关联源码和Doc的方式)
删除行代码 :在Eclipse中将光标移至待删除的行上,然后按Ctrl+d 组合键 快速导入包 :在Eclipse中将光标移至相应的类上面,按Ctrl+Shift+M 组合键 批量行注释 :Ctrl+ ...
- 再论hibernatetemplate
自动生成hibernate配置文件的时候,会在dao层用到getSession()方法来操作数据库记录,但是他还有个方法getHibernateTemplate(),这两个方法究竟有什么区别呢? 1. ...
- No value for key [org.hibernate.impl.SessionFactoryImpl 异常解决
使用Hibernate+Spring进行CRUD操作时.出现例如以下类似异常信息: java.lang.IllegalStateException: No value for key [org.hib ...
- getHibernateTemplate()的用法
getHibernateTemplate() spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修改和删除等操作,此方法是在配置了spring以后,hi ...
随机推荐
- visual studio添加lib库
方法1: 通过设置工程配置来添加lib库. A.添加工程的头文件目录:工程---属性---配置属性---c/c++---常规---附加包含目录:加上头文件存放目录. B.添加文件引用的lib静态库路径 ...
- sublimeserver启动本地服务器(sublime text)
今天又get到了一个新知识点,就是在sublime text上也可以模拟一个本地服务器的环境,前提是要先安装sublimeserver这个插件.这个插件的安装办法有两种. 1.我们可以直接在subli ...
- NodeJS、NPM安装配置与测试步骤(windows版本)
1.windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的"INS ...
- LNMP状态管理命令
https://lnmp.org/faq/lnmp-status-manager.html LNMP状态管理命令: LNMP 1.2+状态管理: lnmp {start|stop|reload|res ...
- ASP.NET网络爬虫小研究 HtmlAgilityPack基础,爬取数据保存在数据库中再显示再自己的网页中
1.什么是网络爬虫 关于爬虫百度百科这样定义的:网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些 ...
- 常用SQL笔记总结
DDL(data definition language)创建和管理表 1.创建表 1.直接创建 例如: create table emp( name varchar(20), salary int ...
- action之间传参为中文;type='redirect'和 type='redirectAction'主要区别
摘录自:http://blog.csdn.net/lhi705/article/details/7446156 Struts2中action之间传参中文乱码的问题 解决方法一(已经验证,可以): 两个 ...
- centos如何安装python库?
通过yum install安装,先解决yum不能安装python库的问题 yum install -y epel-release #先安装epel源,参考http://sharadchhetri. ...
- freemarker常用值格式化方法
freemarker常用的值格式化方法: 1.${price?string('0.00')} 对price进行格式化,小数点后不足2位用0补足. 比如:price=1 输出:1.00 2.${pric ...
- awk的+=用法
awk增加统计列值为增加列数或进行运行结果统计,使用符号 + =.增加的结果赋给符号左边变量值,增加到变量的域在符号右边.例如将 $ 1加入变量total,表达式为toatl+=$1.列值增加很有用. ...