简体   繁体   English

使用Hibernate迁移数据

[英]Using Hibernate to migrate data

Developing a brand new schema/app which uses hibernate to create tables from pojo's. 开发一个全新的架构/应用程序,它使用hibernate从pojo创建表。

My desire is to now migrate existing data from legacy db to new schema, which is a matter of creating approriate sql and populating my new pojo's. 我的愿望是现在将现有数据从遗留数据库迁移到新模式,这是创建适当的SQL并填充我的新pojo的问题。

Secondly, it is intended that this app will be used by others, migrating from different db schema's. 其次,这个应用程序将被其他人使用,从不同的数据库模式迁移。 My initial thinkign is to therefore put the relevant sql into xml files so they can be easily changed without altering the java. 我最初的想法是将相关的sql放入xml文件中,以便在不改变java的情况下轻松更改它们。

For example my current system will select loginname from logintable into pojo : User.setUsername() and then do a hibernate save. 例如,我当前的系统select loginname from logintable到pojo: User.setUsername()然后进行休眠保存。 And then I can simply change the sql at a later date makign sure it returns the correct data. 然后我可以在以后简单地更改sql,确保它返回正确的数据。

Does this sound sensible ? 这听起来合情合理吗? Is their existing functionality in hibernate for doing migrations/bulk inserts ? 他们现有的hibernate功能是否可以进行迁移/批量插入? Or will I simply be calling this n times : 或者我只是简单地打电话给n次:

User user = new User();
session.save(user);

Few thoughts here. 这里几点想法。

schema/app which uses hibernate to create tables from pojo's.

It is not a good practice to use the hibernate to generate the table. 使用hibernate生成表不是一个好习惯。 There should be separate scripts created for that. 应该为此创建单独的脚本。 Basically hbm2ddl.auto=create setting in hibernate is a temporary way to create the tables, but you should not be using it in a standard application. 基本上hbm2ddl.auto=create在hibernate中hbm2ddl.auto=create设置是创建表的临时方法,但是你不应该在标准应用程序中使用它。 It is even unsafe to have this entry in your configuration settings.Read here for more. 在配置设置中输入此条目甚至是不安全的。请阅读此处了解更多信息。

Schema migration/ data migration

There are lots of database available that will help you to migrate bot the schema and the data and hibernate in not really meant for this. 有很多可用的数据库可以帮助你迁移僵尸程序架构,数据和休眠并不是真正意义上的。 I am not aware of those but I have seen my DB Admins doing it with ease. 我不知道这些,但我看到我的数据库管理员轻松地做到了。 You might achieve it using some tricks but it is better to refrain from that. 你可能会使用一些技巧来实现它,但最好不要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM