【发布时间】:2014-06-25 17:08:07
【问题描述】:
在 Spring 中编写一些集成测试(例如在 this question 中)我希望避免使用大脚本在 HSQL 中创建所有表:
<jdbc:script location="classpath:createSchema.sql" />
当 Spring 自动从模型创建和更新我的开发表时。 如何让 Spring 为我的集成测试做同样的事情?
我的数据源文件是
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns= "http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "http://www.springframework.org/schema/context"
xmlns:jee= "http://www.springframework.org/schema/jee"
xmlns:p= "http://www.springframework.org/schema/p"
xmlns:jdbc= "http://www.springframework.org/schema/jdbc"
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/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<!-- Usamos HSQLDB para hacer las pruebas con in-memory db -->
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:create_schema.sql"/>
<jdbc:script location="classpath:import.sql"/>
</jdbc:embedded-database>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.myProject.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.default_schema">TESTING</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
更新:在 NimChimpsky 回答之后,我将架构名称包含在休眠属性中并创建了一个脚本,其中仅包含:
CREATE SCHEMA TESTING
在尝试第一次插入到 import.sql 脚本后,我看到: org.hsqldb.hsqlexception 用户缺少权限或找不到对象[MyTable]
【问题讨论】:
-
为什么不删除
create_schema.sql并让 Hibernate 生成表(使用您已经拥有的 create-drop)? -
谢谢。您的评论很有帮助
标签: spring hibernate hsqldb integration-testing