16

persistence.xml为了测试,我有两个文件:

  • src/main/resources/META-INF/persistence.xml
  • src/test/resources/META-INF/persistence.xml

如何指示Maven在测试期间忽略第一个文件?现在它没有被忽略,因为 OpenEJB 说:

ERROR - FAIL ... Finder: @PersistenceContext unitName has multiple matches: 
unitName "abc" has 2 possible matches.
4

4 回答 4

11

查看针对您正在尝试做的事情的备用描述符功能。

试试这个设置:

  • src/main/resources/META-INF/persistence.xml
  • src/main/resources/META-INF/test.persistence.xml

然后,您可以通过将System 或 InitialContext 属性设置为来构造 OpenEJB 以首选test.persistence.xml文件openejb.altdd.prefixtest

另一种可能的解决方案是覆盖测试中的持久性单元属性。使用这种方法,您可以避免需要一秒钟persistence.xml,这可能很好,因为维持两个可能会很痛苦。

您可以使用 Maven 方法,但请注意,根据规范,持久性提供程序只会@Entity在找到的确切 jar 或目录中查找(也称为扫描)bean persistence.xml。所以要敏锐地意识到在 Maven 中这是两个不同的位置:

  • target/classes
  • target/test-classes

编辑有关覆盖功能的更多详细信息

您可以通过系统属性或初始上下文属性(包括 jndi.properties 文件)覆盖测试设置中的任何属性。格式为:

<unit-name>.<property>=<value>

因此,例如以下内容persistence.xml

<persistence>
  <persistence-unit name="movie-unit">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>movieDatabase</jta-data-source>
    <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      <property name="hibernate.max_fetch_depth" value="3"/>
    </properties>
  </persistence-unit>
</persistence>

您可以在测试用例中覆盖添加持久性单元属性。目前没有删除它们的设施(如果您需要,请告诉我们——到目前为止还没有真正出现)。

Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.LocalInitialContextFactory");

p.put("movie-unit.hibernate.hbm2ddl.auto", "update");
p.put("movie-unit.hibernate.dialect", "org.hibernate.dialect.HSQLDialect");

context = new InitialContext(p);

或者通过jndi.properties文件

java.naming.factory.initial=org.apache.openejb.client.LocalInitialContextFactory
movie-unit.hibernate.hbm2ddl.auto = update
movie-unit.hibernate.dialect = org.hibernate.dialect.HSQLDialect
于 2010-10-11T17:25:06.840 回答
1

我认为您可以在 pom.xml 中创建两个配置文件:

<properties>
  <environment>dev</environment>
</properties>
<profiles>
  <profile>
    <id>prod</id>
    <properties>
      <environment>test</environment>
    </properties>
  </profile>
</profiles>

之后,在您的 src 文件夹中,创建两个名为 dev/resoruces 和 test/resources 的文件夹,并将您的不同资源复制到那里。之后,添加如下内容:

<resources>
  <resource>
    <directory>${basedir}/src/main/resources</directory>
    <filtering>false</filtering>
  </resource>
  <resource>
    <directory>${basedir}/src/main/${environment}/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>

${basedir} 取决于命令行参数,它可以是 test 或 dev。您像这样运行 maven 命令:mvn clean package -P test

于 2010-10-05T14:24:58.267 回答
0

我一直在测试这些和其他类似的解决方案而不涉及 pom.xml ......在我看来,解决这个问题的最佳方法是拥有两个 application-context.xml (一个仅用于测试类)并在测试的 application-context.xml 中添加一个自定义持久性单元管理器 bean。像这个例子:

<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
  <property name="persistenceXmlLocation">
     <value>classpath*:META-INF/test.persistence.xml</value>
  </property>
  <property name="defaultDataSource" ref="dataSource"/>
</bean>

此解决方案运行。:)

于 2014-04-16T10:46:09.073 回答
-2

更好地添加这两个文件 - 通常,在构建中区分测试/生产或调试/配置文件/生产只会带来麻烦。最好尝试为生产(比如 abc-production)和测试(abc-tests)使用不同的持久性单元名称。

于 2010-10-12T11:16:12.090 回答