Towards Hibernate Application

To develop a Hibernate application first we need to create the user in Database and assign some privileges. The following commands helps you to create the user in Database.
  • Login to the DBServer using administrative username and create the user as follows:
SQL> create user HIBER identified by NATE;
Grant connect,resource to HIBER;
Grant create sequence to HIBER;

  •  Now create a table with primary key.
we can get Hibernate software from www.hibernate.org  Hibernate software is a collection of jar files, the most important jar file is Hibernate.jar. To develop hibernate application we need the following three files. 
  1. Hibernate Configuration file
  2. POJO classes (Plain Old Java Object)
  3. Hibernate Mapping Files (hbm) 

Hibernate Configuration File: 

       Generally every Framework uses the Configuration file, this is the starting point of any framework. Generally Configuration files are .xml files or properties files. In Hibernate we use Hibernate.cfg.xml as the configuration file. It contains the data that is required to communicate with database server. The following figure shows the contents of configuration file.

Hibernate Configuration File
Hibernate Configuration File

POJO Classes:

         Generally in hibernate we represent the database records in the form of objects, this can be done by using java programs, Because of this reason we have to develop a couple of java programs. These program contains instance variables, setXXX() and getXXX() methods. Developing these programs are easy because of this reason they are called as POJO(Plain Old Java Object). The no.of POJO classes are based on the no.of tables present in the database server. Suppose if we have two tables, we need to develop two POJO classes. Let us think we have emp and product tables in our database, then we need to develop two POJO classes.

  • Emp.java
  • Product.java

Note: There is no rule saying that POJO class names and table names should be same.

The POJO class must follow the following rules:

  • It must contain a default constructor.
  • It must be placed inside a package.
  • The POJO class supports properties,setXXX() and getXXX() methods are called as Properties.

HBM Files:

         To work with Hibernate we have to develop Hibernate Mapping Files. These files contains the information about which POJO class is mapped with which table and which property is mapped with which column. The following is the sample POJO class.

Procedure to generate Configuration file using MyEclipse:

The following are the steps to generate configuration file.
Step1: Start MyEclipse IDE pointing to workspace folder.
Step2:Configure MyEclipse IDE to interact with the Database Server.
Step3:Create a java project and add a package to it.
Procedure to generate Configuration file using MyEclipse
Fig: Adding Package to the Project
Step4: Add Hibernate capabilities to the above project. To do this select Project --> MyEclipse in menu bar --> select Hibernate capabilities.
Procedure to generate Configuration file using MyEclipse
Fig: Adding Hibernate capabilities to Project
Step5: The above step displays a dialogue box, choose the required jar files to be added. 
Procedure to generate Configuration file using MyEclipse
Fig: Add required jar files
Step6: Choose new Hibernate Configuration file and click on Next.
Procedure to generate Configuration file using MyEclipse
Fig: Configuration file name
Step7: Choose the DB driver and provide url, username and password click on Next.
Procedure to generate Configuration file using MyEclipse
Fig: Choose the driver
Step8: Choose the package and click on Finish.

Procedure to generate POJO classes and HBM files: 

Step1: Go to MyEclipse DB explorer perspective and establish the connection with Database server.
Step2: Select the DB --> open --> Expand DB --> Expand User(The user you have created in Database) --> Expand Table --> select the required tables --> Right click --> Select Hibernate reverse engineering option. 
Procedure to generate POJO classes and HBM files
Fig: generating POJO classes and HBM files
Hence, as of now we have created Configuration file, POJO classes and HBM files. Now our responsibility is to develop a java program. Let us see that.

                                                                      

    No comments:

    Post a Comment