Feeds

Hibernate Object Relational Mapping

First in a two-part series by Java guru John Hunt

  • alert
  • submit to reddit

Build a business case: developing custom apps

Installing and Configuring Hibernate

Whenever you start with a new framework one of the issues can be getting the thing up and running in the first place. We will therefore step through setting up Hibernate using MySQL. There is a wide range of database that could be used, however MySQL is freely available and illustrates what would be done if we were to use an Oracle, DB2, Postgres or SQLServer database.

1. Download the Hibernate distribution file. First, you need to down load the current Hibernate distribution file. You can obtain this from the Hibernate home page - look for the download link on the left hand side. Once you have selected this, you will be presented with a number of different options to download. Select the current production release, as this will be the most stable version to work with. For example:

Package Version Release dated Category
Hibernate Core 3.0.5 353,665 Production

Then select to download. This will take you to a page from which you can download the current Hibernate release. At the time of writing this means downloading the hibernate-3.0.5.zip file.

2. Extract the contents of the ZIP file You can now extract he contents of the Hibernate distribution ZIP file using whatever unzip tool you use (for example WinZip see http://www.winzip.com). Place the contents of the Hibernate ZIP file somewhere appropriate (personally I put all these downloads into a directory called javalibs so that I can easily find them).

3. Install in the Development directory

You can now copy the various JAR files required by hibernate into your working directory (personally I store all jars I use for any particular development project within a lib directory of my working directory structure). The jars you will need include the hibernate3.jar file that contains the actual hibernate specific classes. Various other jar files are also needed to support hibernate. These include those illustrated in Figure 1.

Hibernate Jar files working directory

4. Obtaining and Install MYSQL driver

I am using MySQL 4.1.14 in this tutorial along with a JDBC driver for MySQL. MySQL can be obtained from http://www.mysql.com/. On this site, select 'downloads' and then the appropriate version for your operating system. For example, for Windows, scroll down to the windows section and choose

Windows (x86) 4.1.14 37.0M

Now install the MySQL database. You will find that you have downloaded a ZIP file that contains the MySQL setup.exe program. Extract this and run the setup.exe. This will take you through the installation process. Once you have done that, open a command prompt in the bin directory of your MySQL installation and type in mysql. This should startup the MySQL console. If this works then you have successfully installed MySQL.

Next, you need to download a JDBC driver for MySQL. The MySQL driver, MySQL Connector/J, is available from the MySQL site. Go to the URL http://www.mysql.com/products/connector-j/index.html and click on the link for downloading version 3.1:

MySQL Connector/J 3.1 is the production-ready version of the driver, and is available under the GPL (recommended).

This will download a zip file containing the JAR with the JDBC driver. Extract the JAR, called mysql-connector-java-3.1.10-bin.jar, and add it to your lib directory within your development directory.

You have now installed MySQL and made the MySQL JDBC driver available for later use.

5. Setting up MySQL

Next we need to create a database, with some tables and a user that will be plugged into hibernate in the next step. We will create a database called hib, that can be accessed by hibuser using the password hib123. This is done by first starting the MySQL console as the root user. This is done by issuing:

> mysql –u root

Once MySQL is started, you can then issue the following commands:

>GRANT ALL on hib.* to 'hibuser'@'localhost' IDENTIFIED by 'hib123'; >CREATE DATABASE hib;

If this is successful, quit from the MySQL console. This is illustrated in the following diagram:

MySQL console screenshot

Further statements will be SQL and will be issued as a standard user. Next restart the MySQL console (but without the –u root option). Once we have started the console we need to tell MySQL which database we want to work with (so that we don’t have to pre-fix everything with hib.). To do this we say:

> USE hib;

We can now define the table that will be used by hibernate to store our persistent objects. We will call this table books. It will have an id (which will be auto incremented), a title, author and price. The id will be the primary key and the table type will be MyISAM (which is the default MySQL table type). This is done by issuing the following CREATE statement:

> CREATE TABLE books (id int(11) NOT NULL auto_increment, title varchar(250) default NULL, author varchar(250) default NULL, price char(6) default NULL, PRIMARY KEY (id)) TYPE=MyISAM;

This is also illustrated in the following screen dump:

MySQL screendump 2

6. Link hibernate to MySQL

We now need to tell Hibernate about our MySQL database. We can do this using the hibernate.cfg.xml file. You will find this in the hibernate3 distribution directories in the etc directory. Copy the hibernate.cfg.xml file to your classes directory of your development directory (this is so that Java can pick it up form the class path). You can now safely edit the copy of the hibernate configuration file in your development directory.

If you examine this file you will find that it lists numerous different (database) platforms that Hibernate can work with. As we are working with MySQL, find the section that lists MySQL details.

You will need to uncomment this section (remove the “#” at the start of each line). You will also need to specify the MySQL database to use (hib), and the user to connect to MySQL as (in this case hibuser with a password of hib123). I must also specify the JDBC driver to be used (in this case the com.mysql.jdbc.Driver).

This configuration information can be defined either in a hibernate.properties file or in the hibernate.cfg.xml file. We will use the XML format which is more in keeping with current trends. Note that I have placed this file again the class subdirectory of my development directory. This is illustrated below:

We have now configured MySQL and hibernate to work together. The next thing to think about is creating our Hibernate application. We will consider the steps to be performed when creating a Hibernate application in the next article. ®

Part 2 is here.

Gartner critical capabilities for enterprise endpoint backup

More from The Register

next story
Why has the web gone to hell? Market chaos and HUMAN NATURE
Tim Berners-Lee isn't happy, but we should be
Microsoft boots 1,500 dodgy apps from the Windows Store
DEVELOPERS! DEVELOPERS! DEVELOPERS! Naughty, misleading developers!
'Stop dissing Google or quit': OK, I quit, says Code Club co-founder
And now a message from our sponsors: 'STFU or else'
Apple promises to lift Curse of the Drained iPhone 5 Battery
Have you tried turning it off and...? Never mind, here's a replacement
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
Scratched PC-dispatch patch patched, hatched in batch rematch
Windows security update fixed after triggering blue screens (and screams) of death
This is how I set about making a fortune with my own startup
Would you leave your well-paid job to chase your dream?
prev story

Whitepapers

Top 10 endpoint backup mistakes
Avoid the ten endpoint backup mistakes to ensure that your critical corporate data is protected and end user productivity is improved.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Backing up distributed data
Eliminating the redundant use of bandwidth and storage capacity and application consolidation in the modern data center.
The essential guide to IT transformation
ServiceNow discusses three IT transformations that can help CIOs automate IT services to transform IT and the enterprise
Next gen security for virtualised datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.