Monday, August 1, 2016

What is Hibernate configuration file?

Hibernate uses this file to establish connection to the database server.
§  It is an XML file which is used to define
§  Database connection details: Driver class, URL, username and Password.
§  Hibernate properties: Dialect, show_sql, second_level_cache ..etc
§  Mapping files names.
§  One configuration file for each database.
 Number of database’s = that many number of configuration files. 
§  Standard name for this file is hibernate.cfg.xml

Syntax of Configuration xml:




More details on hibernate configuration file.
1. connection.pool_size : Used to configure connection pooling in hibernate.cfg.xml.    
<property name="connection.pool_size">10</property>

2. show_sql : If the value is true, We can see generated sql statements in console.  
<property name="show_sql">true</property>

3. format_sql : If the value is true, We can see generated sql statements in a readable format.
<property name="format_sql">true</property>

4. use_sql_comments : If the value is true, We can see comments in generated sql statements.
<property name="use_sql_comments">true</property>

5. hbm2ddlauto: 
  create: Creates schema, destroys previous data.
  create-drop: Drops the schema at the end of a session.
  update: Updates the schema.
  validate: Validates the schema. It makes no changes to database.

6. In real time projects, developers will not define database connection details in hibernate.cfg.file. Instead they use JNDI name of data source. For example, if we want to use JNDI data source using Tomcat, the configuration will be as below.

server.xml (Tomcat server.xml (C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\server.xml))

      global="jdbc/MyTestDB"
      auth="Container"
      type="javax.sql.DataSource"
      driverClassName="oracle.jdbc.driver.OracleDriver"
      url="jdbc:oracle:thin:@localhost:1521/TEST"
      username="system"
      password="system"      
      maxActive="100"
      maxIdle="20"
      minIdle="5"
      maxWait="10000"/>

context.xml (Tomcat server.xml (C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\server.xml))

              global="jdbc/MyTestDB"
              auth="Container"
              type="javax.sql.DataSource" />

Hibernate.cfg.xml
java:comp/env/jdbc/MyTestDB

No comments:

Post a Comment