CSS Drop Down Menu

Sunday, 26 January 2014

Hibernate : Core Concept

Note:-

This tutorial expects the user have knowledge of both Java and SQL. If you have a limited knowledge of JAVA or SQL, it is advised that

you start with a good introduction to that technology prior to attempting to learn Hibernate.

Hibernate Framework

Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source,

lightweight, ORM (Object Relational Mapping) tool. An ORM tool simplifies the data creation, data manipulation and data access. It is a

programming technique that maps the object to the data stored in the database.
Java Application ---- object----ORM----Database


The ORM tool internally uses the JDBC API to interact with the database.

Advantages of Hibernate Framework:-
There are many advantages of Hibernate Framework. They are as follows:

1)Opensource and Lightweight: Hibernate framework is opensource under the LGPL license. The open source GNU
LGPL
(Lesser General Public License) is sufficiently flexible to allow the use of Hibernate in both open source and commercial projects.

2) Fast performance: The performance of hibernate framework is fast because cache is internally used in hibernate framework.
There are two types of cache in hibernate framework first level cache and second level cache. First level cache is
enabled bydefault.

3) Database Independent query: HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database
independent queries. So you don't need to write database specific queries. Before Hibernate, If database is changed for the project,
we need to change the SQL query as well that leads to the maintenance problem.

4) Automatic table creation: Hibernate framework provides the facility to create the tables of the database automatically. So there is
no need to create tables in the database manually.

5) Simplifies complex join: To fetch data form multiple tables is easy in hibernate framework.

6) Provides query statistics and
database status:
Hibernate supports Query cache and provide statistics about query and database status.

Hibernate Architecture:-

The Hibernate architecture includes many objects persistent object, session factory, transaction factory, connection factory, session,
transaction etc.

There are 4 layers in hibernate architecutre java application layer, hibernate framework layer, backhand api layer and database layer.
The high level architecture of Hibernate with mapping file and configuration file.

Hibernate framework uses many objects session factory, session, transaction etc. alongwith existing Java API such as JDBC
(Java Database Connectivity), JTA (Java Transaction API) and JNDI (Java Naming Directory Interface).

Core Object Of Hibernate:-

1-SessionFactory

2-Session

3-Transaction

4-ConnectionProvider

5-TransactionFactory

SessionFactory

The SessionFactory is a factory of session and client of ConnectionProvider. It holds second level cache.(optional) of data

. The org.hibernate.SessionFactory interface provides factory method to get the object of Session. You configure Hibernate's

SessionFactory. SessionFactory is a global factory responsible for a particular database. If you have several databases, for easier

startup you should use several configurations in several configuration files.

Session

The session object provides an interface between the application and data stored in the database. It is a short-lived object and wraps

the JDBC connection. It is factory of Transaction, Query and Criteria. It holds a first-level cache (mandatory) of data. The

org.hibernate.Session interface provides methods to insert, update and delete the object. It also provides factory methods for

Transaction, Query and Criteria.

Transaction

The transaction object specifies the atomic unit of work. It is optional. The org.hibernate.Transaction interface provides methods for

transaction management.

ConnectionProvider

It is a factory of JDBC connections. It abstracts the application from DriverManager or DataSource. It is optional.

TransactionFactory

It is a factory of Transaction. It is optional.

Steps to create A sample Example Of Hibernate (Without Using Annotation):-

Software requirement:-

1-Jdk 1.6 2-Eclipse Any Updated Version(I have Used here Eclipse Helios 3.6) 3-Hibernate Library 4-MySql DataBase

Step-1:-

First Download the hibernate jar from http://sourceforge.net/projects/hibernate/files/hibernate3/ Set it to Java build classpath (If You

Are Using the Eclipse.):-as Sample Project->right click->Properties->Java Bulid Path->Libraries->Add External JARs->ok.

Steps- 2:-

Now Create Entity Class (The Class which contains data,meta data which is to stored in to database Here )Here UserDetails.java which Is given below:-

Step 3:-

Now Create UserDetails.hbm.xml file where we configured data ,metadata attributes name of Table name where our data has to be stored in database. As given belows. This file should be in classpath or in side the src folder:-

Step 4:-

Now Create An Schema in MySql database(since here I am using MySql database), Here I have created hibernatesample schema.

Step 5:-

Now Create a Configuration File hibernate.cfg.xml, where I have configured database configuration and mapping file UserDetails.hbm.xml. which is given below:-

Step 6:-

Now create the main class For Hibernate like given below:-
Now Run The main Class of Hibernate.java as here is HibernateTest.java Then Check the schema Definatly You will find Datas stored in Schema configured in .cfg file.

Steps to create A sample Example Of Hibernate (Using Annotation):-

Software requirement:-

1-Jdk 1.6 2-Eclipse Any Updated Version(I have Used here Eclipse Helios 3.6) 3-Hibernate Library 4-MySql DataBase

Step-1:-

First Download the hibernate jar from http://sourceforge.net/projects/hibernate/files/hibernate3/ Set it to Java build classpath (If You

Are Using the Eclipse.):-as Sample Project->right click->Properties->Java Bulid Path->Libraries->Add External JARs->ok.

Steps- 2:-

Now Create Entity Class (The Class which contains data,meta data which is to stored in to database Here )Here UserDetails.java which Is given below:-

Step 3:-

Now Create An Schema in MySql database, Here I have created hibernatesample schema:-

Step 4:-

Now Create a Configuration File hibernate.cfg.xml, where I have configured database configuration and mapping file UserDetails.java which is given below. This file should be in classpath or in side the src folder:--

Step 5:-

Now create the main class For Hibernate like given below:-
Now Run The main Class of Hibernate.java as here is HibernateTest.java Then Check the schema Definatly You will find Datas stored in Schema configured in .cfg file.

0 comments:

Post a Comment