Hibernate Snapshot

---------------------
Basics
--------------------------
hbm2ddl.auto
·         Create - drop+create
·         Update - alter+create
·         Validate - check schema
·         create-drop - drop+create+drop

hbm.cfg.xml
·         dialect
·         mapping
·         show_sql
·         format_sql

------------------------------------------
Cache support
--------------------------------

1. Session Level cache    : per single user Ex. gmail.com

2. SessionFactory Level : per all users

3. Query Level                   : per one query/ One Instance / one program

<cache usage="read-write"



--------------------------------
Criteria
---------------------------------------
For select operations with conditions

createCritera(Emp.class)
Emp.class = select * from emp;


1. Criteria Interface
               
List
uniqueObject


2. Restrictions Class

eq
gt
lt
between
distinct

3. Projections
min
max
avg


-------------------------------------
CURD Operations
------------------------------------
1.       Insert
·         int    - s.save(ob)
·         void - persist(ob)
·         void - saveorupdate(ob)

2. Update
·         void - update(ob)  : cannot update complete column. Not pkey update
·         void - merge(ob) : get or duplicate obj data to be update

3. Delete
·         void - delete(ob)

4. Select - Single row select Only
·         Object - get(Entity.class, 101)  :Eger Select :  no id exception
·         Object - load(Entity.class,101) :Lazy Select : no id no exception

---------------------------------------------------
HQL
-----------------------------------------------------
·         Object Oriented Query’s
·         Independent on Database
·          Uses Class name instead of table name
·         Ex. Select ob.name form Student ob
·         Query  q= session.createQuery(qry)
·         int i = q.executeUpdate();


1.       Insert - we can’t insert user data
2.       Update - we can update single column
3.       Delete
4.       Select

·         Single Row- single column select
                                          Object = q.uniqueObject()
·         Multiple Rows - Single Column select
                                          List<String> = q.list();
·         Multiple Rows - Multiple Column select
                                          List<Object[]> = q.list();

=====================================================================
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 their 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_ddl, show_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                     :

==============================================
Hibernate ORM Relations
===================================================
Inheritance (IS - A)
·         Table - per - Class
·         Table - per - SubClass
·         Table - per - concreate Class
Association (HAS - A)
·         one-to-many
·         many-to-one
·         many-to-many
·         one-to-one

Table props
·         Software                     : id,name,email,sal, tool
·         Hardware                    : id,name,email,sal, Hours
·         Admin                           : id,name,email,sal,branch

1. Table-Per-Class
·         Save all data in single table
·         emp : id,name,email,sal,tool,Hours,branch
Discriminator column
    <discriminator column="etype">
Subclass
   <subclass name="" discriminator-column="SE">
    <property name="tool">
</subclass>

2. Table per subclass
·         Save commaon data in single table
·         specific data in seperate table
·         emp : id,name,email,sal,
·         sw : id,tool
·         hw :id,Hours
·         admin : id,,branch

<joined-subclass name="software" table="sw">
                                        <key id="id" />
                                        <property name="tool">
</joined-subclass>

<joined-subclass name="software" table="hw">
                                        <key id="id" />
                                        <property name="hours">
</joined-subclass>

<joined-subclass name="software" table="admin">
                                        <key id="id" />
                                        <property name="branch">
</joined-subclass>

3. Table per Concrete class
·         Create separate class for each table
·         place specific data in union-subclass
·         emp : id,name,email,sal,
·         sw : id,name,email,tool
·         hw :id,name,email,Hours
·         admin : id,name,email,,branch

<union-subclass name="software" table="sw">
                                          <property name="tool">
</union-subclass>

<union-subclass name="software" table="hw">
                                          <property name="hours">
</union-subclass>

<union-subclass name="software" table="admin">
                                        <property name="branch">
</union-subclass>

Association (HAS - A)
actor : aid,name [1,pawan]
movie: mid, name[1,kishi | 2.Balu | 3. Jalsa]
one-to-many
one actor many movies
pawan have many movies
If we save actor data , movies data saves automaticalluy

many-to-one
many-to-many
one-to-one
========================================
Annotations
======================================
use annotations in POJO classes
no need of xml files
we use AnnotationConfiguartion class instead of Configyartion class
·         Configuration cfg = new AnnotationConfiguartion ();
From Hibernate 4.x version Configuration  is enough for both annotation and xml congiguration
we have to configure POJO class in hbm.xml
<mapping class="bean.Student">

@Entity                        :
@Table                         :
@Id                                :
@Column                    :
@Transient                 :

Validations
-------------
we have ValidatorFactory to do validations
@Max                           :
@Min                            :
@NotEmpty               :
@Email                         :

=============================================
Hibernate Page Nation
================================================
Say we have 1000 Records in Student Table
To display data in page
1st 100 records first
by clicking NEXT button nect 100, then NEXT next 100....so on
we can achive this in HQL+Criteria [setFirstResult(5), setMaxResult]

======================================
Primary Key Auto generators
===================================
Automatically Hibernate Generate Primary Keys
We have to pass type of generator in<id/>
                                        <id name="id" column="sid">
                                        <generator class=" xxx ">
                                        </id>
assigned
increment
sequence
identity
native
hilo
forign
custom

assigned
default
User have to pass

Increment
·         Fisrt select the max id if their, if no 1 as max
·         for each record it increment by 1 (i++)
·         Increment will taken care by Application Layer[Hibernate]
                                        <id name="id" column="sid">
                                        <generator class=" increment">
                                        </id>

Identity
·         First select the max id if their, if no 1 as max
·         for each record it increment by 1 (i++)
·         Increment will taken care by DB Layer[MySQL]
·         MySQL, DB2 Support this. Oracle Wont
                                        <id name="id" column="sid">
                                        <generator class=" identity">
                                        </id>

Sequence
·         Sequence start with 1 and Incremented by 1
·         Internally it create Sequence table, and increment operations done here
·         Increment will take care by Both Application Layer[Hibernate] & DB Layer[MySQL]
·         MySQL, DB2 Support this. Oracle Wont
                                        <id name="id" column="sid">
                                        <generator class=" sequence">
                                        </id>

Hilo
·         hilo start with 0
·         Internally it create hilo table
·         It will take High value form Hilo table, and it will Increment
·         for each Deployment/Restart application, High value incremnet by 32768
·         1 st Deploy:  1,2,3,4...
·         2 nd Deploy : 32768,32769..........
·         MySQL, DB2 Support this. Oracle Wont
                                        <id name="id" column="sid">
                                        <generator class=" hilo">
                                        </id>
Native
  it will use any of increment,identity,sequence

Foreign
One to one mapping we use this

Custom generator
·         For ur application, u want to generate keys as petr ur wish
·         like icici_101,icici_102,icici_103,..........
·         write IciciGenerator class implementing IdentityGenerator
·         override generate method
·         and write logic for keys

                                        <id name="id" column="sid">
                                        <generator class=" IciciGenerator ">
                                        </id>

Post a Comment

Thank You

Previous Post Next Post