1

这是我使用 spring 和嵌入式数据库 H2 进行集成测试的设置

<?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:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <jdbc:embedded-database id="dataSource" type="H2" />

    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="classpath:sql/globalParams.sql"/>
        <jdbc:script location="classpath:sql/customersGroupView.sql"/>
        <jdbc:script location="classpath:sql/recentIntegrationsTableAndTrigger.sql"/>
        <jdbc:script location="classpath:sql/insertIntegrationDate.sql"/>
        <jdbc:script location="classpath:sql/toCharRoutine.sql"/>
    </jdbc:initialize-database>
</beans>

集成测试的抽象父级

@ContextConfiguration(locations = [
    "classpath:com/dhl/dcc/dcc-core.xml",
    "classpath:com/dhl/dcc/test-security.xml",
    "classpath:com/dhl/dcc/dcc-audit.xml",
    "classpath:com/dhl/dcc/test-dataSource.xml",
    "classpath:com/dhl/dcc/test-beans.xml",
    "classpath:com/dhl/dcc/dcc-forms.xml"
])
public abstract class AbstractIntegrationTestCase extends AbstractTransactionalJUnit4SpringContextTests {

并在实体管理器工厂的核心配置中

<property name="generateDdl" value="${dcc.orm.generateDdl:false}"/>

属性 dcc.orm.generateDdl 在属性中设置为 true。

它运行良好(数据库模式是从注释@Entity 的类生成的),但现在我将域模型分离到它自己的项目中,并将这个项目作为依赖项添加到 Maven 中。之后,由于缺少数据库架构,我的集成测试开始失败。我如何配置嵌入式数据库应该在哪里寻找域模型?谢谢。

编辑:实体工厂配置

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="persistenceUnitName" value="DCC"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="${dcc.orm.generateDdl:false}"/>
                <property name="showSql" value="${dcc.orm.showSql:false}"/>
                <property name="databasePlatform" value="${dcc.orm.dialect}"/>
            </bean>
        </property>
    </bean>
4

1 回答 1

1

您是否尝试过在persistence.xml中指定 jar 文件?见https://stackoverflow.com/a/1780362/204950

或者尝试将exclude-unlisted-classes设置为 false,请参阅https://stackoverflow.com/a/1780437/204950

于 2013-08-13T11:07:27.390 回答