2006-10-27

spring+hibernate之applicationContext.xml配置

关键字: spring hibernate
最近一段时间潜心自学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>
评论
111111 2008-08-03   回复
datasource ne???
111111 2008-08-03   回复
datasource ne???
volking 2008-02-25   回复
我运行怎么有警告
16:07:43,671     WARN   Cglib2AopProxy:250   -   Unable   to   proxy   method   [public   final   org.hibernate.SessionFactory   org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSessionFactory()]   because   it   is   final:   All   calls   to   this   method   via   a   proxy   will   be   routed   directly   to   the   proxy.
16:07:43,671     WARN   Cglib2AopProxy:250   -   Unable   to   proxy   method   [public   final   org.springframework.orm.hibernate3.HibernateTemplate   org.springframework.orm.hibernate3.support.HibernateDaoSupport.getHibernateTemplate()]   because   it   is   final:   All   calls   to   this   method   via   a   proxy   will   be   routed   directly   to   the   proxy.
16:07:43,671     WARN   Cglib2AopProxy:250   -   Unable   to   proxy   method   [public   final   void   org.springframework.orm.hibernate3.support.HibernateDaoSupport.setHibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate)]   because   it   is   final:   All   calls   to   this   method   via   a   proxy   will   be   routed   directly   to   the   proxy.
16:07:43,671     WARN   Cglib2AopProxy:250   -   Unable   to   proxy   method   [public   final   void   org.springframework.orm.hibernate3.support.HibernateDaoSupport.setSessionFactory(org.hibernate.SessionFactory)]   because   it   is   final:   All   calls   to   this   method   via   a   proxy   will   be   routed   directly   to   the   proxy.
16:07:43,671     WARN   Cglib2AopProxy:250   -   Unable   to   proxy   method   [public   final   void   org.springframework.dao.support.DaoSupport.afterPropertiesSet()   throws   java.lang.IllegalArgumentException,org.springframework.beans.factory.BeanInitializationException]   because   it   is   final:   All   calls   to   this   method   via   a   proxy   will   be   routed   directly   to   the   proxy.
kaki 2007-09-07   回复
十分感谢!!
daiing 2007-04-13   回复
果然是这样,找了很久终于找到问题了
发表评论

您还没有登录,请登录后发表评论

wiley
搜索本博客
我的相册
Ea8d44f2-fd0e-3321-9c0a-322c4a0372d1-thumb
skoda_1.jpg
共 1 张
最近加入圈子
存档
最新评论
评论排行榜