spring 整合hibernate出错

<?xml version ="1.0" encoding ="utf-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/spring-tx.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/aop " >

<bean id ="testService" class ="com.hsp.test.TestService">
    <property name ="name" value="test"/>
</bean>

<!--  配置数据源-->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>    
        <property name="url" value="jdbc:mysql://localhost:3306/mynews"/>
        <property name="username" value="root"/>
        <property name="password" value=" "/>

         <!--   连接池启动时的初始值 
        <property name="initialSize" value="3"/>
            连接池的最大值
        <property name="maxActive" value="500"/>
             最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止
        <property name="maxIdle" value="2"/>
             最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请
        <property name="minIdle" value="1"/> -->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource"  ref  ="dataSource"/>  
    <property name="mappingResources">
        <list>
          <value>com/hsp/domain/Employee.hbm.xml </value>
        </list>
    </property> 
    <property name="hibernateProperties">  
        <props>      
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>           
            <prop key="hibernate.hbm2ddl.auto">update</prop> 
            <prop key="hibernate.show_sql">false</prop> 
            <!--  <prop key="javax.persistence.validation.mode">none</prop> -->
        </props>  
    </property>  
</bean>  


提示:
16:06:37,972 INFO [main] ClassPathXmlApplicationContext:456 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Mon Aug 10 16:06:37 CST 2015]; root of context hierarchy
16:06:38,036 INFO [main] XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [applicationContext.xml]
16:06:38,174 INFO [main] DefaultListableBeanFactory:555 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5b275dab: defining beans [testService,dataSource,sessionFactory]; root of factory hierarchy
16:06:38,363 INFO [main] Version:66 - HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
16:06:38,368 INFO [main] Version:54 - HHH000412: Hibernate Core {4.3.6.Final}
16:06:38,370 INFO [main] Environment:224 - HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
16:06:38,371 INFO [main] Environment:346 - HHH000021: Bytecode provider name : javassist
16:06:38,407 WARN [main] DTDEntityResolver:75 - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
16:06:38,527 INFO [main] Dialect:145 - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
16:06:38,534 INFO [main] LobCreatorBuilder:88 - HHH000422: Disabling contextual LOB creation as connection was null
16:06:38,598 INFO [main] TransactionFactoryInitiator:62 - HHH000399: Using default transaction strategy (direct JDBC transactions)
16:06:38,602 INFO [main] ASTQueryTranslatorFactory:47 - HHH000397: Using ASTQueryTranslatorFactory
16:06:38,942 INFO [main] SchemaUpdate:182 - HHH000228: Running hbm2ddl schema update
16:06:38,942 INFO [main] SchemaUpdate:193 - HHH000102: Fetching database metadata
16:06:38,943 ERROR [main] SchemaUpdate:247 - HHH000299: Could not complete schema update
java.lang.UnsupportedOperationException: Not supported by BasicDataSource
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1062)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:522)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:453)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:438)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
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:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.hsp.test.Test.main(Test.java:19)

1个回答

你是在做测试时候出的问题吧

你的配置文件有问题

BasicDataSource连接数据库报错

这种错误找 控制台信息

16:06:38,943 ERROR [main] SchemaUpdate:247 - HHH000299: Could not complete schema update
java.lang.UnsupportedOperationException: Not supported by BasicDataSource

1 需要这三个jar文件commons-pool.jar、commons-dbcp-1.2.2.jar和commons-collections-3.2.jar

2 hibernate.cfg.xml 和 applicationContext-common.xml 中,不能都对 DataSource 即连接数据库进行了配置。解决办法是去掉 hibernate.cfg.xml 数据源的配置,改在 applicationContext-common.xml 中配

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