屌丝哥
2010-08-31 23:41
浏览 247
已采纳

深夜疑惑:SH整合时,getSession()正确,getHibernateTemplate()报错

[size=medium]
1、applicationContext-common.xml:
[code="java"]

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
    </bean>


    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

    <!-- 设置哪些类哪些方法使用上面的事务  -->
    <aop:config>
        <aop:pointcut expression="execution(* com.spring.manager.*.*(..))" id="allManagerMethod"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
    </aop:config>

    <!-- 设置事务传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="modify*" propagation="REQUIRED"/>
            <tx:method name="*" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>

[/code]
2、applicationContext-bean.xml:
[code="java"]



<bean id="logManager" class="com.spring.manager.LogManagerImp">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

[/code]
3、测试代码:
[code="java"]
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
UserManager um = (UserManager)factory.getBean("userManager");

    User u = new User();
    u.setId(1);
    u.setName("张三");

    um.addUser(u);

[/code]
[/size]
4、UserManagerImp.java:
[code="java"]
public String addUser(User user) {
[color=red] //Session session = (Session) this.getHibernateTemplate();
Session session = (Session) this.getSession();[/color]
session.save(user);

    Log log = new Log();
    log.setDetail("增加一个用户");
    log.setTime(new Date());
    log.setType("查询");

    logManager.addLog(log);

    return null;
}

[/code]
为什么使用getSession()正确执行,但是使用getHibernateTemplate()则报错,信息如下:
[code="java"]
[color=red]java.lang.ClassCastException: org.springframework.orm.hibernate3.HibernateTemplate cannot be cast to org.hibernate.Session[/color]
at com.spring.manager.UserMangerImp.addUser(UserMangerImp.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy1.addUser(Unknown Source)
at com.spring.test.UserMangerImpTest.testAddUser(UserMangerImpTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
[/code]

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题