弹簧套房源spring3 + Hibernate4 +行家3 + 5的MySQL(Spring so

2019-07-29 11:57发布

我得到我的应用程序错误,但我不知道这意味着什么。 我张贴一些示例代码波纹管。 这是我的错误:

> SEVERE: Exception sending context initialized event to listener
> instance of class
> org.springframework.web.context.ContextLoaderListener
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'transactionManager' defined in ServletContext
> resource [/WEB-INF/spring/root-context.xml]: 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:1455)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
>   at
> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
>   at
> org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
>   at
> org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
>   at
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
>   at
> org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
>   at
> org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
>   at
> org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
>   at
> org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
>   at
> org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
>   at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>   at
> org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
>   at
> org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
>   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
>   at java.util.concurrent.FutureTask.run(FutureTask.java:166)     at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
>   at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
>   at java.lang.Thread.run(Thread.java:722) Caused by:
> org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to
> requested type [javax.sql.DataSource]     at
> org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.unwrap(DriverManagerConnectionProviderImpl.java:91)
>   at
> org.springframework.orm.hibernate4.SessionFactoryUtils.getDataSource(SessionFactoryUtils.java:101)
>   at
> org.springframework.orm.hibernate4.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:264)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
>   ... 22 more
> 
> ERROR: org.springframework.web.context.ContextLoader - Context
> initialization failed
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'transactionManager' defined in ServletContext
> resource [/WEB-INF/spring/root-context.xml]: 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:1455)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
>   at
> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
>   at
> org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
>   at
> org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
>   at
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
>   at
> org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
>   at
> org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
>   at
> org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
>   at
> org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
>   at
> org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
>   at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>   at
> org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
>   at
> org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
>   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
>   at java.util.concurrent.FutureTask.run(FutureTask.java:166)     at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
>   at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
>   at java.lang.Thread.run(Thread.java:722) Caused by:
> org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to
> requested type [javax.sql.DataSource]     at
> org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.unwrap(DriverManagerConnectionProviderImpl.java:91)
>   at
> org.springframework.orm.hibernate4.SessionFactoryUtils.getDataSource(SessionFactoryUtils.java:101)
>   at
> org.springframework.orm.hibernate4.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:264)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
>   ... 22 more

这是我的POM文件:

<modelVersion>4.0.0</modelVersion>
<groupId>com.reservosity</groupId>
<artifactId>component</artifactId>
<name>Reservosity</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.1.0.RELEASE</org.springframework-version>
    <org.aspectj-version>1.6.9</org.aspectj-version>
    <org.slf4j-version>1.5.10</org.slf4j-version>
</properties>
<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.20</version>
    </dependency>
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.15.0-GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <additionalProjectnatures>
                    <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                </additionalProjectnatures>
                <additionalBuildcommands>
                    <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                </additionalBuildcommands>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>org.test.int1.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

该SERVET上下文:

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.reservosity.component.controller" />

根context.xml中:

<!-- Root Context: defines shared resources visible to all other web components -->


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

在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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/******</property>
    <property name="hibernate.connection.username">***</property>
    <property name="hibernate.connection.password">***</property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <mapping class="com.company.component.domain.Company" />
</session-factory>

如果我删除豆transactionManager的,它工作正常,但是当我再次添加它,我得到那个脏兮兮的错误。 不知道为什么。

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

我是新来春休眠,可能真的有一些帮助呢!

谢谢

安德鲁

Answer 1:

我有完全相同的问题。 KyelJmD表明这里是有缺失的连接池,似乎他是对的,但他并没有发布解决方案。

所以我花了一些时间和finaly搞不定。 为了解决这个问题,你应该做两两件事:

  1. 添加这些行到hibernate.cfg.xml文件(它可能是你并不需要全部的话):

     <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.timeout">300</property> <property name="hibernate.c3p0.max_statements">50</property> <property name="hibernate.c3p0.idle_test_period">3000</property> 
  2. 在pom.xml中添加这种依赖关系:

     <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.1.9.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.1.9.Final</version> </dependency> 

如果没有行家依赖Hibernate的只是默默地忽略C3P0配置。

通过上面的配置我dont't得到了UnknownUnwrapTypeException例外,似乎现在工作得很好。

我的设置:春天3.2.1,4.1.9的Hibernate,Tomcat的7.0.35。

Dislaimer:在陈述这个问题,你应该宁可不使用hibernate.cfg.xml文件。 使用dataSource豆代替,因为,例如,您可以在以后使用它的JdbcTemplate。

编辑:

它已经有一段时间,我回答了这个问题。 这似乎是正确的,但我从来不喜欢它,因为它是一种猜测,我们真的应该避免。

所以,因为我发现这个interresting和我无法找到Hibernate的4.1文档什么的,我想证明我和其他评论者“猜测”。

这里是一步一步的过程中,也可能在其他图书馆解决其他问题的怪异有用。 原来,这一次是真的很容易,到底是怎么回事。

  1. 创建一个Maven项目(或使用现有的),加Hibernate来扶养(休眠-EntityManager的,我这次使用4.1.7版本)。 将其导入到IDE可以自动连接的源代码(I使用的Intellij)
  2. 从堆栈跟踪,发现其中的异常(类UnknownUnwrapTypeException )已被抛出( DriverManagerConnectionProviderImpl )和导航那里(CTRL-N和类型的类名),点击“下载源”。
  3. 查找的用法DriverManagerConnectionProviderImpl (ALT + F7)
  4. 瞧! 原来存在的只有一个使用DriverManagerConnectionProviderImpl -这是一个initiateService(...)的方法ConnectionProviderInitiator ,其中-或多或少-试图一对夫妇ConnectionProvider实现中,回退之前DriverManagerConnectionProviderImpl ,这在我们的情况下不工作,抛出UnknownUnwrapTypeException

从Hibernate的ConnectionProviderInitiator.java:

        ConnectionProvider connectionProvider = null;
    String providerClassName = getConfiguredConnectionProviderName( configurationValues );
    if ( providerClassName != null ) {
        connectionProvider = instantiateExplicitConnectionProvider( providerClassName, classLoaderService );
    }
    else if ( configurationValues.get( Environment.DATASOURCE ) != null ) {
        connectionProvider = new DatasourceConnectionProviderImpl();
    }

    if ( connectionProvider == null ) {
        if ( c3p0ConfigDefined( configurationValues ) && c3p0ProviderPresent( classLoaderService ) ) {
            connectionProvider = instantiateExplicitConnectionProvider( C3P0_PROVIDER_CLASS_NAME,
                    classLoaderService
            );
        }
    }

    if ( connectionProvider == null ) {
        if ( proxoolConfigDefined( configurationValues ) && proxoolProviderPresent( classLoaderService ) ) {
            connectionProvider = instantiateExplicitConnectionProvider( PROXOOL_PROVIDER_CLASS_NAME,
                    classLoaderService
            );
        }
    }

    if ( connectionProvider == null ) {
        if ( configurationValues.get( Environment.URL ) != null ) {
            connectionProvider = new DriverManagerConnectionProviderImpl();
        }
    }

    if ( connectionProvider == null ) {
        LOG.noAppropriateConnectionProvider();
        connectionProvider = new UserSuppliedConnectionProviderImpl();
    }

代码大致表明:

  1. 如果Environment.DATASOURCE (hibernate.connection.datasource)被设置,则DatasourceConnectionProviderImpl被使用;
  2. 如果存在C3P0,然后C3P0提供实例化
  3. 其他一些供应商被选中(PROXOOL_PROVIDER_CLASS_NAME)
  4. 然后,如果Environment.URL (hibernate.connection.url)时,我们使用DriverManagerConnectionProviderImpl

仅此而已。 这就解释了为什么如果我们增加C3P0作为Maven的依赖或定义数据源(如迈克尔·阿古Tecourt在下面的评论说的)解决了这个问题。

另一个问题是,为什么DriverManagerConnectionProviderImpl抛出UnknownUnwrapTypeException - wheather这是一个错误或不 - 反正我看不出在Hibernate中4.2.0,甚至4.3.0.Beta的代码进行任何更改。



Answer 2:

只是看着它从这个代码来的问题:

if ( ConnectionProvider.class.equals( unwrapType ) ||
                DriverManagerConnectionProviderImpl.class.isAssignableFrom( unwrapType ) ) {
            return (T) this;
        }
        else {
            throw new UnknownUnwrapTypeException( unwrapType );
        }

其中unwrapType是javax.sql.DataSource

虽然我无法解释它到底是什么关于你的配置导致此我可以建议修复。

定义一个DriverManagerDataSource在您的Spring应用程序上下文bean,然后提供这作为财产的LocalSessionFactoryBean 。 请确保您从Hibernate配置文件中删除数据源配置。



Answer 3:

尝试包括spring和hibernate库lib文件夹到您的项目。 我希望这些帮助。



Answer 4:

本文认为,你没有在Hibernate的配置文件中定义的数据源。



Answer 5:

这篇文章有详细的春天3.1指令和Hibernate 4.1集成:

http://blog.springsource.org/2012/04/06/migrating-to-spring-3-1-and-hibernate-4-1/



文章来源: Spring source suite spring3 + Hibernate4 + maven 3 + MySQL 5