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 :)

Friday 25 September 2015

Step by step Installation of JBoss Tools - Hibernate plugin in Eclipse Kepler

Hibernate JBoss tool eases developer's work with JPA (or Hibernate). You can easily generate lot of stuff related to Hibernate which is difficult (and error prone) while doing manually.

       You can generate Hibernate configurations (*.hbm.cfg.xml), java model classes and mapping file or annotated model classes. Basically it follows concept of reverse-engineering. So if you are not using hbm2ddl to generate Tables from Java model then this plugin will help you generate model classes from existing Tables.

This is Eclipse used in this post.


Eclipse : Version: Kepler Service Release 2

Follow these steps to install this Hibernate JBoss plugin in you Eclipse Kepler.

Click on : Hep --> Install New Software 


This window will appear on screen.



Click on : Add button

Now copy following URL and paste it in "Location field"

http://download.jboss.org/jbosstools/updates/stable/kepler/


And click 'Ok' button.

Now following screen will appear displaying all plugin available from JBoss tool site (there are lot of helpful plugins there from JBoss but we will focus on Hibernate plugin for now)


Now we have to select required Hibernate plugin out of all available ones.

Expand option 'JBoss Application Development' available in list. And you can see 'Hibernate Tools' in expanded list. Select this tool in that list.



Now click next and finish it. It will take time to load and install plugin.

Once installation is finished restart your eclipse.

Now you can see your installed plugin in perspectives.

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

Following window will appear displaying all perspectives. You can see your recently install Hibernate plugin available in that.



And done :)

Download sites and References :