Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Hibernate JBoss Tools Installation in Eclipse + Mapping Generation Example

Generating .hbm.cfg files or creating annotated files to map your classes is always a bit boring and it involves writing a lot of boilerplate code. In Eclipse you can use JBoss Hibernatate Tools, with which you can automatically generate all the files you need to work with Hibernate. In this tutorial we are going to see how to install Hibernate Tools in Eclipse and explore how you can generate mapping files automatically.

So these are the tools we are going to use on a Windows 7 platform:

  • JDK 1.7
  • Maven 3.0.5
  • Hibernate 4.2.3.Final
  • MySQL JDBC driver 5.1.9
  • Eclipse 4.3 Kepler

And this is the project I am going to use as the basis for the example : HibernateMySQLExample.zip. Download it and open it with Eclipse IDE.

JBoss Hibernate Tools Installation

In this example I’m using the brand new Eclipse IDE 4.3 Kepler. The same steps should apply basically on all versions of Eclipse.

1. Go to JBoss Hibernate Tools Web Site

Go to http://www.jboss.org/tools/download. And in the Downloads section click on the link that suits your IDE :

jboss-page

In the next window copy the repository URL. For my version of Eclipse that would be: http://download.jboss.org/jbosstools/updates/development/kepler/

link-repository

2. Install to Eclipse IDE

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

install-new-software

In the “Work with:” text field paste the repository URL and click Add.

add-resource

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

add-repository

After that, type “Hibernate” in the “type filter text” text field. It will bring up just what you need to work with Hibernate and not the full stack of JBoss Tools. If you want you can download them all but it will take much longer and not all of them are necessary for Hibernate:

hibernate-filter

Click Next. 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:

warning

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 :

perspective

And check weather Hibernate Perspective exists:

hibernate-perspective

Hibernate Code Generation

1. Hibernate configuration

Now open Hibernate Perspective and select “Add configuration”:

hibernate-perspective-windows

In the next Window:

add-config

  • In the “Project” text field click “Browse” and navigate to the Project you want to use.
  • In the “Configuration files” text field click Setup and choose “Use existing…”:

set-config-file

And the browse to the hibernate.cfg.xml of your project:

use-existing-config

  • In “Database Connections” you can click “New” if you want to create your own connection.

This is the hibernate.cfg.xml file we used to configure the connection:

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.bytecode.use_reflection_optimizer">false</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password"></property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tutorials</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="show_sql">true</property>
		<mapping resource="com/javacodegeeks/Student.hbm.xml"></mapping>
	</session-factory>
</hibernate-configuration>

After that you ou can see all the databases and the tables you’ve created:

database-explorer

2. Code Generation

While in Hibernate Perspective click the “Hibernate Run” button and select “Hibernate Code Generation Configurations”:

code-gen-config

In the next window click on New Configuration:

new-configuration

In the “Output Direcotory” choose the folder you want Eclipse to put the generated files into. I’ve created a hibernateconfig folder inside the folder of the project. Make sure “Reverse Engineering from JDBC Connection” is checked. The click “Apply” and “Run”:

manage-config

Now, if you go back to the Package Exlorer you will see that Eclipse has generated mapping files and the corresponding classes for all the tables in the database:

generated-files

This was an example on Hibernate JBoss Tools Installation in Eclipse and Mapping Generation. Download the completed Eclipse project of this example: HibernateMySQLExample.zip

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books