What is Hibernate ?
- Hibernate is an open source java based library used to work with relational databases.
- The term Object/Relational Mapping refers to the programming technique for converting data between relational databases and object oriented programming languages such as Java, C# etc.
- It is a very powerful ORM solution build on top of JDBC API.
- It was initiated by Gavin King in 2001.
- It is a persistence framework which is used to store and retrieve data from database. It is a powerful, high performance Object-Relational Persistence and Query service for any Java Application
- Hibernate makes use of persistent objects called POJOs (Plain Old Java Objects).
- Hibernate maps Java classes(POJO) to database tables and from Java data types to SQL data types and relieve the developer from 95% of common data persistence related programming tasks.
- These objects works as a data carrier called DTO (Data Transfer Object). They are used to carry data between different layers.
Advantages of Hibernate :
- Hibernate takes care of mapping Java classes to database tables using annotation or XML files. You don't need to write any code to map it.
- It provides simple APIs for storing and fetching Java objects directly to and from the database.
- If there is change in database or in any table then the only need to change XML file properties. and Java classes which are related to the table.
- No need of SQL knowledge, you just need to work on familiar Java data types.
- It does not require an application server to operate.
- Reduce database access complexity by easy fetching techniques.
- It has easy and simple query for data access.
- It is layers architecture and you can use the components as per your application need.
- Hibernate is database independent and you can use any database of your choice. It supports all major RDBMS like HSQL Database Engine, DB2/NT, MYSQL, PostgreSQL, FrontBase, Oracle, Microsoft SQL Server, Database, Sybase SQL Server, Informix Dynamic Server.
- It also provide caching framework that works with Hibernate. You can use any one in your application to improve the performance of your application.
Hibernate Architecture :
The figure given below represents the architecture of Hibernate :
Hibernate framework uses various existing Java APIs, like JDBC, Java Transaction
API(JTA), and Java Naming and Directory Interface (JNDI). JDBC provides a
rudimentary level of abstraction of functionality common to relational
databases, allowing
almost any database with a JDBC driver to be supported by Hibernate.
JNDI and JTA allow Hibernate to be integrated with J2EE application
servers.
Below are some definitions of the object depicts in
above diagram :
- Persistent Object : These are short-lived, single threaded objects containing persistent state and business function. These can be ordinary JavaBeans/POJOs. They are associated with exactly one Session. Once the Session is closed, they will be detached and free to be used in any application layer (for example, directly as data transfer objects to and from presentation).
- Transient Object : Instances of persistent classes those are not currently associated with a Session. They may have been instantiated by the application and not yet persisted, or they may have been instantiated by a closed Session.
- Configuration (org.hibernate.cfg.Configuration ): The configuration of Hibernate is handled by the instance of org.hibernate.cfg.Configuration . It is the first Hibernate object you create in any Hibernate application and usually created only once during application initialization. It represents a configuration or properties file required by the Hibernate.
Configuration cfg = new Configuration()
- SessionFactory (org.hibernate.SessionFactory ) : Session factory is a thread safe, immutable cache of compiled mappings for a single database. Configuration object is used to create a SessionFactory object. Only single instance of session factory is required for an application, so it is based on a singleton pattern. Typically SessionFactory is require for per database using a separate configuration file. So in case If you are using multiple databases then you would have to create multiple SessionFactory objects. The SessionFactory is heavyweight object so usually it is loaded at the start of the application.
SessionFactory sessions = cfg.buildSessionFactory();
- Session (org.hibernate.Session): The session object provides an interface between the application and data stored in the database. It is a lightweight object and it wraps JDBC connection. It is factory of Transaction, Query and Criteria. It holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier.
Session session = sessions.openSession();
Transaction (org.hibernate.Transaction): It is a single-threaded, short-lived object used by the application to
specify atomic units of work. It is Optional. It provides methods for transaction management.
Query : Query object use Hibernate Query Language(HQL) or SQL to fetch data from the database.
Criteria : Criteria object are used to create and execute object oriented criteria queries to retrieve objects.
really nice blog ... it gave more useful information and helpful blog to everyone... thanks for sharing
ReplyDeletejava j2ee training course contents | java j2ee training institutes in chennai | java j2ee training | java training in chennai velachery