Generate Hibernate Pojo Classes from DB Tables

Revision as of 23:48, 8 January 2016 by Kipkis (Kipkis | contribs) (importing article from wikihow)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Hibernate is a powerful, high performance object/relational persistence and query service. Hibernate uses JavaBean classes to represent database tables in memory. The columns of the table are represented by the fields of the java class. The class-to-table, field-to-column information is kept in XML files.

This explains the steps to reverse-engineer database tables to generate hibernate POJO classes and mapping XML files using hibernate-tools.

Steps

  1. Download Hibernate Tool and extract content to root folder of Eclipse (should merge plugins and features folders).
  2. Start eclipse.
  3. Click on [File -> New -> Other -> Hibernate -> Hibernate Configuration File] and create a cfg file. The following properties should be specified : jdbc url , username, password, DB schema, driver class and dialect.
  4. Click on [File -> New -> Other -> Hibernate -> Hibernate Console Configuration ] and create a new console configuration. Add the jar file that contains your DB driver in the classpath section at the bottom.
  5. Enter the name of the console configuration. Click Browse button against the Configuration file and select the cfg.xml file created in step 3.
  6. Click on [File -> New -> Other -> Hibernate -> Hibernate Reverse Engineering File(reveng.xml) ] and select the location of the file.
  7. Select the cfg.xml file created in step 3 as the Console Configuration. Click on include button and specify the schema and table name(s) to reverse engineer. Multiple table names can be specified using sql wild char (eg: TBL_%). Click Finish.
  8. Switch to the Hibernate perspective and Click on the Hibernate icon in the tool bar and select the [Hibernate Code Generation ..] option.
  9. Right click on the [Hibernate Code Generation]tree node and select New.
  10. Give the name of the configuration and select the console configuration created in step 5 and reveng.xml created in step6-7. Also select the Output Directory and a relevant package.
  11. Go to the Exporters tab and check Generate domain code(.java) and Generate mappings (hbm.xml). Click run.
  12. The hibernate mapping xml files and Java classes will be created in the output directory specified in step 11.

Related Articles

  • Create an XML Schema
  • Generate N Different Random Numbers
  • Become a Programmer
  • Write PHP Scripts
  • Generate Sql Statements and Hibernate Config Files from Java Objects
  • Generate a Random Number in Java

Sources and Citations