简体   繁体   English

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

[英]Is JPA persistence.xml classpath located?

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.xml in the META_INF folder of my main app and it works great in the container (Glassfish). 我的主要应用程序的META_INF文件夹中有我的主要persistence.xml ,它在容器(Glassfish)中运行良好。

I placed a second persistence.xml in the META-INF folder of my test-classes directory. 我在test-classes目录的META-INF文件夹中放置了第二个persistence.xml 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. 在eclipse中,我将此文件夹放在类路径中比默认文件夹更高,它似乎工作。

Now when I run the maven build directly from the command line and it attempts to run the unit tests, the persistence.xml override is ignored. 现在,当我直接从命令行运行maven构建并尝试运行单元测试时,将忽略persistence.xml覆盖。 I can see the override in the META-INF folder of the maven generated test-classes directory and I expected the maven tests to use this file, but it isn't. 我可以在maven生成的test-classes目录的META-INF文件夹中看到覆盖,我希望maven测试使用这个文件,但事实并非如此。 My Spring test configuration overrides, achieved in a similar fashion are working. 我的Spring测试配置覆盖,以类似的方式实现了工作。

I'm confused at to whether the persistence.xml is located through the classpath. 我很困惑persistence.xml是否通过类路径定位。 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". 如果是,我的覆盖应该像弹簧覆盖一样工作,因为maven surefire插件解释 “[测试类目录]将包含在测试类路径的开头”。

Did I wrongly anticipate how the persistence.xml file is located? 我是否错误地预测了persistence.xml文件的位置?

I could (and have) create a second persistence unit in the production persistence.xml file, but it feels dirty to place test configuration into this production file. 我可以(并且已经)在生产persistence.xml文件中创建第二个持久性单元,但是将测试配置放入此生产文件中会感觉很脏。 Any other ideas on how to achieve my goal is welcome. 关于如何实现我的目标的任何其他想法都是受欢迎的。

It is unclear where you placed the "second" persistence.xml (the test version) but you should place it in src/test/resources/META-INF . 目前还不清楚您放置“第二个” persistence.xml (测试版)的位置,但您应将其放在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 . 测试资源会自动添加到Maven为您的单元测试设置的类路径中,并优先于src/main/resources

persistence.xml is loaded from the classpath; persistence.xml是从类路径加载的; in the past, I've done exactly what you described. 在过去,我完成了你所描述的。

It's most likely an issue with maven. 这很可能是maven的一个问题。 You can debug the maven classpath by running it with the -X option. 您可以通过使用-X选项运行它来调试maven类路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM