November 24, 2014
Hot Topics:

An Introduction to Object-Relational Mapping with Hibernate

  • August 5, 2004
  • By Olexiy & Alexander Prokhorenko
  • Send Email »
  • More Articles »

Implementation of object-relational mapping (O/R mapping) is a general need for many projects in software development. Usually, work with the automation process of data storage is very boring, and at manual implementation there is a danger of mistakes and bugs. Also, to add constantly varying requirements, it is necessary for the developer to take into account the complex process of synchronization of all initial code and data storage structures. More than that, taking into account the necessity of portability of application between platforms, it all becomes even more complex and confusing.

Hibernate sounds like the best solution. It allows us to store the data in constant storehouse without serious consequences, thus a choice of such as storehouse, installation, and configuration do not make big difficulties. Hibernate allows us to store objects of any kind; therefore, it is not necessary for an application to know that its data will be kept with Hibernate. Clearly, with help of Hibernate we can not only keep the data in a storehouse, but also update and delete the data.

Preparing the Workspace

To begin, we will need to download the Hibernate distribution from http://www.hibernate.org. We will use version 2.1.4. As a database, we will use MySQL version 4.0.16, which also can be downloaded from http://www.mysql.com. I need to mention that Hibernate also supports not only MySQL but different other open-source and commercial databases; for example, Hypersonic SQL, PostgreSQL, Oracle, DB2, and others.

After you download all necessary packages, you must adjust your environment to include, generally, everything that we need to make it include all downloaded JAR files into our CLASSPATH. It should consist of at least two files: hibernate2.jar from a directory in which you have unpacked Hibernate, and mysql-connector-java-3.0.9-stable-bin.jar (which is JDBC Connector/J to MySQL database server; it could be downloaded from the MySQL Web site). Hibernate requires few more additional libraries that are in a directory, called <hibernate-dir>/lib /, where <hibernate-dir> is a directory with the unpacked Hibernate package. From this directory, we do not need all JAR files, but it is better to use them all now. Before we start work with Hibernate, we shall formulate a problem all over again and we try to solve it with the help of Hibernate.

Formulate a Problem

Certainly, every developer has faced the problem of creating a client servicing system. The general circuit can look as follows: We create an object for Order, we set into it Product objects, which by then becomes Order Items, and then we save Order.

To build a database structure, we will use the following SQL script (or you can just simply type these commands in the MySQL console client):

DROP DATABASE HIBERNATE;
CREATE DATABASE HIBERNATE;
USE HIBERNATE;

CREATE TABLE PRODUCTS (ID VARCHAR(32) NOT NULL PRIMARY KEY,
                       NAME VARCHAR(32) NOT NULL,
                       PRICE DOUBLE NOT NULL,
                       AMOUNT INTEGER NOT NULL);

Apparently, this model of the data is very simple. In the case of a data model for real projects, we first will need to add all tables of other classes, and also to define FOREIGN KEY fields, indexes, additional fields, and other items. However, for our example, such a model will be quite good.

Now, we will result a Java code that will be the Product class. For brevity, we will not give the results for the full getter/setter methods. You can add them easily.

public class Product {
private String id;
private String name;
private double price;
private int amount;

public String getId() {
return this.id;
}

public void setId(String s) {
this.id = s;
}

// default constructor
// and usual getter/setter methods
... ...
... ...
... ...
}
Note: Hibernate works with any kind of Java objects if they support the JavaBeans specification.




Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel