Javatpoint Logo
Javatpoint Logo

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

Property Description
hibernate.connection.driver_class It represents the JDBC driver class.
hibernate.connection.url It represents the JDBC URL.
hibernate.connection.username It represents the database username.
hibernate.connection.password It represents the database password.
Hibernate.connection.pool_size It represents the maximum number of connections available in the connection pool.

Hibernate Datasource Properties

Property Description
hibernate.connection.datasource It represents datasource JNDI name which is used by Hibernate for database properties.
hibernate.jndi.url It is optional. It represents the URL of the JNDI provider.
hibernate.jndi.class It is optional. It represents the class of the JNDI InitialContextFactory.

Hibernate Configuration Properties

Property Description
hibernate.dialect It represents the type of database used in hibernate to generate SQL statements for a particular relational database.
hibernate.show_sql It is used to display the executed SQL statements to console.
hibernate.format_sql It is used to print the SQL in the log and console.
hibernate.default_catalog It qualifies unqualified table names with the given catalog in generated SQL.
hibernate.default_schema It qualifies unqualified table names with the given schema in generated SQL.
hibernate.session_factory_name The SessionFactory interface automatically bound to this name in JNDI after it has been created.
hibernate.default_entity_mode It sets a default mode for entity representation for all sessions opened from this SessionFactory
hibernate.order_updates It orders SQL updates on the basis of the updated primary key.
hibernate.use_identifier_rollback If enabled, the generated identifier properties will be reset to default values when objects are deleted.
hibernate.generate_statistics If enabled, the Hibernate will collect statistics useful for performance tuning.
hibernate.use_sql_comments If enabled, the Hibernate generate comments inside the SQL. It is used to make debugging easier.

Hibernate Cache Properties

Property Description
hibernate.cache.provider_class It represents the classname of a custom CacheProvider.
hibernate.cache.use_minimal_puts It is used to optimize the second-level cache. It minimizes writes, at the cost of more frequent reads.
hibernate.cache.use_query_cache It is used to enable the query cache.
hibernate.cache.use_second_level_cache It is used to disable the second-level cache, which is enabled by default for classes which specify a mapping.
hibernate.cache.query_cache_factory It represents the classname of a custom QueryCache interface.
hibernate.cache.region_prefix It specifies the prefix which is used for second-level cache region names.
hibernate.cache.use_structured_entries It facilitates Hibernate to store data in the second-level cache in a more human-friendly format.

Hibernate Transaction Properties

Property Description
hibernate.transaction.factory_class It represents the classname of a TransactionFactory which is used with Hibernate Transaction API.
hibernate.transaction.manager_lookup_class It represents the classname of a TransactionManagerLookup. It is required when JVM-level caching is enabled.
hibernate.transaction.flush_before_completion If it is enabled, the session will be automatically flushed during the before completion phase of the transaction.
hibernate.transaction.auto_close_session If it is enabled, the session will be automatically closed during the after completion phase of the transaction.

Other Hibernate Properties

Property Description
hibernate.connection.provider_class It represents the classname of a custom ConnectionProvider which provides JDBC connections 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. However, it is not recommended.
hibernate.connection.release_mode It specifies when Hibernate should release JDBC connections.
hibernate.current_session_context_class It provides a custom strategy for the scoping of the "current" Session.
hibernate.hbm2ddl.auto It automatically generates a schema in the database with the creation of SessionFactory.
Next TopicHibernate Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA