Hibernate Configuration Example
In this post, we feature a comprehensive article about Hibernate Configuration.
Working with an Object-Oriented Language and relation database can often be cumbersome and Object Relational Mapping frameworks are built to solve this problem. Hibernate is one of Java’s Object Relational Mapping framework. It abstracts us from underlying database implementations and application can be ported to other databases easily with a very minimal impact on the source code.
In this article, we are going to have a little sneak peek into the hibernate framework and how to configure it.
1. What and Why Hibernate?
Object Relational Mapping provides a way to map Java objects to relational database tables and vice versa. Not only does Hibernate solve the data mapping problems. It also provides ways to query and retrieve data.
1.1. Hibernate Architecture
Hibernate implements Java Persistence API specification and sits between Java application’s data access layer and physical database. Java application uses hibernate to query, fetch and create data. Under the hood hibernate makes use of JDBC and abstracts developers from writing legacy JDBC related code.
1.1. Advantages of hibernate
- Removes boilerplate code introduced by JDBC
- Eliminates the majority of common persistence related development tasks
- Lets you think the relational database data as an object
- Hibernate’s query language works on objects and it is database independent. With minimal changes, the application can be easily ported to another database
- It does not hide the power of SQL. You can still write custom SQL queries
- Supports caching and can improve performance significantly
- Supports transactions
- No need for extensive SQL knowledge though it helps
- Rich annotations support to minimize the programming efforts
1.2. Pitfalls
- Hibernate is not the best solution to data-centric application
- It has an abstraction layer and is not as efficient as SQL queries
- Sometimes create a tight coupling between the objects and database tables
2. How to configure Hibernate?
Hibernate is a framework and can be easily integrated with either console or web application easily with some configuration. Hibernate supports both XML and Java-based configuration.
System Requirements
Hibernate 5.2 or later requires minimum Java 1.8 and JDBC 4.2
Hibernate 5.1 or older requires Java 1.6 and JDBC 4.0
In this article, I am making use of Hibernate 5.2, Java 1.8 and Java’s in-memory H2 database.
Hibernate supports both XML based and Java-based configurations. It offers very fine-grained configurations. In the example, I have shown only the required configurations.
2.1. XML configuration
Below is the hibernate.cfg.xml defining the XML configuration.
A full working example can be downloaded from the download section
Apart from connection properties, each of the entity class (POJO mapping to a relational database table) should be present in the ml file inside a mapping tag.
2.2. Java configuration
The below image shows the basic Java configuration.
A full working example can be downloaded from the download section
2.3. Hibernate major configurations and description
Following are the necessary configurations required,
Property | Description |
---|---|
hibernate.connection.url | Database server URL |
hibernate.dialect | Database specific and makes sure SQL generated to match the chosen database |
hibernate.connection.driver_class | JDBC driver class |
hibernate.connection.username | Database user name |
hibernate.connection.password | Database password |
hibernate.connection.pool_size | Limits number of connections |
hibernate.connection.autocommit | Enables auto-commit property for the transaction |
Below are some of the optional helpful configurations,
Property | Description |
---|---|
hibernate.show_sql | Helpful for debugging. Generates SQL query for each of the database operations |
hibernate.hbm2_ddl | Enables automatically create/drop the database tables |
hibernate.cache.use_second_level_cache | Enables hibernate’s second-level cache |
hibernate.cache.use_query_cache | Enables hibernate’s query cache |
hibernate.cache.regio.factory_class | Required configuration if caching is enabled |
3. Download the Source Code
There are 2 projects, one showing XML configuration and another showing the Java-based configuration.
Development environment needs IntelliJ Idea and Java 11.
You can download the full source code of this example here: Hibernate Configuration Example