Professional Documents
Culture Documents
Hibernate Mahesh
Hibernate Mahesh
Presentation Layer
|
Business Processing Layer
|
Data Storage and Access Layer
DataPersistency :---
Represeting the data permanantely in Backend System is called Data persistency
1.To achieve this ,data base application use set of operations like [CRUD]
operations
C-Create ,R-Retrieve ,U-Update ,D-Delete
Tech used are w.r.t Java
1.Serialization and Deserialization
2.JDBC
3.ORM implementations
i.Hibernate
ii.EJBs Entity beans
iii.JPA
iv.Open JPA
v.Toplink
#Serialization:---
--------------------
java Application
class Employee implements Serializable { |Employee empObj1=new Employee();|
ObjectOutputStream============Serialized Data===>---1010100(FileInputStream)---->|
File.txt |
int eno =100;
String ename ="aaa";
int esal =3;
}
|empObj2| <========(ObjectInputStream)=======<-------
10010100(FileInputStream)------| File.txt|
int eno =100;
String ename ="aaa";
int esal =3;
1.In serialization and deserialization ,we will store data in file system ,where
system is providing the permanent storage for our data.
2.In Ser,DeSer .. Two byte oriented streams are used ObjectOutputStream
,ObjectInputStream to perdorm serilaixation ,Deserialization .So that this data
persistency mechanism is possible fro only byte oriented data.
3.But its not suggestible to store data in file systems, due to security, data
redundandancy and less storage issues .
4.If we have to use data persistency mechanism in Entrprise apps. a lot of code is
required
5.In Data persistency mechanism, we are only able to perform store and retreival
process but not delete ,update operations.
6.Query language is not possible so database storage operations are not possible
ResultSet rs = stmt.executeQuery(sqlQuery);
Hibernate Config file will provide all config details like driver class
name,driver URL, DB username , password ,which are needed to create a DB
connection .
Hibernate Mapping file will provide mapping details like Bean class name mapped
to DB table name ,Properties mapped to Db tables Column.
The main purpose of persistance object or class is manage persistance data which we
want to store in Db or by using this we want to perform Db operations like select,
update , insert
Note : Its possible to configure more than a single POJO class in single mapping
file
Note: -- if bean property name and column name are same then its optional to
provide the column attribute in mapping file.
<!DOCTYPE...>
<hibernate-property>
<class name="POJOCLASS" table ="tablename">
<id name="id" column="eno"/>
<property name="ename"/>
<property name="esal" column="emp_sal" />
<property name="eaddress" column="emp_address"/>
</class>
</hibernate-property>
hibernate.cfg.xml
<!DOCTYPE ...">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">@Raman@Ladda@100</property>
<property name="hibernate.connection.pool_size">10</property>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<mapping resource="Account.hbm.xml"/>
</session-factory>
</hibernate-configuration>
1.<hibernate-configuration> tag is the root tag
2.<session-factory> tag is used to include HB properties Configuration
3.<property> ttag is used ot provide single property config.
4."name" attribute is used to provide property name and body of <property > tag
will take a property value.
5.<mapping> tag provide mapping file config.
6."resource" attribute is used to provide mapping file name and location
Note:- The above approach to get SessionFactory object was used till
Hibernate3.x ,its deprecated in Hibernate4.x
1.In HB ,for each and every Database interaction Session object is created
2.In HB ,Session object is able to provide number of persistance methods to
perform persistance operations
3.To represent Session Object Hb has provided predefined interface in form of
"org.hibernate.Session"
In HB Session Object is light weight but not thread safe , because for each
and every thread a new Session objec is created.
it will return Transaction object without beginning txn , For starting txn ,we
can use
public void begin();
When we create hibernate Pojo object following are the states of object:--
---------------------------------
1.Transient state
2.Persistence state
3.Detached state
4.Removed state
1.Transient State:--
i when we create HB object then automatically POJO object will come into
transient state.
ii.If POJO object is in transient state ,then HB is not aware of the POJO
object.
iii.If POJO Object is present in Transien state ,then it is representing any
record in Database.
iv. In this state ,HB is not able to provide synchronization b/w POJO object and
record in database.
v.In this state if we perform any modification in POJO object then that
modifications are not reflected in Database.
vi.If we keep POJO object in Transient state for long time ,then it will
become eligible for Garbage Collection and GC will destroy it after its scope
is over.
Persistence State:--
i.It will peerform the operations like save () , saveOrUpdate() , update()
....over POJO object which is available in transient state.
ii.In HB app. if we perform operations like get() , laod() , find() ,
automaticaly HB will create object in persistence state directly without
Transient state.
iii. In Peersistance state the HB is aware of the POJO object and database
table record and its synchronization .
iv. In persistance state , if we perform any modifications in POJO object then
that modifications are reflected in Database table record .
3.Detached state:--
i.If we preform any operations like close() , clear() , evict() ,over the
session object then POJO object will come from persistance state to Detached
state
ii. In Detached state , HB is not aware of the POJO object and it will not
provide the synchrnization b/w POJO object and database table record.
4.Removed State:--
Example :--
-----------
Employee emp = new Employee() //Transient state
-----
session.save(emp)//persistance state
---
------
session.delete(emp) //removed state
session.close();//detached state
In Hibernnate app. To perform operations like insert ,update ,delete ,we can
use methods like save() , update() , delete() from org.hiebrnate.Session
interface provided by HB soft.
1.SchemaExport :-
This tool is provided by Hibernate soft. in the form of
org.hibernate.tool.hbm2ddl.SchemaExport predefined class in hibernate3.jar file.
Note: all mapping , configuration ,files must be at same place where this
schema is being activated.
<hibernate-configuration>
--
<property name="hibernate.hbm2ddl.auto">create-drop</property>
--
</hibernate-configuration>
Hibernate has represented all the primary key generation algos. in the form of
a set of predefined classes provided in
"org.hibernate.id" package.
if we wanna use any of the primary key generation algos in HB app. we have
to configure that algo. in hibernate mapping file by using following tags,
<hibernate-mapping>
<class name=" .." table=".." >
<id name=".." column=".." >
<generator class="..">
<param name=".." > value</param>
</generator>
</id>
</class>
</hibernate-mapping>
Assigned:--
----------
1.its default primary key generation algo. in HB app. it doesnt require any
configuration in HB mapping file.
2.It doesnt have its own mechanism of primary key generation , rather it
requires user to enter the primary key value explicitly
3.it supports various primary key values of int, short , long, String
2. This algo. ia able to generate the primary key of data types of data type
short , int , long.
3.It doesnt require any input parameter to generate the primary key values.
4.This algo. supported by most of DBs which are supoorting numeric values as
primary key values.
5.This algo is represented by HB in form of short name "incerement" ans in
form of a predefined class "org.hibenrate.id.IncrementGenerator"
ex:-
<hibernate-mapping>
<class name="classname" table="tablename">
<id name="eno" column="emp1">
<generator class="org.hibernate.id.IncrementGenerator">
</id>
</class>
</hibernate-mapping>
3.Sequence:--
---------------
1.This primary key generation algo. is able to generate primary key value on
basis of the sequence provided the underlying Db.
2.This algo is able to generate primary key values of the data types like
short , int , long .This algo. required "sequence" input parameter with the
sequence name as value inorder to generate primary key value.
3.If we have not provided any sequence name as input parameter then this
algo is able to take "hibernate_sequence" as default sequence name.here
devvelopers must create "hibernate_sequence" in database explicitly.
This algo. is supported by almost all the databases which are supporting
sequences like Oracle ,DB2.
:
This algo. is represented by HB software in the form of short name like
"sequqnce" and in form of a predefined class like
"org.hibernate.id.SequenceGenerator".
ex.
In DB
SQL>create sequqnce ny_sequqnce increment by 5;"
SQL>commit;
In mapping file:
<hibernate-mapping>
<class name="classname" table="emp1">
<id name="eno" column="ENO" />
<generator class="sequqnce" >
<param name="" >my_sequqnce</param>
</generator>
</id>
</class>
</hibernate-mapping>
4.Identity:--
-----------------
This algo is able to generate the primary key value on the basis of
underlying database table provided bby identity column.
This algo is able to provide the primary key values of the data types like
int ,short , long.
5.(hilo) High-Low
--------------------
This algo is able to generate the primary key value by getting high value
from a particular global resource and low value from mapping file.
Note:-- Global resource is particular table with a column in the same DB.
This algo is able to produce primary key values of the data types like short
,int ,long..
ex.
<hibernate-mapping>
<class name="Employee" table="emp1">
<id name="eno" column="ENO" />
<generator class="hilo" >
<param name="table" >my_table</param>
<param name="column" >my_column</param>
<param name="max_lo" >10</param>
</generator>
</id>
</class>
</hibernate-mapping>
In Db:--
SQL> create table my_table(my_column int(11) primary key);
SQL> insert into my_table values(100);
SQL> commmit;
6.Sequence-Hilo:--
1.This algo works by generating prumary keys values on the basis of sequence
value provided by the underlying database and the mapping file provided
max_lo property value.
2.This algo is almost same as HILO but here sequence value is used
inplace of high value from global resource.
3.This algo is able to generate primary keys of type int, short ,long.
4.This algo is represented by Hibernate software in short form of "seqhilo"
and the predefined class name as "org.hibernate.id.SequenceHiloGeneratr".
5.This algo is supported by all DBs which are supporting sequence internally.
6.This algo. requires following two input parameters.
<hibernate-mapping>
<class name="Employee" table="emp1">
<id name="eno" column="ENO" />
<generator class="seqhilo" >
<param name="sequence" >my_sequence</param>
<param name="max_lo" >10</param>
</generator>
</id>
</class>
</hibernate-mapping>
7.Native:---
<hibernate-mapping>
<class name="Employee" table="emp1">
<id name="eno" column="ENO" />
<generator class="native" >
----------------provide paramters depending on requirement------------------
<param name="sequence" >my_sequence</param>
</generator>
</id>
</class>
</hibernate-mapping>
8.select :--
1.This algo is able to generate primary keys on the basis of underlying DB
provided triggers.
2.This algo . is able to provide primary key values of the data types like
short ,int ,long.
3. This algo. is represented by HB software in the form of a short name like
"select" and a predefined class name "org.hibernate.id.SelectGenerator".
4.This algo. may take input parameter which are related to triggers..
5.this algo is supperted by all Dbs which are supporting triggers.
<hibernate-mapping>
<class name="Employee" table="emp1">
<id name="eno" column="ENO" />
<generator class="select" >
----------------provide paramters depending on triggers------------------
<param name="trigger" >trigger_name</param>
</generator>
</id>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Employee" table="emp1">
<id name="eno" column="ENO" type="string"/>
<generator class="org.hibernate.id.UUIDHexGenerator" >
</generator>
</id>
</class>
</hibernate-mapping>
10.GUID : --
1.This algo is used to generate the primary keys on the basis of underlying
database provided identity column with String type.
2.This algo is represented in the form of a predefined class
"org.hibernate.id.GUIDGenerator" and in the short name like "guid".
3.This algo doesnt need any input parameters for generating primary keys.
4.This alo is suppported by DBs which are supporting String type id
columns.
<hibernate-mapping>
<class name="Employee" table="emp1">
<id name="eno" column="ENO" type="string"/>
<generator class="org.hibernate.id.GUIDGenerator" >
</generator>
</id>
</class>
</hibernate-mapping>
11.Foreign :----
1.This algo is used to generate primary keys on the basis of foreign keys of
the present table.
2.generates Short ,int , long type primary keys
3.Its doesnt require any input parameters .
4.Represented in short form of "foreign" and in the form of predefined
class as "org.hibernnate.id.ForeignGenerator"
<hibernate-mapping>
<class name="Employee" table="emp1">
<id name="eno" column="ENO" type="string"/>
<generator class="org.hibernate.id.ForeignGenerator" >
</generator>
</id>
</class>
</hibernate-mapping>
--------------------------------------------------------------
Annotations Support for Primary Key Generation Algorithm
--------------------------------------------------------------
use
@Id
@GeneratedValue(strategy =GeneratedValue.SEQUENCE))
1. HQL
HQL is query language provided by Hibernate inorder to perform
manupulations pver multiple records.
HQL is object oriented query language. it supports object oriented
features like encapsulation , polymorphism. but SQl is structure query
language.
HQL is DB dependent independent language, but SQL is DB dependent query
language.
In HQL we will prepare queries using POJO class names and their
propertires names but in SQL we will prepare queries on basis of DB table
names and table columns.
HQL queries are prepared by using syntaxes which are similar yo SQL
query language.
HQL is mailny for retreival operation but from HB 3.x version we can
use HQL to perform insert ,update, and delete opertions with select operation
but SQL is able allow any type of DB operations..
In case of JDBC , SQL if we execute the Select query then records are
retreived from DB table and stored in form ResultSet object which is not
implementing Serializable interfavce so its not possible to transfer in the
network but in case of HQL , if we retrieve records then that records
will be stored in Collection object whic is serializable bydefault , so that
we are able to carry objects through network.
.Ex. PL/SQL procedures and functions are totally database dependent queries
so HQL cannt be used.
----------------
ScrollableResult
public boolean next();
public void first();
public void last();
public void previous();
public Object get(int position);