Hibernate is one of the most popular ORM used in Java ecosystem. Hibernate works by mapping database tables to their corresponding entities (Java Classes) and Operations to be performed on database tables as DAO Classses. One of the basic tasks of Hibernate configuration is generating this Java Code. All popular IDEs provide tools to assist in this task. Following are the steps to be performed for Hibernate Code Generation in Eclipse IDE.

1. Install Hibernate Tools

  • Go to Help –> Eclipse Marketplace.
  • Search Jboss Tools.

Hibernate Code Generation

  • From Jboss Tools, install Hibernate Tools.
  • Eclipse would prompt you to restart it for changes to take effect.

2. Hibernate Configuration

  • Go to Windows –> Perspective –> Open Perspective –> Other –> Hibernate Perspective to switch to Hibernate Perspective.
  • Right click on Hibernate Configurations Pane and then click on Add Configuration.

Hibernate Code Generation

  • In Edit Configuration Window, make following changes:
  1. Browse your target project by clicking on Browse button next to Project Selection Box.
  2. Create a New Database Connection by clicking on New button from Database Connections Selection Box.
  3. In New Connection Profile, select Target Database and Click Next.

    Hibernate Code Generation
  4. Click on New Driver Definition icon to add JDBC driver for your database.

    Hibernate Code Generation
  5. Select your Database Name.

    Hibernate Code Generation
  6. Open JAR List tab and Add Jar/Zip of JDBC Driver.

    Hibernate Code Generation
  7. In New JDB Connection Profile Window, verify connection details and Test your connection.

    Hibernate Code Generation
  8. Click Ok and then click Finish.
  9. In Edit Configuration window click on Setup under Configuration File selection box.
  10. Click on Create New from Setup Configuration File dialog box.

    Hibernate Code Generation
  11. In Create Hibernate Configuration File (cfg.xml) window, select Parent folder where generated code will be placed.

    Hibernate Code Generation
  12. In Hibernate Configuration File (cfg.xml) window, click on Get values from Connection link.

    Hibernate Code Generation
  13. Select the recently created Database Connection Profile and click Ok.
  14. Click Finish.

    Hibernate Code Generation
  15. Make sure Type is selected as Annotations (jdk 1.5+).
  16. Click Apply and then click Ok.

    Hibernate Code Generation

3. Code Generation using Hibernate Configuration.

  • Make sure Hibernate Configuration and Database connection inside the configuration is accessible.

Hibernate Code Generation

  • Click on Hibernate Icon on Toolbar and then click on Hibernate Code Generation Configurations.

Hibernate Code Generation

  • Create a New Launch Configuration.

Hibernate Code Generation

  • Select Hibernate Configuration created at step 2 from Console Configuration Dropdown.
  • Browse the Output Directory where you want to store generated code.
  • Make sure Reverse engineer from JDBC connection checkbox is checked.

Hibernate Code Generation

  • In Exporters tab, Check Use Java 5 Syntax and Generate EJB3 annotations checkboxes under General Settings.
  • Check Domain Code (.java) and DAO Code (.java) under Exporters section.
  • Click on Apply and Run.

Hibernate Code Generation

4. Verify Generated Code.

  • In Project Explorer view, verify that entities corresponding to database have been generated.

Hibernate Code Generation

  • An example entity code is given below.

Hibernate Code Generation

  • To remove errors from code, you need to add required Hibernate and JPA jars. You can download them from following link or add them as a POM dependency in maven project.

https://sourceforge.net/projects/hibernate/files/hibernate4/4.3.11.Final/hibernate-release-4.3.11.Final.zip/download

That’s it. You are done with Hibernate Code Generation.

Hibernate Code Generation in Eclipse

Leave a Reply

Your email address will not be published. Required fields are marked *