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可以帮助我们进行资源管理和适当的异常转换!

转载于:https://www.cnblogs.com/kagome2014/p/6952778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值