Hibernate+Spring整合时报错

来源:互联网 发布:精英男 知乎 编辑:程序博客网 时间:2024/05/17 03:25

问题描述

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]

解决方案

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

给上述bean增加一个属性   <property name="dataSource" ref="dataSource"></property>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="configLocation" value="hibernate.cfg.xml"></property>
            <property name="dataSource" ref="dataSource"></property>
    </bean>

0 0
原创粉丝点击