Sunday, April 15, 2012

Using Hibernate for Java - 1

Hibernate is a framework for object-relational mapping with Java language. It facilitates mapping classes, attributes and class relationships between an application class model and its implementation in a relational database. For defining the mappings, XML files or Java anotations can be used.
Hibernate works as a middleware layer between the Java objects and the relational database.

In this article, and in the next one, we will use Hibernate version 3 to ilustrate, through a small example, the utilization of Hibernate in a Java program.

As database system we will be using Oracle 10g.

To start with, we need to crate the database tables. In the article Example of Java Swing Application with Oracle Database - 1 this process is explained.
In this article, we will only be mapping class BOOK.


 Using Netbeans, or other Java IDE, we create a new Java application:


We create a package for the classes corresponding to database tables, and create class Book:







We add the attributes to the class (the attributes' names can be different from the ones used in the table's columns):


And, we add getters and setters for each attribute:




As the value of column ID in table BOOK is automatically assigned by the database, we change the corresponding setter access mode to private:


And, we create a constructor without parameters, for class Book:


Now, we are going to create the mapping file between class Book and the database table BOOK.
In the same folder of class Book, we create a XML file, called Book.hbm.xml, which will contain the mappings:


The file will be constrained by a DTD, but we can add that information later:


We add the following contents to the file:


The syntax used in the file conforms to the DTD in http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd.

Elemento class defines the mapping between the Java class and the corresponding database table.
Elemento id maps an attribute to the table primary key.
Elemento property maps a non-key attribute.

We, now, need to create a configuration file for Hibernate, which will contain the database conection data, the reference to all mapping files, and other configuration information.


This file will be used to configure the object hibernate.connection, for database connection.
It contains, also, information about the mapping files.
All this information could be, in alternative, supplied through code.

Now, we're going to create a class, HibernateUtil, which will supply a singleton instance of class SessionFactory. This instance will be used to create a database session object everytime it is needed. 
We create a new package:




Import Hibernate's relevant classes:


And, we declare, as static, the object sessionfactory instance of SessionFactory, and also its initialization.


The invocation of method configure(), in the instance new Configuration(), will trigger the reading of configuration file hibernate.cfg.xml.


Let us now test the database operations using Hibernate. In the file of the class containing the method main(), we add some imports:


And, in method main(), we add code for, in a transaction aware mode, insert a book.


And then, to list all books:



 In the next article, we will see how to configure and use tables related by foreign key constraints.


No comments:

Post a Comment