In this article, we are going to discuss the Introduction to Hibernate framework.
Before proceeding, we need to understand concepts of Core Java, SQL as well as the working of JDBC. Let’s understand Object Relational Mapping(ORM).
What is ORM(Object Relational Mapping)?
Object-Relational Mapping (ORM) technologies are responsible for saving/reading/editing/deleting the objects from our application to the relational database tables. ORM makes our Java application more flexible by protecting developers from working with SQL Languages. Each relational database has its own standards of SQL language.
Why it is required?
- It shields developers from SQL
- Productivity improves: (Less Java code to write, High-level object-oriented API, No SQL to write)
- Performance improves: (Sophisticated caching, Lazy and Eager loading)
- Maintainability improves: (A lot less code to write)
- Portability improves: (ORM framework generates database-specific SQL)
It is an object-relational mapping (ORM) tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database. Also handles object-relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions.
Primary feature is mapping from Java classes to database tables and mapping from Java data types to SQL data types. Also provides data query and retrieval facilities. It generates SQL calls and relieves the developer from the manual handling and object conversion of the result set.
Hibernate is an ORM framework with a layered architecture that helps the user to operate without having to know the underlying APIs. It also makes use of the database and configuration data to provide persistence services to the application.
There are basically 3 layers in this architecture:-
- Application Layer
- Hibernate Core Layer
- Database Layer
Core Components of Hibernate
Let’s have a brief description of each of the class objects involved in this Architecture.
- org.hibernate.cfg package contains the Configuration class.
- Configuration cfg = new Configuration() used to activate the Framework.
- It reads both cfg file and mapping files cfg.configure();
- Configuration object created once, at the time of initialization.
- org.hibernate.sessionFactory package contains the SessionFactory Interface.
- It is immutable and thread-safe in nature.
- SessionFactory factory = cfg.buildSessionFactory() used while creation of SessionFactory object
- From cfg object it takes the JDBC information and create a JDBC Connection.
- org.hibernate.session package contains the Session interface
- Session object created based upon SessionFactory object i.e. factory.
- It is a lightweight object and not thread-safe.
- Session object used to execute CRUD operations.
- Session session = factory.buildSession() used while the creation of Session object
- org.hibernate.transaction package contains the Transaction interface
- Transaction object used whenever we perform any operation and based upon that operation there is some change in the database.
- It is a short-lived single-threaded object.
- Transaction tx = session.beginTransaction();
tx.commit(); used while creation of Transaction object.
- org.hibernate.query package contains the Query interface.
- The session objects are used to create query objects.
- Query objects use Hibernate Query Language (HQL) to get data from the database.
- Query query = session.createQuery() used while initialisation of Query object.
- org.hibernate.criteria package contains the Criteria interface.
- The session objects are used to create Criteria Objects.
- Criteria criteria = session.createCriteria() used while initialisation of Criteria object.
I hope you liked the blog and was able to understand the brief introduction. Stay tuned for the next part related to annotations and more.
To know more about Hibernate, you can visit this link.