当前位置: 首页 > 面试题库 >

UnknownUnwrapTypeException:无法解包为请求的类型[javax.sql.DataSource]

松安民
2023-03-14
问题内容

我知道这个问题之前已经回答过,但是由于我没有在项目中使用数据源,因此所有答复似乎都没有帮助。

我有一个与Spring和Hibernate集成的Maven项目,该项目连接到SQL Server 2014数据库

这是我的pom.xml,您可以在其中查看版本:

<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>

这是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>

我创建会话工厂的类:

@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;
    }

我使用Tomcat8。无论何时运行它,都会得到UnknownUnwrapTypeException:无法解包为请求的类型[javax.sql.DataSource]

我已经尝试添加c3p0连接池的详细信息,正如我在较早的帖子中看到的那样,我再也没有收到错误,但是服务器也无法启动,只是在创建该池后挂在那里。

有人可以帮忙吗?


问题答案:

好吧,我知道了。似乎不是我当前的sessionFactory创建,而是应该有以下内容:

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);

这意味着我像这样从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");
    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;        
}


 类似资料:
  • 有没有人能告诉我如何修复这个配置? 谢谢

  • 我想在spring boot和hibernate 5.2.6中使用sessionfactory,在尝试了一些解决方案后,我尝试使用LocalSessionFactoryBuilder,但是我遇到了这个异常。我的数据源被spring boot注入。我不知道为什么 我得到了这个例外

  • 原因:组织。冬眠QueryException:无法从com.dso.model.comptabilite.cegid.etapejudiciairnkfactureacteshonoraires将请求的类型转换为:INT[选择子字符串(referenceComptable,8,14)作为seqNum,子字符串(referenceComptable,4,7)作为yearCegid,子字符串(refe

  • 我想实现使用Spring Cloud的客户端请求。我试过这个: 外国客户: 请求DTO: 控制器: 外型: 但是当我发出POST请求时,我得到了例外: 你知道我怎样才能解决这个问题吗?

  • 问题内容: 这是我得到的错误: 这是我的代码: 另外,我在一个单独的类中声明了这一点: 是什么导致此错误,我该如何解决? 编辑:缩进,空格,命名约定和可读性问题已得到解决。 问题答案: 问题在于,这似乎是一个内部类(很难说,您的缩进是不好的),并且由于它不是静态的,因此您无法单独实例化它。 您要么需要自己创建一个类,要么使其成为一个类并将其实例化为(或者无论父类是什么,您的缩进在这里实际上都没有帮

  • 问题内容: 尝试遵循本教程:教程 尝试像这样使用 JSONParser : 但是Eclipse给了我。 该怎么办? 问题答案: JSONParser类在本教程的下方,它看起来像这样…… 我会考虑看看杰克逊图书馆通过http://jackson.codehaus.org/