UnknownUnwrapTypeException:无法解开请求的类型[javax.sql.DataSource] [英] UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
问题描述
我知道这个问题之前已经得到解答,但没有一个答案似乎对我有帮助,因为我没有在我的项目中使用数据源。
I a有一个与Spring和Hibernate集成的Maven项目,连接到SQL Server 2014数据库。
这是我的pom.xml文件,您可以在其中看到以下版本:
/ p> < project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/行家-v4_0_0.xsd>
< modelVersion> 4.0.0< / modelVersion>
< groupId> BachelorDegree< / groupId>
< artifactId> BuildingAdministration< / artifactId>
<包装>战争< / packaging>
< version> 0.0.1-SNAPSHOT< / version>
< name>建筑管理Maven Webapp< / name>
< url> http://maven.apache.org< / url>
< build>
< finalName>建筑物管理< / finalName>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-compiler-plugin< / artifactId>
< version> 3.2< / version>
<配置>
< source> 1.8< / source>
< target> 1.8< / target>
< / configuration>
< / plugin>
< / plugins>
< / build>
<属性>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< spring.version> 4.3.1.RELEASE< /spring.version>
< spring-security.version> 4.1.1.RELEASE< /spring-security.version>
< hibernate.version> 5.2.1.Final< /hibernate.version>
< hibernate.ga.version> 3.5.4-最终< /hibernate.ga.version>
< hibernate-annotation.version> 3.5.6-最终< /hibernate-annotation.version>
< javax-servlet-api.version> 3.1.0< /javax-servlet-api.version>
< jquery.version> 2.1.1< /jquery.version>
< bootstrap.version> 3.2.0< /bootstrap.version>
< javax-servlet.version> 1.2< /javax-servlet.version>
< log4j.version> 2.6.2< /log4j.version>
< spring.simple> 2.5.6< /spring.simple>
< jta.version> 1.1< /jta.version>
< aspectjweaver.version> 1.8.9< /aspectjweaver.version>
< / properties>
<依赖关系>
< dependency>
< groupId> org.apache.logging.log4j< / groupId>
< artifactId> log4j-api< / artifactId>
< version> $ {log4j.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.apache.logging.log4j< / groupId>
< artifactId> log4j-core< / artifactId>
< version> $ {log4j.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-orm< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-beans< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework.security< / groupId>
< artifactId> spring-security-web< / artifactId>
< version> $ {spring-security.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework.security< / groupId>
< artifactId> spring-security-config< / artifactId>
< version> $ {spring-security.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-web< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> javax.servlet-api< / artifactId>
< version> 3.1.0< / version>
< scope>提供< / scope>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-core< / artifactId>
< /依赖关系>
<! - 提供核心功能的spring-context - >
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-context< / artifactId>
< /依赖关系>
<! - Spring-aop模块提供符合AOP联盟的面向方面的
编程实现,允许您定义 - >
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-aop< / artifactId>
< /依赖关系>
<! - Spring-webmvc模块(也称为Web-Servlet模块)包含
Spring的模型 - 视图 - 控制器(MVC)和REST Web服务实现
用于Web应用程序 - >
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-webmvc< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-tx< / artifactId>
< /依赖关系>
<! - - http://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs - >
< dependency>
< groupId> org.springframework.security< / groupId>
< artifactId> spring-security-taglibs< / artifactId>
< /依赖关系>
<! - <依赖关系> <&的groupId GT; org.hibernate作为< /&的groupId GT; < artifactId的>冬眠的注解< / artifactId的>
< version> $ {hibernate-annotation.version}< / version> < /依赖性> - >
<! - <依赖关系> <&的groupId GT; org.hibernate.javax.persistence< /&的groupId GT; < artifactId的>冬眠-JPA-2.0-API< / artifactId的>
< version> 1.0.1.Final< / version> < /依赖性> - >
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< /依赖关系>
<! - <依赖关系> <&的groupId GT; org.hibernate作为< /&的groupId GT; < artifactId的>冬眠< / artifactId的>
< version> $ {hibernate.ga.version}< / version> < /依赖性> - >
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate< / artifactId>
< version> $ {hibernate.ga.version}< / version>
< type> pom< / type>
< /依赖关系>
< dependency>
< groupId> javax.transaction< / groupId>
< artifactId> jta< / artifactId>
< version> $ {jta.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.webjars< / groupId>
< artifactId> jquery< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.webjars< / groupId>
< artifactId>引导程序< / artifactId>
<排除项>
<排除>
< groupId> org.webjars< / groupId>
< artifactId> jquery< / artifactId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> jstl< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.aspectj< / groupId>
< artifactId> aspectjweaver< / artifactId>
< version> $ {aspectjweaver.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-c3p0< / artifactId>
< version> $ {hibernate.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-ehcache< / artifactId>
< version> $ {hibernate.version}< / version>
< /依赖关系>
< /依赖关系>
< repositories>
< repository>
< id> org.springframework.maven.milestone< / id>
< name> Spring Maven里程碑存储库< /名称>
< url> http://repo.springsource.org/libs-milestone-local< / url>
<快照>
<启用> false< / enabled>
< / snapshots>
< / repository>
< repository>
< id> apache.snapshots< / id>
<名称> Apache版本分发库< /名称>
< url>
http://repository.apache.org/snapshots
< / url>
< / repository>
< / repositories>
< dependencyManagement>
<依赖关系>
<! - https://mvnrepository.com/artifact/org.springframework/spring-core - >
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-core< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-context< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-aop< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-webmvc< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-tx< / artifactId>
< version> $ {spring.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> $ {hibernate.version}< / version>
<排除项>
<排除>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-annotations< / artifactId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> $ {hibernate.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.webjars< / groupId>
< artifactId> jquery< / artifactId>
< version> $ {jquery.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.webjars< / groupId>
< artifactId>引导程序< / artifactId>
< version> $ {bootstrap.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework.security< / groupId>
< artifactId> spring-security-taglibs< / artifactId>
< version> $ {spring-security.version}< / version>
< /依赖关系>
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> jstl< / artifactId>
< version> $ {javax-servlet.version}< / version>
< /依赖关系>
< /依赖关系>
< / dependencyManagement>
< / project>
这是hibernate.cfg.xml:
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD // EN
http://hibernate.sourceforge.net/hibernate-configuration-3.0。 DTD>
< hibernate-configuration>
< session-factory>
< property name =connection.driver_class> com.microsoft.sqlserver.jdbc.SQLServerDriver
< / property>
< property name =connection.url> jdbc:sqlserver:// localhost:1433; databaseName = Licenta
< / property>
< property name =connection.username> sa< / property>
< property name =connection.password> admin9< / property>
< property name =default_schema> dbo< / property>
< property name =dialect> org.hibernate.dialect.SQLServerDialect
< / property>
< property name =hbm2ddl.auto>建立< / property>
<! - 禁用二级缓存 - >
< property name =cache.provider_class> org.cache.internal.NoCacheProvider
< / property>
<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> true< / property>
<! - < property name =transaction.factory_class> org.transaction.JDBCTransactionFactory< / property> - >
< property name =current_session_context_class>线程< / property>
< / session-factory>
< / hibernate-configuration>
以及创建会话工厂的类:
@Configuration
@EnableTransactionManagement
@EnableWebSecurity
@EnableGlobalMethodSecurity
@Import({SecurityConfig.class})
public class HibernateUtils {
@Autowired
@Qualifier(sessionFactory)
private static SessionFactory sessionFactory = buildSessionFactory();
$Be
private static SessionFactory buildSessionFactory(){
try {
if(sessionFactory == null){
org.hibernate.cfg.Configuration配置= new org.hibernate.cfg.Configuration()。configure(/ hibernate.cfg.xml);
addAnnotatedClasses(configuration);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()。applySettings(
configuration.getProperties())。build();
sessionFactory = new org.hibernate.cfg.Configuration()。configure()。buildSessionFactory(serviceRegistry);
}
return sessionFactory;
} catch(Throwable ex){
System.err.println(Initial SessionFactory creation failed。+ ex);
抛出新的ExceptionInInitializerError(ex);
$ b @Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
System.out.println(enter transactionManager的);
HibernateTransactionManager txManager = new org.springframework.orm.hibernate5.HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
返回txManager;
$ / code>
我使用Tomcat 8.每次运行它时,我都会收到UnknownUnwrapTypeException:无法解开请求类型[javax.sql.DataSource]
我已经尝试添加c3p0连接池的详细信息,正如我在前面的文章中看到的,我没有错误了,但服务器也没有启动,只是在创建池后挂在那里。
有人可以帮忙吗?
好吧,我已经知道了。看起来像,而不是我当前的sessionFactory创建,我应该有这样的:
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
DataSource datasource = getDataSource();
serviceRegistryBuilder.applySetting(Environment.DATASOURCE,datasource);
serviceRegistryBuilder.applySettings(configuration.getProperties());
属性hibernateProperties = setHibernateProperties();
serviceRegistryBuilder.applySettings(hibernateProperties);
StandardServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
这意味着我将hibernate.cfg.xml中的属性外化为这样:
@Bean
public static DataSource getDataSource(){
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(com.microsoft.sqlserver.jdbc.SQLServerDriver);
dataSource.setUrl(jdbc:sqlserver:// localhost:1433; databaseName = Licenta);
dataSource.setUsername(sa);
dataSource.setPassword(admin9);
返回dataSource;
私有静态属性setHibernateProperties(){
属性properties = new Properties();
properties.put(hibernate.dialect,org.hibernate.dialect.SQLServer2008Dialect);
properties.put(hibernate.hbm2ddl.auto,create);
properties.put(hibernate.show_sql,true);
返回属性;
}
I know that this question has been answered before, but none of the responses seem to help me, since i am not using a datasource in my project.
I a have a Maven project, integrated with Spring and Hibernate, connecting to a SQL Server 2014 database.
This is my pom.xml, where you can see the versions:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BachelorDegree</groupId>
<artifactId>BuildingAdministration</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>BuildingAdministration Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>BuildingAdministration</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.1.RELEASE</spring.version>
<spring-security.version>4.1.1.RELEASE</spring-security.version>
<hibernate.version>5.2.1.Final</hibernate.version>
<hibernate.ga.version>3.5.4-Final</hibernate.ga.version>
<hibernate-annotation.version>3.5.6-Final</hibernate-annotation.version>
<javax-servlet-api.version>3.1.0</javax-servlet-api.version>
<jquery.version>2.1.1</jquery.version>
<bootstrap.version>3.2.0</bootstrap.version>
<javax-servlet.version>1.2</javax-servlet.version>
<log4j.version>2.6.2</log4j.version>
<spring.simple>2.5.6</spring.simple>
<jta.version>1.1</jta.version>
<aspectjweaver.version>1.8.9</aspectjweaver.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<!-- spring-context which provides core functionality -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- The spring-aop module provides an AOP Alliance-compliant aspect-oriented
programming implementation allowing you to define -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<!-- The spring-webmvc module (also known as the Web-Servlet module) contains
Spring’s model-view-controller (MVC) and REST Web Services implementation
for web applications -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
<!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId>
<version>${hibernate-annotation.version}</version> </dependency> -->
<!-- <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version> </dependency> -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId>
<version>${hibernate.ga.version}</version> </dependency> -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>${hibernate.ga.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>${jta.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<exclusions>
<exclusion>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>org.springframework.maven.milestone</id>
<name>Spring Maven Milestone Repository</name>
<url>http://repo.springsource.org/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>apache.snapshots</id>
<name>Apache Release Distribution Repository</name>
<url>
http://repository.apache.org/snapshots
</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>${jquery.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>${bootstrap.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${javax-servlet.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
This is hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=Licenta
</property>
<property name="connection.username">sa</property>
<property name="connection.password">admin9</property>
<property name="default_schema">dbo</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect
</property>
<property name="hbm2ddl.auto">create</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.cache.internal.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- <property name="transaction.factory_class">org.transaction.JDBCTransactionFactory</property> -->
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
And the class where i create the session factory:
@Configuration
@EnableTransactionManagement
@EnableWebSecurity
@EnableGlobalMethodSecurity
@Import({ SecurityConfig.class })
public class HibernateUtils {
@Autowired
@Qualifier("sessionFactory")
private static SessionFactory sessionFactory = buildSessionFactory();
@Bean
private static SessionFactory buildSessionFactory() {
try {
if (sessionFactory == null) {
org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration().configure("/hibernate.cfg.xml");
addAnnotatedClasses(configuration);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = new org.hibernate.cfg.Configuration().configure().buildSessionFactory(serviceRegistry);
}
return sessionFactory;
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
System.out.println("enter transactionManager");
HibernateTransactionManager txManager = new org.springframework.orm.hibernate5.HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
I use Tomcat 8. Whenever i run it, i get UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
I have tried adding the details for the c3p0 connection pool as i have seen in an earlier post and i don't get the error anymore, but the server doesn't start up either, just hangs there after creating the pool.
Can someone, please, help with that?
Well, i have figured it out. Seems like, instead of my current sessionFactory creation, I should have had something like:
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
DataSource datasource = getDataSource();
serviceRegistryBuilder.applySetting(Environment.DATASOURCE, datasource);
serviceRegistryBuilder.applySettings(configuration.getProperties());
Properties hibernateProperties = setHibernateProperties();
serviceRegistryBuilder.applySettings(hibernateProperties);
StandardServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Which means i externalized the properties from hibernate.cfg.xml like that:
@Bean
public static DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl("jdbc:sqlserver://localhost:1433;databaseName=Licenta");
dataSource.setUsername("sa");
dataSource.setPassword("admin9");
return dataSource;
}
private static Properties setHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2008Dialect");
properties.put("hibernate.hbm2ddl.auto", "create");
properties.put("hibernate.show_sql", "true");
return properties;
}
这篇关于UnknownUnwrapTypeException:无法解开请求的类型[javax.sql.DataSource]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!