java 在 Maven 测试期间未找到persistence.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/76591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
persistence.xml not found during maven testing
提问by sblundy
I'm trying to load test data into a test DB during a maven build for integration testing. persistence.xml is being copied to target/test-classes/META-INF/correctly, but I get this exception when the test is run.
我正在尝试在 maven 构建期间将测试数据加载到测试数据库中以进行集成测试。persistence.xml 正在被target/test-classes/META-INF/正确复制,但在运行测试时出现此异常。
javax.persistence.PersistenceException: No Persistence provider for EntityManager named aimDatabase
javax.persistence.PersistenceException:没有名为aimDatabase 的EntityManager 的持久性提供程序
It looks like it's not finding or loading persistence.xml.
看起来它没有找到或加载persistence.xml。
回答by jhumble
Just solved the same problem with a Maven/Eclipse based JPA project.
刚刚用基于 Maven/Eclipse 的 JPA 项目解决了同样的问题。
I had my META-INF directory under src/main/javawith the concequence that it was not copied to the target directory before the test phase.
我将 META-INF 目录放在下面src/main/java,因为在测试阶段之前它没有被复制到目标目录。
Moving this directory to src/main/resourcessolved the problem and ensured that the META-INF/persistence.xmlfile was present in target/classeswhen the tests were run.
移动此目录以src/main/resources解决问题并确保META-INF/persistence.xml在target/classes运行测试时该文件存在。
I thinkthat the JPA facet put my META-INF/persistence.xmlfile in src/main/java, which turned out to be the root of my problem.
我认为JPA facet 将我的META-INF/persistence.xml文件放入中src/main/java,结果证明这是我问题的根源。
回答by sblundy
I'm using Maven2, and I had forgotten to add this dependency in my pom.xml file:
我正在使用 Maven2,但我忘记在我的 pom.xml 文件中添加此依赖项:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
</dependency>
回答by Nils Schmidt
Is your persistence.xml located in scr/test/resources? Cause I was facing similar problems.
你的persistence.xml 是否位于scr/test/resources?因为我也面临着类似的问题。
Everything is working fine as long as my persistence.xmlis located in src/main/resources.
只要我的persistence.xml位于src/main/resources 中,一切都正常。
If I move persistence.xmlto src/test/resourcesnothing works anymore.
如果我将persistence.xml移动到src/test/resources 就没有任何效果了。
The only helpful but sad answer is here: http://jira.codehaus.org/browse/SUREFIRE-427
唯一有用但悲伤的答案在这里:http: //jira.codehaus.org/browse/SUREFIRE-427
Seems like it is not possible right now for unclear reasons. :-(
由于不明原因,现在似乎不可能。:-(
回答by kbaribeau
If this is on windows, you can use sysinternal's procmon to find out if it's checking the right path.
如果这是在 Windows 上,您可以使用 sysinternal 的 procmon 来确定它是否正在检查正确的路径。
Just filter by path -> contains -> persistence.xml. Procmon will pick up any attempts to open a file named persistenc.xml, and you can check to see the path or paths that get tried.
只需按路径过滤 -> 包含 -> persistence.xml。Procmon 将接收任何打开名为persistenc.xml 的文件的尝试,您可以检查以查看尝试的路径。
See here for more detail on procmon: http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx
有关 procmon 的更多详细信息,请参见此处:http: //technet.microsoft.com/en-us/sysinternals/bb896645.aspx
回答by stevemac
I had the same problem and it wasn't that it couldn't find the persistence.xml file, but that it couldn't find the provider specified in the XML.
我遇到了同样的问题,并不是找不到persistence.xml 文件,而是找不到XML 中指定的提供程序。
Ensure that you have the correct JPA provider dependancies and the correct provider definition in your xml file.
确保您的 xml 文件中具有正确的 JPA 提供程序依赖项和正确的提供程序定义。
ie. <provider>oracle.toplink.essentials.PersistenceProvider</provider>
IE。 <provider>oracle.toplink.essentials.PersistenceProvider</provider>
With maven, I had to install the 2 toplink-essentials jars locally as there were no public repositories that held the dependancies.
使用 maven,我必须在本地安装 2 个 toplink-essentials jar,因为没有包含依赖项的公共存储库。
回答by stevemac
we got the same problem, does some tweaking on the project and finaly find following problem (more clear error description): at oracle.toplink.essentials.ejb.cmp3.persistence. PersistenceUnitProcessor.computePURootURL(PersistenceUnitProcessor.java:248)
我们遇到了同样的问题,对项目进行了一些调整,最终发现了以下问题(更清晰的错误描述):在 oracle.toplink.essentials.ejb.cmp3.persistence。PersistenceUnitProcessor.computePURootURL(PersistenceUnitProcessor.java:248)
With that information we recalled a primary rule: NO WHITE SPACES IN PATH NAMES!!!
有了这些信息,我们想起了一个主要规则:路径名称中没有空格!!!
Try this. Works for us smile. Maybe some day this will be fixed.
试试这个。为我们工作微笑。也许有一天这会被修复。
Hope this works for you. Good luck.
希望这对你有用。祝你好运。

