Sunday, June 14, 2015

Hibernate Class generation



This tutorial explain how to perform hibernate reverse engineering using eclipse.

First of all before we start you need to install Hibernate Plugin in Eclipse

Goto this link http://download.jboss.org/jbosstools/updates/development/kepler/

1). Now Open Eclipse IDE and go to Help -> Install New Software :


2). copy and paste URL in Work with Textbox as shown in below image and click Add.


3). Then fill the form as shown in the picture below (You can put any name you like):



4). You can search hibernate Tool in typing text field. Then check the appropriate check boxes and then click the next button as shown in the picture below.

Install Hibernate tool

Accept the license and wait for the installion to finish. At some point you migh get a warning (very common when installing third party software on Eclipse). Just click OK and the installation will be continued:

When the installation is completed you will be asked to restart the IDE. When the IDE is up again you will notice the new Hibernate Perspective. If it doesn’t show up click the button in the left of the Java Perspective button :


And check weather Hibernate Perspective exists:



Create new project with name HibGenerator 

a). Generating the xml mapping classes from the database :

Here create a new project with name HibGenerator and package com.gen.main.




Here we require three jar files as shown in above image.

1.hibernate3.jar
2.javax.persistence.jar
3.mysql-connector-java-5.1.21-bin.jar /
Now open hibernate perspective Goto Window > Open Perspective > Other .. as shown in below image.
open perspective

Select Hibernate.

select hibernate

Press right click on src folder and create new hibernate.cfg.xml file.

Hibernate Config file

Conf File Properties

properties of cfg file



Press Next and then press Finish button.

Press right click on project folder and create new file in project folderhibernate.reveng.xml as shown in below image.

Reverse Engi Files

just press next from reveng file


reverse prop


Now select top most small icon and select Hibernate Code Generation Configuration…as shown in below image.

select top small icon

code generate config

final config prop

then it generate Claasses with hibernate xml mappings

b). Generating the annotated classes from the database :

Now choose the ‘hibernate’ perspective and you will see a new small icon in your toolbar Click on it and choose ‘Hibernate Code Generation Configurations …

- You need hibernate-common-annotations.jar to generate annotations







then it generate Claasses with annotations.