What is hibernate.cfg.xml file in hibernate

In this post, we will see  What is hibernate.cfg.xml file in hibernate and what it does. Also, we will see how does it get loaded?

In hibernate.cfg.xml we provide database related information. It will get loaded through the Configuration class. In configuration class, we have the configure() method which loads the hibernate.cfg.xml file and returns SessionFactory reference, Which further used to get session reference.

By default, Hibernate tries to find the file with the name hibernate.cfg.xml or hibernate.properties files.

public Configuration configure() throws HibernateException {
configure( "/hibernate.cfg.xml" );
return this;
}

In the above code, the configure() method looks for hibernate.cfg.xml and will load it.

Getting SessionFactory reference using the configuration file.


SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Example of hibernate.cfg.xml file. in hibernate.

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>

<session-factory>

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>

<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>

<property name="connection.username">SYSTEM</property>

<property name="connection.password">oracle</property>
 
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

<property name="show_sql">true</property>

<property name="hbm2ddl.auto">create</property>

 <mapping class="onetomany.Book" />

 <mapping class="onetomany.Story" />
 
</session-factory>

</hibernate-configuration>

 

Let’s see all elements in details.

 1. <hibernate-configuration>

This is the root element having <session-factory> is a child element.

2.  <session-factory>


<session-factory> contains database and mapping information.

3. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

Way to register or load the driver. We provide fully qualified class name so that hibernate will figure out which class need to be load. OracleDriver is a class which implements Driver interface and available in ojdc6.jar.

4. <property name="connection.username">SYSTEM</property>

Username of database which is SYSTEM.

5. <property name="connection.password">oracle</property>


The password of the database.

6.  <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>

jdbc:oracle:thin — Which type of driver. As of now Type 4 diver.
localhost — Where we have installed the oracle. localhost means we have installed oracle locally.
1521 — At what port oracle is running.
XE — SID name

7. <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>


We provide dialect details means which dialect our application is going to execute. As of now Oracle10gDialect.
What does this dialect mean? It means a variant of the language. For example, if we are using oracle database then we need to use oracle dialect if we are using MySql database then we can for MySql Dialect.

8. <property name="show_sql">true</property>

If we define property name show_sql is true then hibernate generated query will come on the consol during program execution.
For example, on consol it will come like –
insert into Book (price,book_name,book_uoid) values (?,?,?).

9. <property name="hbm2ddl.auto">create</property>

There are four possible value of hbm2ddl.auto
1. validate – validate the table makes no changes in the database.
2. update – Update the existing table, if the table is not there create a new one.
3. create – first it will drop the existing table then it will create a new one.
4. create-drop – Drop the existing table when SessionFactory is closed explicitly.

10. <mapping class="hibernateexamp.Book" />

We need to map all java classes(entity) here.

 

That’s all about What is hibernate.cfg.xml file in hibernate.

You may like.

Spring Data JPA tutorial.