Recent Posts

Monday, 16 May 2016

Hibernate Configuration and Sessions

Configuration object
* Object Oriented representation of hibernate configuration file along with mapping file is known as Configuration object.
* By default Hibernate reads configuration file with name "hibernate.cfg.xml" which is located in "classes" folder.
* If we want to change the file name or if we want change the location of "hibernate.cfg.xml" then we need to pass user given configuration file name (along with path) to "configure()" method of Configuration class
* Configuration object Stores the configuration file data in different variables. Finally all these variables are grouped and create one high level hibernate object called SessionFactory object.
* So Configuration object only meant for creating SessionFactory object
* If we want we can provide the configuration information programmatically, without writing configuration file.(But it will become Hard coding, so not advisable)

Programmatic Configuration
     Adding mapping files to the configuration object programmatically
Configuration cfg = new Configuration()
     Adding Entities(annotated persistent classes) to configuration object programmatically
Configuration cfg = new Configuration()
     To add hibernate properties to Configuration object programmatically
Configuration cfg = new Configuration()
    .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
    .setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
    .setProperty("hibernate.order_updates", "true");
     This is not the only way to pass configuration properties to Hibernate. Some alternative options include:
1. Pass an instance of java.util.Properties to Configuration.setProperties().
2. Place a file named in a root directory of the classpath.
3. Set System properties using java -Dproperty=value.
4. Include <property> elements in hibernate.cfg.xml (this is discussed later).

About SessionFactory
* SessionFactory is an interface and SessionFactorylmpl is the implimented class
* It is factory of Session objects
* It is heavy weight object that has to be created only once per application. SessionFactory object provides lightweight Session objects.
* SessionFactory is not singleton. Lets create it only once using Util/Helper class
* SessionFactory is a Thread safe object.
* You need one SessionFactory object per database. So if you are using multiple databases then you would have to create multiple SessionFactory objects.
* SessionFactory is also responsible for second-level caching.
* A SessionFactory is pretty heavyweight. so, we recommend creating one and caching it in a singleton type of way. Then, you can create as many Session objects from it as you like.
* SessionFactory encapsulates Session objects, Connections, Hibernate-properties, cashing and mappings. That's why SessionFactory is heavy weight.

About Session object
* Session is an interface and Sessionlmpl is implemented class, both are given in orghibernate.";
* Session object is called persistent manager for the hibernate application.
* It is a single-threaded(not-thread safe), short-lived object
* It Wraps a JDBC connection
* The Hibernate Session operates using a single JDBC connection which can be injected by the hibernate while constructing session object.
* It has convenience methods to perform persistent operations.
* It is a factory for Transaction objects
* Holds a mandatory (first-level) cache of persistent objects
Note: After we complete the use of the Session, It has to be closed, to release all the resources such as associated objects and wrapped JDBC connection.

About Transaction
* Transaction used by the application to specify atomic units of work (Transaction management).
* Using Session Object we can create Transaction object in two ways.
Transaction transaction =Session.getTransaction();
Transaction transction = session.beginTransaction();
* Transaction object is unique per session object.
* Transaction interface defines following methods to deal with transactions.
transaction.begin() (Transaction beginning)
transaction.commit(); ( successful transaction ending )
transactin.rollback(); (un successful transaction ending )
* Default auto commit value is false in Hibernate.
* Default auto commit value is true in JDBC
* In hibernate even to execute one DML operation also we need to implement Transactions.
Hibernate supprots
JDBC Transaction.
JTA Transaction.
Spring Transaction
Sample transaction code is as follows ...
Session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// DML operations
} catch(Exception e) {
} finally {

Why we have written SessionUtil class?
* When some common logic i s repeating across the multiple classes of an application, it is better to move the common logic into some util class. Where ever we need that common logic we make use of util class.
* SessionFactory is heavy weight and it's not singleton, so we should make one SessionFactory per database.
* Using Util /Helper class we read the configuration file only once and we create one SesisonFactory and we are providing facility to get session object and to close session object.

Hibernate complete architecture

Hibernate Complete Architecture

No comments:

Post a Comment