gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




Vote for the Developer.com Product of the Year Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
The Year of Living the OpenSocial    November 14, 2008
Firefox Fixes New and Older Versions    November 13, 2008
Can Apache Maven Make It by Going Commercial?    November 12, 2008
Novell Goes After Red Hat Linux Users    November 11, 2008
Free Tech Newsletter -

An Introduction to Object-Relational Mapping with Hibernate
By Olexiy & Alexander Prokhorenko

Go to page: 1  2  3  Next  

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.

Go to page: 1  2  3  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Data & Java Archives






internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers