百事通信息网 最近一段时间潜心自学struts,
spring,
hibernate框架,写了一些独立框架的例子还算顺利,今天试着将
spring与
hibernate整合,遇到一些问题,故写此文。
主要功能:完成数据库表的查、增、删、改操作。
问题描述:查询功能一切正常,但是在增、删、改操作中发现,数据库表中数据没有改变,查看控制台日志也没有任何异常发现,纳闷了好久,仔查检查日志,发现在新增操作中表id有在自增,由此确定是问题出在事务没有提交。将事务配置上去后出现如下报错:java.lang.ClassCastException: $Proxy1,百度上搜到一文,在业务类如果实现了接口,得增加如下代码
<property name="proxyTargetClass">
<value>true</value>
</property>
果然如此,再次运行,成功了!
以下是我applicationContext.xml的内容:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="mySessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
- <bean id="myTransactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref bean="mySessionFactory" />
- </property>
- </bean>
- <bean id="myBaseTransactionProxy"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
- abstract="true">
- <property name="transactionManager" ref="myTransactionManager" />
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- <!--
- <prop key="insert*">PROPAGATION_REQUIRED</prop>
- <prop key="save">PROPAGATION_REQUIRED</prop>
- <prop key="update*">PROPAGATION_REQUIRED</prop>
- <prop key="edit*">PROPAGATION_REQUIRED</prop>
- <prop key="del*">PROPAGATION_REQUIRED</prop>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="disPlay*">PROPAGATION_REQUIRES_NEW</prop>
- -->
- </props>
- </property>
- </bean>
- <bean id="UsersDAO" class="com.notepad.dao.UsersDAO">
- <property name="sessionFactory">
- <ref bean="mySessionFactory" />
- </property>
- </bean>
- <bean id="userTarget" class="com.notepad.bussies.UserService">
- <property name="usersDao">
- <ref local="UsersDAO" />
- </property>
- </bean>
- <bean id="UserService" parent="myBaseTransactionProxy">
- <property name="proxyTargetClass">
- <value>true</value>
- </property>
- <property name="target">
- <ref local="userTarget" />
- </property>
- </bean>
- </beans>