Spring 无法展开为请求的类型[javax.sql.DataSource]
我想在spring boot和hibernate 5.2.6中使用sessionfactory 在尝试了一些解决方案之后,我尝试使用LocalSessionFactoryBuilder来解决这个问题,但我得到了这个异常。我的数据源由spring boot注入。我不知道为什么Spring 无法展开为请求的类型[javax.sql.DataSource],spring,hibernate,spring-boot,Spring,Hibernate,Spring Boot,我想在spring boot和hibernate 5.2.6中使用sessionfactory 在尝试了一些解决方案之后,我尝试使用LocalSessionFactoryBuilder来解决这个问题,但我得到了这个异常。我的数据源由spring boot注入。我不知道为什么 @Configuration @EnableTransactionManagement public class DatabaseConfiguration { @Inject public DataSou
@Configuration
@EnableTransactionManagement
public class DatabaseConfiguration {
@Inject
public DataSource dataSource;
@Inject
public JpaProperties prop;
@Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
builder.scanPackages("or.roshan");
builder.setProperties(getHibernateProperties());
return builder.buildSessionFactory();
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
return properties;
}
@Bean
public HibernateTransactionManager transactionManager(@Autowired SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}
}
我得到这个例外
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [org/roshan/framework/config/DatabaseConfiguration.class]: 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:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at org.roshan.uaa.UaaApplication.main(UaaApplication.java:20)
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.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.unwrap(UserSuppliedConnectionProviderImpl.java:38)
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)
... 21 common frames omitted
我不知道你是从哪里得到这个建议的,但我觉得很奇怪。如果要公开
会话工厂
,应执行以下操作:
@Configuration
public class MyConfig {
@Bean
public SessionFactory sessionFactory(EntityManagerFactory factory) {
if (factory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("factory is not a hibernate factory");
}
return entityManagerFactory.unwrap(SessionFactory.class);
}
}
我不知道你是从哪里得到这个建议的,但我觉得很奇怪。如果要公开
会话工厂
,应执行以下操作:
@Configuration
public class MyConfig {
@Bean
public SessionFactory sessionFactory(EntityManagerFactory factory) {
if (factory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("factory is not a hibernate factory");
}
return entityManagerFactory.unwrap(SessionFactory.class);
}
}
我最近遇到了同样的错误,结果是问题出在我的数据库中,SQL不正确。我最近遇到了同样的错误,结果是问题出在我的数据库中,SQL不正确。我也有同样的问题,但我不清楚你的答案。如何在不将包设置为扫描、hibernate属性和数据源的情况下获得会话工厂?这是使用Spring Boot,因此自动配置在默认设置下实现了这一点。我有同样的问题,但我不清楚您的答案。如何在不将包设置为扫描、hibernate属性和数据源的情况下获得会话工厂?这是使用Spring Boot,因此自动配置使用默认设置来实现