`
c_fanatic
  • 浏览: 64224 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

继承类的@Transactional---转

阅读更多
继承类的@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
分享到:
评论
1 楼 piaoxue_x 2011-03-24  
嗯,很好,谢谢了。对我没什么,帮助,不过为了积分,啰嗦两句……

相关推荐

    Spring-Reference_zh_CN(Spring中文参考手册)

    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. 用...

    spring chm文档

    3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用...

    cms后台管理

    类ContentListDirective继承自AbstractContentDirective,最主要的是execute方法 public class ContentListDirective extends AbstractContentDirective { /** * 模板名称 */ public static final String TPL...

    Spring中文帮助文档

    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. ...

    Spring API

    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. ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....

    HibernateAPI中文版.chm

    策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....

    hibernate3.2中文文档(chm格式)

    策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....

    Spring 2.0 开发参考手册

    3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用...

    Javashop开发规范V2.2

    @Transactional(propagation = Propagation.isrequired) 9 路径的规范 返回的路径变量最后不带 ‘/’ 如:String path =”user/1”; 使用者: path = path+”/”+myVar; 10 样式/HTML规范 10.1 前台分页 ...

    Hibernate+中文文档

    策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....

    springboot旅游网站

    比较符合现实项目的结构,对于需要重复使用代码进行了一定的封装统计放入到util里面,体现java封装、继承、多态的特点,设计对于数据库的操作,采用Transactional注解进行控制,保证数据操作的一致性、原子性、一致...

    Hibernate中文详细学习文档

    策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....

    Spring.3.x企业应用开发实战(完整版).part2

    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 ...

    Spring3.x企业应用开发实战(完整版) part1

    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 ...

    hibernate 体系结构与配置 参考文档(html)

    4. 持久化类(Persistent Classes) 4.1. 一个简单的POJO例子 4.1.1. 实现一个默认的(即无参数的)构造方法(constructor) 4.1.2. 提供一个标识属性(identifier property)(可选) 4.1.3. 使用非final的类 ...

    Hibernate 中文 html 帮助文档

    策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....

    hibernate 框架详解

    5. 持久化类(Persistent Classes) 5.1. 一个简单的POJO例子 5.1.1. 为持久化字段声明访问器(accessors)和是否可变的标志(mutators) 5.1.2. 实现一个默认的(即无参数的)构造方法(constructor) 5.1.3. 提供...

    Hibernate参考文档

    策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2....

    hibernate3.04中文文档.chm

    策略:事务缓存(transactional) 20.3. 管理缓存(Managing the caches) 20.4. 查询缓存(The Query Cache) 20.5. 理解集合性能(Understanding Collection performance) 20.5.1. 分类(Taxonomy) ...

Global site tag (gtag.js) - Google Analytics