我尝试了作者和@thehpi 的建议。两者都是可信的,但是当我尝试它们时遇到了一些问题。我的设置如下,maven 项目,使用集成测试maven-failsafe-plugin
,使用 JPA 和用于告诉容器如何连接到数据库的 persistence.xml 文件。
长话短说,下面详细介绍的是如何使用 JVM 属性来告诉你的真实代码简单地使用不同的persistence-unit
. 其他解决方案让我在使用 Hibernate 4.1 和 Jetty 7 时遇到了麻烦(查找数据源的问题)。
唯一的缺点是你最终会在项目的发布版本中得到一个无用的配置。辅助持久性单元永远不会在 Maven 集成测试之外使用。我确信有一种方法可以将它分开,但对我来说,我可以接受这种方法。我什至将 hsqldb.jar 从构建中拉出,并使其成为仅用于插件执行的依赖项。
来自 POM 的相关信息
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1</version>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.2.0.v20101020</version>
<dependencies>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>
</dependencies>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopPort>8005</stopPort>
<stopKey>STOP</stopKey>
<contextPath>/</contextPath>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
<systemProperties>
<systemProperty>
<name>RUNNING_TESTS</name>
<value>true</value>
</systemProperty>
</systemProperties>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
请注意名为“RUNNING_TESTS”的系统属性。
HibernateUtil.java
public class HibernateUtil {
private static final EntityManagerFactory emfInstance;
static {
String istest = System.getProperty("RUNNING_TESTS");
System.out.println("RUNNING_TESTS: " + istest);
if (istest != null && istest.equalsIgnoreCase("true")) {
emfInstance = Persistence.createEntityManagerFactory("integration-tests");
}
else {
emfInstance = Persistence.createEntityManagerFactory("productionname");
}
}
public static EntityManagerFactory getInstance() {
return emfInstance;
}
private HibernateUtil() {
}
}
persistence.xml
(在 /src/main/resources/META-INF 中)
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<!-- persistence.xml -->
<persistence-unit name="productionname">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:comp/env/jdbc/selectivemailpush</non-jta-data-source>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
</properties>
</persistence-unit>
<persistence-unit name="integration-tests">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>...</class>
<class>...</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:integration-tests" />
<property name="hibernate.showSql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>