博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC+hibernate4事务处理
阅读量:6973 次
发布时间:2019-06-27

本文共 5316 字,大约阅读时间需要 17 分钟。

首先spring-hibernate.xml里配置事务:

然后,使用的时候要注意,要用注解的方式在Service层配置事务:

@Override    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)    public String Save(String template_code, String block_code, String prop_code, String rule_code, String tpl,            String par_prop, String title) {        // TODO Auto-generated method stu        return teValidationRuleDao.Save(template_code, block_code, prop_code, rule_code, tpl, par_prop, title);    }

最后,要注意如果需要事务回滚,一定要在Dao层抛出RuntimeException这个运行时错误,否则不好使!

@Override    public String Save(String template_code, String block_code, String prop_code, String rule_code, String tpl,            String par_prop, String title) {        // TODO Auto-generated method stub        String json = "{status: 'OK', msg: '保存成功!'}";        Session session = this.getCurrentSession();        try {            TeValidationRule vModel = new TeValidationRule();            List tpls = session.createSQLQuery("select * from te_template_sql t where t.sql_id = '"+tpl+"'")                    .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();            String muban = tpls.get(0).get("SQL_TEMPLATE").toString();            String mainSql = "select b.table_name,p.data_field,p.prop_name from te_template a "                    + "left join te_template_block b on b.template_code = a.template_code "                    + "left join te_template_property p on p.block_code = b.block_code "                    + "where a.template_code = '"+template_code+"' and b.block_code = '"+block_code+"' and p.prop_code = '"+prop_code+"'";            List mainProp = session.createSQLQuery(mainSql).setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();            String table = mainProp.get(0).get("TABLE_NAME").toString();            String datafield = mainProp.get(0).get("DATA_FIELD").toString();            String pname = mainProp.get(0).get("PROP_NAME").toString();                        muban = muban.replaceAll("tableName", table);            muban = muban.replaceAll("dataField", datafield);            muban = muban.replaceAll("parField", par_prop);                        String rid = "";            if(rule_code!=null && !rule_code.equals("")){                rid = rule_code;                vModel.setRuleCode(rule_code);            }else{                rid = UUID.randomUUID().toString();            }            vModel.setRuleName(title);            vModel.setRuleType(2);            vModel.setRuleContent(muban);            vModel.setErrorMsg(pname+"格式错误!");            vModel.setRuleCategoryCode("8e267df45a7a4f59b257f5c15cc09bbb");            vModel.setRuleStatus(1);            vModel.setCreateUser("admin");            vModel.setCreateTime(new Date());            vModel.setUpdateUser("admin");            vModel.setUpdateTime(new Date());                        if(rule_code!=null && !rule_code.equals("")){                session.update(vModel);            }else{                session.save(vModel);            }                        String numSql = "select count(*) nums from te_template_validation_rule "                    + "where block_code = '"+block_code+"' and prop_code = '"+prop_code+"' and rule_code = '"+rule_code+"'";            List nums = session.createSQLQuery(numSql)                    .addScalar("NUMS").list();            int has = Integer.parseInt(nums.get(0).toString());            TeTemplateValidationRule teTemplateValidationRule = new TeTemplateValidationRule();            String tbp = "";            teTemplateValidationRule.setBlockCode(block_code);            teTemplateValidationRule.setPropCode(prop_code);            teTemplateValidationRule.setRuleCode(rid);            teTemplateValidationRule.setRuleType(2);            teTemplateValidationRule.setRuleContent(muban);            teTemplateValidationRule.setErrorMsg(pname+"格式错误!");            teTemplateValidationRule.setCreateUser("admin");            teTemplateValidationRule.setCreateTime(new Date());            teTemplateValidationRule.setUpdateUser("admin");            teTemplateValidationRule.setUpdateTime(new Date());                        if(has == 0){                tbp = UUID.randomUUID().toString();                teTemplateValidationRule.setTemplateRuleCode(tbp);                session.save(teTemplateValidationRule);            }else{                String hasTbpSql = "select template_rule_code from te_template_validation_rule where block_code = '"+block_code+"' and prop_code = '"+prop_code+"' and rule_code = '"+rule_code+"'";                List tbp_code = session.createSQLQuery(hasTbpSql).addScalar("TEMPLATE_RULE_CODE").list();                tbp = tbp_code.get(0).toString();                teTemplateValidationRule.setTemplateRuleCode(tbp);                session.update(teTemplateValidationRule);            }                    } catch (Exception e) {            // TODO: handle exception            json = "{status: 'ERROR', msg: '保存失败!'}";            throw new RuntimeException();        }        return json;    }

 

转载地址:http://lxesl.baihongyu.com/

你可能感兴趣的文章
go异常处理原则
查看>>
左侧固定,右侧自适应的布局方式理解margin负值理论
查看>>
Roman numerals/Encode - Rosetta Code
查看>>
高质量C++/C编程指南
查看>>
数据持久化Nbear使用经验分享(一)附NBearLite源码和测试Demo
查看>>
usp10.dll和lpk.dll病毒如何处理
查看>>
FPGA同步复位,异步复位以及异步复位同步释放实例分析
查看>>
窗体传值
查看>>
《转》从程序员到项目经理(五):不是人人都懂的学习要点
查看>>
如何让做好领导助理工作
查看>>
跟我一起云计算(3)——hbase
查看>>
vim与外部文件的粘帖复制
查看>>
3、数字签名
查看>>
Entity Framework DBFirst尝试
查看>>
dd测试硬盘性能
查看>>
C# 图像处理:实现鼠标选择矩形截图
查看>>
SWIG在系统中安装
查看>>
MySQL在大型网站的应用架构演变
查看>>
C++ 求阶乘 四种方法
查看>>
pojBuy Tickets2828线段树或者树状数组(队列中倒序插队)
查看>>