04 - Hibernate Configuration Approaches

4.1 Overview

As we discussed earlier, Hibernate needs to know about the database configurations to connect to the database. There are three approaches with which we can do the configurations. These approaches are-

Programmatic Configurations – Hibernate does provides a way to load and configure database and connection details programmatically.

XML configurations – We can provide the database details in an XML file. By default hibernate loads the file with name hibernate.cfg.xml but we can load the file with custom name as well.

Properties configurations- This approach uses a properties file for the configuration. By default hibernate loads the file with name hibernate.properties but we can load the file with custom name as well.

4.2 Programmatic Configurations

Hibernate does provide a Configuration class which provides certain methods to load the mapping files and database configurations programmatically .

4.2.1 Loading Mapping Files

To load the mapping files (also known as hbm files) there are three ways–

addResource() – We can call addResource() method and pass the path of mapping file available in a classpath. To load multiple mapping files, simply call addResources() method multiple times.

addJar() – We can call addJar() to specify the path of jar file containing all the mapping files. This approach provides a generic way and need not to add mapping every time we add a new mapping.

Configuration configuration = new Configuration();

configuration.addJar(new File("mapping.jar"))

4.2.2 Loading Database Configurations

In earlier section we saw how to load the mapping files programmatically, but along with mapping files, Hibernate requires database configurations as well.

We can use the Configuration object to load the database configurations from an instance of properties files, System properties or even set the database properties directly. Lets discuss all approaches in details.

setProperty()- we can call setProperty() method on configuration object and pass the individual property as a key value pair. We can call setProperty() multiple times.

4.3 XML Configurations

The XML configuration approach is widely used and Hibernate loads the configurations from a file with name hibernate.cfg.xml from a class path. Alternatively, we can create an XML file with another name and pass the name of the file.

To add mapping resources we can use <mapping resource> tag in hibernate.cfg.xml file .This is similar to addResource() method. Similarly, we can use <mapping jar> and <mapping class> tags for addJar() and addClass()

Just need to instantiate Configuration object like below

Configuration configuration = new Configuration().configure();

new Configuration() call will load the hibernate.properties file and calling configure() method on configuration object loads hibernate.cfg.xml. In case any property is defined in both hibernate.properties and hibernate.cfg.xml file then xml file will get precedence.

The Above code snippet will load myConfiguration.cfg.xml from the configuration subdirectory of class path.

Note: We can skip prefix “hibernate” from the hibernate properties like hibernate.connection.password is equivalent to connection.password

4.4 Properties file Configurations

Hibernate looks for a file named hibernate.properties file in the class path. Properties file provides the similar functionality as XML file provides with the difference of “ we cannot add a mapping resource in properties file)

Logs the formatted generated SQL on console. Possible values are true or false

4.6 Dialects

As we mentioned earlier that Hibernate is Database independent, but each database uses some database specific SQLs and functions like column data types, SQL functions, etc. So Hibernate must be aware of the database that our application is connecting to so that database supported SQLs can be generated internally by Hibernate. Complete list of supported Dialects can be seen in Hibernate Official Documentation but most commonly used are-

DB2- org.hibernate.dialect.DB2Dialect

DB2 AS/400- org.hibernate.dialect.DB2400Dialect

DB2OS390- org.hibernate.dialect.DB2390Dialect

PostgreSQL-org.hibernate.dialect.PostgreSQLDialect

MySQL-org.hibernate.dialect.MySQLDialect

MySQL with InnoDB- org.hibernate.dialect.MySQLInnoDBDialec

tMySQL with MyISAM- org.hibernate.dialect.MySQLMyISAMDialect

Oracle (any version)- org.hibernate.dialect.OracleDialect

Oracle 9i/10g- org.hibernate.dialect.Oracle9Dialect

Sybase- org.hibernate.dialect.SybaseDialect

4.7 Naming Strategy

Hibernate does allow us to provide a custom naming strategy with which we can direct Hibernate on how to map Java classes with tables. We can override the configurations done in mapping files in naming strategy implementation. Simple example, we can think of is to add prefix to table names. This functionality can help us from doing repetitive configurations.

4.8 Configure Logging

Hibernate uses slf4j framework for logging which is an open source framework which can direct the log outputs to application logging framework like log4j, log-book etc.

To display the SQL statements on console, we can simply enable it with hibernate. show_sql property. Similarly to log formatted SQL, we can enable it using hibernate. format_sql property.

If we want to change the logging levels or want to log in additional appenders, we can create log4j.properties file in the class path of the application.