转自:https://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/
In this article, we show you how to use Hibernate / JBoss Tools to generate Hibernate mapping files (hbm) and annotation code from database automatically.
Tools in this article
Open your “Hibernate Perspective“. In Eclipse IDE, select “Windows” >> “Open Perspective” >> “Others…” , choose “Hibernate“.
In Hibernate Perspective, right click and select “Add Configuration…”
In “Edit Configuration” dialog box,
hibernate.cfg.xml
.See your list of your tables in “Hibernate Perspective“.
Sample of “hibernate.cfg.xml
“, connect to Oracle 11g database.
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriverproperty>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:MKYONGproperty>
<property name="hibernate.connection.username">mkyongproperty>
<property name="hibernate.connection.password">passwordproperty>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialectproperty>
<property name="hibernate.default_schema">MKYONGproperty>
session-factory>
hibernate-configuration>
Now, you are ready to generate the Hibernate mapping files and annotation codes.
– In “Hibernate Perspective”, click “Hibernate code generation” icon (see below figure) and select “Hibernate Code Generation Configuration”
– Create a new configuration, select your “console configuration” (configured in step 2), puts your “Output directory” and checked option “Reverse engineer from JDBC Connection“.
– In “Exporter” tab, select what you want to generate, Model , mapping file (hbm) , DAO, annotation code and etc.
See result