Monday, December 24, 2012

Hibernate reverse engineering demo using Eclipse

Reverse Engineering: The most powerful feature of Hibernate Tools is a database reverse engineering tool that can generate domain model classes and Hibernate mapping files, annotated EJB3 entity beans, HTML documentation or even an entire JBoss Seam application in seconds. With the help of Eclipse you can do reverse Engineering. Lets see step by step to reverse-engineer database tables to generate hibernate POJO classes and mapping XML files using hibernate-tools (eclipse).

About the example: I am using My Eclipse 8.6 and MySQL. We will generate POJO classes and mapping XML files etc and latter run a test program. Please see the self explanatory screen shots below.

1. Create a New Java project
File -> New -> Java Project

2. Add name of the project lets say HibernateExample and click Finish

3. Add Hibernate Capabilities to the project
right click on the project -> MyEclipse -> Add Hibernate Capabilities

4. Keep default settings as shown below and click Finish.
Note- Here we are using Hibernate 3.3

5. Default hibernate configuration file name is hibernate.cfg.xml. If you wish to change it else keep it as it is. Also do remember this is the file which will be referred from and used to create Hibernate Session.

6. Here add database credentials. Add driver, Database URL, Username and password. I am using MySql hence added JAR file mysql-connector-java-5.1.5.jar as shown below.

7. Add package name accordingly. I am creating new package 'com.javaxp.common' and class which will be used to create Hibernate Session. You can change it else keep it default as shown below and click Finish.

8. You can see below files are created.

9. Now we will open Hibernate perspective.
Window -> Open Perspective -> MyEclipse Hibernate

10. You can add new database, if you are connecting for the first time else Just open the connection.

11. Provide the username and password.

12. Now you can see available tables under the schema.

13. Now creating packages 'com.javaxp.dao' to hold all DAO classes and 'com.javaxp.pojo' to hold all POJO classes.

14. Now we will do actual reverse engineering. Right click on the tables for which you want to create POJO classes.

right click on the tables -> Hibernate Reverse Engineering

15. Browse the POJO package, Click on Create POJO<>DB table mapping information and Java Data Object (POJO<>DB Table)

16. Here keep default settings

17. You can see all POJO classes and their respective hbm.xml files created.

18. Now create DAO classes as we did for POJO in step 14. Click on Java Data Access Object (DAO) (Hibernate 3 only), click next.

 19. Now you can see DAO classes generated in package 'com.javaxp.dao'. Add import statements wherever required in DAO.

20. Now we willl create a test class i.e to test our project.

package com.javaxp.common;

import java.util.List;
import com.javaxp.dao.BsBooksDAO;
import com.javaxp.pojo.BsBooks;

public class TestHibernate {

      public static void main(String[] args) {
            BsBooksDAO bDao = new BsBooksDAO();
            List allBooks = bDao.findAll();
            BsBooks book = null;
            for(int i=0;i
                  book = allBooks.get(i);
                  System.out.println(book.getBookId()+" "+book.getBookTitle());



Run the program and you can see the output.