使用 Hibernate 进行数据迁移

发布于 2024-07-29 05:03:45 字数 135 浏览 2 评论 0原文

我正在为一家公司开展一个项目,我需要将其以前的数据迁移到新系统中。 显然,这两种结构完全不同,我使用 hibernate 进行数据库操作,并且对迁移数据的最佳方法感到困惑。 我应该使用休眠,我应该编写存储过程,还是还有其他选择? 欢迎任何聪明的想法。

I am working on a project for a company, where I need to migrate its previous data into a new system. Obviously both structures are totally different and I'm using hibernate for database manipulation and confused as to what would be the best approach to migrate data. Should I use hibernate, should I write stored procedures, or is there another option? Any bright ideas are welcome.

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

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

发布评论

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

评论(5

池予 2024-08-05 05:03:45

在过去尝试过这两种方法后,我可以肯定地说,这不是 ORM 设计的场景,也不是它蓬勃发展的场景。 您最终必须构建两组不同的对象,并且很难获得大规模迁移所需的效率。 我能想到使用休眠之类的东西的唯一原因是,如果您正在构建一个将永久位于两个系统之间以集成它们的系统,但听起来这是相对短期的。

我对 python 脚本、一堆 SQL 和一些用于转换数据的 python 对象的结果非常满意。

Having tried both approaches in the past, I can definitely say that this is not a scenario for which ORM was designed, nor one where it flourishes. You end up having to build two different sets of objects, and it's difficult to gain the efficiency required for a mass migration. The only reason I can think of for using something like hibernate would be if you were building a system which was going to permanently sit in between two systems to integrate them, but it sounds like this is relatively short-term.

I have been extremely happy with the results of a python script, a bunch of SQL and some python objects to transform the data.

A君 2024-08-05 05:03:45

Hibernate 不能很好地处理存储过程——它不是“天生的合适”。 如果您的 ORM 已经映射出来,并且您已经设置了“原样”和“未来”对象,请使用正常的 hibernate 方法将数据写入新布局。

如果您被迫采用 StoredProcedure 方式,那么您可以决定是否要忍受痛苦,并在存储过程中编写转换代码 - 这样您的所有迁移脚本将保持在一起。

正如另一位发帖者所说,像 Python 这样的脚本语言可以很好地为您服务 - 它对我来说也很有效。

Hibernate does not play very well with stored procedures - its not a 'natural fit'. If your ORM is already mapped out , and you have your 'as-is' and 'to-be' objects setup, use normal hibernate methods to write data to your new layout.

If you are forced to go the StoredProcedure way, then you can decide if you want to swallow the pain, and code your transforms in the stored procedures - that way all your migration scripts will stay together.

Like the other poster said, a scripting language like Python can serve you well here - it has worked well for me too.

菩提树下叶撕阳。 2024-08-05 05:03:45

当我通过各种版本更改代码和数据表示时,我尝试管理数据迁移。 每次,我最终都会编写特定的 sql 来查询旧状态的对象,并填充新列。 如果有一种直接的方法来管理数据迁移,同时将所有内容视为对象,我还没有想到,到目前为止,新列始终具有我可以在 sql 中计算的简单解释。

其中一些非常简单,代码最终以 java 形式出现,而其他更改则非常复杂,我需要多个 sql 语句,因此我最终将它们嵌入到 shell 和 python(两者都是为了可移植性)脚本中。 脚本位于此处java 代码方法名称如 updateDB2008_4()。

I've tried to manage data migration as I change my code and my data representation through various releases. Each time, I've ended up writing specific sql to query for objects in the old state, and to populate new columns. If there's a straightforward way to manage data migration while viewing everything as an object, I haven't thought of it, and so far, the new columns have always have simple interpretations that I could calculate in sql.

Some of them have been simple enough that the code has ended up in java, and other changes have been complex enough that I wanted multiple sql statements, and so I ended up embedding them in shell and python (both, for portability) scripts. The scripts are here and the java code with method names like updateDB2008_4().

淡淡绿茶香 2024-08-05 05:03:45

虽然我不确定什么是“最好”,但如果我处于您的位置,我更愿意使用 Hibernate 或类似的 ORM。 原因是您可以在两个数据库之间使用对象层次结构。 如果模式极其相似,那么简单的 SQL 脚本可能会更容易。 这实际上取决于您的情况及其具体情况。

编辑:我真的需要一些早晨咖啡因......

我试图注意到的内容被在我之后发帖的两个人注意到了。 如果模式已定义,您已经拥有对象或擅长逆向工程工具,那么 hibernate 是一种简单的方法。 但是,如果这是一次性的事情,脚本绝对是更好的方法。

While I am not sure about "best", I would prefer to use Hibernate or a similar ORM if I was in your position. The reason is that then you have an object hierarchy to use between the two databases. If the schemas are extremely similar, then a simply SQL script might be easier. It really depends on what your situation is, and the specifics of it.

edit: I really need some morning caffeine...

What I was attempting to note, was noted by the two people who posted after me. If the schemas are defined, you already have objects or are good with the reverse engineering tools, then hibernate is an easy way to go. However, if it is a one time thing, scripts are definitely the better way to go.

赠意 2024-08-05 05:03:45

就我个人而言,我可能会使用像 SSIS 这样的 ETL 工具(如果您要往返于 SQL Server),大量数据的移动是 ETl 工具设计和优化的目的。

Personally I would probably use am ETL tool like SSIS (if you are going from or to SQL Server) for this, movement of large amounts of data is what ETl tools are designed and optimized to do.

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