JPA使用替代的“persistence.xml”

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

我知道用指令:

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

JPA持久性机制读取persistence.xml文件,查找称为持久性的持久性单元-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 采取与持久性不同的文件。 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:09:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JPA   persistence   xml

发布评论

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

>www.elefans.com

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