spring hibernate整合问题

启动的时候遇到下面这个问题,查了很多资料也没有找出原因
严重: 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 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1488)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5003)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:261)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:225)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:863)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:782)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1485)
... 21 more
Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider]
at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:197)
at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:120)
at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:55)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:105)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:251)
... 36 more
Caused by: java.lang.ClassCastException: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider cannot be cast to org.hibernate.engine.jdbc.connections.spi.ConnectionProvider
at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:194)
... 40 more

6个回答

以下是我的application.xml:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
    <!-- 指定连接数据库的JDBC驱动 -->  
    <property name="driverClass">  
        <value>com.mysql.jdbc.Driver</value>  
    </property>  
    <!-- 连接数据库所用的URL -->  
    <property name="jdbcUrl">  
        <value>jdbc:mysql://localhost:3306/hospital?useUnicode=true&amp;characterEncoding=UTF-8</value>  
    </property>  
    <!-- 连接数据库的用户名 -->  
    <property name="user">  
        <value>root</value>  
    </property>  
    <!-- 连接数据库的密码 -->  
    <property name="password">  
        <value>123456</value>  
    </property>  
    <!-- 设置数据库连接池的最大连接数 -->  
    <property name="maxPoolSize">  
        <value>20</value>  
    </property>  
    <!-- 设置数据库连接池的最小连接数 -->  
    <property name="minPoolSize">  
        <value>2</value>  
    </property>  
    <!-- 设置数据库连接池的初始化连接数 -->  
    <property name="initialPoolSize">  
        <value>2</value>  
    </property>  
    <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->  
    <property name="maxIdleTime">  
        <value>20</value>  
    </property>  
</bean>    
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <!-- 注入连接池 -->
    <property name="dataSource" ref="dataSource"></property>
    <!-- 配置Hibernate的属性 -->
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
        </props>
    </property>
    <!-- 加载Hibernate中的映射文件 -->
    <property name="mappingResources">
        <list>
            <value>model/Admin.hbm.xml</value>
            <value>model/Doctor.hbm.xml</value>
            <value>model/Patient.hbm.xml</value>
            <value>model/Sickroom.hbm.xml</value>
        </list>
    </property>
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="admin" class="model.Admin">  </bean>
<bean id="doctor" class="model.Doctor">  </bean>
<bean id="patient" class="model.Patient">  </bean>
<bean id="Sickroom" class="model.Sickroom">  </bean>
<bean id="admindao" class="dao.AdminDAO">  
    <property name="sessionFactory" ref="sessionFactory" />  
</bean> 
<bean id="doctordao" class="dao.DoctorDAO">  
    <property name="sessionFactory" ref="sessionFactory" />  
</bean> 
<bean id="patientdao" class="dao.PatientDAO">  
    <property name="sessionFactory" ref="sessionFactory" />  
</bean>
<bean id="sickroomdao" class="dao.SickroomDAO">  
    <property name="sessionFactory" ref="sessionFactory" />  
</bean>
<bean id="adminservice" class="service.AdminService">  
    <property name="admindao" ref="admindao" />  
</bean>
<bean id="doctorservice" class="service.DoctorService">  
    <property name="doctordao" ref="doctordao" />  
</bean>  
<bean id="patientservice" class="service.PatientService">  
    <property name="patientdao" ref="patientdao" />  
</bean>  
<bean id="sickroomservice" class="service.SickroomService">  
    <property name="sickroomdao" ref="sickroomdao" />  
</bean> 
<bean id="action.AdminLoginAction" class="action.AdminLoginAction" scope="prototype">  
    <property name="adminservice" ref="adminservice" />  
    <property name="admin" ref="admin" />
</bean>  
<bean id="action.DoctorLoginAction" class="action.DoctorLoginAction" scope="prototype">  
    <property name="doctorservice" ref="doctorservice" />  
    <property name="doctor" ref="doctor" />
</bean> 
<bean id="action.PatientLoginAction" class="action.PatientLoginAction" scope="prototype">  
    <property name="patientservice" ref="patientservice" />  
    <property name="patient" ref="patient" />
</bean>   

然后我引用的包是:
![图片说明](https://img-ask.csdn.net/upload/201706/05/1496634557_837076.png)图片说明

还有图片说明

去网上找找 spring和hibernate的整合
你写的啥东西 不知道啥意思 不需要那么多

hibernate版本要用对,分3-4-5,用那个就写哪个,还有有些包是不用的,别一股脑的全copy进去

整个的时候相同的包有版本冲突,给去掉低版本的

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!