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.
In the above configuration file 'hibernate.cfg.xml' the head tag element is <hibernate-configuration> which contains the <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.
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.
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).
The userName is specified as 'myuser' and the password as 'Password123'.
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 :
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.
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.
We will be seeing the details of the mapping file in the next tutorial.