继承类的@Transactional:org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
假设有以下类:
@Transactional
public class SubClass extends SuperClass {
public void loadDb(){
//数据库操作
}
}
public class SuperClass {
public void savedb() {
//数据库操作
}
}
savedb是父类的方法,loadDb是子类的方法。如果有以下调用:
@Test
public void test(){
SubClass o = new SubClass();
o.savedb();//将会报没有Session的错误
o.loadDb();//正常
}
可以看到在调父类方法时Hibernate报没有Session的错误(org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here),子类没有问题。
解决办法一:在子类中重载父类方法:
@Transactional
public class SubClass extends SuperClass {
public void loadDb(){
}
@Override
public void savedb() {
super.savedb();
}
}
这样显然繁琐了,子类savedb没有任何新操作。
解决方法二:在父类中标注@Transactional(父类是抽象类也可以):
@Transactional
public class SuperClass {
public void savedb() {
}
}
这需要父类的修改权限。
--- http://www.cnblogs.com/xiefeifeihu/archive/2010/07/27/1785958.html
分享到:
相关推荐
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.1.1. 使用BeanPostProcessor的Hello World示例 3.7.1.2. RequiredAnnotationBeanPostProcessor示例 3.7.2. 用...
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用...
类ContentListDirective继承自AbstractContentDirective,最主要的是execute方法 public class ContentListDirective extends AbstractContentDirective { /** * 模板名称 */ public static final String TPL...
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. The ApplicationContext 3.8.1. ...
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. The ApplicationContext 3.8.1. ...
策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....
策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....
策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用...
@Transactional(propagation = Propagation.isrequired) 9 路径的规范 返回的路径变量最后不带 ‘/’ 如:String path =”user/1”; 使用者: path = path+”/”+myVar; 10 样式/HTML规范 10.1 前台分页 ...
策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....
比较符合现实项目的结构,对于需要重复使用代码进行了一定的封装统计放入到util里面,体现java封装、继承、多态的特点,设计对于数据库的操作,采用Transactional注解进行控制,保证数据操作的一致性、原子性、一致...
策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....
9.6.1 使用@Transactional注解 9.6.2 通过AspectJ LTW引入事务切面 9.7 集成特定的应用服务器 9.7.1 BEA WebLogic 9.7.2 BEA WebLogic 9.8 小结 第10章 Spring的事务管理难点剖析 10.1 DAO和事务管理的牵绊 10.1.1 ...
9.6.1 使用@Transactional注解 9.6.2 通过AspectJ LTW引入事务切面 9.7 集成特定的应用服务器 9.7.1 BEA WebLogic 9.7.2 BEA WebLogic 9.8 小结 第10章 Spring的事务管理难点剖析 10.1 DAO和事务管理的牵绊 10.1.1 ...
4. 持久化类(Persistent Classes) 4.1. 一个简单的POJO例子 4.1.1. 实现一个默认的(即无参数的)构造方法(constructor) 4.1.2. 提供一个标识属性(identifier property)(可选) 4.1.3. 使用非final的类 ...
策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....
5. 持久化类(Persistent Classes) 5.1. 一个简单的POJO例子 5.1.1. 为持久化字段声明访问器(accessors)和是否可变的标志(mutators) 5.1.2. 实现一个默认的(即无参数的)构造方法(constructor) 5.1.3. 提供...
策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....
策略:事务缓存(transactional) 20.3. 管理缓存(Managing the caches) 20.4. 查询缓存(The Query Cache) 20.5. 理解集合性能(Understanding Collection performance) 20.5.1. 分类(Taxonomy) ...