Saturday 26 September 2015

hbm2dao & hbm2java: Auto generate POJO model and DAO classes from database schema using JBoss Tools Hibernate plugin.

This tutorial shows how to generate POJO model classes and DAO classes from database tables. This approach is reverse engineering approach to generate code from already existing database tables.

We will use use Hibernate Plugin provided in JBoss tools for this tutorial. If you want to configure this plugin in your eclipse please refer this post:


Tools used :
Please follow these steps to generated Annotated Java code from database.

1. Generate Hibernate.cfg.xml & Hibernate Console configuration using Hibernate plugin

We will auto generate Hibernate.cfg.xml using Hibernate plugin. This is same configuration will be used in Hibernate Console creation (which is required to create java code).

Select location where you want to generate Hibernate.cfg.xml file as follow.

eg. src/main/resources/META-INF

Right click on folder --> New --> Other


Now Following window will open. Showing various option. 

Select Hibernate --> Hibernate Configuration File (cfg.xml)



Click on : Next --> Next 

Now following window will appear. 


Now you have to add the database properties in this box.

Database Dialect : org.hibernate.dialect.MySQL5InnoDBDialect
Driver Class     : com.mysql.jdbc.Driver
Connection URL   : jdbc:mysql://localhost:3306/test (test is schema)
usernam          : root
password         : *****

Add these properties in box. And make sure you check the check box saying 'Create a Console Configuration


Click On : Next

Following window will appear.


Please make sure you select Type : 'Annotations (jdk1.5+)' to enable annotations on POJO classes, So that you do not require mapping file.

Select Appropriate Hibernate version as per your requirements.

Now you can see Hibernate.cfg.xml generated in your project as follow.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">*****</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    </session-factory>
</hibernate-configuration>

2. Generate model POJO and DAO classes.

Now you have to generate classes from Hibernate Console configuration you have created in previous step.

 Open Hibernate Perspective

Click On : window --> Open Perspective --> Other ..

This window will appear on screen.


Now you have to Select 'Hibernate' from list and click 'Ok'. If not appear please follow this post to install it properly

Once you open Hibernate perspective, you should be able to see the Hibernate Console Configuration you have created in previous step.



Now you can see all the table available in schema you have configured in previous step.

Click on : Run --> Hibernate Code Generation ... --> Hibernate Code Generations Configurations ... 




Now this window will open.



You have to select Console Configuration created in previous step.

Then you can select the Output Directory where source will be generated eg. {PROJECT}\src\main\java

Then check option 'Reverse Engineering From JDBC Connection'



Now we need to create reveng.xml file which will contain tables for which we want to generate Java code.

Click on : reveng.xml setup --> (Pop up) will open --> Create new.. 

Select Directory where you want to store this reveng.xml file. (keep it same as Hibernate.cfg.xml directory)



Click on : Next 

Select Console Configuration. Now schema will appear in left panel, if you cant see schema then click 'Refresh' present below.



Select tables and click on 'Include'

Once you have selected all the tables click on Finish. 

Now your reveng.xml is created. 

Now you have to select what are the things which you have to generate for selected tables.

Select 'Exporter' tab in window as follow.



Select 'Domain code (Java)' to generate POJO classes
'DAO code (Java)' to generate Dao classes

Check general setting as mentioned to enable Annotation in generated code.

Now you can see the generated code in package you had mentioned.



You can restructure the package as per your design.

Done :)

1 comment:


  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me. I am a regular follower of your blog.
    Really very informative post you shared here. Kindly keep blogging.
    Java training in Btm layout
    Java training in Rajaji nagar
    Java training in Kalyan nagar
    Java training in Kalyan nagar
    Java training in Jaya nagar




    ReplyDelete