java - <jdbc :embedded-database> in context. xml 不起作用。想要启动一个 sql 脚本进行单元测试

标签 java spring hibernate junit

这是我的上下文文件:

<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"> <!--Line 11-->

<tx:annotation-driven transaction-manager="transactionManager"/>



<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <!---->
    <property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:mydb"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="hibernate.cfg.xml.incDTD"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.shutdown">true</prop>
        </props>
    </property>
</bean>

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

<jdbc:embedded-database id="embedded" type="HSQL"/>
<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="classpath:ctl_data-scrubd.sql"/>
</jdbc:initialize-database>

我收到这个错误

Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from class path resource [PersistenceHelper-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 64; <Line 11, Column 64>: XML-24500: (Error) Can not build schema 'http://www.springframework.org/schema/jdbc' located at 'http://www.springframework.org/schema/jdbc/spring-jdbc.xsd'

我想启动一个sql脚本进行测试。我需要从这个 xml 文件中启动它。我已经阅读过使用它不起作用。有谁知道我如何解决这个问题,或者从内部启动 .sql 文件的替代方法。如果您想查看我的更多代码,请告诉我。

编辑:

我在另一个网站上发布了同样的问题,并得到了一些建议。现在的错误是:

at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean   with name 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource []: bin
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean

这也是我的配置文件。我所做的只是添加一个版本并移动一些声明:

<?xml version="1.0" encoding="UTF-8"?>
<!-This is the spring configuration file for test cases.-->
<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

<tx:annotation-driven transaction-manager="transactionManager"/>

其他都是一样的

最佳答案

只是为了完成答案,这是对我有用的上下文(Spring 3.2.1):

<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:jdbc="http://www.springframework.org/schema/jdbc"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                                                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                                                    http://www.springframework.org/schema/jdbc
                                                    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
    <jdbc:embedded-database id="dataSource" type="HSQL">
        <jdbc:script location="classpath:schema.sql" />
        <jdbc:script location="classpath:data.sql" />
    </jdbc:embedded-database>
</beans>

关于java - <jdbc :embedded-database> in context. xml 不起作用。想要启动一个 sql 脚本进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11144369/

相关文章:

java - JDBC Appender 不写入异常堆栈跟踪

spring - 如何记录 Spring 加载的属性?

java - 我如何告诉我的数据库某些更改是由 hibernate 而不是其他应用程序进行的?

java - Postgis 点数据类型未持久化到数据库中

java - 如何配置 slf4j 使其能够写入控制台和文件?

java - timeBasedFileNamingAndTriggeringPolicy 中的 MaxFileSize 不起作用?

java - Java 中的动态数组 - 我有哪些选择?

Spring @Scheduled execute方法在VPS中执行三次

java - 无法导入 org.hibernate.session

java - @Transactional(readOnly = false,propagation = Propagation.REQUIRED)抛出异常