Spring的事务管理难点剖析(1):DAO和事务管理的牵绊(三)

2014-11-24 08:46:59 · 作者: · 浏览: 2
ser WHERE user_name ='tom'");

51 System.out.println("score:"+score);

52 jdbcTemplate.execute("DELETE FROM t_user WHERE user_name='tom'");

53 }

54 }


此时,采用hiberWithoutTx.xml的配置文件,其配置内容如下所示:


01 < xml version="1.0" encoding="UTF-8" >

02 http://www.springframework.org/schema/beans"

03 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

04 xmlns:context="http://www.springframework.org/schema/context"

05 xmlns:p="http://www.springframework.org/schema/p"

06 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

07 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

08

09 …

10

11 class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"

12 p:dataSource-ref="dataSource">

13

14

15 com.baobaotao.User

16

17

18

19

20 org.hibernate.dialect.MySQLDialect

21 true

22

23

24

25

26

27 class="org.springframework.orm.hibernate3.HibernateTemplate"

28 p:sessionFactory-ref="sessionFactory"/>

29

30


com.baobaotao.User是使用了Hibernate注解的领域对象,代码如下所示:

view sourceprint 01 package com.baobaotao;

02

03 import javax.persistence.Entity;

04 import javax.persistence.Table;

05 import javax.persistence.Column;

06 import javax.persistence.Id;

07 import java.lang.reflect.Field;

08 import java.io.Serializable;

09

10 @Entity

11 @Table(name="T_USER")

12 public class User implements Serializable{

13 @Id

14 @Column(name = "USER_NAME")

15 private String userName;

16

17 private String password;

18

19 private int score;

20

21 @Column(name = "LAST_LOGON_TIME")

22 private long lastLogonTime = 0;

23

24 …

25 } www.2cto.com

运行UserHibernateWithoutTransManagerService,程序正确执行,并得到类似于 UserJdbcWithoutTransManagerService的执行结果。这说明Hibernate在Spring中,在没有事务管理器的情况 下,依然可以正常地进行数据的访问。