简体   繁体   中英

Hibernate: generate classes based on mappings in runtime

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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