Saturday, September 8, 2012

Hibernate Code Generation

Hibernate Code Generation
  • Before code generation, you have a database server includes database with some tables. Here, we have installed MySQL and built a database (i.e. chtrh) with some tables (i.e., user, stock). 
  • There are two required files and one console configuration (i.e., Hibernate Configuration File, Hibernate Reverse Engineering File, and Hibernate Console Configuration) for code generation.
    • Create Hibernate Configuration file and name as hibernate.cfg.xml, then press next.

    •  Set your database url, username and password, then press finish. 

    • Create Hibernate Console Configuration (maybe you can't find this file in eclipse). 
      • Here, you have to choose the location of hibernate.cfg.xml. 
      • Select the "Annotations (jdk 1.5+)" radio button, then press finish. 

    • Create Hibernate Reverse Engineering file and name as hibernate.reveng.xml then press next. 
    • In the "Type Mappings" tab, you can press "Refresh" botton, and you can get the database and its tables, in here, you can free choose table which to be generated code.
    • In the "Table Filters" tab, you can press "Refresh" button, and you can get the database and its tables, in here, you can also free choose table which to be generated code.
    •  In the "Table and Columns" tab, you can press "Add" button, and you can get the database and its tables, in here, you can also free choose table or columns which to be generated code.


    • After generating hibernate.cfg.xml and hibernate.reveng.xml files and hibernate console configuration. Switch to Hibernate perspective view, and choose Hibernate Code Generation Configuration function.

    •  In the "Console configuration" drop list, you can choose console configuration.
    • In the Exporters tab, you have to choose output type (usually select Domain code, Hibernate XML Mappings, DAO code). 
    • If you prefer use hibernate annotation, you have to check the "Use Java 5 syntax" and "Generated EJB3 annotations"
    • Lastly, after pressing run button, the hibernate will generate threes (i.e.,Domain code, Hibernate XML Mappings, DAO code ) file for each table. 

No comments:

Post a Comment