java JPA persistence.xml 类路径是否位于?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2638448/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:09:47  来源:igfitidea点击:

Is JPA persistence.xml classpath located?

javaunit-testingmaven-2jpaclasspath

提问by Vinnie

Here's what I'm trying to do. I'm using JPA persistence in a web application, but I have a set of unit tests that I want to run outside of a container.

这就是我想要做的。我在 Web 应用程序中使用 JPA 持久性,但我有一组要在容器外部运行的单元测试。

I have my primary persistence.xmlin the META_INFfolder of my main app and it works great in the container (Glassfish).

我的主应用程序位于我的主应用程序persistence.xmlMETA_INF文件夹中,它在容器(Glassfish)中运行良好。

I placed a second persistence.xmlin the META-INFfolder of my test-classesdirectory. This contains a separate persistence unit that I want to use for test only. In eclipse, I placed this folder higher in the classpath than the default folder and it seems to work.

persistence.xmlMETA-INF我的test-classes目录的文件夹中放置了第二个。这包含一个单独的持久性单元,我只想将其用于测试。在 eclipse 中,我将此文件夹放在类路径中比默认文件夹更高的位置,它似乎可以工作。

Now when I run the maven build directly from the command line and it attempts to run the unit tests, the persistence.xmloverride is ignored. I can see the override in the META-INFfolder of the maven generated test-classesdirectory and I expected the maven tests to use this file, but it isn't. My Spring test configuration overrides, achieved in a similar fashion are working.

现在,当我直接从命令行运行 Maven 构建并尝试运行单元测试时,persistence.xml覆盖被忽略。我可以META-INF在 maven 生成的test-classes目录的文件夹中看到覆盖,我希望 maven 测试使用这个文件,但事实并非如此。我以类似方式实现的 Spring 测试配置覆盖正在工作。

I'm confused at to whether the persistence.xmlis located through the classpath. If it were, my override should work like the spring override since the maven surefire plugin explains"[The test class directory] will be included at the beginning the test classpath".

我对是否persistence.xml通过类路径定位感到困惑。如果是这样,我的覆盖应该像 spring 覆盖一样工作,因为 maven surefire 插件解释了“[测试类目录] 将包含在测试类路径的开头”。

Did I wrongly anticipate how the persistence.xmlfile is located?

我是否错误地预测了persistence.xml文件的位置?

I could (and have) create a second persistence unit in the production persistence.xmlfile, but it feels dirty to place test configuration into this production file. Any other ideas on how to achieve my goal is welcome.

我可以(并且已经)在生产persistence.xml文件中创建第二个持久性单元,但是将测试配置放入这个生产文件中感觉很脏。欢迎任何其他关于如何实现我的目标的想法。

采纳答案by Ken Liu

persistence.xmlis loaded from the classpath; in the past, I've done exactly what you described.

persistence.xml从类路径加载;过去,我完全按照你的描述做了。

It's most likely an issue with maven. You can debug the maven classpath by running it with the -Xoption.

这很可能是maven的问题。您可以通过使用该-X选项运行 maven 类路径来调试它。

回答by Pascal Thivent

It is unclear where you placed the "second" persistence.xml(the test version) but you should place it in src/test/resources/META-INF. Test resources are automatically added to the classpath set up by Maven for your unit tests and take precedence over resources placed in src/main/resources.

不清楚您将“第二个” persistence.xml(测试版本)放在哪里,但您应该将它放在src/test/resources/META-INF. 测试资源会自动添加到 Maven 为您的单元测试设置的类路径中,并优先于放置在src/main/resources.