Introduction
In this chapter, we will take a look at how the hibernate and ORM (Object-relational Mapping) frameworks work, how to configure hibernate in different ways, and the libraries that are required for the hibernate application. An essential part of the application is the hibernate configuration. Through the configuration, we can provide database information to the hibernate engine, such as the database host, port, username, password, the database name, the drive class, and so on.
In the older era of Java development, developers used some methodologies to persist data. To persist data means to save or store data in some storage medium by maintaining it in a certain state. Once the data is successfully persisted, it can be used at any given time. A database is the more preferable storage medium for a transactional operation. Usually, we use JDBC (Java Database Connectivity) to perform such operation with the database.
If we use the JDBC operation, we need to work a bit harder and take care of the following processes:
- Opening a database connection
- Maintaining an open connection
- Building a query
- Executing a query
- Getting a response to the query
- Mapping the query response with the custom classes
- Closing the database connection
To avoid this hectic process, we can use the ORM tools available in the market. ORM stands for Object Relation Mapping. It works as a bridge between the application and database by simplifying the communication between them.
The benefits of the ORM framework are as follows:
- It reduces the development time/cost.
- It speeds up the development.
- It provides us with portability. Hibernate supports multiple databases, so there is no need to write a database-specific code.
This is a useful feature of hibernate. Generally, all databases have their own syntax made up of Data Definition Language (DDL) or Data Manipulation Language (DML) statements. If we used JDBC, we would need to write a database-specific code as our database is changed. However, hibernate gets rid of the developer's headache by handling this issue.
The syntax of a query may be different for different database parameters; still, hibernate works in the same way for all types of databases. Hibernate's term dialect
helps achieve this type of functionality. The implementation of the dialect
class is provided by the database provider to inform hibernate about the syntax of this particular database.
Some useful features of hibernate are as follows:
- Code reusability
- Transaction management
- Efficient collection/custom classes mapping
- The caching mechanism supported by hibernate