好的,抱歉,我花了几个小时寻找这个问题的答案,但是我花了我输入整个问题的 StackOverflow 来冒泡我正在寻找的链接。你可以在这里阅读很多相关信息。
我有一个使用 Spring Roo 创建的 Spring 项目来使用 Hibernate 和 MySQL。但是,为了进行测试,我想在内存中使用 HSQLDB,因为 Roo 集成测试会删除 ID(主键)0 到 10 的数据(而不是使用为他们已经创建的数据分配的数据库 ID 删除数据),这意味着它删除数据库中已经存在的数据,在我的情况下,这会导致在回滚事务之前违反约束。
这有点额外困难,因为我正在切换整个数据库提供程序,这意味着不同的 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'
为什么mvn test
还在捡非测试资源?
所以然后我重命名并复制src/test/resources/META-INF/spring
到它。我将测试类中的上下文配置更改为spring-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"/>
至
<context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/>
更改了持久性单元的名称:
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
至
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="testPersistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
我对持久性单元名称进行了相应的更改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];
为什么 Spring/Hibernate 在此配置中丢失了实体映射?
所以接下来我尝试合并这两个persistence.xml
文件,以便一个文件src/main/resources/META-INF
包含两个持久性单元。
这样可行!!??
我认为这很难看,因为现在我的生产代码中有测试配置,但这就是我正在接受的。
有什么更好的方法?
据我了解,persistence.xml 中的属性不像 Spring XML 文件中那样可用。所以我认为我不能只用一个特定于测试的属性文件来做我想做的事。
理想情况下,我会使用 src/main/resources 下的所有配置运行测试,除了 src/test/resources 中特别覆盖的配置。 有没有办法做到这一点?
感谢您提供的任何见解!