Hibernate Configuration Properties

Hibernate Configuration

Hibernate Configuration is a Java class, which allows a Java application to specify configuration parameters used in the application. As Hibernate is designed to serve in different environments, it needs a broad range of configuration parameters. These configuration parameters contain the information of the database connection, transaction, cache, and other properties.

The Configuration class is available in the org.hibernate.cfg package. Hibernate provides the configuration in two ways, an XML file (hibernate.cfg.xml) or by setProperty method (like hibernate.properties).

An object of Configuration class is used to specify the configuration properties and mapped document which are going to use when SessionFactory is created. In an application, there should be only one Configuration object. The Configuration object is defined as an initialization-time object.  

Following code shows the initialization of the Configuration object:

Configuration cfg = new Configuration();

The configure() is a method provided by the Configuration class, uses the properties specified in the mapping file( hibernate.cfg.xml).

Example of the Configuration file

  1. XML based
  • By setProperty method
Configuration cfg = new Configuration();
cfg.setProperty("hibernate.dialect", "com.mysql.jdbc.Driver");
cfg.setProperty("hibernate.connection.datasource", "jdbc:mysql://localhost:3306/hibernate");
// other configuration properties 

Properties of Hibernate Configuration

  1. Hibernate JDBC Properties
                   Property Name                          Description              
hibernate.connection.driver_class It is used to represent the JDBC driver class.
hibernate.connection.url It is used to represent the JDBC URL.
hibernate.connection.username It is used to represent the database username.
hibernate.connection.password It is used to represent the database password.
hibernate.connection.pool_size It is used to represent the maximum number of pooled connections available in the pool.
  • Hibernate Datasource Properties
                       Property Name                              Description
hibernate.connection.datasource It defines the data source JNDI name used by the application.
hibernate.jndi.url It specifies the URL of the JNDI provider. It is optional.
hibernate.jndi.class It specifies the class of the JNDI InitialContextFactory. It is optional.
  • Hibernate  Configuration Properties
                      Property Name                            Description
hibernate.dialect It specifies the dialect which allows Hibernate to generate the SQL statements for the relational database.
hibernate.show_sql It displays all the executed SQL statements on the console.
hibernate.format_sql It prints the SQL in the log and console.
hibernate.default_schema It is used to qualify the unqualified table names with the given schema in generated SQL.
hibernate.default_catalog It is used to qualify the unqualified table names with the given catalog in generated SQL.
hibernate.session_factory_name It is used to bind the name with the SessionFactory automatically in JNDI.
hibernate.max_fetch_depth It sets a maximum “depth” for the outer join fetch tree for single ended-associations.
hibernate.default_batch_fetch_size It sets default size for Hibernate batch fetching associations.
hibernate.default_entity_mode It sets a default mode for entity representation for all the sessions opened from this SessionFactory.
hibernate.order_updates It forces the Hibernate to order SQL updates based on the updated primary key.
hibernate.generate_statistics If hibernate.generate_statistics is enabled, the Hibernate will collect statistics useful for performance tuning.
hibernate.use_identifier_rollback If enabled, the generator identifier properties will be reset to default when the objects are deleted.
hibernate.use_sql_comments If turned on, the Hibernate will generate comments inside the SQL for easier debugging.
  • Hibernate JDBC and Connection Properties
                      Property Name                          Description
hibernate.jdbc.fetch_size In this Property, a non-zero value determines the JDBC fetch size.
hibernate.jdbc.batch_size A non-zero value determines the use of JDBC2 batch updates by Hibernate.
hibernate.jdbc.batch_versioned_data Set this property true, if the JDBC driver returns the correct row count from excuteBatch(). Then Hibernate will automatically use the batched DML for versioned data.
hibernate.jdbc.factory_class This property is optional. Most of the applications do not need this.
hibernate.jdbc.use_scrollable_resultset It enables the use of JDBC2 scrollable resultsets by Hibernate.
hibernate.jdbc.use_streams_for_binary It uses streams when reading/ writing binary or serializable types to/ from JDBC.
hibernate.jbc.use_get_generated_keys It enables the use of JDBC3 PreparedStatement.getGeneratedKey() to retrieve generated keys after insertion.
hibernate.connection.provider_class It provides the JDBC connection to Hibernate.
hibernate.connection.isolation It is used to set the JDBC transaction isolation level.
hibernate.connection.autocommit It enables auto-commit for JDBC pooled connections.
hibernate.connection.release_mode It specifies when Hibernate should release the JDBC connections.
hibernate.connection.<propertyName> It passes the JDBC property <propertyName> to the DriverManager.getConnection().
hibernate.jndi.<propertyName> It passes the property <propertyName> to the JNDI InitialContextFactory.
  • Hibernate Cache Properties
                         Property Name                             Description
hibernate.cache.provider_class It specifies the name of the custom cache provider.
hibernate.cache.use_minimal_puts It optimizes the second-level cache operation to minimize writes, at the cost of more frequent reads.
hibernate.cache.use_query_cache It enables the query cache.
hibernate.cache.use_second_level_cache It can be used to completely disable the second-level cache, which is enabled by default.
hibernate.cache.query_cache_factory It specifies the name of a custom QueryCache interface, defaults to the built-in StandardQueryCache.
hibernate.cache.region_prefix It provides a prefix to use second-level cache region names.
hibernate.cache.use_structured_entries It forces the Hibernate to store data in the second-level cache in a more human-friendly format.
  • Hibernate Transaction Properties
                         Property Name                            Description
hibernate.transaction.factory_class It defines the classname of a TransactionFactory to use with Hibernate transaction API.
jta.UserTransaction It provides a JNDI name for JTATransactionFactory to obtain the JTA UserTransaction from the application server.
hibernate.transaction.manager_lookup_class It provides the classname of a TransactionManagerLookup and is required when using hilo generator in the JTA environment.
hibernate.transaction.flush_before_completion If this property is enabled, the Session will be automatically flushed during the before completion phase of the transaction.
hibernate.transaction.auto_close_session If this property is enabled, the Session will be automatically closed during the after completion phase of the transaction.
  • Other Hibernate Properties
                       Property Name                           Description
hibernate.current_session_context_class It supplies a custom strategy for the scoping of the “current” Session.
hibernate.query.factory_class It is used to choose the HQL parser implementation.
hibernate.query.substitutions It is used to map from tokens in Hibernate queries to tokens in SQL queries.
hibernate.hbm2ddl.auto It is used to create a schema or table in the database automatically.
hibernate.cglib.use_reflection_optimizer It enables the use of CGLIB instead of runtime reflection.