Hibernate Configuration File

More about hibernate.cfg.xml
  • This xml file used to specify locations of mapping files/Entities
  • In projects we don’t give the database details(ur1, username, password, driverclass) in the configuration file, instead of that, we give JNDI name of DataSource.
<property name="connection.datasource">dataSourceName</property>
  • Hibernate supports default connection pooling but which will not be used in projects We use always server provided connection pooling.

1. connection.pool_size: Used to configure hibernate provided connection pooling in hibernate.cfg.xml

<property name="connection.pool_size">10</property>

A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database.

2. show-sql: if the ‘show_sql’ value is true we can view all the hibernate generated queries in the console.

<property name="show_sql">true</property>

3. use_sql_comments: To add SQL comment to the SQL query generated by Hibernate

<property name=" use_sql_comments">true</property>

4. format_sql: Format the SQL query, so that easy to read

<property name=format_sql>true</property>

5. hbm2ddl.auto: It has two values

  • create or create-drop
  • update

1. Create

If its value is create while running the application

Case 1: Table does not exist Create new schema based on the mapping file configurations
Case 2: Table exists Drop the existing schema and create a new schema based on the mapping file configurations

2. Update

 If its value is update while running the application

Case 1: table doesn’t exist Create a new schema based on the mapping file configurations
Case 2: table exists Use the existing schema

About Dialect in Hibernate 
  • Dialect class is a simple java class, which contains mapping between java language data type and database data type.
  • Dialect class contains queries format for predefined hibernate methods.
  • Hibernate generates queries for the specific database based on the Dialect class. If you want to shift from one database to another just change the Dialect class name in hibernate.cfg.xml file.
  • All Dialect classes must extend ‘Dialect’ (abstract) class
  • Hibernate supports almost 30 dialect classes.
  • If we want we can write our own dialect by extending Dialect class
  • Dialect class is used convert HQL queries into database specific queries.
  1. DB2 – org.hibernate.dialect.DB2Dialect
  2. DB2 AS/400 – org.hibernate.dialect.DB2400Dialect
  3. DB2 OS390 – org.hibernate.dialect.DB2390Dialect
  4. PostgreSQL – org.hibernate.dialect.PostgreSQLDialect
  5. MySQL – org.hibernate.dialect.MySQLDialect
  6. MySQL with InnoDB – org.hibernate.dialect.MySQLInnoDBDialect
  7. MySQL with MyISAM – org.hibernate.dialect.MySQLMyISAMDialect
  8. Oracle 8 – org.hibernate.dialect.OracleDialect
  9. Oracle 9i/10g – org.hibernate.dialect.Oracle9Dialect
  10. Sybase – org.hibernate.dialect.SybaseDialect
  11. Sybase Anywhere – org.hibernate.dialect.SybaseAnywhereDialect
  12. Microsoft SQL Server – org.hibernate.dialect.SQLServerDialect
  13. SAP DB – org.hibernate.dialect.SAPDBDialect
  14. Informix – org.hibernate.dialect.InformixDialect
  15. HypersonicSQL – org.hibernate.dialect.HSQLDialect
  16. Ingres – org.hibernate.dialect.IngresDialect
  17. Progress – org.hibernate.dialect.ProgressDialect
  18. Mckoi SQL – org.hibernate.dialect.MckoiDialect
  19. Interbase – org.hibernate.dialect.InterbaseDialect
  20. Pointbase – org.hibernate.dialect.PointbaseDialect
  21. FrontBase – org.hibernate.dialect.FrontbaseDialect
  22. Firebird – org.hibernate.dialect.FirebirdDialect

Hibernate Configuration File
Scroll to top