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.
- DB2 – org.hibernate.dialect.DB2Dialect
- DB2 AS/400 – org.hibernate.dialect.DB2400Dialect
- DB2 OS390 – org.hibernate.dialect.DB2390Dialect
- PostgreSQL – org.hibernate.dialect.PostgreSQLDialect
- MySQL – org.hibernate.dialect.MySQLDialect
- MySQL with InnoDB – org.hibernate.dialect.MySQLInnoDBDialect
- MySQL with MyISAM – org.hibernate.dialect.MySQLMyISAMDialect
- Oracle 8 – org.hibernate.dialect.OracleDialect
- Oracle 9i/10g – org.hibernate.dialect.Oracle9Dialect
- Sybase – org.hibernate.dialect.SybaseDialect
- Sybase Anywhere – org.hibernate.dialect.SybaseAnywhereDialect
- Microsoft SQL Server – org.hibernate.dialect.SQLServerDialect
- SAP DB – org.hibernate.dialect.SAPDBDialect
- Informix – org.hibernate.dialect.InformixDialect
- HypersonicSQL – org.hibernate.dialect.HSQLDialect
- Ingres – org.hibernate.dialect.IngresDialect
- Progress – org.hibernate.dialect.ProgressDialect
- Mckoi SQL – org.hibernate.dialect.MckoiDialect
- Interbase – org.hibernate.dialect.InterbaseDialect
- Pointbase – org.hibernate.dialect.PointbaseDialect
- FrontBase – org.hibernate.dialect.FrontbaseDialect
- Firebird – org.hibernate.dialect.FirebirdDialect