Chitika - Earn Money  |  Bookmark this blog on   Del.icio.us  or  Furl

Saturday, October 01, 2005

Quick Hibernate - Part 1

Hibernate is a powerful, ultra-high performance object/relational persistence and query service for Java. Hibernate gives you the option to code and work with Java and use Hibernate's capabilities to interact with the database. No need to write SQL in your Java code. You work with Java objects and Hibernate does the rest.

In this "Quick Hibernate" article, Satish Talim gets you started with Hibernate. Satish's hands-on approach should have you developing with Hibernate in no time.

About the author:
Satish Talim was interviewed by Java and J2EE Today. This article appeared in java.net

He has several blogs to his credit.
PuneJava
Chess Online Blog
Appliblog

Let's get started:
We need a means of taking information from a SQL database and turn it into Java objects, and vice-versa. The general problem is known as Object/Relational Mapping. Hibernate is a lightweight O/R mapping service for Java and gives you the means for persisting your Java objects to and from an underlying database. Rather than you writing the SQL and converting queries to and from first class objects, Hibernate can take care of all this for you. You only concern yourself with the objects, Hibernate worries about the SQL and making sure things end up in the right tables.

There are many books and articles available on Hibernate and what I want to do here is quickly get you started with Hibernate. I want to take this opportunity to thank Glen Smith for showing me the quick way to get started with Hibernate and this blog is based on that and dedicated to him.

Quick Installation and Setup:

  • Install J2SDK 1.4.2

  • Install Ant

  • Go to http://www.hibernate.org/ and download Hibernate

  • Install Hibernate by unzipping the file hibernate-2.1.6.zip to drive say F:

  • Setup a Project Hierarchy by doing the following. Create folder F:hlogin and then create sub-folders F:hloginsrc and F:hloginlib

  • Copy all the contents of F:hibernate-2.1lib to F:hloginlib

  • Copy hibernate2.jar to F:hloginlib

  • The example classes we are going to create are all going to reside in the folder F:hloginsrccomtalimdemo. Create these sub-folders.

Quick Compile:
To quickly compile your Java code:
At the folder F:hlogin type ant

To run your Hibernate programs:
At the folder F:hlogin type ant run

Resource - build.xml

The Development Process:
There are several ways to approach Hibernate development. Here's the one that is probably the most straightforward to understand:

  • Create your SQL table to hold your persistent objects

  • Create a Java bean that will represent that object in code

  • Create a mapping file that Hibernate knows which bean properties map to which SQL fields

  • Create a properties file so Hibernate knows your JDBC settings to get to the database

  • Start using the Hibernate API

As you get more proficient, there are tools to help auto-generate either Beans from SQL or SQL from Beans (and even plug-ins to make the mapping file for you), but let's do it the long way first, so you don't get distracted.

To be continued...
Technorati Tags: ,
Blogs linking to this article

Valid XHTML 1.0!