查看alternate descriptors 功能,该功能针对您正在尝试做的事情。
试试这个设置:
src/main/resources/META-INF/persistence.xml
src/main/resources/META-INF/test.persistence.xml
然后您可以通过将openejb.altdd.prefix System 或 InitialContext 属性设置为test 来构造OpenEJB 以首选test.persistence.xml 文件
另一个可能的解决方案是override the persistence unit properties in the test。使用这种方法,您可以避免需要第二个persistence.xml,这很好,因为维护两个可能会很痛苦。
您可以使用 Maven 方法,但请注意,根据规范,持久性提供程序只会在找到 persistence.xml 的确切 jar 或目录中查找(也称为扫描)@Entity bean。所以要敏锐地意识到在 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