Hibernate:在运行时根据映射生成类
有没有办法基于Hibernate映射生成域类?我期待有关 hbm2java 工具的答案,但它被设计为在构建时使用。是否可以在运行时执行此类任务?
Is there a way to generate domain classes based on Hibernate mapping? I anticipate answers about hbm2java tool, but it is designed to be used in build time. Is it possible to perform such task at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答:这可能是可能的,但通常不是一个好主意。
Hibernate 和其他 ORM 库旨在成为应用程序中数据库和面向对象域之间的桥梁。如果您想在运行时生成域,那么您就否定了拥有域或使用 Hibernate 的全部原因。老实说,Hibernate 的设计初衷并不是为了做到这一点。
但是,如果您想要一个工具来调查模式并报告存在哪些表、列等结构,我建议使用常规 sql。
正如您所提到的,如果您想从一组 .hbm.xml 文件生成域,请使用 hbm2java 工具并利用生成的代码。
Short answer: this may be possible, but generally not a good idea.
Hibernate and other ORM libraries are meant to be a bridge between a database and a object oriented domain in your application. If you want to generate your domain during runtime, you would negate the whole reason for having a domain or using Hibernate. Honestly, Hibernate was not designed to do this.
However, if you want a tool to investigate a schema and report back on what tables, columns, etc structure exists I would suggest using regular sql.
As you mentioned, if you want to generate a domain off of a set of .hbm.xml files, use the hbm2java tool and leverage the generated code.