Table of contents
1.
Introduction 
2.
What is Hibernate Configuration? 
3.
Hibernate Properties
4.
Dialects in Hibernate Configuration
5.
JDBC Properties for Hibernate Configuration
6.
Hibernate Mapping File in Hibernate Configuration
7.
Frequently Asked Questions 
7.1.
Mention a few Hibernate Datasource Properties.
7.2.
What are the advantages of Hibernate Framework?
7.3.
What are the four layers of Hibernate architecture? 
8.
Conclusion
Last Updated: Mar 27, 2024

Hibernate Configuration

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Hibernate is a Java framework that simplifies the development of Java applications for interaction with the Database. It is a lightweight open-source, Object Relational Mapping(ORM) tool. Hibernate implements the specifications of Java Persistence API(JPA) for data persistence. 

So, in this article, we will cover the concept of Hibernate Configuration in Java. 

What is Hibernate Configuration? 

Hibernate configuration has the information on the mapping of Java classes into the database table. This information is supplied by using any of the two files:

  • hibernate.cfg.xml
     
  • hibernate.properties 
     

Hibernate needs a set of configuration settings associated with the database and other related parameters called Hibernate Configuration. All this information is usually supplied as a standard Java properties file, which can be hibernate.cfg.xml or hibernate.properties

Most of the properties have their own default values and it is not needed to explicitly specify them in the property file unless a change is required. These files (hibernate.cfg.xml and hibernate.properties) are stored in the root directory of the classpath of the application.  

The configuration of hibernate.properties file can be seen by using the following script:

hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/databse_name
hibernate.connection.username = root
hibernate.connection.password = root
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

 

The configuration of hibernate.cfg.xml file can be seen by using the following script:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
 
     <property name = "hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
     </property>
    
     <property name = "hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
     </property>
    
     <!-- database name: database_name -->
    
     <property name = "hibernate.connection.url">
        jdbc:mysql://localhost/database_name
     </property>
    
     <property name = "hibernate.connection.username">
        root
     </property>
    
     <property name = "hibernate.connection.password">
        root
     </property>
    
     <!-- XML mapping files-->
     <mapping resource = "Student.hbm.xml"/>
    
  </session-factory>
</hibernate-configuration>

Hibernate Properties

Following is the list of some important properties, that will be required for the configuration of a database in a standalone situation:

Configuration Properties


For a database along with an application server and JNDI, the following properties need to be configured- 

Additional Configuration Properties

Dialects in Hibernate Configuration

The dialect in the hibernate.cfg.xml file tells which type of database will be used in the application. To connect with the database, the SQL dialect needs to be provided in the configuration file.

Since there are so many RDBMS available, therefore, there are different dialects available for each one of them. A few of the databases along with their dialects are: 
 

  • Oracle: org.hibernate.dialect.OracleDialect
     
  • MySQL: org.hibernate.dialect.MySQLDialect
     
  • Microsoft SQL Server: org.hibernate.dialect.SQLServerDialect 
     
  • DB2: org.hibernate.dialect.OracleDialect
     
  • PostgreSQL: org.hibernate.dialect.PostgreSQLDialect
     
  • SAPDB: org.hibernate.dialect.SAPDBDialect 
     
  • HypersonicSQL: org.hibernate.dialect.HypersonicSQLDialect
     
  • Informix: org.hibernate.dialect.InformixDialect
     
  • Ingres:org.hibernate.dialect.Ingres 


Following syntax can be used to find the dialect used by the application:

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

JDBC Properties for Hibernate Configuration

A few of the JDBC properties in Hibernate are as follows: 
 

  • hibernate.connection.url: It specifies the URL of JDBC.
     
  • hibernate.connection.username: It specifies the database username.
     
  • hibernate.connection.driver_class: It mentions the JDBC driver class. 
     
  • hibernate.connection.password: It specifies the database password. 
     
  • hibernate.connection.autocommit: It specifies the mode of the JDBC connection. 
     
  • hibernate.connection.pool_size: It specifies the size of the maximum number of connections in the pool. 

Hibernate Mapping File in Hibernate Configuration

Relational/object mappings are generally defined in an XML document. This mapping file basically instructs Hibernate on how to map the defined class or classes to the database tables.

Several tools are available for generating the mapping document. These include MiddlegenXDoclet, and AndroMDA for advanced Hibernate users.

Since Hibernate can operate in different environments, it needs a wide range of configuration parameters. These configurations contain the mapping information that offers different functionalities to Java classes. 
Usually, database-related mappings are provided in the configuration file. Hibernate facilitates providing the configurations either in an XML file such as hibernate.cfg.xml or a properties file such as hibernate.properties. An instance of the Configuration class enables specifying properties and mappings to applications.  The following script is used for Mapping: 
 

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 


<hibernate-mapping>
   <class name="name_of the class" table="name_of_the_table">
      
      <meta attribute="class-description">
         Write the description here
      </meta>
      
      <id name="name" type = "data_type" column = "column_name">
         <generator class="native"/>
      </id>
      
      <property name = "name_of_the _property" column = "name_of_the_column" type = "data_type"/>
           
   </class>
</hibernate-mapping>  

 

Let us look a little deeper at the mapping elements used in the mapping file:
 

  • The mapping document is an XML document having <hibernate-mapping> as the root element, which has all the <class> elements.
     
  • The <class> elements define specific mappings from Java classes to the database tables. The Java class name is mentioned with the name attribute of the <class> element and the database table name is mentioned with the table attribute.
     
  • The <meta> element is not a necessary element and can be used to create the class description.
     
  • The <id> element maps the unique ID attribute in class to the primary key of the database table. The column attribute conveys the column in the database table and the name attribute of the id element conveys the property in the class. The type attribute stores the hibernate mapping type, this mapping converts from Java to SQL data type.
     
  • The <generator> element within the id element generates the primary key values automatically. 
     
  • The <property> element maps a Java class property to a column in the database table. The name attribute of the element tells the property in the class and the column attribute tells the column in the database table. The type attribute has the hibernate mapping type, this mapping type will convert from Java to SQL data type.

Frequently Asked Questions 

Mention a few Hibernate Datasource Properties.

Hibernate Datasource Properties

What are the advantages of Hibernate Framework?

A few advantages of Hibernate Framework are as follows : 

  1. The Hibernate Query Language(HQL) is database-independent. 
  2. Hibernate framework offers the facility to create the tables of the database automatically.
  3. It supports Query cache and offers statistics about query and database status.
  4. It is easy to fetch data from multiple tables in hibernate framework. 

What are the four layers of Hibernate architecture? 

The four layers of the Hibernate architecture are:

  • Java application layer
  • Hibernate framework layer
  • Backhand API layer
  • Database layer

Conclusion

This article extensively discusses the concept of Hibernate Configuration. It goes on to extensively explain Hibernate Properties, Dialects in Hibernate, JDBC properties in Hibernate, and Hibernate Mapping File. It also includes appropriate tables to make the understanding of the topic better

To hold a tight grip on the topic of Hibernate Configuration,  we recommend going through the topics: ORMInterview Questions, and JDBC.
We hope that this blog has helped you enhance your knowledge, check out our articles on Coding Ninjas Blogs

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSQLSystem Design, and many more!

If you want to test your competency in coding, you may check out the Mock Test Series and participate in the Contests organized on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the ProblemsInterview Experiences, and Interview Bundle for placement preparations.

Nevertheless, you may consider our Courses to give your career an edge over others!

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass