Hibernate Tools Eclipse Plugin

Filed Under: Hibernate

If you have worked on hibernate projects, you must be familiar with a lot of properties we need for hibernate mapping and configuration file. Without any proper tool that can guide us in looking for correct properties, it will become very hard to correctly configure our application. This is when Hibernate Tools Eclipse Plugin comes handy and a must have plugin for hibernate projects.

Hibernate Tools

Today we will look into installing hibernate tools in Eclipse and how to use it for generating Hibernate mapping and configuration xml files.

Hibernate Tools Eclipse Plugin Installation

Installing Hibernate Tools eclipse plugin requires following steps.

  1. Go to “Eclipse Marketplace” from the Help Menu, as shown in below image.

    Hibernate Tools Plugin Eclipse Marketplace

  2. Use the search option to find the “Hibernate Tools” plugin, hibernate plugins are Eclipse version specific. So make sure you choose the same one as your eclipse. You can find Eclipse version from “About Eclipse” popup page.

    Hibernate Tools Eclipse

  3. Click on the install button, make sure the “Hibernate Tools” check box is selected and follow the instructions to install.

    Install Hibernate Tools Eclipse Plugin

  4. Once the plugin is installed, it will ask to restart the Eclipse. Just restart it and you are ready to use it for hibernate projects.

Generating Hibernate Mapping File using Hibernate Tools Eclipse Plugin

Now that Hibernate Tools Plugin is installed, let’s see how to generate Hibernate Mapping XML file using it.

For my example, I have created a simple Java Bean in one of my example projects.

Customer.java


package com.journaldev.hibernate.model;

public class Customer {

	private int id;
	private String name;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

Now let’s see how we can easily generate Hibernate Mapping file using hibernate tools plugin.

  1. Select the project and go to “New” -> “Other” or you can open this window using keyboard Command+N (Mac), Ctrl+N (Windows). In the popup window, select “Hibernate XML Mapping File” as shown in below image.

    Hibernate Tools - New Hibernate Mapping XML

  2. When you will click Next button, a new popup will come to add Classes, packages etc. Use it to add above Customer class.

    Hibernate Mapping XML Add Class

  3. When you will click Finish button, a new file will get added in the same package where your java bean is present. For our Customer class, file name will be Customer.hbm.xml. Below is the content of the auto generated file.

    Customer.hbm.xml

    
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated May 18, 2014 12:31:51 AM by Hibernate Tools 3.4.0.CR1 -->
    <hibernate-mapping>
    	<class name="com.journaldev.hibernate.model.Customer" table="CUSTOMER">
    		<id name="id" type="int">
    			<column name="ID" />
    			<generator class="assigned" />
    		</id>
    		<property name="name" type="java.lang.String">
    			<column name="NAME" />
    		</property>
    	</class>
    </hibernate-mapping>
    
  4. Note that the hibernate tool points to old DTD location from SourceForge, you should change is to use from hibernate official website.
    
    <!DOCTYPE hibernate-configuration PUBLIC
    		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    		"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
  5. The major benefit of using Hibernate tool is the content assist when we want to change something in the hibernate mapping file, as shown in the below image.

    Hibernate Tools - Mapping File Assist

Generating Hibernate Configuration File using Hibernate Tools Eclipse Plugin

Hibernate Configuration XML file contains properties used to configure Database settings, connection pool settings etc. We can use Hibernate Tools plugin to generate the basic hibernate configuration xml file and then use it’s content assist to add additional properties.

  1. Select the project and go to “New” -> “Other” or you can open this window using keyboard Command+N (Mac), Ctrl+N (Windows). In the popup window, select “Hibernate Configuration File” as shown in below image.

    New Hibernate Configuration File

  2. In the next window, you can select the file location and set the file name, as shown below.

    Hibernate Configuration File Location

  3. The next window lets you set the database properties such as dialect, database user, password and connection URLs.

    Hibernate Configuration File Details

  4. When you click on Finish button, below file will get generated.

    hibernate.cfg.xml

    
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
    		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    	<session-factory>
    		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    		<property name="hibernate.connection.password">pankaj123</property>
    		<property name="hibernate.connection.url">jdbc:mysql://localhost/TestDB</property>
    		<property name="hibernate.connection.username">pankaj</property>
    		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    	</session-factory>
    </hibernate-configuration>
    

    Note that again the DTD is pointing to SourceForge and you should change it to point to Hibernate official DTD location, as specified above.

  5. Again the major benefit of using hibernate tools is the content assist that comes with it, as shown in the below image.

    Hibernate Tools Configuration File Assist

    Because of content assist, we can easily find out the properties that we are looking for, rather than going through a lot of documentations or online resource. This saves a lot of development time and helps us in configuring our application easily.

That’s all for using hibernate tools eclipse plugin, I use it for all my hibernate projects and it saves a lot of time every single day.

Comments

  1. xmen says:

    donot be lazy please try to find answers yourself instead of posting such silly questions here

  2. safdar says:

    Good Document !!
    Is there any tool like hibernate tool for other versions of eclipse including Mars ?

    Thanks

  3. Lova Chittumuri says:

    Sounds Good ………..

  4. vinod says:

    Hi, if we want to install hibernate tools in eclipse .

    we want any permissions for installing hibernate tools

  5. Pranish says:

    I have created database and I want it to map to hibernate I have questions :-
    1. Do I have to map all the database tables including the join table?
    2. How do I declare foreign key in mapping file
    thank you

  6. Rahul Wagh says:

    Nice article on the Hibernate setup.

    For setting hibernate without using Hibernate eclipse tool you can refer to following link.

    This is just alternate if you do not have the Hibernate eclipse tool with you…

    http://techpost360.blogspot.in/2015/07/configuring-hibernate-with-java.html

  7. Danny says:

    thanks a lot, i’ve googled so many sites to know how to install hibernate api in eclipse, but did not find anything working for me, finally i have get this done by your blog.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages