Before getting started with the Hibernate framework, let's familiarize ourselves with a few basic concepts of the hibernate framework, its architecture, its benefits and advantages over JDBC, etc.
Let's start with what is ORM?
What is ORM?
Object-relational mapping or ORM is the programming technique to map application domain model objects to the relational database tables. Hibernate is a Java-based ORM tool that provides a framework for mapping application domain objects to relational database tables and vice versa.
Read more about ORM and why to use ORM at https://www.tutorialspoint.com/hibernate/orm_overview.htm.
Read more about ORM and why to use ORM at https://www.tutorialspoint.com/hibernate/orm_overview.htm.
What is Hibernate Framework?
Hibernate is a java based ORM tool that provides a framework for mapping application domain objects to the relational database tables and vice versa.
Example: Below diagram shows an Object Relational Mapping between the Student Java class and the students table in the database.
What is the Java Persistence API (JPA)?
Java Persistence API (JPA) provides a specification for managing the relational data in applications.JPA specifications are defined with annotations in a jakarta.persistence package. Using JPA annotation helps us in writing implementation-independent code.
Learn complete JPA at JPA Tutorial - Java Persistence API
How does Hibernate relate to JDBC?
Hibernate uses JDBC for all database communications. Hibernate uses JDBC to interact with the database.
Hibernate acts as an additional layer on top of JDBC and enables you to implement a database-independent persistence layer:
Hibernate acts as an additional layer on top of JDBC and enables you to implement a database-independent persistence layer:
Architecture
Hibernate, as an ORM solution, effectively "sits between" the Java application data access layer and the Relational Database, as can be seen in the diagram above.The Java application makes use of the Hibernate APIs to load, store, query, etc its domain data. Here we will introduce the essential Hibernate APIs.
As a JPA provider, Hibernate implements the Java Persistence API specifications and the association between JPA interfaces and Hibernate-specific implementations can be visualized in the following diagram:
Know more about JPA architecture at https://www.javaguides.net/2018/12/jpa-architecture.html.
SessionFactory (org.hibernate.SessionFactory)
SessionFactory is an interface that provides methods to create and manage Hibernate Session objects. It is a thread-safe and immutable object that provides a cache of immutable objects, which is used to create new Session objects.Session (org.hibernate.Session)
The Session is an interface that represents a single unit of work with the database. It is responsible for managing the persistence of objects and providing transactional support for the persistence operations. The Session interface provides methods for querying, saving, updating, and deleting objects in the database.
Behind the scenes, the Hibernate Session wraps a JDBC java.sql.Connection and acts as a factory for org.hibernate.Transaction instances. It maintains a generally "repeatable read" persistence context (first-level cache) of the application domain model.
Transaction (org.hibernate.Transaction)
The Transaction is an interface that represents a database transaction. It
provides methods to control the transaction, such as commit(),
rollback(), and begin(). The Transaction interface also provides methods
to set the transaction isolation level and check the status of the
transaction.
A single-threaded, short-lived object is used by the application to demarcate individual physical transaction boundaries. EntityTransaction is the JPA equivalent and both act as an abstraction API to isolate the application from the underlying transaction system in use (JDBC or JTA).What are the important benefits of using the Hibernate Framework?
Some of the important benefits of using hibernate framework are:- Hibernate eliminates all the boilerplate code that comes with JDBC and takes care of managing resources, so we can focus on business logic.
- Hibernate framework provides support for XML as well as JPA annotations, which makes our code implementation independent.
- Hibernate provides a powerful query language (HQL) that is similar to SQL. However, HQL is fully object-oriented and understands concepts like inheritance, pol, morphism, and association.
- Hibernate is an open-source project from Red Hat Community and is used worldwide. This makes it a better choice than others because the learning curve is small and there are tons of online documentation and help is easily available in forums.
- Hibernate is easy to integrate with other Java EE frameworks, it’s so popular that Spring Framework provides built-in support for integrating hibernate with Spring applications.
- Hibernate supports lazy initialization using proxy objects and performs actual database queries only when it’s required.
- Hibernate cache helps us in getting better performance.
- For a database vendor-specific feature, hibernate is suitable because we can also execute native SQL queries.
- Overall hibernate is the best choice in the current market for the ORM tool, it contains all the features that you will ever need in an ORM tool.
What are the advantages of Hibernate over JDBC?
Some of the important advantages of Hibernate framework over JDBC are:- Hibernate removes a lot of boilerplate code that comes with JDBC API, and the code looks cleaner and readable.
- Hibernate supports inheritance, associations, and collections. These features are not present with JDBC API.
- Hibernate implicitly provides transaction management, in fact, most of the queries can’t be executed outside a transaction. In JDBC API, we need to write code for transaction management using commit and rollback.
- JDBC API throws SQLException which is a checked exception, so we need to write a lot of try-catch block code. Most of the time it’s redundant in every JDBC call and used for transaction management. Hibernate wraps JDBC exceptions and throws JDBCException or HibernateException un-checked exceptions, so we don’t need to write code to handle it. Hibernate built-in transaction management removes the usage of try-catch blocks.
- Hibernate Query Language (HQL) is more object-oriented and close to a Java programming language. For JDBC, we need to write native SQL queries.
- Hibernate supports caching that is better for performance, JDBC queries are not cached hence performance is low.
- Hibernate provides an option through which we can create database tables too, for JDBC tables must exist in the database.
- Hibernate configuration helps us in using a JDBC-like connection as well as JNDI DataSource for a connection pool. This is a very important feature in enterprise applications and completely missing in JDBC API.
- Hibernate supports JPA annotations, so the code is independent of the implementation and easily replaceable with other ORM tools. JDBC code is very tightly coupled with the application.
Comments
Post a Comment
Leave Comment