生成 Hibernate hbm xml 文件和 现有数据库模式中的实体类
如何生成 Hibernate hbm xml 文件和 来自现有数据库模式的实体类?
How can I generate Hibernate hbm xml files & entities classes from an existing DB schema?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我非常高兴地使用了 Hibernate Tools(其网站上给出的示例)。
下面,我将详细介绍我的具体、高级和有趣的(我认为)用例。
实际上,我在我们的大项目中面临着一个有趣的挑战(接近 800 个表,数据库驱动的团队)
(使用 HibernateTools,并生成带注释的实体)(我们现在实际上正在使用另一个进程...)
但是大多数表都不是新的,我已经有了 java 实现和 .hbm.xml。
两者有时都会根据最初生成它们的数据库进行修改,因此不可能在保证不破坏任何内容的情况下重新生成它们。 我需要迁移实体,尽可能少地更改(即仅更改注释)!
这也需要很快,因为我们典型的旧实体有大约 100 个成员(自己的数据库列,加上来自反向外键的实体集合!)。
注意:两个实体无法使用生成的完整构造函数进行编译,它们打破了 256 个参数的限制! 但我觉得这个Constructor反正没什么用,谁能记住256个参数的顺序,所以我把它去掉了。
对于映射迁移,我使用了 Hibernate Tools(根据需要定制、模板和代码),如下所示:
信息来源是 .hbm.xml 文件,以及 hibernate.cfg.xml 文件
注意:我必须首先提取 hibernate.cfg.xml,替换用于包含列表的 spring bean。 但这对于 Squirrel 等数据库工具也很有用,可以使用它来启用 HQL 完成...
生成的输出是仅包含字段的 X2.java 文件(对于 X.java 类,在同一包中) 、吸气剂和注释
(没有 setter 或构造函数)(通用集)
我将使用 Eclipse 编译器(错误“重复...”)来仔细检查我的编辑,以使其更快且不易出错(错误不是一个选项,我们有许多客户正在生产中!)。 对于每个迁移的类,我将从生成的
删除该方法的第二个实例,将其注释复制到第一个实例
(使用大纲视图进行导航)(类中方法的顺序被保留,这对于 CVS 的历史很重要,特别是向不相信的人证明迁移没有破坏他们的代码,它之前已经被破坏了!)。
出于好奇,本月我们有接近 200 个带注释的实体:-)。
典型的 100 个字段实体需要大约 30 分钟的迁移时间。
只剩下 300 个小时来完成剩余 600 个实体的剪切和粘贴! ;-)
I've used Hibernate Tools (examples given on their site) with much pleasure.
Below, I give details on my specific, advanced and interesting (I think) use case.
Actually, I was facing an interesting challenge on our big project (approaching 800 tables, database driven team)
(using HibernateTools, and producing Annotated Entities)(we're actually using another process right now ...)
But mosts tables were not new, I already had the java implementations and the .hbm.xml.
Both had sometimes been modified from the DB they were originally generated with, so it was impossible to re-generated them with the guaranty not to break anything. I needed to migrate the Entities, changing as little as possible (that is, only the annotations) !
This needed to be fast also, because our typical old entities have around 100 members (own db columns, plus entity collections coming from reverse foreign keys!).
Note : Two entities couldn't compile with a generated full-constructor, they broke the 256 parameters limit ! But I though this Constructor was useless anyway, who could remember the order of 256 parameters, so I removed it.
For the mapping migration, I used Hibernate Tools (customized as needed, template and code) as follow:
the source of the information was the .hbm.xml files, with the hibernate.cfg.xml file
Note : I had to extract the hibernate.cfg.xml first, replacing the spring bean that used to contain the list. But this was also useful for db tools such as Squirrel, that could use it to enable HQL completion...
the generated output was X2.java files (for X.java class, in the same package) containing only the fields, getters and annotations
(no setters or constructors)(generic Sets)
I would use the Eclipse compiler (error "duplicate ...") to double-check my editing, to make it faster and less error-prone (mistake was not an option, we have many client in production !). For each migrated class, I would copy from generated to existing class :
delete the second instance of the method, copy its annotations to the first instance
(navigation using the outline view)(the order of the methods in the class is preserved, which has been important for CVS history, notably in proving to unbelievers that the migration didn't break their code, it was already broken before !).
For the curious, this month we're close to 200 annotated entities :-).
A typical 100 fields entity requires about 30 minutes work to migrate.
Only 300 hours left to finish this cut'n paste for the remaining 600 entities ! ;-)
为此,您应该使用 hibernate 逆向工程工具。 请参阅 hibernate 逆向工程工具文档 了解更多信息。
我不清楚如何生成 JPA 带注释的类,但如果这是一个支持注释的新项目,您可能需要考虑不再使用 hbm.xml 文件。
You should use the hibernate reverse engineering tools for this. See the hibernate reverse engineering tools documentation for more information.
It's not clear to me how to generate JPA annotated classes, but you might want to think about not using hbm.xml files anymore if this is a new project, favoring annotations.
我会推荐 Hibernate 工具
I would recommend Hibernate Tool
Netbeans 具有生成配置文件、注释文件等的功能
Netbeans has functionality for generating config files, annotated files and more