Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Retrive records from Database using Hibernate

As of now we have seen how to store data into the Database using Hibernate Application. Now let us see, how to retrieve the data from the Database. To retrieve the data from the database, Hibernate uses two methods, they are                                        

Storing data into DataBase using Hibernate

To store data into Database using Hibernate Framework we use the following three methods.
  1. save
  2. persist
  3. save or update
save() : This method is used to store the data in Database. When we use save() method, it generates a new identifier and INSERT record into the database using INSERT query. It generates a new identifier and returns the Serializable identifier back. The following is the syntax of save() method.
  • serializable save(Object object)
persist() : This method is also used for storing the data in the DataBase.This method is same as the save(). But this method does not returns a value. The following is the syntax of persist() method.
  • void persist(Object object)
saveorupdate() : This method also used to store the data into the database. When we use this method it checks for the record, if the record exists it just updates the record. If the record is not exist then it inserts a new record.

The following is the Hibernate application which inserts the data into Database. Here we are using save().

Program to Insert values into database using Hibernate
Program to Insert values into database using Hibernate

                                              

Analyzing Hibernate Application

We have seen how to write a Hibernate Application, which inserts the values into DataBase Server. Now let us know what each step is doing in the background. 


Step1: When we create the Configuration object a plain java object will be created. This configuration object stores all the configuration details. Initially when we create the object no data is available in that.

First Hibernate Application

First let us know, what are the important interfaces ans classes. The following are the most important interfaces and classes. 
Important interfaces and classes in Hibernate
Fig: Important interfaces and classes in Hibernate

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;

Working with MyEclipse IDE

IDE (Integrated Development Environment):
There are so many IDE's available in market, some of them are:
  • Eclipse 
  • MyEclipse
  • Net Beans
  • RAD    etc,.,,,

Introduction to Hibernate

Hibernate Logo
Hibernate is a framework which is used to interact with Database Servers. We use Hibernate framework as an alternative to JDBC. Apart from Hibernate there are many tools available in the market which are responsible to interact with Database Servers. The following are some of them:
  • JDO
  • JPA
  • Toplink
  • SDO    etc...,