JPA使用替代的“persistence.xml” [英] JPA using alternative "persistence.xml"

查看:88
本文介绍了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”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆