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

  1. <!-- 配置事务管理器 -->
  2. <bean id="transactionManager"
  3. class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  4. <property name="sessionFactory" ref="sessionFactory" />
  5. </bean>
  6.  
  7. <!-- 配置事务增强处理Bean,指定事务管理器 -->
  8. <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
  9. <!-- 配置详细事务处理语义 -->
  10. <tx:attributes>
  11. <tx:method name="insert*" propagation="REQUIRED" />
  12. <tx:method name="update*" propagation="REQUIRED" />
  13. <tx:method name="delete*" propagation="REQUIRED" />
  14.  
  15. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
  16. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
  17. <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
  18. <tx:method name="load*" propagation="SUPPORTS" read-only="true" />
  19.  
  20. <!-- 其他采用默认事务方式 -->
  21. <tx:method name="*" />
  22.  
  23. </tx:attributes>
  24. </tx:advice>

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

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

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

  1. @Override
  2. public String Save(String template_code, String block_code, String prop_code, String rule_code, String tpl,
  3. String par_prop, String title) {
  4. // TODO Auto-generated method stub
  5. String json = "{status: 'OK', msg: '保存成功!'}";
  6. Session session = this.getCurrentSession();
  7. try {
  8. TeValidationRule vModel = new TeValidationRule();
  9. List<Map> tpls = session.createSQLQuery("select * from te_template_sql t where t.sql_id = '"+tpl+"'")
  10. .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
  11. String muban = tpls.get(0).get("SQL_TEMPLATE").toString();
  12. String mainSql = "select b.table_name,p.data_field,p.prop_name from te_template a "
  13. + "left join te_template_block b on b.template_code = a.template_code "
  14. + "left join te_template_property p on p.block_code = b.block_code "
  15. + "where a.template_code = '"+template_code+"' and b.block_code = '"+block_code+"' and p.prop_code = '"+prop_code+"'";
  16. List<Map> mainProp = session.createSQLQuery(mainSql).setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
  17. String table = mainProp.get(0).get("TABLE_NAME").toString();
  18. String datafield = mainProp.get(0).get("DATA_FIELD").toString();
  19. String pname = mainProp.get(0).get("PROP_NAME").toString();
  20.  
  21. muban = muban.replaceAll("tableName", table);
  22. muban = muban.replaceAll("dataField", datafield);
  23. muban = muban.replaceAll("parField", par_prop);
  24.  
  25. String rid = "";
  26. if(rule_code!=null && !rule_code.equals("")){
  27. rid = rule_code;
  28. vModel.setRuleCode(rule_code);
  29. }else{
  30. rid = UUID.randomUUID().toString();
  31. }
  32. vModel.setRuleName(title);
  33. vModel.setRuleType(2);
  34. vModel.setRuleContent(muban);
  35. vModel.setErrorMsg(pname+"格式错误!");
  36. vModel.setRuleCategoryCode("8e267df45a7a4f59b257f5c15cc09bbb");
  37. vModel.setRuleStatus(1);
  38. vModel.setCreateUser("admin");
  39. vModel.setCreateTime(new Date());
  40. vModel.setUpdateUser("admin");
  41. vModel.setUpdateTime(new Date());
  42.  
  43. if(rule_code!=null && !rule_code.equals("")){
  44. session.update(vModel);
  45. }else{
  46. session.save(vModel);
  47. }
  48.  
  49. String numSql = "select count(*) nums from te_template_validation_rule "
  50. + "where block_code = '"+block_code+"' and prop_code = '"+prop_code+"' and rule_code = '"+rule_code+"'";
  51. List nums = session.createSQLQuery(numSql)
  52. .addScalar("NUMS").list();
  53. int has = Integer.parseInt(nums.get(0).toString());
  54. TeTemplateValidationRule teTemplateValidationRule = new TeTemplateValidationRule();
  55. String tbp = "";
  56.  
  57. teTemplateValidationRule.setBlockCode(block_code);
  58. teTemplateValidationRule.setPropCode(prop_code);
  59. teTemplateValidationRule.setRuleCode(rid);
  60. teTemplateValidationRule.setRuleType(2);
  61. teTemplateValidationRule.setRuleContent(muban);
  62. teTemplateValidationRule.setErrorMsg(pname+"格式错误!");
  63. teTemplateValidationRule.setCreateUser("admin");
  64. teTemplateValidationRule.setCreateTime(new Date());
  65. teTemplateValidationRule.setUpdateUser("admin");
  66. teTemplateValidationRule.setUpdateTime(new Date());
  67.  
  68. if(has == 0){
  69. tbp = UUID.randomUUID().toString();
  70. teTemplateValidationRule.setTemplateRuleCode(tbp);
  71. session.save(teTemplateValidationRule);
  72. }else{
  73. 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+"'";
  74. List tbp_code = session.createSQLQuery(hasTbpSql).addScalar("TEMPLATE_RULE_CODE").list();
  75. tbp = tbp_code.get(0).toString();
  76. teTemplateValidationRule.setTemplateRuleCode(tbp);
  77. session.update(teTemplateValidationRule);
  78. }
  79.  
  80. } catch (Exception e) {
  81. // TODO: handle exception
  82. json = "{status: 'ERROR', msg: '保存失败!'}";
  83. throw new RuntimeException();
  84. }
  85. return json;
  86. }

SpringMVC+hibernate4事务处理的更多相关文章

  1. springmvc+hibernate4事务管理配置

    1.事务的特性 事务的四种特性: 原子性:体现一个事务的操作的不可分割,要么权执行,要么全不执行. 一致性:事务的执行结果必须从一种一致性状态变到另一种一致性状态.最典型的就是转账,两个账户A.B总金 ...

  2. Spring4+SpringMVC+Hibernate4整合入门与实例

    配置web.xml <? xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&q ...

  3. Spring4 SpringMVC Hibernate4 Freemaker 集成示例

    变更更正(2014-05-30 13:47:22):一些IDE在web.xml我们会报告这个错误: cvc-complex-type.2.4.a: Invalid content was found ...

  4. springMVC+Hibernate4+Spring整合一(配置文件部分)

    本实例采用springMvc hibernate 与 spring 进行整合, 用springmvc 取代了原先ssh(struts,spring,hibernate)中的struts来扮演view层 ...

  5. Spring4 SpringMVC Hibernate4 Freemaker 整合样例

    更正改动(2014-05-30 13:47:22):有的IDE中web.xml会报这个错: cvc-complex-type.2.4.a: Invalid content was found star ...

  6. 搭建SpringMVC+Hibernate4+Spring3+Ajax+Maven项目

    首先新建一个Maven项目.百度一下会有非常多实例,这里就不介绍了,直接奔主题. 如题:这里使用的是Hibernate4和Spring3,使用的JPA和Spring注解,然后JDK版本号是1.7 以下 ...

  7. springMVC+Hibernate4+spring整合实例二(实例代码部分)

    UserController.java 代码: package com.edw.controller; import java.io.IOException; import java.io.Print ...

  8. Maven下Spring + SpringMvc + Hibernate4 配置实例

    1. 开发环境 IDEA 2. 在pom.xml中配置引用相关的包. <properties> <junit.version>4.10</junit.version> ...

  9. maven学习日记(三)-------开发环境搭建(springmvc+hibernate4)各种maven错误汇总

    1.maven编码 gbk 的不可映射字符 解决这个问题的思路: 在maven的编译插件中声明正确的字符集编码编码——编译使用的字符集编码与代码文件使用的字符集编码一致!! 安装系统之后,一般中文系统 ...

随机推荐

  1. Git-标签管理【转】

    本文转载自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 标签管理 发布一个版本 ...

  2. ExtJS实现分页grid paging

    背景 分页查询在Web页面中比例很大,我自己也写过分页框架,也用过很多第三方分页. 基于jquery的dataTables,那么多例子.清晰API.应用广泛.开源,即使是新手也可以很快上手. ExtJ ...

  3. 【web】支持jsp+mvc访问

    直接使用SpringMVC时配置访问jsp页面时很容易的事,但是由于spring Boot使用内嵌的servlet容器,所以对jsp的支持不是很好,而且也不建议使用jsp,但是为了满足这种返回jsp页 ...

  4. Python学习札记(二十八) 模块1

    参考:模块 NOTE 1.模块:一个.py文件称为一个模块. 2.代码模块化的意义:a.提升程序的可维护性 b.不用重复造轮子 3.避免模块冲突,解决方法:引入了按目录来组织模块的方法,称为包(Pac ...

  5. 转:常用svn命令

    在公司需要提交代码,常用的就是co.ci.add.up.和log 首先 svn help 可以看到 svn 所支持的全部命令: 命令不多,如果用过Tortoise SVN的客户端,从字面上也不难理解这 ...

  6. Linux查看版本当前操作系统内核信息

    1. # uname -a (Linux查看版本当前操作系统内核信息) 输出 Linux xxx --generic #~-Ubuntu SMP Wed Jul :: UTC x86_64 x86_6 ...

  7. git 使用和安装

    http://www.git-scm.com/download/ http://www.git-scm.com/download/win http://www.git-scm.com/download ...

  8. linux下运行jar

    方式一: java -jar XXX.jar 特点:当前ssh窗口被锁定,可按CTRL + C打断程序运行,或直接关闭窗口,程序退出 那如何让窗口不锁定? 方式二: java -jar XXX.jar ...

  9. POJ 3126 primepath bfs

    题目链接:http://poj.org/problem?id=3126 题意:1维的坐标轴,给出起点和终点,求从起点到终点变换经历的最短的步数.起点,终点和中间变换的数字都是4位,而且都是质数. 思路 ...

  10. plsql的快速生成sql语句设置

    单 单击tool(工具)->的preferences(首选项)  ,进入到首选项页面 在点击user interface 的editor下的autoreplace 的edit按钮        ...