Hibernate Configuration File
In this section we will read about the hibernate configuration file. In this file we will see how we can provide the database connection information to a file.
Hibernate Configuration file i.e. hibernate.cfg.xml file is a file where we can provide the database connectivity information from outside of our class. Using this information hibernate makes the connection between the program and database. We didn't need to write the database connectivity code into our program. This makes the loose coupling between database and the application. Using this feature you can change your database at any time this will not put effect on your application i.e. you will not be required to compile your program again and again when you change your database. In this configuration file you provides the database connectivity information inside the <session-factory></session-factory>, sub tag of <hibernate-configuration> </hibernate-configuration>. In the hibernate configuration file you can also map the Java class and database table's relationship. <mapping></mapping> tag is used to add the hibernate XML mapping file. The hibernate XML file i.e. xxxx.hbm.xml file is a file where the Java class and database table is mapped at one place in this file you can map the class properties with the database table's field. You can also add Hibernate mapping file programmatically using the addResource() method of Configuration class.
Hibernate configuration file provides the information to hibernate in advance that how the Java class and database tables are related to each other. This information can be provided as in the form of the hibernate.properties file or in the hibernate configuration file. The hibernate configuration file contains various properties into which some have their default values and some are required to specify. This file should be kept into the root directory of the application class path.
Properties of Hibernate Configuration File
Hibernate configuration file has the various properties. Some of them are as follows :
- Hibernate JDBC Properties
- hibernate.connection.driver_class : JDBC driver class.
- hibernate.connection.url : JDBC URL
- hibernate.connection.username : database user
- hibernate.connection.password : database user password
- hibernate.connection.pool_size : maximum number of pooled connections
- Hibernate Datasource Properties
- hibernate.connection.datasource : datasource JNDI name
- hibernate.jndi.url : URL of the JNDI provider (optional)
- hibernate.connection.username : database user (optional)
- hibernate.connection.password : database user password (optional)
Except the above Hibernate properties there are more properties that are used in the hibernate configuration file. Some of these are as follows :
- hibernate.dialect : This property provide the facility to hibernate for generating SQL optimized for a particular relational database.
- hibernate.show_sql : This property is used to show the SQL statement at console.
- hibernate.connection.release_mode : This property determines the releasing of JDBC connection release i.e. when Hibernate should release JDBC connection.
- hibernate.transaction.auto_close_session : This property is used to automatically close the session, if its value is set to true, after the transaction phase is complete.
SQL Dialects
The specified SQL dialects allows the Hibernate to set some of the required default properties of the corresponding database. In this case you will not need to specify those properties explicitly.
Hibernate SQL Dialects are as follows :
RDBMS | hibernate.dialect |
---|---|
DB2 | org.hibernate.dialect.DB2Dialect |
DB2 AS/400 | org.hibernate.dialect.DB2400Dialect |
DB2 OS390 | org.hibernate.dialect.DB2390Dialect |
PostgreSQL | org.hibernate.dialect.PostgreSQLDialect |
MySQL | org.hibernate.dialect.MySQLDialect |
MySQL with InnoDB | org.hibernate.dialect.MySQLInnoDBDialect |
MySQL with MyISAM | org.hibernate.dialect.MySQLMyISAMDialect |
Oracle (any version) | org.hibernate.dialect.OracleDialect |
Oracle 9i | org.hibernate.dialect.Oracle9iDialect |
Oracle 10g | org.hibernate.dialect.Oracle10gDialect |
Sybase | org.hibernate.dialect.SybaseDialect |
Sybase Anywhere | org.hibernate.dialect.SybaseAnywhereDialect |
Microsoft SQL Server | org.hibernate.dialect.SQLServerDialect |
SAP DB | org.hibernate.dialect.SAPDBDialect |
Informix | org.hibernate.dialect.InformixDialect |
HypersonicSQL | org.hibernate.dialect.HSQLDialect |
Ingres | org.hibernate.dialect.IngresDialect |
Progress | org.hibernate.dialect.ProgressDialect |
Mckoi SQL | org.hibernate.dialect.MckoiDialect |
Interbase | org.hibernate.dialect.InterbaseDialect |
Pointbase | org.hibernate.dialect.PointbaseDialect |
FrontBase | org.hibernate.dialect.FrontbaseDialect |
Firebird | org.hibernate.dialect.FirebirdDialect |
Example
Here we will see a simple hibernate configuration file example. In this example we will use the RDBMS "MySQL" so we will use the respective hibernate.dialect to "org.hibernate.dialect.MySQLDialect".
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/record </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.current_session_context_class">thread</property> </session-factory> </hibernate-configuration>