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
- <?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>
<?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.
- <mapping resource="Person.hbm.xml"/>
- <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.