Hibernate反向工程导致的数据无法存储
在使用MyEclipse过程中,用Hibernate反向工程生成的DAO有时会发生对象无法存储到数据库,或对象修改后无法存储到数据库的现象。原因是没有运用事务,可以在调用dao对象的代码前后加事务控制,但这样破坏了dao对数据库操作的 例如:
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 update(Exam instance) { log.debug("attaching update Exam instance"); try { Session session = getSession(); Transaction tr = session.beginTransaction(); //开始事务 session.update(instance); tr.commit(); //提交事务 session.flush(); log.debug("attachUpdate successful"); } catch (RuntimeException re) { log.error("attachUpdate failed", re); throw re; } }封装,让业务层中掺杂了持久层代码。
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30