使用单独的persistence.xml文件进行生产并使用Spring 3.1进行测试

Old*_*Pro 6 testing spring persistence.xml

好的,对不起,我几个小时都在寻找答案,但是我为StackOverflow输入了整个问题来冒泡我正在寻找的链接.您可以在此处阅读大量相关信息.


我有一个用Spring Roo创建的Spring项目来使用Hibernate和MySQL.但是,为了进行测试,我想在内存中使用HSQLDB,因为Roo集成测试使用ID(主键)0到10删除数据(而不是使用数据库为他们已创建的数据分配的数据删除数据),这意味着它删除已存在于数据库中的数据,在我的情况下会导致约束违规,然后才能回滚事务.

这有点困难,因为我正在切换整个数据库提供程序,这意味着不同的Hibernate方言以及不同的DDL设置(在生产中验证,在测试中创建 - 丢弃).但它并没有像我期望的那样工作,我为什么难以理解.

如果您知道它为什么不起作用,请说明,即使您没有解决方案.

这是一个Roo项目,我当然正在使用Maven.所以我尝试的第一件事就是拥有一个特定于测试的src/test/resources/META-INF/persistence.xml文件,同样也是一个特定于测试的src/test/resources/META-INF/spring/database.properties文件.这不起作用,因为当我运行mvn test所有内容时,相关消息正在运行

Conflicting persistence unit definitions for name 'persistenceUnit'
Run Code Online (Sandbox Code Playgroud)

为什么mvn test仍然拿起非测试资源?

于是我改名src/test/resources/META-INF/springspring-test并复制applicationContext.xml到它.我将测试类中的上下文配置更改为

@ContextConfiguration(locations = "classpath:/META-INF/spring-test/applicationContext*.xml")

完成(或者我认为)分离,我做了几处编辑spring-test/applicationContext.xml:

更改了属性文件的路径:

<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
Run Code Online (Sandbox Code Playgroud)

<context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/>
Run Code Online (Sandbox Code Playgroud)

更改了持久性单元的名称:

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="testPersistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

并且我在持久性单元名称中进行了相应的更改 src/test/resources/META-INF/persistence.xml

好吧,好吧,现在没有冲突,但Hibernate失去了实体映射(例如Product实体),我得到:

org.springframework.dao.InvalidDataAccessApiUsageException:
org.hibernate.hql.ast.QuerySyntaxException: Product is not mapped [SELECT o FROM Product o]; 
Run Code Online (Sandbox Code Playgroud)

为什么Spring/Hibernate在此配置中丢失了实体映射?

所以接下来我尝试合并这两个persistence.xml文件,以便下面的一个文件src/main/resources/META-INF包含两个持久性单元.

这样可行!!??

我认为这很丑陋,因为现在我的生产代码中有测试配置,但这就是我所接受的.

什么是更好的方法?

据我所知,persistence.xml中的属性不像它们在Spring XML文件中那样可用.所以我认为只用一个特定于测试的属性文件就可以做我想做的事情.

理想情况下,我使用src/main/resources下的所有配置运行测试,除了在src/test/resources中专门覆盖的内容. 有没有办法实现这个目标?

感谢您提供的任何见解!

Mik*_*Lue 1

在我的工作中,我曾经在没有数据库连接信息的情况下配置persistence.xml。数据库连接由Spring的上下文配置定义。在Spring框架中,有几种方法可以改变对象的属性:

  1. PropertyPlaceholderConfigurer - 使用不同的属性文件来覆盖数据库连接或 ORM 方言的值。如果您使用Maven ,则可以使用资源过滤器生成单个属性文件的不同值
  2. Bean 定义继承- 使用另一个上下文配置来“覆盖”默认配置。

以下代码是我的应用程序上下文的摘录:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="classpath:frontend.properties" p:ignore-resource-not-found="true"
    p:systemPropertiesModeName="SYSTEM_PROPERTIES_MODE_OVERRIDE"
/>

<util:properties id="jpaProperties">
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
</util:properties>

<!-- Global entity manager factory(may not be overrided) -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="dataSource" p:jpaProperties-ref="jpaProperties"
>
    <property name="JpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>
<!-- :~) -->

<!-- Data Source -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"
    p:driverClass="${database.driverClass}" p:jdbcUrl="${database.url}"
    p:username="${database.username}" p:password="${database.password}"
    p:partitionCount="${database_conn.pooling.partition_count:2}"
    p:maxConnectionsPerPartition="64" p:minConnectionsPerPartition="${database_conn.pooling.min_connections:4}"
    p:acquireIncrement="4" p:statementsCacheSize="64"
    p:connectionTimeoutInMs="1800000" p:IdleConnectionTestPeriodInMinutes="420"
/>
<!-- :~) -->
Run Code Online (Sandbox Code Playgroud)

这是测试的配置:

<!-- import configuration of application -->
<import resource="classpath:database.xml" />

<!--
  - I override some of settings of connection pooling while testing
  -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"
    p:driverClass="${database.driverClass}" p:jdbcUrl="${database.url}"
    p:username="${database.username}" p:password="${database.password}"
    p:maxConnectionsPerPartition="8" p:minConnectionsPerPartition="2"
    p:acquireIncrement="2" p:statementsCacheSize="32"
/>
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,我在Maven Surefire中设置系统属性来配置不同的数据库。如下示例:

<!-- import configuration of application -->
<import resource="classpath:database.xml" />

<!--
  - I override some of settings of connection pooling while testing
  -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"
    p:driverClass="${database.driverClass}" p:jdbcUrl="${database.url}"
    p:username="${database.username}" p:password="${database.password}"
    p:maxConnectionsPerPartition="8" p:minConnectionsPerPartition="2"
    p:acquireIncrement="2" p:statementsCacheSize="32"
/>
Run Code Online (Sandbox Code Playgroud)