Hibernate envers 不能在 Arquillian 中工作
Hibernate envers not working in Arquillian
我正在尝试执行一些关于休眠实体版本控制系统的 Arquillian 测试。
无论我做什么,都不会创建审计表 (_AUD),并且 envers 通常无法正常工作。
我搜索了其他帖子并确保我在 hibernate-core 和 envers 上遵循相同的版本。
这是我的pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>sandbox</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>sandbox</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.jee.api>7.0</version.jee.api>
<version.ejb>3.2</version.ejb>
<java.version>1.8</java.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<!-- other plugin versions -->
<version.compiler.plugin>3.1</version.compiler.plugin>
<version.ear.plugin>2.10</version.ear.plugin>
<version.ejb.plugin>2.3</version.ejb.plugin>
<version.surefire.plugin>2.16</version.surefire.plugin>
<version.hibernate5-ddl-maven-plugin>2.0.4</version.hibernate5-ddl-maven-plugin>
<!-- Other libs -->
<version.deltaspike>1.8.0</version.deltaspike>
<modelMapper.version>1.1.0</modelMapper.version>
<!-- Hibernate -->
<hibernate.core.version>5.2.11.Final</hibernate.core.version>
<hibernate.modelgen.version>5.2.11.Final</hibernate.modelgen.version>
<hibernate.annotations.version>3.5.6-Final</hibernate.annotations.version>
<hibernate.javax.persistence.version>1.0.0.Final</hibernate.javax.persistence.version>
<hibernate.validator.version>6.0.2.Final</hibernate.validator.version>
<hibernate.jpa.api.version>1.0.0.Final</hibernate.jpa.api.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>${hibernate.javax.persistence.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.core.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate.core.version}</version>
</dependency>
<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
<version>1.1.11.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<scope>test</scope>
<version>1.1.11.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.narayana.jta</groupId>
<artifactId>narayana-jta</artifactId>
<version>5.7.1.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-wls-remote-12.1.2</artifactId>
<version>1.0.0.Alpha3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-integration-tests</artifactId>
<version>1.0.0.Alpha7</version>
<scope>test</scope>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-impl-maven</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
和 Arquillian 基础测试 class:
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.UserTransaction;
import java.io.File;
@RunWith(Arquillian.class)
public class BaseTest {
@Inject
protected Logger logger;
@Inject
protected EntityManager em;
@Inject
protected UserTransaction utx;
@Deployment(name = "SandboxTest")
public static Archive<?> createTestArchive() {
File[] lib1 = Maven.configureResolver().loadPomFromFile("pom.xml")
.importRuntimeDependencies().resolve().withTransitivity().asFile();
return ShrinkWrap.create(WebArchive.class, "SandboxTest2.war")
.addAsLibraries(lib1)
.addPackages(true, PersonT.class.getPackage(), EMProducer.class.getPackage(), BaseTest.class.getPackage())
.addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
.addAsResource("log4j.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource("weblogic.xml");
}
@Before
public void startTransaction() throws Exception {
utx.begin();
em.joinTransaction();
}
@After
public void commitTransaction() throws Exception {
utx.commit();
em.clear();
}
}
所以在评论的帮助下,我找到了解决方案。
我必须导入所有 pom 依赖项,而不仅仅是运行时依赖项!
正确的 ShrinkWrap 设置是:
@Deployment(name = "SandboxTest")
public static Archive<?> createTestArchive() {
File[] lib1 = Maven.configureResolver().loadPomFromFile("pom.xml")
.importCompileAndRuntimeDependencies().resolve().withTransitivity().asFile();
return ShrinkWrap.create(WebArchive.class, "SandboxTest.war")
.addAsLibraries(lib1) //add lib1
.addPackages(true, PersonT.class.getPackage(),
PersonDTO.class.getPackage(),
PersonRepository.class.getPackage(),
CurrentUser.class.getPackage(),
Service.class.getPackage(),
BaseTest.class.getPackage())
.addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
.addAsResource("log4j.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource("weblogic.xml");
}
我正在尝试执行一些关于休眠实体版本控制系统的 Arquillian 测试。
无论我做什么,都不会创建审计表 (_AUD),并且 envers 通常无法正常工作。
我搜索了其他帖子并确保我在 hibernate-core 和 envers 上遵循相同的版本。
这是我的pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>sandbox</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>sandbox</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.jee.api>7.0</version.jee.api>
<version.ejb>3.2</version.ejb>
<java.version>1.8</java.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<!-- other plugin versions -->
<version.compiler.plugin>3.1</version.compiler.plugin>
<version.ear.plugin>2.10</version.ear.plugin>
<version.ejb.plugin>2.3</version.ejb.plugin>
<version.surefire.plugin>2.16</version.surefire.plugin>
<version.hibernate5-ddl-maven-plugin>2.0.4</version.hibernate5-ddl-maven-plugin>
<!-- Other libs -->
<version.deltaspike>1.8.0</version.deltaspike>
<modelMapper.version>1.1.0</modelMapper.version>
<!-- Hibernate -->
<hibernate.core.version>5.2.11.Final</hibernate.core.version>
<hibernate.modelgen.version>5.2.11.Final</hibernate.modelgen.version>
<hibernate.annotations.version>3.5.6-Final</hibernate.annotations.version>
<hibernate.javax.persistence.version>1.0.0.Final</hibernate.javax.persistence.version>
<hibernate.validator.version>6.0.2.Final</hibernate.validator.version>
<hibernate.jpa.api.version>1.0.0.Final</hibernate.jpa.api.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>${hibernate.javax.persistence.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.core.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate.core.version}</version>
</dependency>
<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
<version>1.1.11.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<scope>test</scope>
<version>1.1.11.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.narayana.jta</groupId>
<artifactId>narayana-jta</artifactId>
<version>5.7.1.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-wls-remote-12.1.2</artifactId>
<version>1.0.0.Alpha3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-integration-tests</artifactId>
<version>1.0.0.Alpha7</version>
<scope>test</scope>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-impl-maven</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
和 Arquillian 基础测试 class:
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.UserTransaction;
import java.io.File;
@RunWith(Arquillian.class)
public class BaseTest {
@Inject
protected Logger logger;
@Inject
protected EntityManager em;
@Inject
protected UserTransaction utx;
@Deployment(name = "SandboxTest")
public static Archive<?> createTestArchive() {
File[] lib1 = Maven.configureResolver().loadPomFromFile("pom.xml")
.importRuntimeDependencies().resolve().withTransitivity().asFile();
return ShrinkWrap.create(WebArchive.class, "SandboxTest2.war")
.addAsLibraries(lib1)
.addPackages(true, PersonT.class.getPackage(), EMProducer.class.getPackage(), BaseTest.class.getPackage())
.addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
.addAsResource("log4j.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource("weblogic.xml");
}
@Before
public void startTransaction() throws Exception {
utx.begin();
em.joinTransaction();
}
@After
public void commitTransaction() throws Exception {
utx.commit();
em.clear();
}
}
所以在评论的帮助下,我找到了解决方案。 我必须导入所有 pom 依赖项,而不仅仅是运行时依赖项!
正确的 ShrinkWrap 设置是:
@Deployment(name = "SandboxTest")
public static Archive<?> createTestArchive() {
File[] lib1 = Maven.configureResolver().loadPomFromFile("pom.xml")
.importCompileAndRuntimeDependencies().resolve().withTransitivity().asFile();
return ShrinkWrap.create(WebArchive.class, "SandboxTest.war")
.addAsLibraries(lib1) //add lib1
.addPackages(true, PersonT.class.getPackage(),
PersonDTO.class.getPackage(),
PersonRepository.class.getPackage(),
CurrentUser.class.getPackage(),
Service.class.getPackage(),
BaseTest.class.getPackage())
.addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
.addAsResource("log4j.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource("weblogic.xml");
}