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

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

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