Cannot unwrap to requested type [javax.sql.DataSource]
配置hibernate4和spring4时,出现错误,解决方法:
1、我去了spring4中的事务配置,数据库可以正常执行并访问。如下:
<!-- 配置Hibernate 数据事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置Hibernate 配置注解事务-->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置Hibernate 配置注解事务-->
<tx:annotation-driven transaction-manager="transactionManager" />
将这个内容去掉可以正常使用
2、将在hibernate中配置的数据源更改到spring中配置,问题可以解决。如下:
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
p:driverClass="org.sqlite.JDBC"
p:jdbcUrl="jdbc:sqlite:E:/upingbiao/upingbiao/WebRoot/db/upingbiao.db3"
p:password=""
p:user=""
/>
<!-- 配置Hibernate 数据源 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
p:driverClass="org.sqlite.JDBC"
p:jdbcUrl="jdbc:sqlite:E:/upingbiao/upingbiao/WebRoot/db/upingbiao.db3"
p:password=""
p:user=""
/>
<!-- 配置Hibernate 数据源 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>