事务中的多个实体管理器
我正在编写一个应用程序,该应用程序将使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,使用 JTA。不过,我不确定您的情况是否需要全球交易。您是否真的将实体从一个数据库移动或复制到另一个数据库?在后一种情况下,您可以顺序使用两个事务。
假设它们具有相同的结构并且您没有任何冲突的 PK,则应该可以使用第一个实体管理器读取实体,将其分离,然后使用另一个实体管理器合并它。如果您可能存在 PK 冲突,则必须使用 DIY 方法(而不是简单的合并)。
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.
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).