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();
cfg.configure(“hibernate.cfg.xml”); 

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
  
   
   
   
 update   
 org.hibernate.dialect.MySQL5Dialect
 com.mysql.jdbc.Driver
 jdbc:mysql://localhost:3306/hibernate
 root                        
 root                        
    
    
  • 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.

Related Topics

Hibernate Named Query using Annotation

In annotation-based named query, we use @NamedQuery annotation inside the POJO class. @NamedQuery- It is used to describe a single named query. It consists of four attributes- two of which are compulsory, and...

2 minutes read.

Hibernate Many-to-One Mapping

Many-to-One Hibernate Mapping with Example A Many-to-One association mapping is the reverse of One-to-Many association mapping. For example, many (customers) are associated with one (vendor). In Hibernate, Many-to-One association mapping is applied from child class...

3 minutes read.

Hibernate Web Application with Example

Let us create a hibernate web Application. Here we need JSP file for presentation. There are steps given below for the web app creation. index.jsp This page will show a registration form. It takes the...

2 minutes read.

Hibernate vs. JPA

What is JPA? JPA stands for Java Persistence API, which defines a set of functionalities, standards, and concepts to the ORM tool. It is a framework used to manage relational databases. It is available...

2 minutes read.

Hibernate Lifecycle

Hibernate Lifecycle In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do...

3 minutes read.

Hibernate Tutorial

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

3 minutes read.

Hibernate Mapping

Hibernate Mapping Association- It means that two or more things are related to each other by some specific relation. In other words, it specifies how the objects are associated with each other. Hibernate mapping is one...

2 minutes read.

Hibernate Inheritance Joined Table

In this inheritance strategy, mapping of the child class fields is done with the common fields of the parent class in a separate table. In other words, the common entities between the child...

3 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

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...

6 minutes read.

Hibernate Web Application Example

Web Application Example with Hibernate In this section, we create a hibernate web Application. Here we are using a JSP file for presentation. Example of creating a web application using hibernate index.jsp This page will show a...

2 minutes read.

Hibernate Dialects

Hibernate Dialects Dialect is a Java class available in org.hibernate.dialect package, which helps to map Java Application with the database. To interact with the database, we need to define the required database dialect in...

2 minutes read.

Hibernate First-Level Cache

The first-level cache is related to the Session object. First level cache objects of one Session are not visible to the other Sessions, and all the cache data is lost when the Session...

2 minutes read.

Hibernate Inverse

Hibernate Inverse An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping. In Hibernate, the...

2 minutes read.

Hibernate Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

3 minutes read.

Annotation Example in Hibernate

Hibernate Annotation Example Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses...

3 minutes read.

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

3 minutes read.

Hibernate Named Query Using XML

A named query is a technique used to assemble all the queries (Native SQL and HQL) in a specific location and refer them by some name. It helps in reducing the mess...

3 minutes read.

Architecture of Hibernate

Next → Hibernate is an ORM framework with a layered architecture. It consists of many objects, such as a persistent object, sessionfactory object, transaction object, session, etc.  There are mainly three layers in Hibernate...

2 minutes read.

Cascade in Hibernate

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/...

3 minutes read.