Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Hibernate Configuration


When Hibernate starts up, Configuration file is the first thing that is loaded into memory by Hibernate. The Configuration file is named as 'hibernate.cfg.xml' and contains all the necessary configurations needed to run Hibernate.

The below configuration file contains a few tag elements which we will be discussing in detail.


hibernate.cfg.xml

<?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.PostgreSQLDialect
    </property>
    <property name="hibernate.connection.driver_class">
     com.postgresql.Driver
    </property>
    <property name="hibernate.connection.url">
     jdbc:postgresql://localhost/newDb
    </property>
    <property name="hibernate.connection.username">
     myuser
    </property>
    <property name="hibernate.connection.password">
     Password123
    </property>
    <mapping resource="Employee.hbm.xml"/>
   </session-factory>
</hibernate-configuration>

In the above configuration file 'hibernate.cfg.xml' the head tag element is <hibernate-configuration> which contains the <session-factory> tag element.



<session-factory> tag element

Now, this <session-factory> should contain the details of the database we need to connect to. i.e. For connecting to a database we need to know, to which database we are connecting to, the user name, password, connection details e.t.c.


<property> tag element

The <session-factory> contains the <property> tag element where you can specify the individual details of the database.

In the above scenario we are connecting to PostgreSQL database and thus specified 'com.postgresql.Driver' in the below property of Hibernate.


<property name="hibernate.connection.driver_class">
com.postgresql.Driver
</property>


Similarly, we have to specify the entire path of the database (i.e. postgresql) along with the IP address (i.e. localhost), DB name (i.e. newDb) and the driver (i.e. jdbc).


<property name="hibernate.connection.url">
jdbc:postgresql://localhost/newDb
</property>

The userName is specified as 'myuser' and the password as 'Password123'.


<property name="hibernate.connection.username">
myuser
</property>
<property name="hibernate.connection.password">
Password123
</property>

Lastly, we have to specify the dialect i.e. 'hibernate.dialect'. A dialect is used because the databases (Oracle, Postgresql e.t.c) uses SQL which are slightly different from one another. All Hibernate does is checks for a particular dialect below :


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

and determines which dialect to use for that particular database. For every database you use, you have to specify a particular dialect. In the above case we have used PostGreSqlDialect. For Oracle, we had to use a dialect specific to Oracle and so on.


<mapping> tag element

Say you have an Employee class and an Employee table in the DB with similar structure.Now, how would you tell Hibernate to map this table and the java class.

And the<mapping> tag element comes into rescue.

<mapping resource="Employee.hbm.xml"/>

It tells Hibernate that there is an XML file 'Employee.hbm.xml' which contains the details of each and every fields from both the table and the java class.

hibernate_Object_Relational_Mapping_ORM

We will be seeing the details of the mapping file in the next tutorial.