1. Hibernate Introduction

Hibernate Introduction

Elements of Hibernate

1.    SessionFactory
It holds second level cache (optional) of data like Dialect,username and password.
The org.hibernate.SessionFactory interface provides factory method to get the object of Session.


2.    Session
It Opens a Session between DB and our Application
It holds a first-level cache (mandatory) of data. The org.hibernate.Session interface provides methods to insert, update and delete the object


3.    Transaction
The org.hibernate.Transaction interface provides methods for transaction management.


4.    Mapping file (student.hbm.xml)
The mapping file contains mapping from a pojo class name to a table name
Mapping from a pojo class variable names to table column names.

Ex:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="bo">
      <class name="StudentBo" table="student">
            <id name="id" column="id">
                  <generator class="assigned" />
            </id>
            <property name="name" column="name" index="1" type="java.lang.String" />
            <property name="address" column="address" index="2"      type="java.lang.String" />
      </class>
</hibernate-mapping>

5.    Configuration: (hibernate.cfg.xml)

Configuration is the file loaded into an hibernate application when working with hibernate.

Configuration file contains 3 types of information..
            Connection Properties
            Hibernate Properties
            Mapping file name(s)

  <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">infy</property>        
        <property name="connection.pool_size">1</property>
        
        <!-- Hibernate Properteies -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
            <property name="hbm2ddl.auto">validate</property>  
      

        <mapping resource="res/student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
----------------------------------------------------------------------

=====================================================================
Object States
============================================================
1.       Transient Object
2.       Persistence Object
3.       Detached Object


Transient Object
·         Just Object is created and added the data with setters

Persistence Object
·         Connected to Hibernate DB, doing CURD Operations

Detached Object
·         Removed Connection from Hibernate DB

For Hibernate App we need Following Files
1. POJO class
·         All the props which are there in Table
·         Class must be Public
·         Must contain Def. Constructor / but we don’t need to write manually
·         Must contain Public Setters and Getters for each property

2. Mapping File
·         Configure Bean class with Table name and Schema
·         Configure ID's                    :               <id name="id" column="sid">
·         Configure Properties      :               <property name="name" column="sname">

3. Hibernate Mapping File(hibernate.hbm.xml)
·         Configure DB Driver Properties:               Dialect, driver,username,password,database
·         Configure DB Connection pools :             
·         Configure Hibernate Internal commands:auto_ddlshow_sql
·         Configure Hibernate Mapping Files : student.hbm.xml

4. Test Class (Main class)
·         Create Configuration Object       :               Configuration cfg = new Configuration();
·         Load Configuration                         :               cfg.cinfigure(xml);
·         Build session Factory                      :             
·         Open Session                                    :
·         Open Transaction                            :
·         Do CURD's                                           :
·         Commit                                                :
·         Close All Connections                     :