Hibernate Configuration File - hibernate.cfg.xml

In this post, we understand the  hibernate.cfg.xml configuration file with an example.

Learn to hibernate in-depth at https://www.javaguides.net/p/hibernate-tutorial.html.

Hibernate Configuration File - hibernate.cfg.xml

The hibernate.cfg.xml file defines the Hibernate configuration information. It contains information about the database connection, resource mappings, and other connection properties.

SessionFactory is the factory class through which we get sessions and perform database operations. Session is the main runtime interface between a Java application and Hibernate. The main function of the session is to offer to create, read, and delete operations for instances of mapped entity classes.
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/java_demo?useSSL=false</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">1</property>
        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Set the current session context -->
        <property name="current_session_context_class">thread</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create-drop</property>
        <!-- dbcp connection pool configuration -->
        <property name="hibernate.dbcp.initialSize">5</property>
        <property name="hibernate.dbcp.maxTotal">20</property>
        <property name="hibernate.dbcp.maxIdle">10</property>
        <property name="hibernate.dbcp.minIdle">5</property>
        <property name="hibernate.dbcp.maxWaitMillis">-1</property>
        <mapping class="net.javaguides.hibernate.entity.Student" />

The connection.driver_class, connection.url, connection.username and connection.password <property/> elements define JDBC connection information.

The hbm2ddl.auto property enables the automatic generation of database schemas directly into the database.

Finally, add the mapping file(s) for persistent classes to the configuration. The resource attribute of the <mapping/> element causes Hibernate to attempt to locate that mapping as a classpath resource using a java.lang.ClassLoader lookup.

Here is a hibernate properties with description for your reference:

This property makes Hibernate generate the appropriate SQL for the chosen database.
The JDBC driver class.
The JDBC URL to the database instance.
The database username.
The database password.
Limits the number of connections waiting in the Hibernate database connection pool.
Allows auto-commit mode to be used for the JDBC connection.

Hibernate complete example using hibernate.cfg.xml

Checkout below post, here you will learn how to create a simple Hibernate application step by step with hibernate.cfg.xml configuration in Eclipse IDE. We use Maven as a build tool and MySQL database to store the data.

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course