Hibernate Configuration

Hibernate requires set of configuration settings related to database and other related parameters.

Database specific properties:

We can use XML based or properties based configuration for database connection related configurations.

hibernate.properties or hibernate.cfg.xml.

//creating configuration object  
    Configuration cfg=new Configuration();  
    cfg.configure("hibernate.cfg.xml");

Class and database table mapping:

We can use XML based or annotation based configurations for providing model classes and database tables mapping.

<classname>.hbm.xml or JPA annotations in java classes. It will be discussed in details in other tutorials.

SessionFactory: When all the configurations are correct and parsed by org.hibernate.cfg.Configuration then factory of org.hibernate.Session instances should be created which is intended to be shared by all application thread.

//creating session factory object  
    SessionFactory factory=cfg.buildSessionFactory();  

Session: With SessionFactory we can obtain session object to interact wtih database:

//creating session object  
    Session session=factory.openSession();  

Session is to offer create, read and delete operations for instances of mapped entity classes.

Instances may exist in one of the following three states at a given point in time:

transient: A new instance of a persistent class which is not associated with a Session and has no representation in the database and no identifier value is considered transient by Hibernate.

persistent:You can make a transient instance persistent by associating it with a Session. A persistent instance has a representation in the database, an identifier value and is associated with a Session.

detached: Once we close the Hibernate Session, the persistent instance will become a detached instance.

Hibernate JDBC Properties:

Property name Purpose
hibernate.dialect This property makes Hibernate generate the appropriate SQL for the chosen database.
hibernate.connection.driver_class JDBC driver class
hibernate.connection.url JDBC URL
hibernate.connection.username database user
hibernate.connection.password database user password
hibernate.connection.pool_size maximum number of pooled connections
hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection.

Hibernate Datasource Properties

Property name Purpose
hibernate.connection.datasource datasource JNDI name
hibernate.jndi.url URL of the JNDI provider (optional)
hibernate.jndi.class class of the JNDI InitialContextFactory (optional)
hibernate.connection.username database user (optional)
hibernate.connection.password database user password (optional)

Example:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost/test
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
      ankit@123
   </property>

   <!-- List of XML mapping files -->
   <mapping resource="Employee.hbm.xml"/>

</session-factory>
</hibernate-configuration>