Advertisment

Using 'Hibernate Tools' with Eclipse

author-image
PCQ Bureau
New Update

Hibernate is perhaps the most widely accepted framework for ORM (Object

Relational Mapping) and object persistence. However you would always prefer a

wizard based tool for common tasks such as creating configuration files,

generating or editing mappings, or even reverse engineering your applications'

domain classes from hibernate mappings. In this article, we'll configure 'Hibernate

Tools' and see how to reverse engineer your POJOs (Plain Old Java Objects)

using this plug-in for Eclipse. You can download and install the latest version

of this (3.2 Beta 2 at the time of writing this piece) from www.hibernate.org/30.html.

We took a MySQL database for our exercise. If you use some other database, then

you'll have to use different configuration properties.

Advertisment
Direct Hit!
Applies To:

Java/Eclipse developers



USP: Learn how to quickly generate class files from Hibernate
mappings



Primary Link: www.hibernate.org




Google Keywords: Hibernate, persistence

Generate Configuration file



Once installed, you need to first switch to the Hibernate perspective (in your
Eclipse IDE) from Window>Open Perspective>Hibernate Console. From here,

launch the 'Create Hibernate Configuration' wizard as New>Hibernate

Configuration File. Choose a location for the file in your application directory

and proceed.

In the next page you need to provide Database Dialect (MySQL in our case),

the driver class (org.gjt.mm.mysql. Driver or com.jdbc.mysql.Driver), the

database username and password. Other fields such as Default Schema, Default

Catalog, can be left empty (optional). Also check the option for 'Create

Console Configuration' and click on Finish to generate the .cfg.xml

configuration file.

Advertisment
Using the plug-in, your database structure

and the corresponding XML file (that defines all mappings), are both

displayed in one perspective; making it easier to analyze and change

configuration properties

Console configuration



Create the Console configuration by New>Hibernate Console Configuration
option. In the wizard you need to provide values for a name of the console, and

also add the JAR file that represents the connector you are using for your

application. In our case this was the mysql-connector-java-bin-3.1.12-bin.jar.

Click on Finish to complete the task. You can then view your database in the 'Hibernate

Configurations' tab of the Project Explorer window.

Reverse Engineering classes



To create POJOs, go to Run>Hibernate Code Generation. Then populate a value
for the o/p directory and provide an optional package name. The Exporters tab

specifies the type of code that should be generated. Each selection represents

an 'Exporter,' responsible for generating the code. Check the option for 'Use

Java 5 syntax' and click on 'Run.' The classes will be generated in the

specified output directory.

Advertisment
Using the plug-in, your database structure

and the corresponding XML file (that defines all mappings), are both

displayed in one perspective; making it easier to analyze and change

configuration properties
Choose parameters for Exporters and for

specifying whether to use POJOs, Annotations and the Exporter

Exporters tab



The Exporters tab contains seven options categorized in two groups. The first
group contains two options-Use Java 5 syntax and EJB 3 Annotations. As the

name suggests, the first option creates POJOs using JDK 1.5 syntax constructs

only, while the second option creates classes with EJB 3 syntax using

annotations. The second group has five options: Domain Code (.java) that creates

POJOs for all the persistent entities and components; Hibernate XML Mappings

that generates Hibernate mappings for each entity in the database; DAO code

(.java) option that creates a set of DAO for the entities; Hibernate XML

Configuration generates hibernate.cfg. xml file that keeps Mappings updated;

Schema Definition (.html) that generates HTML docs which document the database

schema; and JBoss Seam Skeleton App (misc) that creates the Seam skeleton

app including annotated POJOs, Seam controller Beans, a JSP presentation layer,

and a Build.xml file. Since the option generates the entire application, you

should configure the root dir of your project as the target output dir.

The Reverse Engineered POJO contains all constructors and Getter/Setter methods for their attributes, eliminating the need to hand code them
Advertisment