事务中的多个实体管理器

发布于 2024-09-30 10:05:21 字数 141 浏览 0 评论 0原文

我正在编写一个应用程序,该应用程序将使用 JPA EntityManager API 将数据从一个数据库移动到另一个数据库。我的问题是: 1. 我可以在单个事务中使用多个实体管理器吗? 2. 我可以从一个数据库读取实体并将其保存在另一个数据库中吗?可能会遇到哪些问题?

I am writing an application that will move data from one database to another Using JPA EntityManager API. My questions are : 1. Can i use multiple entity managers in a single transaction? 2. Can i read an entity from one database and persist it in the other? what are the issues that am likely to encounter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2024-10-07 10:05:21

我可以在单个事务中使用多个实体管理器吗?

是的,使用 JTA。不过,我不确定您的情况是否需要全球交易。您是否真的将实体从一个数据库移动或复制到另一个数据库?在后一种情况下,您可以顺序使用两个事务。

我可以从一个数据库读取实体并将其保留在另一个数据库中吗?

假设它们具有相同的结构并且您没有任何冲突的 PK,则应该可以使用第一个实体管理器读取实体,将其分离,然后使用另一个实体管理器合并它。如果您可能存在 PK 冲突,则必须使用 DIY 方法(而不是简单的合并)。

Can I use multiple entity managers in a single transaction?

Yes, using JTA. I'm not sure you need a global transaction in your case though. Are you really moving or copying entities from one DB to the other? In the later case, you could use two transactions sequentially.

Can I read an entity from one database and persist it in the other?

Assuming they have the same structure and you don't have any conflicting PK, it should be possible to read an entity using a first entity manager, detach it and then merge it using another entity manager. If you have possible PK conflicts, you'll have to use a DIY approach (vs a simple merge).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文