Understanding hibernate configuration file

We have already learned that Hibernate configuration file gives clear information to Hibernate framework about where to find the mapping files which defines the mapping between Java classes and database tables.

Configuration file also gives information about database details like database URL,credentials,dialect information and other important parameters.

We can use default configuration file name as hibernate.properties or hibernate.cfg.xml or we can also use any custom file name.

Let’s see the configuration file and its details

hibernate.cfg.xml file with MySql DB details

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration>
  6.     <session-factory>
  7.         <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property>
  8.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  9.  
  10.    <!—Defined our database name as  “sample” -->
  11.         <property name="hibernate.connection.url">jdbc:mysql://localhost/sample</property>
  12.         <property name="hibernate.connection.username">root</property>
  13.         <property name="hibernate.connection.password">root1234</property>
  14.         <property name="show_sql">true</property>
  15.         <property name="format_sql">true</property>
  16.         <property name="use_sql_comments">true</property>
  17.  
  18.   <!-- List of XML mapping files -->
  19.        <mapping resource="Person.hbm.xml"/>
  20.        <mapping resource="Address.hbm.xml"/>
  21.  
  22.    </session-factory>
  23. </hibernate-configuration>
<?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>

   <!—Defined our database name as  “sample” -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost/sample</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root1234</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">true</property>
  
  <!-- List of XML mapping files -->
       <mapping resource="Person.hbm.xml"/>
       <mapping resource="Address.hbm.xml"/>

   </session-factory>
</hibernate-configuration>

hibernate.dialect

Dialect class is a java class, which contains code to map between java data type and database data type.

Hence Hibernate uses “dialect” configuration to know which database we are using so that it can write the database specific SQL code wherever necessary.

We have specified org.hibernate.dialect.MySQLDialect which tells Hibernate to use MYSQL specific SQL code wherever necessary.

hibernate.connection.driver_class

This property is used to specify the JDBC driver class
We have defined “com.mysql.jdbc.Driver” for MYSQL DB
We need to define the appropriate JDBC driver class based on our database.

hibernate.connection.url

This property is used to specify the JDBC Connection URL to the database instance.
We have defined “jdbc:mysql://localhost/sample” for MYSQL DB

We need to define the appropriate JDBC connection URL based on our database.
sample” is the name of the schema we have created under MYSQL Database

hibernate.connection.username

This property is used to specify the database user name

hibernate.connection.password

This property is used to specify the database password

mapping

mapping tags are used to specify the hibernate mapping file where we define the mapping between java class and database table.

We have specified 2 mapping files one for Person and other one for Address class.

  1. <mapping resource="Person.hbm.xml"/>
  2. <mapping resource="Address.hbm.xml"/>
<mapping resource="Person.hbm.xml"/>
<mapping resource="Address.hbm.xml"/>

We can specify as many mapping files as we want based on our requirement.

Boolean properties used in the configuration file

These properties should be set to true to use these features.

show_sql

This property is used to display the generated SQL statements in the console

format_sql

This property is used to format the generated SQL statement to make it more readable.

use_sql_comments

This property is used to put comments inside all the generated SQL statements.
This will help in identifying what the generated SQL is trying to do

There are many other properties used in the configuration file on a need basis.
We will see them as and when required in the further examples.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on