论坛首页 →
Java企业应用论坛 →
Spring集成hibernate时,Spring使用事物管理session,但是执行的时候只出现Hibernate: select usertable_seq
浏览 2791 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2014-07-27
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name=""></property> </bean> --> <tx:advice id="advice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <:attributes> <:advice> <aop:config> <aop:pointcut expression="execution ( * com.tmq.service.impl..*(..))" id="px"/> <aop:advisor advice-ref="advice" pointcut-ref="px"/> </aop:config> <aop:config> <aop:aspect id="sessionAdvice" ref="sessionAdvice"> <aop:around method="processSession" pointcut="execution(* com.tmq.dao.impl.*.*(..))"/> </aop:aspect> </aop:config> <bean id="sessionAdvice" class="com.tmq.advice.SessionFactoryAdvice"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.connection.autoReconnect">true</prop> <prop key="hibernate.connection.autoReconnectForPools">true</prop> <prop key="hibernate.connection.characterEncoding">UTF-8</prop> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> </props> </property> <property name="mappingResources"> <list> <value>com/tmq/entity/User.hbm.xml<alue> <st> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <!-- 指定连接数据库的用户名 --> <property name="username" value="test" /> <!-- 指定连接数据库的密码 --> <property name="password" value="test" /> </bean> <bean id="userService" class="com.tmq.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"/> </bean> <bean id="userDao" class="com.tmq.dao.impl.UserDaoImpl"> </bean> </beans> 实体类的hbm文件如下:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tmq.entity"> <class name="User" table="usertable" mutable="true" dynamic-update="true"> <id name="userId" type="java.lang.Integer" column="USERID"> <generator class="sequence"> <param name="sequence">usertable_seq</param> </generator> </id> <property name="userName" type="java.lang.String"> <column name="USERNAME" length="32" not-null="true"/> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="32"/> </property> </class> </hibernate-mapping> 测试程序如下: public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml"); /*UserDao userDao=(UserDao) ac.getBean("userDao"); userDao.add(user, session)*/ User u1=new User(); u1.setUserName("aaaa"); u1.setPassword("123"); User u2=new User(); u2.setPassword("456"); UserService userService=(UserService) ac.getBean("userService"); userService.addUser(u1); //userService.addUser(u2); } 执行结果如下:Hibernate: select usertable_seq.nextval from dual 是Spring管理session,不需要hibernate自己开启事物提交 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |