生成 Hibernate hbm xml 文件和 现有数据库模式中的实体类

发布于 2024-07-27 00:52:37 字数 48 浏览 5 评论 0原文

如何生成 Hibernate hbm xml 文件和 来自现有数据库模式的实体类?

How can I generate Hibernate hbm xml files & entities classes from an existing DB schema?

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

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

发布评论

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

评论(4

年华零落成诗 2024-08-03 00:52:37

我非常高兴地使用了 Hibernate Tools(其网站上给出的示例)。
下面,我将详细介绍我的具体、高级和有趣的(我认为)用例。


实际上,我在我们的大项目中面临着一个有趣的挑战(接近 800 个表,数据库驱动的团队)

  • 新表会不断到达,所以我可以从数据库生成它们
    (使用 HibernateTools,并生成带注释的实体)(我们现在实际上正在使用另一个进程...)
  • 但是大多数表都不是新的,我已经有了 java 实现和 .hbm.xml。
    两者有时都会根据最初生成它们的数据库进行修改,因此不可能在保证不破坏任何内容的情况下重新生成它们。 我需要迁移实体,尽可能少地更改(即仅更改注释)!

    • 这也需要很快,因为我们典型的旧实体有大约 100 个成员(自己的数据库列,加上来自反向外键的实体集合!)。

      注意:两个实体无法使用生成的完整构造函数进行编译,它们打破了 256 个参数的限制! 但我觉得这个Constructor反正没什么用,谁能记住256个参数的顺序,所以我把它去掉了。

    • 我还想将我的 Set 迁移到通用 Set(除了我暂时不关心的 setter)。

对于映射迁移,我使用了 Hibernate Tools(根据需要定制、模板和代码),如下所示:

  • 信息来源是 .hbm.xml 文件,以及 hibernate.cfg.xml 文件

    注意:我必须首先提取 hibernate.cfg.xml,替换用于包含列表的 spring bean。 但这对于 Squirrel 等数据库工具也很有用,可以使用它来启用 HQL 完成...

  • 生成的输出是仅包含字段的 X2.java 文件(对于 X.java 类,在同一包中) 、吸气剂和注释
    (没有 setter 或构造函数)(通用集)

我将使用 Eclipse 编译器(错误“重复...”)来仔细检查我的编辑,以使其更快且不易出错(错误不是一个选项,我们有许多客户正在生产中!)。 对于每个迁移的类,我将从生成的

  • 类复制到现有的类:更改 persistence.cfg.xml 以使用该类而不是 .hbm.xml
  • 在类名之前剪切并粘贴@Entity
  • 在现有字段之后剪切并粘贴所有设置字段,删除只有存在编译错误的现有设置(结果是我现在有带有通用集的字段)
  • 后剪切并粘贴所有获取器(这是类的其余部分)
  • 在现有设置器打开大纲视图 ,仅显示公共动态不以 'set' 开头的方法,按字母顺序排序,
  • 检查每个没有错误的 getter(调查是否出了问题,或者从...开始就被删除了),
  • 遵循大纲视图,仅考虑错误的方法,按顺序检查每个 getter:
    删除该方法的第二个实例,将其注释复制到第一个实例
    (使用大纲视图进行导航)(类中方法的顺序被保留,这对于 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)

  • New tables would keep arriving, so I could generate them from the database
    (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.

    • I also wanted to migrate my Sets to generic ones (except the setter I didn't bother with for now).

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 :

  • change persistence.cfg.xml to use the class instead of the .hbm.xml
  • cut and paste @Entity before class name
  • cut and paste all Set fields after existing fields, delete only the existing ones that have a compile-error (the result is that I now have fields with generic Sets)
  • cut and paste all getters (which is the rest of the class) after the existing setters
  • open the outline view, showing only public dynamic methods not starting with 'set', sorted alphabetically
  • check each getter that has not error (investigate if something went wrong, or it had been dropped since ...)
  • following the outline view, considering only erroneous methods, for each getter in order :
    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 !).
  • ... some details left for further discussion ...

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 ! ;-)

云归处 2024-08-03 00:52:37

为此,您应该使用 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.

浪推晚风 2024-08-03 00:52:37

我会推荐 Hibernate 工具

I would recommend Hibernate Tool

ぃ弥猫深巷。 2024-08-03 00:52:37

Netbeans 具有生成配置文件、注释文件等的功能

Netbeans has functionality for generating config files, annotated files and more

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