JPA 使用替代的“persistence.xml"

编程入门 行业动态 更新时间:2024-10-23 11:24:35
本文介绍了JPA 使用替代的“persistence.xml"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道有说明:

Persistence.createEntityManagerFactory("persistence-unit-name");

JPA 持久化机制读取persistence.xml"文件,寻找名为persistence-unit-name"的持久化单元,并在此基础上构造EntityManagerFactory.

The JPA persistence mechanism reads the "persistence.xml" file, looks for the persistence unit called "persistence-unit-name", and constructs the EntityManagerFactory based on it.

我的问题是,如何强制 JPA 采用不同于persistence.xml"的文件??例如,persistence-test.xml".

My question is, how can I force JPA to take a file different from "persistence.xml"?? for example, "persistence-test.xml".

推荐答案

没有标准化的 JPA 方法可以做到这一点,尽管个别 JPA 提供者可能会提供一种方法.我建议使用标准方法为 main 和 test 设置不同的类路径.

There is no standardized JPA way to do this, although individual JPA providers may provide a way. I would suggest the standard way to have a different class path for main and test.

例如,如果您使用 Maven,并且您有两个 META-INF/persistence.xml 文件,一个在 src/main/resources 中,一个在 中>src/test/resources,测试会选择src/test/resources 中的那个,因为Maven 将测试类/资源放在类路径中的主要类/资源之前.您可以轻松地将 Ant 配置为以类似的方式工作.

For example, if you use Maven, and you have two META-INF/persistence.xml files, one in src/main/resources and one in src/test/resources, tests will pick up the one in src/test/resources, because Maven puts test classes / resources ahead of main classes / resources in the classpath. You can easily configure Ant to work in similar ways.

如果你需要更高级的逻辑,可以考虑使用 Spring 的 JPA 支持.它将让您处理诸如 集成多个persistence.xml文件.

If you need more advanced logic, consider using Spring's JPA support. It will let you deal with advanced situations like integrating multiple persistence.xml files.

更多推荐

JPA 使用替代的“persistence.xml"

本文发布于:2023-10-25 14:08:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:persistence   JPA   quot   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!