问题描述:

public void save(BaseEntity baseEntity) {
Session session = null;
try {
session = currentSession();
session.save(baseEntity);
} catch (HibernateException ex) {
ex.printStackTrace();
}
}

当我执行该save方法时,抛出了该异常。

问题分析:

没有配置事务引起的问题。

解决办法:

我采用的是注解配置事务的方式,在XXX-servlet.xml中加上:

<tx:annotation-driven/>
<!-- <tx:annotation-driven transaction-manager="XXX"/> -->

这样就支持注解的方式配置事务了,这个标签会默认选择id为transactionManager的事务管理器(可以通过修改其transaction-Manager属性值来指定其他事务管理器)。

然后在application-config.xml(数据库的spring配置文件,即配置sessionFactory的地方)中加上:

<!-- 事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

就成功配置事务了。

然后在需要使用事务的方法上加上@Transactional就能解决异常抛出的问题了。

@Transactional
public void save(BaseEntity baseEntity) {
Session session = null;
try {
session = currentSession();
session.save(baseEntity);
} catch (HibernateException ex) {
ex.printStackTrace();
}
}

(在类级加上@Transactional好像会出其他的错误,有待进一步调试...)

出现No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here异常的更多相关文章

  1. spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常

    java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not al ...

  2. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a ...

  3. spring事务管理出错。No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy ...

  4. 报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotatio ...

  5. No Hibernate Session bound to thread, and configuration does not allow creat

    No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...

  6. 解决No Hibernate Session bound to thread, and configuration does not allow creat。。。

    applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  7. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a

    如果出现了这个错误,看看在使用Hibernate的时候有没有在事务的环境下执行操作!

  8. No Hibernate Session bound to thread, and configuration does not allow

    今天晚上挺悲催的,遇到了这个问题花费我很长时间,现在总结如下: 到这这种情况的发生有两种情况: 1,没有配置事物只要在Spring配置文件中添加如下代码: <bean id="txMa ...

  9. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not

    遇到这个问题之前,我去百度和谷歌去搜索了一下.发现各种说法.可是针对我的项目而言,也就是公司的项目而言,这个问题的根源并不是是网上所说的那样. 最后是通过自己的想法做測试得到了解决. 1.首先说说我的 ...

随机推荐

  1. ArcGIS添加鹰眼

    axMapControl1是主地图 axMapControl2是鹰眼地图 private void axMapControl1_OnExtentUpdated(object sender, IMapC ...

  2. 在SQL中修改数据库名称

    假设SQL Server 2008中有个数据库test,现在要将其改名为zhy步骤:(1) 分离数据库:打开management studio,找到test数据库-->右键-->任务--& ...

  3. C#获取本机IP方法,获取本机局域网IP地址方法

    1. private void GetIP() { string hostName = Dns.GetHostName();//本机名 //System.Net.IPAddress[] address ...

  4. Android的Activity屏幕切换滑动动画

    Activity的切换效果使用的是Android的动画效果,Android的动画在官方有相关资料:http://developer.android.com/guide/topics/graphics/ ...

  5. Hibernate学习——映射关系

    学习记录 O(∩_∩)O . 如果你恰巧路过,希望你能停下脚步瞧一瞧,不足之处望指出,感激不尽~ 使用工具: 1.eclipse   2.hibernate压缩包(hibernate_4.3.11)  ...

  6. php工厂设计模式

    class DbFactory { private $errmsg = '未找到类文件'; static function factory($className){ $className = strt ...

  7. Eclipse导入jre方法

    处理步骤:引入本机安装的jre1.8的步骤如下:

  8. Haproxy+PXC实现负载均衡

    软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第 三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载.HA ...

  9. Mobile Matrices

    This is an attempt to compile a list of relevant specifications for all modern smart phones and mobi ...

  10. Android 展示键盘时候布局被修改的问题

    解决方法,在mainfest.xml中,对那个Activity加: <activity android:name=".activity.HomeActivity"androi ...