在maven-verify中没有自动检测JPA实体

如果我将persistence.xml放在src / test / META-INF文件夹中,则自动检测实体不能与maven-verify一起使用。 当persistence.xml位于src / main / META-INF文件夹中时,它可以正常工作。

在两种情况下都可以在eclipse中运行测试。

当persistence.xml位于src / test文件夹中时,有没有办法让autodetection用于maven-verify?

persistence.xml中:

        

默认情况下,自动检测适用于与persistence.xml相同的类路径项中的实体。 它可以由元素配置。

要在persistence.xml位于src/test/resources/META-INF时启用正确的自动检测,请使用以下技巧:

persistence.xml

   ${project.build.outputDirectory} ...   

pom.xml – 为src/test/resources启用资源过滤:

  ...    src/test/resources true     

虽然我不确定如果你的persistence.xml实际上是在src/test/META-INF如何使用它。

如果使用Spring Framework,则可以使用PersistenceUnitPostProcessor执行以下操作

CustomPersistenceUnitPostProcessor:

  package com.yourpackage.utils.jpa.CustomPersistenceUnitPostProcessor; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.persistence.Entity; import net.sourceforge.stripes.util.ResolverUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo; import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor; /** * This PersistenceUnitPostProcessor is used to search given package list for JPA * entities and add them as managed entities. By default the JPA engine searches * for persistent classes only in the same class-path of the location of the * persistence.xml file. When running unit tests the entities end up in test-classes * folder which does not get scanned. To avoid specifying each entity in the persistence.xml * file to scan, this post processor automatically adds the entities for you. * */ public class CustomPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor, InitializingBean { private static final Logger log = LoggerFactory.getLogger(CustomPersistenceUnitPostProcessor.class); /** the path of packages to search for persistent classes (eg org.springframework). Subpackages will be visited, too */ private List packages; /** the calculated list of additional persistent classes */ private Set> persistentClasses; /** * Looks for any persistent class in the class-path under the specified packages */ @Override public void afterPropertiesSet() throws Exception { if (packages == null || packages.isEmpty()) throw new IllegalArgumentException("packages property must be set"); log.debug("Looking for @Entity in " + packages); persistentClasses = new HashSet>(); for (String p : packages) { ResolverUtil resolver = new ResolverUtil(); ClassLoader cl = this.getClass().getClassLoader(); log.debug("Using classloader: " + cl); resolver.setClassLoader(cl); resolver.findAnnotated(Entity.class, p); Set> classes = resolver.getClasses(); log.debug("Annotated classes: " + classes); persistentClasses.addAll(classes); } if (persistentClasses.isEmpty()) throw new IllegalArgumentException("No class annotated with @Entity found in: " + packages); } /** * Add all the persistent classes found to the PersistentUnit */ @Override public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo persistenceUnitInfo) { for (Class c : persistentClasses) persistenceUnitInfo.addManagedClassName(c.getName()); } public void setPackages(List packages) { this.packages = packages; } } 

Spring配置:

            com.yourpackage.model       

persistence.xml中:

    org.hibernate.ejb.HibernatePersistence