Reverse Engineering Hibernate objects using JBoss eclipse plugin

1 1 1 1 1 1 1 1 1 1 Rating 0.00 (0 Votes)

Writing hibernate class and xmls can be a pain, especially, if you have tons of tables in your database. One way to alleviate the pain is to use a reverse engineering tool, which would generate the xml and java files for hibernate by connecting to database. This article walks through the steps from installing the JBoss Tools Eclipse plugin to generate hibernate java and xml files.


•    MySQL and Eclipse are installed
•    MySQL has a database with at least one table
•    You have some basic knowledge about hibernate or any other ORM tool


Installing JBoss Eclipse Tools from marketplace

Open Eclipse and follow the menu, Help -> Eclipse Marketplace..

Search for 'JBoss' or 'Hibernate' and click install on the Jboss Tools

Eclipse Market Place

Un-select all options except ‘Hibernate Tools’ as shown in the below screenshot

JBoss tools install for Eclipse

Click ‘Confirm’ and then ‘Finish’. Accept the license agreement.

Eclipse will prompt to restart

Creating the hibernate configuration (hibernate.cfg.xml)

Once Eclipse has restarted, Open hibernate perspective. Windows -> Open Perspective -> Others

Open Hibernate Perspective

The perspective will be empty. Click on the ‘Add Configuration’ to start the configuration process, as shown below

Hibernate Configurations

A window ‘Edit Configuration’ will show up, as per the below screen shot. Select the Project by clicking Browse.

Hibernate Edit Configuration

Next, click ‘Setup’ from Configuration File section. This will open another windows as shown below.

Create hibernate.cfg.xml

Using this, we will be creating the hibernate.cfg.xml which has the information like the database dialect, database URL, drivers, database type, db user id and db password. Once you click ‘Finish’, this will generate the hibernate.cfg.xml. Then click the ‘OK’ in the ‘Edit Configuration’ window should be open.

Rebuild Hibernate Config

Next, go to the Hibernate Configuration in the Hibernate perspective. Right click the new configuration which you added now and click ‘Rebuild Configuration’. You should now check the section under ‘Database’ in the hibernate configuration.  You should see the tables from your database. If the tables are not showing and it shows an error, you should not proceed further and resolve this issue first. Below are some possible reasons for the error

1.    Database drivers are not in classpath – Resolution: Put the driver jar file in the WEB-INF/lib folder to resolve this.
2.    Database service is not started – Resolution: Start your database and try connecting again.
3.    One or more information entered in the hibernate.cfg.xml is incorrect. Verify all the information entered in the hibernate.cfg.xml, anything which is incorrect.
If you resolved the issue as explained above, you might want to ‘Rebuild Configuration’ after resolving the issue.

Once you can see the Database tables, you can proceed to the reverse engineering steps provided below.

Create a new Hibernate Reverse Engineering file

From the Hibernate Perspective click ‘File’ -> ‘New’ -> ‘Hibernate Reverse Engineering File (reveng.xml)’

Create the reverse engineering file
In the Wizard that opens, select the project and click ‘Next’

Reverse Engineering Step 2

Select the ‘Console Configuration’ from the drop down in the next step and click the ‘Refresh’ button. The Database Schema section will show your database and tables. Select all the tables and click include. Click the ‘Finish’ button to generate hibernate.reveng.xml file.

If you are unable to generate the hibernate objects, check that you have included all individual tables instead of the whole database and re-generate the reveng.xml

Once you have the reverse engineering file generated, you need to do Run -> Hibernate Code Generation -> Hibernate Code Generation Configuration…

Hibernate Code Generation Configuration

In the window that opens, create a new configuration and enter details as per the below screenshot and click ‘Run’.

Hibernate code generation main tab

Hibernate code generation exporters

Go back to Java EE perspective and you will see the hibernate xmls and java files generated in the package specified in the Main tab of the configuration before clicking Run.

You might want to modify some of the content in the xml or java files generated as per your needs. However, remember that, if you have to regenerated the hibernate objects, your manual changes might get lost.

Add Mapping to hibernate.cfg.xml
One last step you would need to perform, is to add the mapping of the bean in the hibernate.cfg.xml. Below shows a screenshot of adding one mapping.

That completes the steps to reverse engineer hibernate objects using JBoss Tools in Eclipse.

Joomla SEO by MijoSEF