Feeds

Hibernate Object Relational Mapping

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

  • alert
  • submit to reddit

Business security measures using SSL

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.

New hybrid storage solutions

More from The Register

next story
'Windows 9' LEAK: Microsoft's playing catchup with Linux
Multiple desktops and live tiles in restored Start button star in new vids
Not appy with your Chromebook? Well now it can run Android apps
Google offers beta of tricky OS-inside-OS tech
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
Greater dev access to iOS 8 will put us AT RISK from HACKERS
Knocking holes in Apple's walled garden could backfire, says securo-chap
NHS grows a NoSQL backbone and rips out its Oracle Spine
Open source? In the government? Ha ha! What, wait ...?
Google extends app refund window to two hours
You now have 120 minutes to finish that game instead of 15
Intel: Hey, enterprises, drop everything and DO HADOOP
Big Data analytics projected to run on more servers than any other app
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
prev story

Whitepapers

Providing a secure and efficient Helpdesk
A single remote control platform for user support is be key to providing an efficient helpdesk. Retain full control over the way in which screen and keystroke data is transmitted.
WIN a very cool portable ZX Spectrum
Win a one-off portable Spectrum built by legendary hardware hacker Ben Heck
Storage capacity and performance optimization at Mizuno USA
Mizuno USA turn to Tegile storage technology to solve both their SAN and backup issues.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Security and trust: The backbone of doing business over the internet
Explores the current state of website security and the contributions Symantec is making to help organizations protect critical data and build trust with customers.