Hibernate自动生成的DAO的修改方法
来源:互联网 发布:淘宝发货需要身份证 编辑:程序博客网 时间:2024/06/05 14:46
转载 Hibernate自动生成的DAO的修改方法
使用MyEclipse中用hibernate反向工程生成的DAO会发生对象无法存储到数据库的现象,原因是没有运用事务。DAO里注释提示如下:
/** * A data access object (DAO) providing persistence and search support for User * entities. Transaction control of the save(), update() and delete() operations * can directly support Spring container-managed transactions or they can be * augmented to handle user-managed Spring transactions. Each of these methods * provides additional information for how to configure it for the desired type * of transaction control. * * @see org.hibernate.entity.User * @author MyEclipse Persistence Tools */
解决方法一、使用Spring的事务管理
方法二、修改DAO,添加事务处理
当然可以在调用dao对象的代码前后加事务控制,但这样破坏了dao对数据库操作的封装,让业务层中掺杂了持久层代码。所以进行以下修改:
import org.hibernate.Session;import org.hibernate.Transaction;public void save(Resource transientInstance) { log.debug("saving Resource instance"); try { Session session = getSession(); Transaction tr = session.beginTransaction(); //开始事务 session.save(transientInstance); tr.commit(); //提交事务 session.flush(); //清空缓存 log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } }public void delete(Resource persistentInstance) { log.debug("deleting Resource instance"); try { Session session = getSession(); Transaction tr = session.beginTransaction(); session.delete(persistentInstance); tr.commit(); session.flush(); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } }
0 0
- Hibernate自动生成的DAO的修改方法
- Hibernate自动生成的DAO的修改方法
- 自动生成的hibernate DAO的几个方法
- hibernate 自动生成 dao 包含的几个方法
- hibernate自动生成的DAO类中的merge() attachDirty() attachClean()方法简单介绍
- hibernate自动生成dao
- Hibernate 反向工程生成的 DAO 文件 save 方法
- 利用hibernate自动生成数据表的方法
- myeclipse的hibernate自动生成的DAO不写数据库的解决办法
- Dorado 7 自动生成Dao的类
- 使用Abator自动生成ibatis的DAO.Model.mapper.xml方法使用说明
- Hibernate 系统自动生成数据库表的两种方法
- hibernate 的自动生成工具
- hibernate主键的自动生成
- hibernate的自动生成主键
- CodeSmith自动生成的Dao文件使用的字符串
- 基于HibernateTemplate的dao和service文件的自动生成
- Hibernate的DAO实现
- java junit测试
- js记录
- IPC基础
- [MongoDB学习笔记-01] 入门
- Spring AOP 深入分析
- Hibernate自动生成的DAO的修改方法
- HDU 3072 Intelligence System 传递的最小费用
- 网站应用架构
- memcache简介以及使用方法
- uploadFile.php
- 1015 Problem O
- ZOJ 3869-Ace of Aces【模拟众数】
- 【代码】SQL分页语句
- CSS定位机制之标准文档流