Hibernate Configuration

As Hibernate can operate in different environments, it requires a wide range of configuration parameters. These configurations contain the mapping information that provides different functionalities to Java classes. Generally, we provide database related mappings in the configuration file. Hibernate facilitates to provide the configurations either in an XML file (like hibernate.cfg.xml) or properties file (like hibernate.properties).

An instance of Configuration class allows specifying properties and mappings to applications. This class also builds an immutable SessionFactory.

We can acquire the Configuration class instance by instantiating it directly and specifying mappings in the configuration file. Use the addResource() method, if the mapping files are present in the classpath.

Let's see an example to provide the configurations in XML file and properties file.

XML Based Configuration

Properties File Configuration

Properties of Hibernate Configuration

Hibernate JDBC Properties

PropertyDescription
hibernate.connection.driver_classIt represents the JDBC driver class.
hibernate.connection.urlIt represents the JDBC URL.
hibernate.connection.usernameIt represents the database username.
hibernate.connection.passwordIt represents the database password.
Hibernate.connection.pool_sizeIt represents the maximum number of connections available in the connection pool.

Hibernate Datasource Properties

PropertyDescription
hibernate.connection.datasourceIt represents datasource JNDI name which is used by Hibernate for database properties.
hibernate.jndi.urlIt is optional. It represents the URL of the JNDI provider.
hibernate.jndi.classIt is optional. It represents the class of the JNDI InitialContextFactory.

Hibernate Configuration Properties

PropertyDescription
hibernate.dialectIt represents the type of database used in hibernate to generate SQL statements for a particular relational database.
hibernate.show_sqlIt is used to display the executed SQL statements to console.
hibernate.format_sqlIt is used to print the SQL in the log and console.
hibernate.default_catalogIt qualifies unqualified table names with the given catalog in generated SQL.
hibernate.default_schemaIt qualifies unqualified table names with the given schema in generated SQL.
hibernate.session_factory_nameThe SessionFactory interface automatically bound to this name in JNDI after it has been created.
hibernate.default_entity_modeIt sets a default mode for entity representation for all sessions opened from this SessionFactory
hibernate.order_updatesIt orders SQL updates on the basis of the updated primary key.
hibernate.use_identifier_rollbackIf enabled, the generated identifier properties will be reset to default values when objects are deleted.
hibernate.generate_statisticsIf enabled, the Hibernate will collect statistics useful for performance tuning.
hibernate.use_sql_commentsIf enabled, the Hibernate generate comments inside the SQL. It is used to make debugging easier.

Hibernate Cache Properties

PropertyDescription
hibernate.cache.provider_classIt represents the classname of a custom CacheProvider.
hibernate.cache.use_minimal_putsIt is used to optimize the second-level cache. It minimizes writes, at the cost of more frequent reads.
hibernate.cache.use_query_cacheIt is used to enable the query cache.
hibernate.cache.use_second_level_cacheIt is used to disable the second-level cache, which is enabled by default for classes which specify amapping.
hibernate.cache.query_cache_factoryIt represents the classname of a custom QueryCache interface.
hibernate.cache.region_prefixIt specifies the prefix which is used for second-level cache region names.
hibernate.cache.use_structured_entriesIt facilitates Hibernate to store data in the second-level cache in a more human-friendly format.

Hibernate Transaction Properties

PropertyDescription
hibernate.transaction.factory_classIt represents the classname of a TransactionFactory which is used with Hibernate Transaction API.
hibernate.transaction.manager_lookup_classIt represents the classname of a TransactionManagerLookup. It is required when JVM-level caching is enabled.
hibernate.transaction.flush_before_completionIf it is enabled, the session will be automatically flushed during the before completion phase of the transaction.
hibernate.transaction.auto_close_sessionIf it is enabled, the session will be automatically closed during the after completion phase of the transaction.

Other Hibernate Properties

PropertyDescription
hibernate.connection.provider_classIt represents the classname of a custom ConnectionProvider which provides JDBC connections to Hibernate.
hibernate.connection.isolationIt is used to set the JDBC transaction isolation level.
hibernate.connection.autocommitIt enables auto-commit for JDBC pooled connections. However, it is not recommended.
hibernate.connection.release_modeIt specifies when Hibernate should release JDBC connections.
hibernate.current_session_context_classIt provides a custom strategy for the scoping of the "current" Session.
hibernate.hbm2ddl.autoIt automatically generates a schema in the database with the creation of SessionFactory.




Latest Courses