我正在为springMVc创建一个示例项目,其中hibernate使用oracle作为后端。我遇到一个问题似乎是一些配置问题,但无法识别什么是错误的。任何指导都会有很大的帮助。
org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
stacktrace:
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getTransactionManager' defined in common.configuaration.AppConfig: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
Apr 05, 2018 5:42:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:oracle:thin:@host:port:SID]
Apr 05, 2018 5:42:38 AM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getTransactionManager' defined in common.configuaration.AppConfig: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4577)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5041)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Catalina.start(Catalina.java:682)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:353)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:493)
Caused by: org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.unwrap(DriverManagerConnectionProviderImpl.java:217)
at org.springframework.orm.hibernate5.SessionFactoryUtils.getDataSource(SessionFactoryUtils.java:198)
at org.springframework.orm.hibernate5.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:353)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
在我的hibernate配置下面找到
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages = "common.spring.service")
@ComponentScan(basePackages = "common.spring.dao")
public class AppConfig {
@Bean
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
Properties props = new Properties();
// Setting JDBC properties
props.put("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
props.put("hibernate.connection.url", "jdbc:oracle:thin:@host:port:SID");
props.put("hibernate.connection.username", "test");
props.put("hibernate.connection.password", "test123");
props.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
//props.put("hibernate.default_schema", "CCTRL1D");
// Setting Hibernate properties
props.put("hibernate.show_sql", true);
// props.put("hibernate.hbm2ddl.auto", "update");
// // Setting C3P0 properties
props.put("hibernate.c3p0.min_size","5");
props.put("hibernate.c3p0.max_size","20");
props.put("hibernate.c3p0.acquire_increment","1");
props.put("hibernate.c3p0.timeout","1000");
props.put("hibernate.c3p0.max_statements","150");
factoryBean.setHibernateProperties(props);
factoryBean.setPackagesToScan("common.spring.model");
return factoryBean;
}
@Bean
public HibernateTransactionManager getTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(getSessionFactory().getObject());
return transactionManager;
}
}
我使用的是spring和hibernate的下面版本
<springframework.version>4.3.10.RELEASE</springframework.version>
<hibernate.version>5.2.11.Final</hibernate.version>
答案 0 :(得分:0)
错误
org.hibernate.service.UnknownUnwrapTypeException:无法解包到 请求类型[javax.sql.DataSource
非常自我描述:Spring是为您配置和设置数据库层的人。为此,它需要从您那里获得已配置的javax.sql.DataSource。相反,您通过一组不可用的属性值来提供它。
尝试像这样修改AppConfig:
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages = "common.spring.service")
@ComponentScan(basePackages = "common.spring.dao")
public class AppConfig {
@Bean
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
Properties props = new Properties();
// Setting JDBC properties
// props.put("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
// props.put("hibernate.connection.url", "jdbc:oracle:thin:@host:port:SID");
//props.put("hibernate.connection.username", "test");
//props.put("hibernate.connection.password", "test123");
props.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
//props.put("hibernate.default_schema", "CCTRL1D");
// Setting Hibernate properties
props.put("hibernate.show_sql", true);
// props.put("hibernate.hbm2ddl.auto", "update");
// // Setting C3P0 properties
props.put("hibernate.c3p0.min_size","5");
props.put("hibernate.c3p0.max_size","20");
props.put("hibernate.c3p0.acquire_increment","1");
props.put("hibernate.c3p0.timeout","1000");
props.put("hibernate.c3p0.max_statements","150");
factoryBean.setHibernateProperties(props);
factoryBean.setPackagesToScan("common.spring.model");
return factoryBean;
}
@Bean
public HibernateTransactionManager getTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(getSessionFactory().getObject());
return transactionManager;
}
@Bean
public static DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("yourDriverClasName");
dataSource.setUrl("yourDbUrl");
dataSource.setUsername("yourDbUser");
dataSource.setPassword("yourDbPassowrd");
return dataSource;
}
}