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.
- 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.
- In Edit Configuration Window, make following changes:
- Browse your target project by clicking on Browse button next to Project Selection Box.
- Create a New Database Connection by clicking on New button from Database Connections Selection Box.
- In New Connection Profile, select Target Database and Click Next.
- Click on New Driver Definition icon to add JDBC driver for your database.
- Select your Database Name.
- Open JAR List tab and Add Jar/Zip of JDBC Driver.
- In New JDB Connection Profile Window, verify connection details and Test your connection.
- Click Ok and then click Finish.
- In Edit Configuration window click on Setup under Configuration File selection box.
- Click on Create New from Setup Configuration File dialog box.
- In Create Hibernate Configuration File (cfg.xml) window, select Parent folder where generated code will be placed.
- In Hibernate Configuration File (cfg.xml) window, click on Get values from Connection link.
- Select the recently created Database Connection Profile and click Ok.
- Click Finish.
- Make sure Type is selected as Annotations (jdk 1.5+).
- Click Apply and then click Ok.
3. Code Generation using Hibernate Configuration.
- Make sure Hibernate Configuration and Database connection inside the configuration is accessible.
- Click on Hibernate Icon on Toolbar and then click on Hibernate Code Generation Configurations.
- Create a New Launch Configuration.
- 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.
- 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.
4. Verify Generated Code.
- In Project Explorer view, verify that entities corresponding to database have been generated.
- An example entity code is given below.
- 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.