Spring4+Hibernate5:报错Cannot unwrap to requested type [javax.sql.DataSource]
来源:互联网 发布:淘宝模特兼职的骗局 编辑:程序博客网 时间:2024/05/21 06:26
使用Spring4+Hibernate5时,如果使用hibernate.cfg.xml配置数据源,则会报错:Cannot unwrap to requested type [javax.sql.DataSource]。
具体原因不明有人给出的解释是:
<property name="hibernate.current_session_context_class">thread</property>
改成
hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
hibernate4不能用thread了 事物交给 spring管理,
但有两种方法解决这个问题:
- 在applicationContext中配置数据源
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}" ></property>
</bean>
<bean id="DBconfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}" ></property>
</bean>
<bean id="DBconfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
2.hibernate配置文件中必须指定hibernate.connection.provider_class属性
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
然后引入Hibernate C3P0相关包(位于hibernate-release-5.0.5.Final\lib\optional\c3p0):
c3p0-0.9.2.1.jar,hibernate-c3p0-5.0.5.Final.jar,mchange-commons-java-0.2.3.4.jar
0 0
- Spring4+Hibernate5:报错Cannot unwrap to requested type [javax.sql.DataSource]
- Cannot unwrap to requested type [javax.sql.DataSource]
- Cannot unwrap to requested type [javax.sql.DataSource]
- Cannot unwrap to requested type [javax.sql.DataSource] 的解决办法
- Cannot unwrap to requested type [javax.sql.DataSource]
- Cannot unwrap to requested type [javax.sql.DataSource]
- Cannot unwrap to requested type [javax.sql.DataSource] 的解决办法
- Cannot unwrap to requested type [javax.sql.DataSource]
- Cannot unwrap to requested type [javax.sql.DataSource]
- Cannot unwrap to requested type [javax.sql.DataSource] 解决
- 关于spring4和hibernate4整合,配置事务报“Cannot unwrap to requested type [javax.sql.DataSource]”错误的解决方法及心得
- spring和hibernate整合的时候一个错误Cannot unwrap to requested type [javax.sql.DataSource]
- 配置Spring Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource]
- Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching e
- Spring4+Struts2+Hibernate5报错:Unable to load configuration. - action -
- Class<SpringJUnit4ClassRunner> cannot be resolved to a type spring4.32的junit测试报错解决
- 报错:javax.servlet.jsp.PageContext cannot be resolved to a type
- 报错:javax.servlet.jsp.PageContext cannot be resolved to a type
- Web前端开发-3
- Perl正则表达式总结_1
- Android--Android四大组件(一)(Android studio)
- textField:shouldChangeCharactersInRange:replacementString: 限制字数,替换字符串
- Python hashmap
- Spring4+Hibernate5:报错Cannot unwrap to requested type [javax.sql.DataSource]
- 关于苹果PUSH(APNS)机制
- Grunt学习——简化静态资源
- android布局优化
- python 切片
- 【hdu1712】ACboy needs your help——分组背包
- windows录音程序原理
- 深入理解Java:注解(Annotation)自定义注解入门
- Integer与int的种种比较你知道多少?