屌丝哥 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条回答 默认 最新

  • Jlins 2010-09-01 12:35
    关注

    //Session session = (Session) this.getHibernateTemplate();
    java.lang.ClassCastException: org.springframework.orm.hibernate3.HibernateTemplate cannot be cast to org.hibernate.Session
    你把 HibernateTemplate 强制转化成 Session 当然不行了?他们又不是父子关系

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题