hibernate

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 Configuration -  Architecture
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.

Hibernate Configuration - XML configuration
Hibernate 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.

Hibernate Configuration - Java configuration
Hibernate 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,

PropertyDescription
hibernate.connection.urlDatabase server URL
hibernate.dialectDatabase specific and makes sure SQL generated to match the chosen database
hibernate.connection.driver_classJDBC driver class
hibernate.connection.usernameDatabase user name
hibernate.connection.passwordDatabase password
hibernate.connection.pool_sizeLimits number of connections
hibernate.connection.autocommitEnables auto-commit property for the transaction

Below are some of the optional helpful configurations,

PropertyDescription
hibernate.show_sqlHelpful for debugging. Generates SQL query for each of the database operations
hibernate.hbm2_ddlEnables automatically create/drop the database tables
hibernate.cache.use_second_level_cacheEnables hibernate’s second-level cache
hibernate.cache.use_query_cacheEnables hibernate’s query cache
hibernate.cache.regio.factory_classRequired 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.

Download
You can download the full source code of this example here: Hibernate Configuration Example

Santosh Balgar

He is a Software Engineer working in an industry-leading organization. He has completed his bachelors from Visweswaraya Technological University. In his career, he has worked in designing and implementing various software systems involving Java/J2EE, Spring/ Spring Boot, React JS, JQuery, Hibernate and related database technologies. He loves to share his knowledge and always look forward to learning and explore new technologies. He loves to spend his free time with his family. He enjoys traveling and loves to play cricket.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button