开发者

JPA : How to avoid hardcoding jpa-jar location?

Here's the facts :

  1. I have 2 jars that contain JPA entities in my webapp,
  2. and entities from one jar (jar A) has references of entities in another jar (jarB)
  3. I put the <jar-file>/WEB-INF/lib/MyJarB.jar</jar-file> in the persistence.xml of jar A

But i dont want to hardcode the /WEB-INF/lib/MyJarB.jar because i could be testing the entities in a test environment, and would love to hav开发者_JAVA技巧e another way of specifying entities jar dependencies other than hardcoding the path.

I tried the <jar-file>MyJarB.jar</jar-file> and <jar-file>./MyJarB.jar</jar-file>, but both failed.

Please share your thoughts.


Create another persistence.xml for pure testing only and put it in project B itself or another sub-project which contains test classes.


I recommend NOT putting the persistence.xml together with your JARs, because the persistence definition is the application concern. Therefore it would be better to have a persistence.xml in your web module, declaring both JARs:

<jar-file>/WEB-INF/lib/MyJarA.jar</jar-file>
<jar-file>/WEB-INF/lib/MyJarB.jar</jar-file>

In case you have tests in your web module, and you want to use another persistece definition, just create an alternate persistence.xml, say in src/test/resources, and just use that one to instantiate your EntityManagerFactory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜