Académique Documents
Professionnel Documents
Culture Documents
p o g a ficem&$.z~,xa
.,...,:,,..: .
m o b 40,g
~ ( g ~ J)
~ e~g$ u s y ? u e ~pus
. 3 (q~~es~g);~rmn)lbipn ; s u o g e l n ~ e ~ 8 u o a
.
. .
. . .
. .
.
. . .... .
k
h
. .
l/~m
nofi
* p mn o f i i & . a..#:~
;cI..:~
,... @ j a q .
u:aym amp b aw03
LL
c,,.~Li:i,i.,.
....:.:
s f i v m,.-~i,,..<>&!>?,'
a*...; ~ p m . a ~uaaya~nofi a u o f i u ~
4 nofi
~ aucpfiun
~
~ ':w3nofi
3.
.<:?
fiu!qjfij~na
mvaT99
;I bernate
j.,
L-i
,
:
I
>.
.:
<:
,.
,.;
YZ1:..r.
., .
..
-$.$
x,
+\:
..&.:
. .?. .,:.App
\.
S1
"l
Hibernate
Jd bc
DB
Here Java file sending a action to hibernate but not sql queries.
Based on the;action (i.e insert or update or select) hibernate will
generate sql query and sends to the database.
If you hiive&equirement of using some'datavery frequently, instead of
readingifiom database every time, read once and keep in the cache and
then 2-??time onwards read from the cache, it reduces number of
.
.
network.calls.
If you are using Hibernate, you can change the databgeone to another
.development
itself.
at the time 6%
.
..
For exampleatthe time of development you used mysql aft;;.
production your client/customer wants to change database to Oracle
thgn;ho need to do%chang&i
in yourjava code. You need to do changes
. .
in:th+&nfiguration
..
.-.
:very
.
successfully.
..
file and:can execute
.
Hibe&$ti$id typically used in ~ a v ~ .. ~ w i ~ ~ ~ . aJava
~ ~Servlet
~ a t i o.; n s ,
applicat?ori$,:orJ2ee applications using session-beans.
Hibernate supports "Transactions"
(~ransaction.;means
making
multiple units,& a single bundle i.e a transaction can contains insert,
one query fails to execute then remaining also
update, delete,iif. .. . any
.
.
....
willcnot
._ .. . .execute$
;
:-.-.,;.:
._ .. .- -......
,~.
.*.. .
We >d.$k:.achie~s~
?
.mapping
through
Hibe&ie!,very
easely.
$<:
:
,...
+>.:
..
, :..
_. . .,
.:?< :::-.
. ...
By H i E e m>,.a t e ~ &
can achieve where conditi(jn$ !very simp]e f:.(i
:$
.
By Hibernate-managing joins is very easy.
Hibernate is developed in java so, it is platform independent.by this we
can achieve pure object oriented (database ioteractions also).
Hibernate can be used for any kind of java application that can be
standalone, web-application, or enterprise application in where.the
database interaction is required.
Hibernate uses Object oriented querying language.
Hibernate is free tc use no need to buy any licences. It is also a
opensource, we can download the source code and can enhance.
Usage of Hibernate is nothing but using sessipn object effectively.
Hibernate maintains two level cache.
.
.
,.,
. ,..
.
.
_ > . .
'
..
.; ....
.. ." . .. .
, ..
'
.:
.,
,...
. . :>.>.
...;,..
:: :..;
,-
.
.
.." . '
"
Lara Technologies
... ... .
080-41310124
Hibernate
-
key features:
Lara Technologies
Persistant Objects
..
-.
.-.
.
.
ExIl
Configuration
Transaction
To use Hibernate, it is required to create a java class that represents the table
in the database and then map the instance variable in the class with the
c o l ~ m n in
s the daiabase. Then Hibernate can be used to perform operations
on the database like select, insert, update and-delete the records in the table.
Hibernate architecture has three main components:
1 . connection Management:
2. transaction Management:
3. object Relational Management:
Hibernate API
Session (orp.hibernate.Session):
Session object is similar to the HttpSession 0bject.s A single-threaded,
short-lived object representing a conversation between the application and
the persistent store. Wraps a JDBC connection. Factory for T r a n s a c t i o n .
Holds a mandatory (first-1evel)cache of persistent objects, used when
navigating the object graph or looking up objects by identifier.
SessionFactory (or~.hibernate.SessionFactorv):
.. .,
.
..
In order to complete
.
configurationi::df&&offi%hibernate
we use this class.
. . .
.;
..
. .
:.
. ,
.. ..
> .
...
'
Query(org.hibednate.cf~.quew):
In order to c o n s t f u c t ' queries
~ ~ ~ we use this interface. Not .only this
interface we can a1s.o use Criteria interface.
Transaction (org.hibernate.Transaction):
Lara Technologies
...
. . . . . :,
Develop "POJO
. ..class"(Pl~iq@ld~Java
.
:...
Object). It is similar to
:;;A .!. :
usebean or forrri bean.
For every pojo cl&&develop one hbm (hibernate mapping) file.
Update ~onfi~uration'file
with hbm file path.
Write client program .Inside this, we have to f ~ l l o wfollowing steps.
I. Open hibernate. session.
2. Start transacfio~n:
3. Use transaction::... .
4. Commit transaction
5. Close the session
.
.!..
':
"..
.: .:
Lara Technoiogies
'
'
I.
2.
3.
..wbi
<property narne="dialectfl>
org.hibemate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's auiornatic session context management -->
<-propefi;.; name="curi-ent-sessio~l
-context-class">thread</property>
L s r a 'I'echi~ologies
080-4131 0124
<property name="cache.provider-classw>
org-hibemate.cache.NoCacheProvider</prope~>
.
<property narne="hbm2ddl.auto">create</property>
.
..
.
-,
. .. .
.. . ;,'; . ::
NOW
create a package.structureand develop a Pojo object.
.
.......
.:
...
.:.
,..' ,;:
. . ' . . .
.
...
.<
. .. .
... :
package corn-lara;
pu.bIic c1ass;Person
.
{: :I.:
.
.
;;.
:::,:~private..l~teger
.
id;
..p<i.vateString personName;
private integer p e r s o d g e ;
.
:.:
'
...
''
. .
public int:getId () (
ret',,;* id;
;
. . . . . . .,
..; .
. -.
.
. . .. .
....
080,41310124
II
this.personNamz = person~ame;
pu blic in t getPersonAge () (
return personAge;
>
>
5. Now develop one hbm file for every Pojo classinone. package.
.
"..: ..
- ally
In hibernate with out primary key we can't develo;::':
. . .
>.
.----
L-B-1-
tLaiJlG.
,;
080-41310124
2.
.?.
htt~:i!javrteasytosi!~c(?m
package corn-lara;
import org.hibemate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Manager {
public staiic void main (String [] args)
x i s a i i
SessionFactory sf = config.bui1dSessionFactory 0;
Session session = sf.openSession 0;
//3. Start the Transaction
session.beginTransaction 0;
se~sion.~et.~ransaction
().cornmil ();
116. Close the sessiorr
session-flush ();
session.close
0;
I
7. Now start the database (i.e MySql4) and run the program.
Eara Technologies
<session-factoiy>
<!-- Database connection settings -->
<property name="connection.driver~class">
corn.mysql.jdbc.Driver</property
<property naine="connection.url">
jdbc:mysq1:l/localhost/lara</property>
<property name="co~mection.username">rootc/property>
<property name="connection.password"></property>
<1-- JDBC connection pooi (use the buiit-in) -->
<property name="dialectN>
org.hibef~ate.dialect.MySQLDialect</propert~>
<!-- Enable Hibernate's automatic session'context management -->
<pmperty name="cu~rent
-session-context-class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider-classn>
org .hibemate.cache.~o~ache~rovide~lproperty~
<!-- Echo all executed SQL to;~dout.-;>
- ..
<property n a m e = " s h o w
. .s q
. 1,~~~e</pr6pe~>
, ,
. .. .
.:* . . . ...
'. .
:
. . .
. . . . .. .
<mapping resource="com/lara/Person.hbm.xml'l~
. .
</session-factory>
4.
.: '
...
:
.. . . ..
. -. ..
. :,.. ,
.,:'.'
A:.,
. i...
::
/'
- - We want to crate$fablefor,
with column names as
<*-: person
. . .
(reis&:-ID, PERSON;
-N .-m. .....
: .,P,?. E.:.. .m O N AGE).So
first develop one.Pojo,:
. .
class . . . . these attrjbufes-$.~~'<.-i
?:,.F; : $;.: :.:-:
.
...
..
'
.:
,.>.
"
-.: .- ' .
....
..
. ..
.- .,.. .
.
. .. . . , ;7,;. .
. . . .
?.,!(%
. .. . <..,
. .. .
..
.,
.. .. .. . .
.. .
. . ... . :
..,
'
. .,
.
,
%
.
:
:.
.. >
..
.....
. -
:.
..
:.
: , . . a >..
.. . .
.... : .
.:..:..
pa~kage'com~lara;
public claisPkrson
. . .
..
..:...;;=::*. .
j F:
%>,
private;qt
. . . . _ . . . id;
: iprivate<s.txin,g
. +.?:.? :..;.
personNarne;
.......:..
...A . : . . . . .
f;pA'ilva
. . .
teintiperson~ge;
. . . . . . . <..
.. . ;:,
,
. . .
.$. :!-i;
. .
:,
. . . . .
:
.Z.?&,
.$j,i
$
:.
::: :.
<,. .:, .::+ . . .
-<'..'.:' .:... ..
pu b!lciln$;,get1d
:1
() '(
re.Ku.'&n
.
id;
.'.:
.
.
.
.
..
..,;
,.
F:
..
......::.. . ....c.:'i
.
. . . .. >-:...::<-
.: ;
; i -! .
.
:I :
. .:
$. ;,
!:.:,,
. . . ..
.. ,
.
..
i.
'
public in t getPersonAge () (
return personAge;
5. Now develop one hbm file for every Pojo class in one package.
package com.1ara;
Lara Technologies
080-41310124
-10
r
- u r e ~ % oaq)
~ d unJ pur? ( p [ b s I C ~a*!)aseqelep aq4 JAeJsMON
.L
..:;
. .
<session-factoiy>
.'..
.
... . . ..
.
..
. ,. . :
<property name="dialect">
org.hibemate.dia1ect.M ySQLDialect</property>
<I-- Enable Hibernate's automatic session context management -->
<property name="cun-ent-session-context-class">thread</property>
<property n'ame="cache.provider
..... : class1'>
org.hibema~k~.~ach6.~o~ache~rovider~/property~
. ...
.
..
. .
<property name="hbm2ddl.auto">create~~property>
. -:,. . .....
. .. .. . :
..:I<
. . . . .
....
.,:
...
....
>. .>
,...
... . .: .
"'
*+.
.+...>.:
+,
;Ti, ,.-,.a
-l
.:.
. . . . . . . . ,... ,: ;
4...
7..
.Y
'
'
:-
-::
>.>..:.
:: .,.
...
..-. ' .
.
.
.
...
package c6m;lara;
..,.
pu$lie
..,.
-.
( !;:!,r i:::; class@e~son
. . .
,:
4 ...? ..
...........
>i !:.
....
,:-:.' .....
...;;.
i ;,.:,
'
." .;
. ;?
...>...*.".a
d..:5'
..
.g<!,:,f5
'
.. .~.
... ..
.. ..
.. ...
.- _
-,, ,
.'L
'. >">.
-,.,.....
.siphlv,ife:,,nty
..-.. ::!.-..:.'
. .....
.~.<:
.r,$
.?.:
.,
.:'.. ..
:. .. . . . i. . .
<.. ,
:. ,::!:
i.
i.
'...I.
A.
.........
*. .. .
.. .:..-.
. , '.:
r. .! ...
...
.
:?.;<;
. .>.
. . . . .
..
<
hi-
.........
G&:,.
.,
... !
:.;:
>
i
_I
..
....I..
1
Lam Technologies
.. :... '..- :
. . . . .
:. ,i,?
, .:
. ;
.5
. .
< ,
' \ .
public in t@ffd () (
retum:id; . . .
..::
. ...:. .
. ,<.'
> i ..:
: . ,..
..:: . . .,: .. p:?.,..; ,.:
_ I
i:.;
.
..
..
public in t getPersonAge (
return personAge;
5. NOW develop one hbm file for every Pojo class in one package.
!i' . . ' ,
..
. ..
>,
! .>
.'
:,.
.
.
.?;
"
. .. 4;..._ ...
., ..
: .i
.
.
..
. . :..
<?xm] version="l:.o"?>
.
<!DOCTYPE hibeinate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3 .O//ENV
"h~:/hibernate.sourcefoge.netkibemate-mapping-3.~.dtd11~
<hibernate-mapping>
..
. ..:i .
..
<class name=~~6m.lara.Person"
table="PERSONS">
<id name="i& co]umn="PERSGN-ID">
<generator class="native"/:
</id>
Lara Technologies
080-41310124
package corn-Iara;
import org.hi bernate.Ssssion;
import org.hibernate.Szssi~nFactory;
import 0rg.hibernate.c fg.Configuration;
public class Manager (
public static void main (String [I( args)
Person pl = new Person 0;.
p l .setPersonName ("RAMESH");
pl .setPersonAge (29);
11 1. Configuration
Configuration config = new Configuration 0;
config.configure 0;
.
.... .. . . .
, .
<
...
...
..
. ....
.
*.~
>
I:.
.. :...
.
.
.: .... .. . .
y,< .;
... ... ..
. . ...
- . ..
..: . . .,
.. .
., .
. .
.. ,. .. i
, .. . .
.
.:
..
~
.;.:
...
...,
.. . .
//.1.;~;&the Transaction
.:..
ses$i&.de~ete(~l);
. .
..
115.:Commit
...- .
the trinsaction
sess~pn.getTransaction~().co~mm~t
0;
;.,c,.:
Lara Technologies
7. Now start the database (i.e MySql4) and run the pro,oram.
..
..
.. . .,. .
,t,
., .
PRODUCTID.
<
.,-
??
...
. .. .....
,t
i.
.
.
I
. .
'
::
......
I.- . .,
..............:...
'2..
. . . . . . . . . . . . .. ' - -. .. ..
2. .'
'
'
.. . .. .. .
. . . .
....
... -
'.
> .. . ...
..
. . . ..,..
*; !.
.?:
<?xml version=~~1.O1
encoding='utf-8'?>
:i........
...: . : .
, . . ... .
. . .. .
<!DOCTYPE fiibete-configuration PUBLIC
"-//Hibemote.Mibemate Configuration DTD ~.o/EN"
"h~p:/hibemate.sourceforge.net/hibe~on-3-O.dtdt1>
'I
-:.
,.
.
,
<session-factory>
,..
~.
...>
<hi bemate-.configui&ion>
.
:. +:. . .,
.
.
. .. .
:....... -:
. .
.?!.
sb:
:I
>.
dbc:mysql://localhost~lara~/property>
<property name="connection.username">root</property>
<property narne="connection.passwo~></property>
<!-- JDBC connectioi~pool (use the built-in) -->
<property narne="conneciion.pool -size"> 1</property>
. . . .
.......
.r
-*,.
,::;
'. . ., . . . .
<<,.
.:
. . .*
.-.
. . ....
.
.....
. . . .
% .
..
<!-- ~ C. h. all
b execut~d~SQL
to stdout -->
~property~name='show
-sqln>true</property>
:. . :
. .
. ....
-.
4.:::
.....
.....
3. N.ow:create
on$,package
.....
structure in "src" folder and deveiop
one
:., .........
. . . .
.-..
....
Pojo .c!aGsL;
. ..
....
..:".
:. .
. . . ..>......
.
.?.g;:::p,a
.:;
c b ':@
com,!ara;
.
:.
.
--..........
:.
::
. .I?:,. . .
, .
.. .
.
. :.,
.r
,;:
?..
,.
At?.
y<.:.
.?..,
....
....-... ...
: :
..:.....
,-, . .. . . .
A:..
::.
..
.
. .
. . C...2..
...
.<l
.:
.. .. .;,..
...
pijljJib..:class'Pi.odu&:~.:;
,..
- . : ..,. .....::
( -,?.: < :.:;!
;
:
..,,,> ;...
,:.
v-
,:.
'
,,,
;, ir; * , A
::
.
j.:,..
' I
. 5 . . . ..:. . ..
. .: .
. . .
.< :.:.
. . .i.
"\.
I
'
.:,..
:I.:
.
,>
priva*g~tringproductN&e;
':
. .
,'
priva&:double product~~ic6i
private int pro,ductQuantity;
,.
.
.
%
:
-.
;',. .'< . , .
.::2: . .
,.....
..
: ...
....
'
int get~a(l'(.:- 5
. . . . ..' ;'
return id;' ' .;.: .$
. . . ..
':
,.
'
,. .
....
."!
. . .
..
.....
....
public in t getProductQuantity() (
retu rn productQuantity ;
. .... ..:. ...;,.... ...
.
...'.;
i:.
..,, > 3 .
: ,<>
'~
;.
.,
..
',.
.. ..
.
.
:.,..G
::,.
. ,
. .?.
.>.:
.,
.?i,
2t.,t!>
Lara Technologies
....
.,. . .;
. . .,.,.
:,..
;:ce
- / "
kdit.jsp
.<.., page ..;. ,?:
.
:.
.<
. .
.
c, i?!.:;::.:. .
:..
. ",$, ,>...::... ... <,:....
,
l.
:.:
''
ed it.i$+;,
.
.ii
&:: .......
.*.f?i
*:., ?
-. ';. ..:&p[:$:
.;.<:;;;:
.:
.:
.::;x;
..:
. . . 2. .?'.
.<:<,;
,,
;i.
. . . . . .
,:,>..
.>,;;c:t,,+,
*.A, i.:
<..,>>
<\
'
080-41310121
delete. i s p
... .
2. : .
.. .=..
...
.. . .
p c k a g e com.lara;
. .
.
import j ava.io.IOException; ..
importjava.io.~rint~rit&..
. . . .. ..... .. . .. .. . . . . .
import javax.servlet.~~rv~~t~~~e'~ti6n;
. . ..<
,. .......
. . . ..:. ..:
im poi~j;avax.servlet.httPP~tt~~~,irt-let~e~~est;
.
..
import jz~ax.sen~let.htt~.~ttp~ervlet~es~onse;
.. . .: .
. . .. ... .. .. . . .
.
import org.hibernate.Session;
. . ...
. .. ..
..
. . . .
import orghibemate.SessionFactory;
import org.hibemate.cfg.Configuration;
. . ... ..:
.. .
<..,.
;,
.....
. .
.: . . :
. .
. .
.,...>*.
'
; .
..
>..
.
~ .
.
. . i.
. .
. ,
.I
'.:I
..
public. c l a s s ~ , ~ n ~ e r t ~ eextends
r v l e t javax.servlet.http.~ttp~:e&let
....
implements javax.servlet.Servlet (
.
static final ,long serialVersionUID = 1L;
public ~nsert~ervlet()
( -1,
. . . .
super();
. .. . . .
i
protected void d o ~ o s t ( ~ t t ~ ~ e r v l e t ~ erequest,
c@est
HttpServ1e.tResponse response)throws
..
ServletException, IOException
.
-',isession. close();
. .
package
com.l$.a;,~:::
..........
.+>. ;,,:,
>;.!
-.
:
. . . .. . . . . .
..:. .$..:<;..:
.
'
,:;.
. - . : .:
im p o r t ~.........
5;~~~~o~IOExce~~on;
r
..;..::.....
............
<..?*.
..;C.; .;
.#
..,::
. .
,.
. .
.':.'
. .
...
. .......
.
.....:.,.. .I...
. .
;.
C
String id = request.getParameter("idn);
String pName = request.getParameter("productName");
String pPrice = req~est.get~ararneter("~roductPrice");
String pQty = request.getPararneter("productQuantity1');
Configuration config = new Configuration();
config.configure();
SessionFactory sf = config.buildSessionFactory0;
Session session = sf.openSession();
session.beginTransaciion();
Product p 1 = (Prociuct)session.load(Product.class,
Integer.parselnf(id));
Lara Technologies
080-41310124
PrintUTriterout = response.getWriter();
out.println("PRODUCT DELETED");
out.println("<br><a hreeindex.jsp>HOME</a>");
-.
..
.. ..
. .?. . . .
. .
'.
One mo&:~a;am~le
..
for how to devlop hibernate $$$in web-app
..
by
doinp all the o ~ e ~ ~iSetizsert
i ~ ~ s ~Fdatz,!is$,s~~~~&~
,...
. - ...
. , '; ..
..
. ..
. ,.
hiberna te.cfg.xm1
!
...
... .
..
<?xml:~y~~sion='
1.0!&icoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-/Hi bernateMib e r n a t e ~ o n f i ~ u r ~ tDTD
i o n 3 .O//EN "
" h t t p : / / h i b e r n a t e . s o u r c e f o r g e . n e t / h i b e ~ a t i o n - 3.O.dtdn>
.
<property name="connection.driver~class">
oracle.jdbc.drive~OracleDriver~/propert~>
<property name="connection.url">
jdbc:oracle:thin:@Iocalhost: 1521 :XE</propedy>
<property narne="connection.usernarne">system<iproperty>
Lara Technologies
080-31310124
-.
org.hibernate.didect.OracleDialect</property>
org.hibernate.cache.NoCacheProvider~/property>
<!-- Echo all executed SQL to stdout -->
<property name="show-~ ~ l ' ' > t r u e 4 ~ r o ~ e r t y >
..
....
.. , ..
<!--
.,
.. .
...
.
..o,n!startup-->
and re-create the ditab&e schema
.
..
...
...
~prope~y?:~n~e="hbm2ddl.auto1~>upda~e</'p~op~rty>
package com.lara;
.
. .
080-41310124
.
. .
..
.: .
1
pu b lic String getHouseNo() ( ..
return houseNo;
>
. :.
.
>
,.
'
i
public String get~ountry()'{
return country;
>
..
.. ..
.... %... .
- .
In hbm file we are not giving any column names for property tag then it
take defaut name as property name.
<body>
<form action="lnsertServlet" method="post">
HouseNo:<input type="text" name="houseNo"><br>
StreetNo:<input type="textWname="streetNo"><br>
City:<input type="textUname="cityM><br>
State:<ir,put type="textWname="statenxbr>
Country:<input type="textUname="countryl'><br>
PinCodeNo:<input type="textn
name="pinCodeNol'><br>
<input type="submitUvalue="ADD ADDRESSv>
</form>
</body>
search.jsp is for serrrching the particular record in the table
. , , by
selsecting the coIumn names and giving the text to search.
search.isp
<body>
<folm action="ListSei-vlet"/>
SearchCriteria:
<select name="searchCriteria">
<option value="id">AdressId<loption>
.
coption value="houseNo">HouseNo</option>
<option value="streetNo">StreetNo</option>
<option value="city">City</option>
'
<option value="state">State</option>
<option value="country">Country</option>
<option value="pinCodeNo">
PinCodeNo</option>
Lara Technoiogies
</select><br>
SearchText:
<input type="textl' name="searchText">
<input type="submit" value="Search">
</form>
</body>
InsertSew1et.java is for inserting the values into the table. If we want to
insert the servlet then
. . ..
we to :call save0 by passing the object.
...
~.
. .
..
. .,r
:..,.
~:
.'
.::.
&
.<
,:. .
.. ,::
..
, ,
. .
.<'
...
package cok.lara;
import j a v a ~ ~ . ~ ~ ~ x c e ~ t i o n ;
import java.i&P*int~rite~
ja~ax.senile~~~ervlet~xce~tion;
.
. import
.>
,;. . .,,.;.....J ...
...
.< :
. . ~import~,.javax.servle~littp;NttpS.ervletRequest;
.
..
. ,. .
~mpor~~~~y.gx.servlet-.http.~ttP~ewlet~esponse;
. . .
.
'
;.:z!:..*
.. . . .
im
p
o
k
t
~
~
'
~
g
~
~
i
b
e
m
a
t
e
.
~
e s s i o + ,:~ C ~ ~ r y ;
...;.
.-....;i~.;:t-..:,:.
. .~.
import org;hlp.emate.cfg.~onfig&ition;.
.!. 6
. . .
.,-.,
: .:>;
. . . . . . ..,.import
.
org.hikieqate.classic.Session;
.>
.. . . ..
class
InseASewlet
:
,
...:.
extends javax.serv1et.http.HttpServlet
.,:.
.r. public
_.
'implements javaxi$ervlet.Servlet {
.
&
.:: i a l ~ e r s i o n
=~1L;
~~
stati&$inal
". long ,&
<p"biic~,nsert&ervlet()
(
$$~z$*
B 7 :
.. .qg
.*gj.y..t3.%&,, pe+l@;$$
:.t<.3.a;T<$rV
3.. ... .*.2:
. . ..I... :ry
. . .:..:.
....
. .... : ...
.**
<>;.;. z . :.:. *<..?
..
"6'.,?'..
>??, ., *:;$
)
..< :. . . .
. I-".
protect6djvoid doPost(H~p.Se~IetRequest
request,
HttpSer:!etRespoii$ci1. ,. response)'th
....
rows ServletException, IOException
.
.
. . . : .
. .
..
.
.
.-::
.. . .
. . .
<
..
:..
'
.. . . .
.;
!
. . .. . . .
..
. .
::.
'
.-.I
...
..
..
* >
3.
>.
'
,:
;.
--
.I''
,:.
i,:t.;.
a.>:.
rx
,..,..-I
:. .;.
!.%-
,:.
-i,.
,:;
.A,
.I
. c .?
String houSe~o-7e~uest.get~irarneter("house~o");
String ~treetN~equest.getParameter("streetNo");
String city=request.getPararneter("city");
String state=request.getParameter("state");
String country=request.getPara,aeter("country ");
string pinCodeNo=request.getPararneter("pinCodeNo");
Address adr=new Address();
adr.setCity(city);
Lara Technologies
080-41310124
session.getTransaction().commit();
session.flush();
session.close~;
Printwriter out = response.getWriter();
OU~.~~~~~~~("A
SAVED");
DDRESS
.
. .!.. :
. :...'.. ..;.. !.. .
..: .,.,<
::, ::; .:>r
;.
'
. . ...t i .
. .. .
.
.:
....
... ..,
:..>
., : !*
.
.. .,
. *.,
....
UpdateServIet-iava
package corn-lara;
import java.io.IOException;
import java.io.PrintWriter;
import javax-sei-vlet.ServletException;
import javax.servlet.http.HttpServ1etRequest;
import javax.~ervlet.http.~tt~ServletResponse;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class UpdateServlet extends javax.servlet.http.HttpScm1et
implements javax.servlet.Serv1et
Lara Technologies
OSG-41310124
i
.
Lara Technologies
080-4131.0124
ListServlet.java is for listing all the records from table. If we want to list
all the records then rve want to use criteria interface or Query
interface. In this application we a r e using Criteria.If we want to create
object for Criteria interface then there is a method createcriteria ()
which there inside the session class.
Eg: session.createCriteria(Address.class);
import java.io.IOException;
i m p ~ rjava.io.Print'Clrriter;
t
import java.util.list;
import javax.servIet.ServletException;
import javax.servlet.http.HttpServletRequest;
irnpo~j~vax.servl~t.http.~ttP~ervletResponse;
import drg.hibernate.criteria;
import org.hibernate.~ession~actory; !.
import 0rg.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
"
i s r z Technologies
o~t.~iin~ln("<b>~~RE~'l'N~</b>~~);
. . .!
out.printlnt:y</td>'.');
.
...!
o~t.~rintln~!'<idY);
.
. . .
'.
:~out.print]n("<b>CITY</b>");
:.
.
.. . . ..
.
o~~t;printlil("</id>");
&it'<:intln "<td>").
. . ..
.,
>:; *,.
p.:
..
,.
. . :,-....
r .
o~~j$&tln(l'<b>~~~~~</b>'l);
..
.:, .
out:$rini]n(tl</td>");
I ;
.. :.
~ut.~rintln("<td>");
:
~ut~.~r'intln("<b>~~~~~~~</b>'~);
:: -
out.prinilG("'</td>'');
.'.
. .
,o;t;ijriniln(!l,<td>'l);
.
.........
o~~println("<b>PINCODENO</b>'~);
,.,. .
.,
out.~iint]n("</td>;;); .'
out.print~hp'</tr>~~);
.
.
i5
L a r s Technologies
080-41310124
;:,
.;...
:s:. _
...
...
6.
' .?.!<<>?
...
for(int i=O;i<list.size();itt-)
add = (Address)list.get(i);
out.println("<tr>");
out.printIn(''<td>l1);
out.println("<a href-'DetailedServlet?recordId="
+add.getId()+'">");
out.println(add.getId());
out.println("</a>");
out.println("</td>");
out.println("<td>");
out.println(add.getHouseNo());
out.prin1ln(lf</td>");
out.println("<td>");
our.println(add.getStreetNo());
out.println("</td>");
, out.printIn(l'<td>");
out.println(add.getCity());
o~t.println("<ltd>~');
out.println("<td>");
out.println(add.getState());
out.println("</td>");
out.println("<td>");
out.println(add .getcountry());
out.priniln(":itd>");
out.println(lT<td>"j;
out.println(add.getPinCodeNo());
out.println("</td>");
out.println("</tr>");
out.println("<ltable>");
out.println("<a href-'index.jsp'>Home</a>");
session.getTransaction().commit();
Larn Technologies
080-41310124
package com.lara;
import java.io.IOException;
import java.io.PrintWriter;
import javax.se~let.ServletException;
import javax.servlet:http.HttpServletRequest;
import javax.servlet.http.HttpServ1etResponse;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibemate.classic.Session;
public class DetailedServlet extends javax-servlet.http.HttpServ1et
implements javax.servlet.Servlet
staticdinal long serialVersionUID = 1L;
public DetailedServlet() (
super( 1;
1
protected void doGet(HttpServ1etRequest request, HttpServletResponse
response) t h rows ServletException, 1 0 ~ x c e ~ t i o n
Lara Technologies
080-41310124
out.println("PWCODENO:"+add.getPinCodeNo()+"<br>");
out.println("<a href-'DispIayForEdit?recordId="
+add.getId()+"'>Edit </a>");
out.prir~t;~l<";a;~i.t.C='Se;eteServlet?recordld="
+add.getId()+"'>DeIete</a>");
out.println("<a href-'index.jsp'>Home</a>");
out.println("<a hreFListServlet'>ListRecordHome</a>");
package corn-lara;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.hitp.ltittpServletResponse;
.
nm port org.hibernate.Session.Fac~~~~;
.
Lsm Technologies
056-3!310i24
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class DeleteServlet extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet
,
1
protected void doGet(HttpServ1etRequest request, HttpServletResponse
response) throws ServletException, IOException
{
String id = request.getPararneter("recordIdI1);
Address add = new Address();
add.setId(new Integer(id));
..
...
..
. .:...
-..
.;
_ ,
..
~ ~ ~ i o n ~ , a c sf
t o=r c- ~
y nfi~.build~ession~act~)~~();
Session session = sf.openSession();
Lara Technologies
package corn-lara;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servl.et.ServletException;
import javax.servlet.http.HttpServ1etRequest;
import javax-sesvlet.http.HttpSefvletResponse;
import org.hibemate.SessionFactory;
import org.hibemate.cfg.Configuration;
impsrt org.hi5e~s~e~clsssic.Session;
.
*%
. . . . .,:
. .....
: ..
. ,. ... ..'.:..
. .
... .
1
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
String recordId = request.getPararneter("record1d");
Configuration config = new Configuration();
config.configure(.);
SessionFactory sf = config.buildsessio&ctory();
Session session = sf.openSession();
session.beginTransaction();
Print\Vriter out = response.getWriter();
. . . .
...
....
....
.
4.
5.
6.
7.
guid
sequence
identity
select
oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost: 1 52 1 :XE</property>
<property name="connection.usemame">system</property>
.
<property name="cormection.password">great 123</property>
.
..:
..
.. .
. ..
>
..
..
. ::i... :
.. , .:?
? .
org.hibemate.cache.NoCacheProvider </property>
Lara Technologies
080-41310124
46
<property name="hbm2ddl.autot1>create</property>
<mapping resource=l~co~larg/Book.hbm.xml"/>
.
.
.. .. .-... ..,a. .
. .
priva$<:bteger
id;
: ....
.............
pri~ate,;Sp-i~ng
, ...
.......
boomame;
........
private Stibg.
.,,... .,:
.. , bookAuther;
private D&@@.
bookprice;
I."
. ,
>-;-.r .<
. . .. .. .
.. ? - :
L.
. . ,
thii:idi=:id;
......
.; . !1
.-'.,:.
. . . . ..
...,
.b :. ..
. '.' Li.y
>%
.:+
'
.~. 3
. . . .:. ..
.
.,.
::.:
. !
>
.>
.,
.
.
.,-
1
public Double getBookPrice() {
return bookprice;
Liwa Technologies
080-41310124
package com.lara;
. ..
,
import java.io.lOException;
import java.io.PrintWriter;
import javaxkerv1et.ServletException;
import javax.servlet.http.HttpServletRequest;
.. .
. . :.
:fmport javax.servlefrhttp.HttpServ1etResponse;
>..
..:
iim port.org.hibernate;SessionFactory;
im'port org.hibernate.cfg.~onfi8uration;
import~~o~g.hibernate.classi~ession;
. ,....:
:_...
.::,
';?
. .,.
.,. ...
.
. ...> .. .
public clas~hJ@,ook~ervlet
.....:.
.
'I:
1'
..
..
..
.
..
public v o i d ; ~ d d ~ o o k ~ e r v l e t ( )
i
.super();
,;;:
:
i
,
\ .!: ..'
I.',
..
..:,,:.,:
.;'.
!;.:
protectedjvoid
.,:
doPost'(HttpServ1etRequestrequest,.
HttpServletR&sponseresponse) throws Ser~letException~
IOException
.
...
String id=reqvest.getParameter(i'id");
String name=request.getParameter("name");
String authe~request.getParameter(c'auther");
String price-request.getPara~neter("price");
Book book = new Book();
bbok.set~d(lOO);
book.setBookName("JAVA");
Lara Technologies
080-41310124
session.ciose();
Printwriter o ~ t = r e s ~ o n s e . ~ e t ~ r i t e r ( ) ;
Outtprintln("check in the database.");
All algorithms will *notsupport all the databases only few algorithms
only support particular databases.
i a r a Technologies
Create one folder where ever and copy that cfg file into that folder then
come to the program and at the time of configuration give the path of the
folder.
EG:
Srcl
1----cfg[
1--hibernate.cfg.xml
Now come to the program
Configuration config = new Configuration();
config.configure("config/hibernate.cfg.ml");
..,
Cascade types:
. .
. .. .
7
.
:....:
.. .. '
:.
.. .:,.. . . . .... . , . . .
,
.r
. ,
... . ...
.
.>. :
. ... . . .%?-*.
.. ..:.
.. . ..
..
. .
.
: :.
.. .
'.':.:;.:
~all,~~~a$e,merge,save:update,delete,lock7refresh,eviet7replicate.
.
,
? . /
.,
..!.
. . . , 9
.. .
. .
. 6 ',<, .
..
'
.:.< ..:>.':
,
.
.
. . .:
~onstra~~,ed;:(optional)
specifies that a:foreign key constraint on the primary
.
.:
.:.
key of the mapped table references thetable of the associated class. This
affectsthei.order
.:-:
in which save() and delete() are cascaded, and
.:option
.
: determines whether.the association may be proxied (it is also used by the
schema export toblf, . ..
.
. ...
.
.>
.. . ...
....
If we
constrain~d+'false" then in USER DETAILS table we will not
get refren&key .i .e
. :foreignkey
i
but we will getdata
The generatgd:qjiery is ; ;'.:,.
create t a ble USER-DETAILS
,. .
..
...
I'
"USER-ID" VARCHAR2(255),
"USER~EDUCATION" VARCAAR2(255),
"USER-DESIGNATION" VARCHAR2(255),
b'USER-ADDR_'ESS" VARCHAR2(255),
PRIMARYKEY(''USER_ID7')ENABLE
)*
If we give constrained="true" then we will get refiencekey i.e foreignkey
and data
create table USER-DETAILS
Lara Technologies
080-41310124
:.
1. Relational Mapping
(a) One-to-one ~ a ~ p i n g
(b) One-to-Many and Many-to-one Mapping
(c) Many-to-many Mapping
2. Polymorphic Mapping.
(a) One table per Class hierarchy
(b) One table per Sub-class
(c) One table per Concreate-Class
3. Component Mapping.
hi bernate.cf~.xmi
<?x;il version=: 1 .Of encoding='utf-8'?>
<!DOCTYPE hibemate-configuration PUBLIC
"-//Hibematemi bernate Configuration DTD 3.O//ENH
" h t t p : / / h i b e m a t e . s o u r c e f o r g e . n e t / h i b e ~ i o n - 3.O.dtdW>
Lara Technologies
- -_ l - I I
080-41310124
. .-
.,
--
52
;TT
'
oracle.jdbc.driver.OracleDriver</propeny~
<property name="connection.urlrr>
jdbc:oracle:thin:@localhost: I 52 1:XE</propem>
<property name="comect ion .username">system</prope~~
<property narne="comection.password">great123</property>
<!-- JDBC corm'ection pool (use the built-in) -->
<property name="connection.pool~size">l</property>
<!-- SQL dialect -->
<property name="dialectH>
.
org.hibemate.dialect.~racle~ialect</~ro~ert~~
..
..
.- .
..:.. .
. .
.
'
,. .
.*.
.-
..,
5 .
org.hibemate.cache.NoCacheProvider</property>
:. .
.. < < ..:
'.i. .., !?
'
,?
.. . .
. .
.:
..
.
.
.
._
.. ... .. . .
8 . .
...
<!-- ~ro~&nd.i&-creite::the
.. . .
database scbema on startup -->
<property na~e="hbm2ddl.auto">create</~roperty>
.
.
.
,.
Eara Technologies
package corn-iara;
public class Person
private String personld;
private String personFirstName;
private String personlastName;
private Integer personAge;
private Mail mailld;
public-String getPersonTd () (
return personid;
1
t
public String getPersonFirstName() (
return personFirstName;
void setPers0~,4ge(~lnteger
personAgej
this.person,.?lge = personAge;
! r ~ biic
ti
>
r.
880-41310124
./
--
54
-.
,
IT-
'
package corn-Iara;
public class Mail
{
private String mailld;
private.Person person;
private String password;
priva t i Date createdTirne;
priva'te.String
.
personId;
.
I.
.-, :.,
..
. . ..
.
.
.I
,.
-.
pu blkiP~~io,n
......
., getperson() (
ret.ultn-person;
.:,.
:...I*:.'<.
<.,.
.:.
. ,
.......
. . .. . .. .
.,j-, . ':.. 7
public void:~se~~erson(Person
....
person) {
this. pefson
= person;
. . .
' ;.
)
.. I.:.i;>,p::. . .f;.?.+::::
..
..: ...- ..
pu a~c~stri~g~~~<tP~ssword~)
.
\T
,;:
>..<..
. *. .?s.
+.,.:,<.
:.
~6ku~.,password;
. .:....
... *.,....
. .;...
.........
......
.'.,.. . .. ,.! .. .,.
;.
..
080-41310124
return personId;
j
public void setPersonId(String personld) {
this.person1d = personld; . ,
'
>
.=
package com.1a1-a;
., , .
import org.hibematk.~ession;
.
import o r ~ ; b ~ a t k . ~ e s s i o . n ~ ~ c t o ~ ;
import org.hiber&te.cfg.configuration;
:.:
080-41310124
080-41310124
-->
"
,:
'
...
. . . .
. .. . .. . .. . .
' - . .:. ,.;
. .,<*>>,.
A
i!
'
.
.
.7..
.$- .:
<!-- ~ ~ .....L7
h.)?. & & 1 1SQL
# ~to~stdout
~ ~ -->
~ ~ ~ d
... hS;2:*.:
<property dgme="show
.........
-,sql">true4property>
:.
?,:..e!,;.: .....
,$.:.
.,<:.. .,
.
$.,:7...:,?,.:;
,,,.$,..
.... i.,.....
.
<k,Drop an&xe&reate
the database schema on startup -->
.. '
<pzoperty
.
.
.
nard~=~hbrn2ddl.auto">create</property>
....
>,! '
; . :
>
. .
,>..
...
....
<
,
. . .:i
:.-,...
: . : .. .. .. .. .. . :..
. .. . . .;.,;
,..
*'lh!..;.
. .: : . : . ' . hbm.xml"/>
<mappi,n~~~spurce~.~~ci"om/Iara/User.
..<:. k:.,. =...:>. :.,. .r".i.;.
. .......
...
.?,
.,.
<mappingi:,res~urce=''~c8mjlara/(
......
<.:. . .:=.,
.UserDetails.hbm.~l"/>
. .
.>..,.
.
-,a
;
.&]::".:, ...: .. .
.>
:&
;
. .. . . . .. .' ... . .
,
<.%?libemate-configuration> 7
Vser.fava
.-C
package corn-lara;
public class User
Lara Technologies
.,?,
..
>
>
>
080-41310124
1.
1
public void setDesignation(String designation) (
this-designation = designation;
-~ser.hbm.xmi
7
<property name=ll~~erFirstName"
colurnn="USER FIRST-NAMEn!>
<property name="userLastNarne" column="^^^^-LAST-NAMEu/>
<propeAy name="userAgeVcolumn="USER AGE'/>
<property name="userEmail" c o l u r n n = " ~ ~EMAILnA
~k
<property name="userMobileNo" column="^^^^-MOBILE-NO"/>
Lara Techno:ogies
080-41310124
62
< n.,,t ~ n
- -~-r,ngme="':GSer
tv "i
..:.
,:.
::
. .
. ..
,?...
,.
:
:'
....
...
.:I,.'
....
<on-to-one'ndme="user"
.. .
c~ass="com.lara.user"constrained="tmel'!>
.
Manager.iava
i:
package com.1ara;
import org.i~ibernate.Session;
import org.hibemate.SesslonFactory;
import org.hibernate.cfg.Configuration;
public class Manager
{
E a r a Technologies
080-41310124
user.setUserEmail("manikanta@gmail.com");
user.setUserFirstName("MANIKANTA");
user.setUserLastNarne("CH0UDHARY");
user.setUserMobileNo("9986340298");
UserDetails ud = neiv UserDetailsO;
ud.setAddress( "TPT");
ud.setDesignation("PM");
ud.setEduc("BTechl');
ud.setUser(user);
user. setUserDetails(ud);
iS'vn5g~irajionconfig = new Configuration();
config.configufe(); ;
.:.
..
"!
. ,
:
;
SessionFactory sf = ~ - c o n f i g $ ~ i l d ~ i s. .s. ~ a c t o r y O ;
Session session = sf.open~ession();
.
. .
Foreign key d e v e l o ~ m e n
t:
Lara Techiloiogies
680-41310124
. .
Usually foreign key is used for to join the two tables In this example we
want to join User and UserDetails. In this we are using one-to-one join if one
modification is done in one table then that modifications will reflect back
to another table also.
... ..
::.r..j.::;.,
: .
:.;.. .. . .
..,'.- .. :
.':,
.. . ....
...
.;
....*,...i. '
hi bernate.cfp.xml,
: >; '
....
.:
.
;:.-;. ,*.,:, . . .
.. ... ..:
<?xrnl ve&on=l.1;.01 en~od~r1utf-8t?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernaie/Hibemate cohfigurltion DTD 3.O//EN"
"h~p:/hibemate.~,~urceforge.net/hib~ate-configuration-3.~.dtd~t~
, . ,.
..
?
<
'
...
. .
'
<!--
080-41310124
org.hibernate.dialect.OracleDialect</property>
<I-- Enable Hibernate's automatic session context management -->
<property name="curent-session-context-classl'>thrzad</property>
<!-- Disable the second-level cache -->
<property name="cache.pr~~irle~-c!~!:s">
org.hibemate.cache.~o~ache~rovide~/prope~~
<!-- Echo all executed SQL to stdout -->
(property name="sho& -sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
package com.lara;
public class Student
{
private Integer id;
Lara Technologies
Batch-iava
package com.lara;
Lara Technologies
import jma.util.HashSet;
import java.util.Set;
public class Batch
private Integer id;
private -StringbatchName;
private Set students = new Hashset();
public Integer getId() {
return id;
.I
public void setId(1nteger id) {
this-id = id;
1
public String get.atchb!me() (
return batchName;
public void;add~tudent~o~atch(~t~dent
std)
ff(students -- null)
1-
students.add(std);
Lara Technologies
import java.uti1.Tterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg:Configuration;
public class Manager
{
public static void main(String[] args)
{
Batch batch = new Batch();
batch.setId(3);
b~tch.set~atch~arne(~~~~~AI~2~~");
Strrdefit std: = nevi' Student();
std 1.setFirstName("MANI");
std 1 .setLastName("LARA");
stdl setAge(23);
stdl .seBatch(batch);
batch.addStudentToBatch(std 1);
Student std2 = new Student();
std2.setFirstName("RAMUM);
std2.set'~astName("~~~~~1);
std2.setAge(23);
std2.setBatch(batch);
batch.addStudentToBatch(std2);
'
i a r a Technologies
//Randomly
.
. . .
UPDATE.
/*
..
.&:Batchbatch =(l3at~h)se~sion.load(~atch.class,3);
;,Iteratori t = batch.getStudents().iterator();
Stbdent std = null;
if(&has~ext())
*
.
{
std =.,(Student) itsnext();
;:
..
s t d . s e t ~ i r s t ~ a m e ( ' ! ,MANJKANTA");
C~'~~~
s,ession.saveOrUpdate(stdj;
..
se,~sion.~et~ransaction().commit();
. ...
* I . ; ..
.
. .
. . ..,
..: .. .....
//PARTICULAR
..
RECORD UPDATE
<;
.,
I*
..
.
.
.: . .>-Batchzbatch
=
(~atch)session.load(~atch.class,3);
i ~ t e r ~it=b;tch.get~tudents().iterator();
or
std = null;. .
.
whilk(itha~~ext()) :
.
t
std ='(Student)it.next();
if(std.getId() =2)
C
break;
1
1
std.setFirstName("CHANGEDAGAIN MANIKANTA");
session.saveOi-Update(std);
Lara Technologies
080-41310124
71
:. .
std = (Student)it.next();
session. flush();
session.close();
1
"
Lars Technologies
080-41310124
72
std = (Student)it.next();
if(std.getId() =2)
break;
batch.getStudents~.remove(std);
..
hibernate.cfp.xml
.
.
,
'..:
12
,...
<--.~
.
..
..
,.<?xm]version='i:~~~~nc,9ding='utf-8'?>
<,;', :< ,:;
:<!DOCTYPE hibem'kte-Sonfiguration
PUBLIC
:>.+ : Y
I)-//Hibemate/'Hibehate Configui-ation DTD 3. O//EN"
"htt~:/mibemate.sourceforge.net/hibemate-configuration-3.0~dtdt1~
!....
. C.. ..>,.-;,. ".-:
.. ::.. +
:
.
.
: ..:
' ;
..
..T
. .,
.:<
.;!
<p~o~$rty;n~e=i!connection.driver
. .
-classn>
...
. . .
om
.m
ysql
.jd
bc,Driver</property>
. ..: .:..:.;.e:
<property n&e="cohnection.url">
,..,.. : . .
~ : ~ j d b ~ : r n ~ ~ ~ l / l o c .ara</property>
alhostn
<property n a r n e = ~ ~ e c t i o n . u ~ ~ r n ~ e ~ ~ ~ r o o t < / ~ r o ~ e r t ~ ~
<property name='i~~nnection.passwo~d"~~/property>
...
'
. .. .
, ..i,'C,..
'...
.,...
..
<property name="dialectn>
org.hibemate.dialect.MySQLDialect</property>
Lara Technologies
org.hibernate.cache.NoCacheP~-ovider
clproperty>
<!-- Echo all executed SQL to stdout -->
<property name="show-sql">true<lproperty>
<!-- Drop and re-create the database schema on startup -->
<property name="hbrn2ddl.auto">update</property>
<mapping~resource="com/lara/Batch.hbm.xm1"/>
<mapping resource="com/lara~Student.hhm .xm! "!>
package com.!ara;,
import java.utiI.HashSet;
import java.util.Set;
public class Student
i
private Integer id;
private String stdName;
yri-vat'e String stdQualification;
private Set batch = new Hashset();
080-4131 0124
. .
..
this-id = id;
1
.
I>
.
-.
.;
+.. .: . ..
. ..
. . .. . ,..
. .
.
package corn-lara;
import j ava.util.Date;
import java.uti1 .Hashset;
import java.uti1.Set;
public class Batch
private Integer id;
private String batcMaine;
private Date startDate;
pri;ate Integer duration;
private Integer fee;
private Set participents = new Hashset();
Lara Technologies
080-41310124
1
public void setld(1nteger id) {
this-id = id;
1
public void addParticipentsToBatch(Student std)
{
pal-ticipents.add(std);
std.getBatch().add(this);
Lara Technologies
080-11310124
I
I
t
participents.remove(std);
std.getE3atch().remove(this);
<property name="stdNameWcolumn="STUDENT-NAMEw/>
<property name+"'tdQualification" column=
"BATCH-QUALIFICATION"/>
,
080-41310124
at
package com.lara;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import org.hiber-nate.Session;
import '6rg.hibernate.SessionFactory;
import org.hibe!nate.cfg.Configuration;
public class Manager
public static void main(String args[])
,l
//add();
llreadBatchWiseStudents(3);
i/LipdateParticularStudent(3,102,"Changed MAMKANTA");
dele1ingRecord(4,104);
\
private static v ~ i da d d o
i
Batch bl =new ~ a t c h ( ) ;
bl .setId(3);
bl .setBatchName("REGULAR JAVA");
bl setDuration(l20);
bl .setFee(5000);
Date date = new Date();
Date startDay = new Date(date.getTime() +
7*24*60*60* 1000);
b 1-setStartDate(startDay);
Batch b2 = new Batch();
-b2.setId(4);
b2.~etBatchName("SI;Rr?u'Ci");
b2.setDuration(3 0);
b2.setFee(l000);
Date SpringstartDay = new Date(date.getTime0 +
10*24*60*60* 1000);
b2.setStartDate(SpringstartDay);
Student std = new Student();
std.setId(l0 1);
std.setStdName("RAMESH1');
std.setStdQualjfjcation('~B.Tech");
b 1.addParticipentsToBatch(std);
b3.addParticipentsToBatch(std);
Lara Technologies
050-41310124
SessionFactory sf = getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(b1);
session.save(b2);
session.save(std);
session.save(stdl);
session.save(std2);
session.save(std3);
session.getT1-ansaction().cornmit();
sessiofi.flush();
sessjon.close();
. .>
....
. .. .,..
.:
1
private static void readBatchWiseStudents(int i)
SessionFactory sf = getSessionFactory();
Session session = sf.openSession();
session.beginTi*ansaction();
.
.
.
..,
....
,.. .
..
,.
:-
while(allStudents.hasNext())
std = (Student)allStudents.next();
System.out.println(std.getld()+":"+std.getStdName());
t::
.
....
~ e s s i o n ~ a & o r y s getSessionFactory();
f=
Session session=j.sf.open~tssion~;
..
session.SeginTiznsa~'iion(?;
~,etqh.
..:
........ batch = (Batch)session.lo~d(Batch.dlass,batchNo);
Iterator<;it= batch.get~artici~ents().iterat&.,-: :
Studkptlstd = null;
while(it:hasNeat())
.
{
a,..;
'i..
Lara Technologies
080-4131 0124
batch.getParticipents().remove(std);
Lara Technologies
{'
'1.
. .. .... ..
Now d&$lop
. .
one hb&
. . . . . . file for every Pojo:class in one package.
.. . . . . .
. . .<.
.
,.
.,
i.:.:,<:t
".
::?
' :;:.......
:
:
;
In hiFe6ate
.--.
....... w ~ t h ; o uprimary
t
key we can't develop any table.
. . . .
.,; ,.. :.:.
In hbrnfil6;we
:,.
wait totake care about all attributes for creating table.
In person??iiblewe want.&omake
...
PERSON-ID column as
autoincrerneni:.:&d
.... primaryliey ..:thenuse id and generator tags.
.......
..: .
. . . . .
iy,:,
.,
:,.I..
:,r< , .
'
.
:
. ..
. . ..,.
<hibernate-mapping>
Lara Technologies
.,
080-41310124
... ...
..
,c.::>.
Now developgne
.
dl~entpro,oramm
.
. .
.. . .
package.
. com.lara;
.
1.
..
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hib.e&,ate.cfg.~cnfiguration;
. .
. . ..
..
..
..
public,class Minager (
:.:publicstatic void main(String [1args).
.{
. .
.. .; .:.:;Rerson p 1
new p&on ();
' & j s e t ~ e r s o n ~ a m("RAMESH");
e
. ...
p ~: i;. s e t ~ e r s o n ~(29);
ge
.
..
..,
. .
..
, ,.
.. . .. . ..
.i. . '.
.
,
.
.
..
.-.
i a r a Technologies
config.add~esource("com/larai~erson.hbrn~xm1~~);
System.out.println("Hibernate got configured.");
s SessionFactory sf = config.buildSessionFactory 0;
System.out.printIn("SessionFactory got configured.");
Session session = sf.openSession ();
System.out.println("session object got configured.");
session.beginTransaction 0;
System.o~t.prjntln("transaction started..");
. . . .
. .>,
. ..: . .. I. .. ...
:
.-; . .<. .<,.,
. ::
. . .. y,<.*..
.- ;..
-:&
s,.
....
n...
4;:
.;.
F.l.G
.
*I;.,
%.
y~
.......
:....
.,
-;
. .
..?
5 . I
. .. ...... .
. . .-.
;;
.
.
--.
...
-.
.
.....
. . ..
..
,
.:
: ..,.,... .....:.. ..
... > .. .::
v, ..::
:. .
.:.
. . ,..
. .
. . . . . .
. ..
.... :.
....:
';;!,&.
, ..
....
,.-:*
":.".
.:?;?*;
.,
..: ;<:;:;:.;,
. .
..-. .. . . .. ..
..:.
*..
. .... ...5:. .:,'
.
2::
..
,,.
i .
~.:?
; :.;z. ,:..;.."":
,,:.. .>:.:,
.<;.
.. . .. ... .. . ..
..:':$..-.
. ,: . ; .
.. . ..
-..-. . .
:,
: ':
:
:
sessi,on.get~ran~acti~n~~O,cornmit
0;
System.out.println(~transactioncommitted..");
.sess~n.flush0;
System-out-println("session got flushed..");
~.~ess~on.close
...
..
0;
:S~stem.~ut.println(~~session
. . .
got closed..");
/;.!
'
',
. ..
,.. .. . . . .'.. . .
.
Lara Technologies
080-41310124
>
;!
Lara Technologies
080-413!0i24
htt p:!/j~vaeasq.toal!.cdsnt
1
&w develop one hbrn file for every Pojo class in one package.
<?xrnl version=" 1.OH?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibematemibemate Mapping DTD 3 .O//ENM
"http:/hibernate.sourceforge.net/hibernate-mapping.C.d:dn,
<hibernate-mappi ng>
2.3' .
package corn-lara;
Lara Technologies
080-41310124
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibemate.cfg.Configuration;
public class Manager {
public static void main (String [I args)
SessionFactory sf = config.bui1dSessionFactory 0;
System.out.println("SessionFactory got configured.");
Lara Technologies
session.save(order);
System.out.println("person object saved.");
.
.
.
'
..
sesision.flush 0;
~~stern.out.~rintln("session
got flushed..");
..: . sessi.on.close
...
();
~~stem..out.~rjntln("session
. ...
got closed..");
..
1
J
:. .
.
. .;..
. . . ..
.
.
.
..
. ,
// To readbll
, .., .(.(
the records
, . .
,.
I*
~ i s t < ~ r d e r > v=
: l session.create~rit~ria(com.lara.~rser.class).list();
~~t
for(0rder order::
.
. list)
.. .
.:.
. . , ..
:{.
~~st~~.o~~~~ntIn(order.~et~rder~d());
. , . . .:,
Sysie,m:.o~t
....
.&ntln(orderfgetOrder~ame());
. .
. .
I.
Sy~tem.~ut.println(oi-deiigetOrderDzite());
_ . . .
~~stem'.out.~ri~~tln(orde~~~~t0rder~escri~tion());
~~stem.out.~~intln(order.~et0~der~rice());
~~stem.out.~rintln(order.~et~rder~~())~
.
...
... .
(OR)
.
Criteria ctr = session.createCriteria~com.larz.Order.class);
~ i r . a d d ( ~ e s ctions.eq("or&~y\
tri
abc));
.
..
..
iist-&rdei>
..
For(0rder order : list)
{
System.out.println(order.getOrderId());
..
.
. . . .
.. ...
.',3 .
Lara Technologies
080-41310124
Ctr.add(Restrictions.sq1Restriction("orderDescption7,1ike "%h%"
abc));
Query query=session.createQuery("fiom com.lara.Order");
List<Order> list = query.list();
For(0rder order : list)
{
System.out.println(order.getOrder~d()j;
System.out.println(order.getOrderName());
System.out.println(order.getOrderDate());
System.out.println(order.get~rder~escription());
System.out.println(order.getOrderPrice());
System.out.println(order.getOrderBy());
Polvmorphic mappings:
They are three types ofpolymorphi
:c-mappings.
...
.! :: 1. One table per class.hierarchy.
.
2. One table per subclass.
3. One table per concrete class.
.
. .
- .
'.
..
..
'
Lara Technologies
<property
name="connection.url11~jdbc:mysql:!/localhost/lara~/property>
<property name="connection.userriame">root<lproperty~
<property name="connsction.password''></property>
<!-- JDBC connection pool (use the built-in) -->
<property
~~me="cache.provider~c1ass">org.hibernate.cache.NoCacheProvide~/prope
*Y>
<!-- Echo all executed SQL to stdout -->
<property name="show-sqlV>true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto1'>update</property>
package com3ara;
1
.
'
...
pa.ckage com.lare:;';:.
. . . . . . . ..
,.. ..
,
.--
.-
.
. . ....
.. ..
:>~.l
.>.
..&
. ..... . . . . .. . ...
.
..
.
..
.:
priva(e:String
empNo;
:..
. . .
. . . .:.
.
.
.. . .. .
. ..: . !*'
. ,
.. 2 .: '.'
'?
. . .
:'>.
'"-.. '2:.
.,.
public st&&
. . . g. k t ~ m p ~ o (
()
retu..........
~:n,g.empNo;
..: ;. 1...
.
.
.:.
.::.
.
,
'
.;.
... ,
. .
. ..,.; .
:i
.
... .
.. .... ..
,.
.. .. .. .. ..
p u ....
b-VI::..y4t
~ ~ c ~ ~..r;o i .d .~ s ~ t ~ m pempNo)
~ o ( ~ t(r i n g
..
..; :;-.:;.-'~~~thlsse~'pN~.:=-:&mpNo;
I;.,;. .: ;.
.
s.~.!.~
1:
. . .
:,,.;5:,
;,.
.. .
......F.
.
'..., ,. .
... .
_ _ .:
,. .
.
. . .:
:.\
. :
.
. ,:
.".
..
PermanentEmp10vee.iava:~
package corn.lai-a;
public class
-,.
PemanentEmployee extends Employee
C
private Double allowances;
<id name='lidVcolumn="PERSON-ID">
<generator class="native"/>
<!id>
<property name="firstNarne" column="PERSON-FIRST-NAME"/>
<discriminator column="P -TYPE type="string"l~
<subclass name="EmpioyeeWdiscriminator-value="e">
<property name="empNon column="ENG-NO";>
<!subcIas+
<subclass name="PermanentEmployee" discridnator-value="pen>
<property name="allowances" column="ALLOWANCES'~>
<subc!ass>
</ciass>
</hibernate-mapping>
*
package com.lara;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Manager
public static void main(String[J args)
,.
':'
-,
...
.. .. . .. .
...
.,.....
<.-.'
. ,... ;...-;i. ::
: .
. !:::;......:>-,?>
.:.. -......-;<k..!;...
<,>:..:, ;. .-.-;> :. ..
). :,, ?
:$
!:.;
.!
L...
;:.. . .
'., ,.....
: . :
..
.. .
:,
.. .. ........
.
.
.
.
,. .
..
.,;..I
&
.
. i
P e r s ~ ~ . h b ~ . ~ a ] ' : ; ; : -.
. ... .
<class name=l'Per~~nll
table="PERSONS">
<id na.me="idUcolumn="PERSON -ID1'>
<generator class="native"/>
Lara Technologies
080-41310124
<jid>
<property name="firstName" column="PERSON FIRST-NAh4E"/>
<joined-subclass name="Employee" table="P-E";
<key column="id"/>
<property name="empNoVcolumn="EMP-NO"/>
</joined-subclass>
<joined-subclass name="PermanentEmployee1'table="P-PEW>
<key column="id"b
<property name="allowances"
co!umn="PERMANENTEMP-ALLOWANCESv/>
</joined-subclass>
</class>
</hibernate-mapping>
3)One table per concrete class:
<property
name="connection.driver -class">com.mysql.jdbc.Driver</Froper~
<property
name="connection.ur1">jdbc:mysql://localhost~lara</propert~>
<property name="connection.usernarne1'~root</property>
<property name="connection.password~/property>
<!-- JOBC connectior? pool (use the built-in) -->
<property name="current~session~context~class">thread</property>
<!-- Disable the second-level cache -->
<property
name="cach~.provider-class">org.hibernate.cache.NoCacheProvide~/prope
...
rtY'
.
.
..
..,
. . . . . . .
... ,.,...
;,.:
,:..,,... ,
>
:..: .....
.....
.
.
. :..
. .
. .
...
. . . .. .
...
....:..
..',?-
. .,
.lC._i..
'
. . .
.
.
. . .
..
</session-factory>
. . ,.
.
..
....
. . .
....
<mapping
.. ,. r.esource="com/i ~ r a / ~ ~ ~ ~ . o n . h b r n . x m i ~ ~ / >
. . .
.....
........
.
..
-.
..
.:.. ..
package corn.1ara;
put!ic class Person
{
private String id;
private String firstName;
privatestring IastName;
private Integer personAge;
Lara Technologies
080-41310124
1
Person.hbm.xm1
<?xml version=" 1 .O''?>
<!DOCTY?E hibemate-mapping PUBLIC
"-//Hi be~xateiliibernateMapping DTD 3 .O//ENW
"http:/hi b e m a t e . s o u r c . ~ f o r g e . n e t / h i.O.dtd
b ~ ">
Lara Technologies
080-41310124
import java.util;List;.:-. ..
...
import org.hibemate.Quer-y;
import org.hibemate.Session;
import org.hibernate.SessionFactory;
import org.hibeinate.cfg.Configuration;
public class Manager(
public static void main(String[] args)
!
/*
Person
pl
. .. ....
.
*.
. .
.,...
= new Person(j;
.
1
_
Lara Technologies
080-41310124
99
If we want to make two attributes as primary key field then create separate
class for two attributes and that class should implement Serialization and
override equals() and hashcode().
EG:
hi bernate.cfe.xm1
<?xrnl version='l.O1 encoding='utf-8'?>
<!DOCTYPE hi bemate-configuration PUBLIC
"-llHibemateMibernate Configuration DTD 3.O//ENV
"http://hibernate.sourceforge.ne~ibemate-confi~ura~ion-3
.O.dtdV>
<property narne="dialect">
Lara 'Technologies
080-41310124
org.hibemate.cache.NoCacheProvider~iproperty>
<!-- Echoall.executed SQL to stdout -->
<property name="show-sqIn>true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbrn2ddl.auto1'>update</property>
.
.
..
. .
'I..
::.
.
. . . .
. .<
SI
080-41310124
package com.lara;
import java.io.Serializable;
public class PKField implements Serializable
(
p'riva t e String id;
private String firstName;
public String getId() (
return id;
....
..-
...
i a r a TechnoIcgies
...
. .
l :
.:;
'
. .
.L
?..
. '.
.>
.. .. . . .
. ,.
..:.*, .;"....
:. . . .
. ,
>.......
,.b>
.. !L!
.
.
! : i(>".
;c': Sii;q . . . . . . . . . . . >..
>
; ;&:,>~'$~.$:*>,.z. * . . <*;:;.,.: >. ,
$.$ .:. . :. .
'?.Y
,.:'~:.*:i~..rz,::,l~
,<5:j8zs>:;;2$>'>.
. .
. .;$.;:?: ., >.
>
6
.
."L
c
...
<!..
...?.>:Y.L
. . .. 5:gF,.!
~-:-
:,.
. .. . ..
< .. : :
configuration
. . . . .
config = new Configuration();
confi&onfigure();
< ..
, ;,;.
<
.:.....
.i-.
...
. . .
. .. . ,
.....
* :;
.;i.q:. ...'.-.
. .,.
><
.....
.. ;. ;: :.' .
<, &
>
.:
'
. j ::.
....
4
. . .<<;,.
'
4..,.! ..?.
SessionFdetdry' sf ,=~&6i;fig.~buijd~ession~actory();
- 2 .;, C
. . . . . .
Session s & S s i o n . ~ : , s f ~ o p e n ~ ~ ~ ~ ~ ~ J ; ~ ~
,. .
.,:,
.g
,, ,
..
.
i
,
.
. . . . .
. . . .. . . .:
: ;:<.; ...;;; .if:'.. ; :.
.*:...
...; .
. ... .
+ .?* ..:,!>
... !..
:.:
.,
.:. :..
:
.,: .:.
. . ...
.'
.. ,
r.
.;
sd38ion.begin~ransaction();
.:..: ..,.
. .
. ;i.+<.
. .
- .,<...
...
....
I.
8
: ,
<, .: .
+..
,..:
- .:...,.,..
..<'3
. ,..
.~
. .,
._ :_
. .,, .:,.k :+: . I.. .-
.
.:
String&@
= "form corn.lara.Person p where piage-?";
* :,.t...;. '.;.I
:.:
. . ....
Querysyery = session.createQuery(hql);
:. --. ...$..
;.,_.:
: ..:_
..:>.
....
.?i
query:-~gtString(0,
"2
9");
:..
.
i..:$[?.:-.,
\
...~
.;.:t;:*
-,.
,,..
. :., ..:
<:. , ..
>....
:...*
<:. .?:.
'!
....
.........
.....
. .',#,.>
:
. ..,.' :. .
.........
.:.,!"
.,
.,
. . .
'I
.,. :
. .. ..
,... 7 .
.
. .. ... ..
.... . . ;,.. ..,. ..:
Iter.ator<Rerson>
it = quejr.iterate(>-;-i.. . . . .
. . . .
-.
whil&(jt.ha&&xt())..
.
.. .. . .
. i."'. . .
{
.....
P<&~=
.~
~ ~ ~ t e m . o u t . ~ r i n t l n. (. ~. ... g e. t p ~ + i ~ l d ( J . g . e t F i r ~ ( ) ) ;
,
~:
.>. .
. . ?
;:.$
,. .
!,
,. '!
.:.
.
*
...
session.flush();
Lara Technologies
080-41310124
..:
:
....
..)
',:
. . ..", .
session.close();
I*
// To read a record
PKField pk 1 = new PKField();
Pk-setld (1 01);
Pk.setFirstName("abc7');
Person p2= (Person)session.load(com.lara.Person.class,pk I);
System.out.println(p2.getLastName());
System.out.println(p2.getPersonAge());
System.out.println(p2. PKField O.getId());
System.out.println(p2. PKField ().getFirstNarne());
-
*/
/*
/*
P2.~etFirstName("LARA");
P2.setAge(6);
Lara Technologies
080-41310124
105
,'*
// To delete one record
PJSieId pk 1 = new PKFieldO;
Pk.setId (1 01);
Pk.setFirstName("abc");
Person p2=(Person)session.load(com.lara.Person.class, 1);
P2 .setFirstName(!'LARA");
P2.setAge(6);
P2.setLastNaine("Tecnologies");
Java Annotations
Lil
080-4 1310124
ra Techno:ogies
....
-7--
7-
--
I,:
y;.:
8- r . , r
, ;;:.. . 'Annotation-based
. .. development is certainly one of the latest
.
. .. ..
.,. : ..
.
.
.
.. .
.. .
:
+\
.,
..
:.
l a v a d~e:y,elopment~?ends.
Annotation-based development
.,...
refieves:3:ava
.,; ,,:..,...
develo'pers from the pain of cumbersome
configuiation.
Quoting from Sun's official site, "It
. ..
; . ..(annotat%"-based development) lets us avoid writing
i : boilerplate
. .. cqde ,.under many circumstances by enabling tools
..t'&i.'
:,.-?,'
:generate it .from annotations in the source code. This
leads-to a decla'kative programming style where the
programmer
o../...
sayswhat should be done and tools emit the
code to~,~:do:;'~it.l1
simply speaking, annotation is a mechanism
for asso$ating. a met&-tag with program elements and
allowing the compiler or theVM to extract program. .
behaviors from these
.. . ... .
annotated elements arid generate
....
. .when n & & e ~ s i y -I ~
n.the first part of
interdependent codes
this three-article series, I'll describe some basics of
annotat-ion, their benefits, as well as some exampie usages.
;
..-
?:'
%. .
.;. ...
: .
.. .,
,?. .
:: : .
The Basics
. .. . .
..
-A
080-41310124
!67
,.
...::,.:,;::......_.
:2d: <,: ..
,. ~.
..:.:;,,; ;..:
.
.:
...
. .. . .:.
. .. . .
... 5, .;:
.
:
.
I
Annotation Types
,. :
. ,,
Example:
.
Usage:
Lara Technologies
050-41310124
p u b l i c v o i d mymethodo
. .
. . ..-.:.. .. . ...
. . . .
@ i n t e r f a c e MyAnnotation
. .
....
.,
-:;:,
S - t r. i n g d o s o m e t h i n g ( ) ;
.
1
Usage:
.. . .
.,
.,
,.
:
.A:
. . .:
. .
..
..
. . . . . . .
. :. ,.
. . . .
..
...
p u b l i c @ i n t e r f a c e ~ ~ ~ n ' n o t a t i{ o n
S t r i n g dosomething ( ) ;
i r ? t c o u n t ; S t r . i n g date ( ) ;
1
Usage:
@ M y A n n o t a t i o n (doSomething=llWhat t o do",
count=l ,
date=1109-09-200511)
. .
:.
.?,.
public void m y m e t h o d o
1 <
Annotations
Simple Annotations
Lara Technologies
080-4131 0124
110
.:
> .
..
'
~:..ald,w
...
. . ex3
+,.
, a p ~ ~ x a n::::aueu
o. , .
uor~eqouueb u r q s a ~ ,,
+ . , ? ~ ) , -.~ a~ dun ~suInqaJ
,~ ~ ~ o ~
' ' ' } ~ ' ( ~ ) ~6ur1r)s
u~rr~
s ~~ o~~q n d
--apTrxaAoa
a p r x ~ a n g - q s a ~ssc;$3
.
. 3r~qnd
.>. .
}
-,
..
.. .
:uo!jejouue apylaho aqq saje~puollra'p1.
aldluex3 - ~ o i i a : . uaje~aua6
e
l l ! ~al!dwo3
~
aqq ! p o q ~ a us,ssep
~
--Iadns s ~ apy-Ia~o
!
qou saop- uojjejouue s!qj q 3 ! ~poqjacu
. . .
'
.
.
::.
'
.r : ..
.. .
. .
. .
.
. . .. : .
.
.
.
.. .. ..
'
. .
:
- :i..,: ;..:.:
::uofieaouue a p ! j j a ~ o >; ~ ~ ~
..
'i
..
..
'sa~durexa
4 j ! 6udle'qjdap
~
w o w u! uo!jejouue aldlujs q3ea aqr~3sap
I[!M s u o ~ j ~ a s 6 u a~ q~l o= s~a~d o
A ~ ~ u o ~ j e ~po~ueu pe u e p
40 jas
e ayAM 03 stls~,~ia.q3o
o j pue s a d 4 uojjejouue luojsn3 a j j m
02 s~s-ii;we~6o~d':.a~1.ol
y a l seM 31 -hj!lpe.j ejepejaw e au yap
o j seM j! paJejs!p A ( p ! ~ pS L T - ~ S-104
C -lay ell:, a q l .aAnjeaj
uo!qeJouue ayl yoddns 03 A~!l!qea y j eAe[ a-IoI sMo(le
j! '~ayje-~
!suo!gejouue u!-y!nq Auelu aAey jou saop A l l e n ~ x
ley1 ajou 02 2ueyoduc! S,JI
.
(~a6!4'SPAOM ~a41ou!):s]:ac
... .. ...
. . .~ .
.
''
. . .
. ... .:
'
.. .
s6u!u~e~ssa~ddn~
pa~e~a-ldaa
ap!-I-lar,G
Example 2
First, create a class with the deprecated method as follows:
public class Test-Deprecated {
@Deprecated
public void dosomething ( ) {
System.out.println("Testing annotation name:
' Deprecated"') ;
1
1
Next, try to invoke this method from another class:
public class TestAnnotations (
public static void rnainistring arg[]) throws
Exception {
neb? TestAnnotations ( ) ;
1
public TestAnnotations ( ) {
Test Dzprecated t2=new Test-Deprecatedo;
t2.dosomething ( ) ;
Lara Technologies
080-J131G124
112
tempNe$;:kald&r
D:
-.
.: E
( 2 ) ~ ~ !btiC.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ l ' i ~ ~ t i ~ ~ ~ ~ ~ ~
Tes'~~notations.
..
-java: 27 :
it
warning :p[deprecation].
dosomething ( ) ; i n
3;
........
test .myanhotation.Test'.~eprecated
has 'been
deprecated.',!.',:-;
.
;.:,;: *
..
..
.
......
:-t.2
doSometh2ng3;(i..).>;
....
.
... ..-...
..
,..
:,,:..
....- *.;: ..*<.
.:;,
. ,.*:.
..,
...
.z
-.
,
,:.,:
,, %.%$:
Q;,-
..
.
, ' I
.... ...,..
?.
....
.. :..;
,,
,%
. : ....,.:
. .. I. .,
%
. . .. . .. . .. . .
.:
..
. ........ .. .. ... .. .. . . . . . . . . .
... ' ; : .
:
. . .. .. .. .. .. . .. .. .. .. . . . .
%~+;~,&~~$~;<
.,+<.
-:...
?. *..&"*-:
<.<
,Li3:*&*. , ' .:
...vp
;:,>
";+.&
', . ..
..
.
.
,.
. . .
. . . .
..
..>
.?:?ii4j.:;.';;
mn..fn.gs
a,,n,ota,tio
<.,::-;.::$;'?
;,
:~<*,
2.s:":q,reswa.
he
.. .
. ;*-..
,>7
>..
.> .;..
i
v:.
. .~
<*:?: ~;:
:.-.
;
.:.:,. .:
<:,.
..; <.i-lj
.
: : . ....
. .":-. : ~:y,:~ ...
~ ~ : ~ , < * :~ . j:+x
~
,i
.:..
. . . . . . . .'i.
=..,...
.
......
:,
;"-';.,".:'
.-.
1 :!,*.;:$..
:.. :.,::.. .
. .. . .. .
, i;:;$<jlwaming
. .
.. . ..
:.,
: .' -.::
-?~C.L:.
......
::,::.? .;. .<
>
.
. .
This ann0U:tib.n
indicates that compiler wamjmgs
should be
.>, ,:.
...I',.....
:<,:
.......
. .': -Shielded in'tKe$nnotated element and all of:its
sub. ,.
....
?e@ments.
s,.,..:
~h.k.'s.et
... ..:y;Lp..of warnings suppressed in an;element is
the!:s$perset
.:: ......
=:.
of;;tb:e
.%"..
warnings in all of its containing sub.....
elernetits..
::.~g~qP~...
As an-example,
. . . .
if you annotate a class t o
suppress$ane
Is'.,yz..:.. :, wariing and one of its methods: t o suppress
.
'i
.c.. .~
r
n
'
i
n
g
,
b
.
o
........
t h ~ w a r n i n gwill
s be suppressed:af:the
another
method level_io"ly.
.......
SeeiExample
.....
3 for theidfipresskarnings
..' .
;..
. . . . ... .
. ..... .. . .. .. ..
.'
...
.I.... :..
.. :!,
.
annotation.
.
:r; . .
.: :. .........
. .
,.
. . . . ..
. ., . ,. .. . ...+
-i
.
.
;t....--,Ln.
,.
x29.G
.,
.:-,%:#.:
...-.
.!
I
;
.-,
..
.'.'?.'.
.:
... L ..
s t y .:. ,:.:.,.
:;I .
..:. --..
.: ,
...
.r.
........
<:.
,;.
,
'
.-.. >.:.,<rV:-..I"..
. a.
. . ....,:<,I...:
.,r.:..
. .-
.!....
,,.....!.
-;
.>
..
;::;.,; ..
..,,... :..:; . . ,".
7 .,,..
:..!,:.$;::.?L<
*>: .
-1
:, .;
,..
..
,.... 1.
. ...l;..
:. ;:. *.. ::..><...
.
..
. . . .
>.,
.;?,
....
.f,::.i,'.
'?
. .. . .
...
Example 3
,
.,
public class ~estKnnotations {
public static void:.hain
.:.. . . ..:.
.
(String arg [ I ) throws
..
Exception {
. ............ .... .. .. ..
new Test~nnotatidn.&():.;
dosome~est~o
( )w;
........
. . .
:!,=b.
?.:
.:
...<.
.t;. : :
.;
....-
1
J
.. . . . .
@Suppresswarnings ( {"deprecation1'))
public void doSomeTestNow() {
....
..
;'.).
:. :
....
Lara Technologies
'
'
--
<.
.. r.. ;.. .
....
.-
...
...
-.
%Meta-annotations
...
are
actually known as
..<'.
'.::;which
1
:..
.;.4;:+::..<,.
.;.., - *."
..>.
a'n:Qotationsof a n ~ ~ f & i o n s , ~ r j t a jfour
n
...
h/pes. These are::!
-. ...? :. . .: , . I I
.,.--.... ... ..
'..!,;:?.>
.-: .'.
. !.
.a;..-.
........
..: .;.. .
. ; ; , > , , .. .. . :.. . . .
.
. . .. .,.... - . .. . .
Target:
. . . ..
.
.
.
.
.
.. -.:,.
.:: - 4 ' ., . . . .
.> . .. . ...?:. ?:I. .
:;.::?..:
Retention.
... .'+.*: ;.;<
".....:
-.
Documed:ted
:,(,.:
* :!::':, ,
.. .*.
...
.*..
.. +:
. , !'?.<,.,r . 1nherited;ji:!:..
,. ~ 5 s
.~
: .<
.?,
:,
. . .
.::3:,-
. W
i.
;
:
.;
.>.:
.-.
:.
I,.
'
I
>&
.. . . . .
.:
. ,
:.
2.
>:
..,
.',
:<
.
.+ . :
I
:
.
1
'
. .
-.
.:;.~,
r.
;:;,.
5,
-.I.%)
>
......
. . . .>,.
. . ,
.
.?.".
<:
be applied t o any
element of a class
@ T a r g e t( ~ l e m e n t ~ y pFIELD)
e.
-can
be applied to a
field or property
. .
@ T a r g e t( ~ l e m e n t ~ y.METHOD)
pe
-can
be applied to a
080-41310124
be appiiec to
114
-.
I"
...
. . . .
.. .
.: ..
.....
-.
.-,
@ T a r g e t( E l e m e n t T y p e .CONSTRUCTOR)
-Carl be applied
to constructors
@ T a r g e t( E l e m e n t T y p e .LOCAL -VARIABLE)
--Can be
.>>.
:.
*....,
..
....
....
7.:
@ T a r g e t( E l e m e n t T y p e . M E T H O D )
-.
r ..
.-
,!ii.
.:
p u b l i c @ i n t e r f a c e Test T a r g e t {
p u b l i C S t r .i n. . g: .. . d o ~ e s f ~ a r q e t o ;
. : .
5. . . .
. ;.
. . . .-.
=-:?.%.:. :
.A
::!;.:;
:: , .;.
... ...-,:;
a
.
,
.
"
:.:
.?< "
:
c
:
. $i.
. :, ...:....;
,,,
-3.
<.
;.
s-;
.::-s2@&
,%
. *,
.
..
. :.
'
.*.
I . . . ;
.<
/_
,.....
'.Y..<.
. . .... .. .
. . .....
.......$..
P,.... .+
.:. ..
'3.;. l.
;.?.
..
_..
I ' ..
.:.....; .>-.
:. .?.
: ._. . ,...
.. . .. :~ . k
?.
:.
:.:
.
.
-:: . >
.
. .
. . ..
-:i
.. . :.:
. .
g2.:, .'.
.
.,.
....: . .+., . . : - ~ . .
V"
'?:''.
'.?.
:!.,.
. . .*... .
. ;;'\;,
<
:-,
.
.
-?:g,,&'
;. ,.:, :.:; ,;.
Next, .,&e_eate
a class t ~ s e
. . . .w
. .J.l;i,u&"the
.i
,Test-Target
. . ... . . . . ., .... :.,
.
Y$y&g;:::
..::
. .. .:. .. .. .. .
annotatrQn.:
....
.:, * :
i
.>.
:.$.....>f...ta*:
.
...
......
..:.
. . . . . . . ..~ .. . .
.
.
.
.
. .. '..
.
'.;:<;,5<>,.+,>;:.
.: :-i.1;:
. ,;,,.;.
: ,
.:.?-i.k?'<'.
.L*
,;. !:. .....L.. .
.:....
.I.
.
.
.
>
\I.
....
.:
,
<-.; ipiublic
. ....
c l a s s T e s t A n n o t a t i o n s . .{
.:. .... .::..'.!.
: . : ; i-........
. 5 ~ - p u b l i csta'csc void m a i n ( S t r i n g a r g [ I ) {:
.
.
..
'";,;i,'::$jnew
T
e
s
k
j4nnotations () . d o T e s t T a r g e t () ;
........
. ..a:%+.
...
&...
,:.,
$.
>
-..
j.
p.&<%$#+&
&%.
.
"i....
. . .
*$$$-. .
.,. . . ; . . .
publsb.i:.yoid
.
:.
d&estTarg,et () {
*.:.: j-.
sy;tem. o u t :!. p3;;.
~&
n ; e z , ( ~ T e s t i n Taxget,
g
.: :.$y.:. " .
;:.....
..
a n n o t a t i o n " ) ; 1:
..,..: :'-,?, ,.: :,:
:-
';,
'<>,
."
.!
...;.:,
"
. . . . .
': .?.
. .
The @ ~ a r g e t ( E l e m , e n t ~ y p e ~ . . ~ E ~indicates
H O D ) that this
annotation type can be u$edr.to
,::..7.,tF..;..
annotate only at the method
.
. ..
no warning
levels. If you compile the p.i&edi~g.code,
messages will be shown. Now',:,i:f$$~ou
declare a String
variable and apply your newlyicreated annotation, what will
happen? Let me demonstrate this as follows:
:
Lara Technologies
.. .
. ,.. ..
The
you..can see from above:;is:iaat the
annotati~&n'-declarationisshifted from me&od-.level t o field:. '>!
level, w h & h ~ i . ~ n correct.
ot
Because you have defined your
e applicable
t
only atmethodannotation ' ' . d ~ ~ s t - ~ ator ~be
l.evel, if you t$to
compile this class, you are likely..:~: get
.
?.
.: ...
~est&n.o&.ations. java:16:
annotationi.~kype
not applicable to this : k i n d of
; ..
. .
:.de:,c:larat
...
ion;i~a,t..
. ..
line
... ... ... .
'. .:.
1 6:%;i'j.c0
lumn
0
%
':
..
1j.f..
.*..,
. .\
{;do~est~ar~et="~ello
z.--:
World ! " 1 ' j L .
@Te's;t.'$I!!arget
-: .,:
.. .
-
.., . ,
:, ;.,:
?
. ::
,-.
,.
,.
.*>
..
..i-P&la.
'.,,. ...
At:.
... >
..,.U.
? :..
.:L
$.
;9-.:
..+
. .
. .. .. .
,.,.:> .
.:.>. .
I
, ,
RetentionPolicy.~~~~~~~no
with
t a tthis
ion
type
s
will b e by retained only at the source level and will be
ignored by the compiler
Eara Technologies
080-41310124
116
,I
Exa mple;;.5
+:5z
.
...
.,
striGg
d o ~ e s t ~ e t e n. . . h i (o) n;
..
. I
...
...
$.-.*,::>::
3 dn,
t*.*h... i s examp1e;;:the.:4;.>-. @~etention(RetentionPolicy.RUNj-IME)
pz
.. :! . ,:.?>..
::. .;"
.: : ;:;;;: .;.:a
G!notation
Test-Retention a
is
. ..-. '.: . "
..:.Is;$@. indicates-~~hat-you,r
.. . .
.. .;:., -.:.
C.!)
. :...I ;
. . . . . ..::c. . ...: :.
': .,....... .
. .. . . .
.
,,.:..:.
.<,>
n,not&&h.
.'<
>a:.;-.
. . ,<:
:
........
<
..'I."
to'be;te,Qajned
-:~&$@p@~. by t h e........
VM;.:so.:jt,hatit
...........
..-...
,;y,'.OY;.
c a n be read reflectivel.y:.at
.
. . ..>. ..'...... . . . .
t-un-t~y@g;.
-. ...,-..
. :.::.. . . . .
. . .
A<,....--i
...,?:..?..
I. :
,, :
: .
.....
. . . .
>,A>.
rL ?.
:. .
.- .
. . .
.,i.
....
. .
. .....
.::,,.
Example 6
,..., . ....
. . ..
;. .. .. . . .... .. . .
. .::.
... ..... ... , ..
',.
."
..
@Documented
.
........
p u b l i c @ i n t e r f a c e Te~.t..~jDocumented
{
.....
S t r i n g doTestDocurnen~t;~~~):~;.
..,., ..
;
....
:"
c..
>. .
. \ i
.'
....b...?.. .:.., . . . . . . . . .
,.;:.. ..:',
?','. :: .:
....
:,. x......
. . .','>.. . . .
..
. ............
.,. .:
. . .
Lara Technologies
...
.. .
.. . ..
: ' I
.
..>..;
. ..
,..
@Test-Retention (do~kst~etention="~ello
retentior! test"1
public void doSomeTestRetention ( ) (
System.~ut.~rintf("~estin~
annotation
'Retention1'') ;
@ ~ e.ts: ~ o c h e n t e(dd o ~ e s t ~ o c u m.. ..&
. . h t = : ~ ~ ~ e l l o
.. .. . . .
document1'.) .
. .. . . . .
p b l i&:.void
doSomeTestDocumented $:.;.it6..,
.+ .:
..
'' ;
;..
System.out.print-f("Testir~gannoetlon
. . !i. .;: :i.::r
lDocument&Vtl?;
.
:.
. .. . .
'I
;,,
*::
?5
P.,.
. , ...
. ./<
.ib
1,;
,.
I . ,
..
1
1.
-:
.
.,..,
,.
;>:
. . . ...... . .: . . .. . .
.. ..: .:
. ...... .,.
:
, :<:, ;. ..;
. . . .-.. . . .>.
.
.
;
..:_.
:. ;s< .<!;.
...... .\
... . -.. +.
....~
.. ..?... .. ...".. . .
. . . ..
...
.
.:
.....
+.:;
. . .
...
. '.
;.:
.
.,<< ., , $.?
:;
I::%.
. ;,. L*.;?
,,:
,;::'.i.f: ,,you
.;
run the j & i G o c '~8fima"dand view the
,
?>
i'
..
;..!:::
-:
i
i
:.
.:- :.
.!:.,
No
..$.<
generated
.
~ ~ ~ t ~ ~ ~
... _:. . ~
. . . t 90"
~ will
t . i see
~ something;
~ ~ . ' . : ; h ~ ~ .... ~
.
1 i ke Fig
..
.
.
:.
,,
.. . % .
. .
Ls?ra Technologies
:.
....
. . .
080-Ji320124
: 2.
'
.:
...
-.
'
7.
i
Constructor Detail
~~
--..-
-.
- -.---.-- -.
---...-,
&I!
-/
>
"
I public TestAnnotationsi)
II
..
1 Method Detail
!
main
public s L a c i r
,..-CILJ
r a i n (lava. l m l g . S t ~ l M] arg)
doSomeTestRe tention
.
.
I!ii
1:
1'
..
.:..*.... ' . ,:
.:
.,.,I
.. .:%.:..
.
-.
.
. ;..
.
5 (,. .
%
. : ,
, . .. .
.... ,... .. .
,.
I I.
:
...
...
i ;.
I .
...,.
'
.
.
doSorneTestDocurnented
:
...
I;
f l c ~ tD o c ~ ~ c d l - 3 0 T c ~ ~ 9 i ~ : v ~ ~I co nddcwant-)
c~~Hc1
public v o i d cloSorrTcstDocumented ( )
-5.
'..:.hi
..y
~
'::
j
i
:*.-. l
:i
..
..
2 ,.:3:
:.>
. . -.,
.. :!:
-.
?.
Fig,die
L..i':-
< ",
.......(:,;:... .:.:y,<:f+,
:,
..,. .Lt
,;,.:. .
.. .?..
a;
F..:?
..,
.\.
,??
Example 7
First, define your annotation:
.'
I
@Inherited
public @interface my~arent0bj6ct {
boolean isInhe.rited( ) default.true;
String doSometlling ( ) default "DO >what?";
1
f
@myparentobject
public Class myChild0bject
I)
,
-.
, >
r.
r 'E'
-.
{
,::... ,, 5%
,,:
.,$
+;'
:i.
.,.:-*$
. ;; ..""il
: .:.. :..,. .~<...--.
.L
. <.
4,.
-,
I .
1
Lara Technologies
. .
:.
p u b i i c String ;tostring() {
r e t u r n "";j
j
p u b l i c Class k! In o t a t i o n. T y p e ( )
r e t u r n null;
"i
*..I
....
.
2
.
:.!. .
Do you see the diff rence? You car1 seerthat you will have to
implement all the methods that the parent 'interface owns.
do~omething(.)fnethodsfrom
Besides the. isInheri
myparentobject,
to irnplernenfthe ..equals(),
tostring(), and hascode() methods of java.lang.0bject
. . .
and
...
also
. .,.
the annotation~ype()rnethod of
dava.i.ang .a n n o t a G o n ; ~ n n o t a mclass. It does not matter
want ~o.~m.$~m&nti&hese
.... -;.; ,-..;.
methods o r not; you
wh&-he.r:you
1.:27. 'p;. .....
will h a v.....
........
e- t o include thesein
nour:.iinherited
object.
.
.
. .,
:i
..\
.-
:;:.
f
.
.. . .
>
;>;:
..........
.:::e
:..
!:
n.:z
,.".
. .... .
.':
. .>;
..: ...
..'...! .: .:,
........ ....:.,:
.'. ..
.......
" < .. . .
.::..
. ...
......
.<
:.. . . .:1 C
. ..,
. . :. . ...
. . . . . . . . . . . . . .
:
: ! ?.
. . . . . . . ... ,.
...
".
..:
Lara Technologics
080-41310124
:.
'.
....
>.,.:.
.. . ..<!.?.
..:.2i:;.: ...... ,
-,.....
........
-.......
..
.
<.:.'>!. .
:3>.
+
.;;
" :
..:
.,.
.&':;.'
...
.
...
.: -:
.-
c..,
;:
....
: ..' .
.\>
. .
constnjct
us,',-"
i.5-
3:
:c:<
.,;, .
.. ;:
, .?.
.;
7*
"
,. '.
..:. !:.
..
:.;
: :; :
.
-,...: ..
?
;', ;
:.
..
... .sir<:.$.*
. *:,;- ;R::%.
-.
. :1
:.-.;...*.'y;..>:
*.\.
"t
.ci:F@:k.
;?: +.,
. .
,,
.
t
Ir:. :
. . . ..;-. **
,.:
....
1. 5<~i;hei;i
SGs iype 'nave no eiements,except the annothtt6@name
...
itself.
....
>....A..
t,:cpF
. . .
-......
. . . I .:
,c
'. ' *. . ,.. ,
..;
'
2.
. . . . . . . . .. . . . . . , . . .
.;;.
::i:
.. . ..
. <
i..
. .
. . .. .. . . .
...
:. ,?
:.
. . .
t ' . . :
. .. ..
.
*i
$.
us4
.. . . ..
. . .. . .
.Q
..
,,,.,
.'..,...:, ....
,
. . .
;<,
. ..'.. .
..?..1.:
'
I.,-
. . .. ..
. .<'. *z,<,
......
.>
...:
-1.
.::.... i
;G:
,
,,:
;.
: ..%
:;..c2..,
.- ; ..
.....
'C,
.- I..
. . . . . .
. .
.
.
.
. .. . ...
.,s .of.~nnotatibhs.&re:
L at;,::...:~i:L
..... ..,>.
. . .'... .
,.
.......
,., ...
,. ..
. :
.,., . . .
._i
.
:
,
.
.
.
a
.
'-
6%ihhe
! .;
,.
k:
..,.... .. .=...
. ,
.... . ,;.,
... . .
. . . . . .
..- . . :.
.
. . ...:..
,;
<
..
3. Run-time prueessing: sone Annotations are available to be examined
at runtime.
Annotations can be applied to a program's declarations of a classes,fields,
methods and other elements.
Eara Technologies
.;:
. . . .. .
. .. ;'
.
,.
The Annotation appears first, often on its own line, and may include
elements with named or unnamed values.
Instead of writing nbm file we can keep that information, we can write
through anotations.it M ;I1 act as a member as like class,interface, enum
etc.anotations mainly to give.
.:.
. .. .
'.
...
.$.:... .
:%:;
':i
'
<'
y;,I .:.....
-.::
5;
..:
.#: ;
:.,:$.:$.&;:<.?.
:>
..:.
&,:
,
. . .
.-
:t?
7: . .$ : .:::**d.
.-;
;I...7 z
:?y
czs
,a
<.
A
,;+
*.,.
i,
?!:
. . .
..
!.
..
>
.&
$,:
.::
%.
'
. .y:, ..?.
P
..
; <.;
% ,.*;.:%.-%
' .
*:+<:.
<
.:<
,
'?&;&$arnle
:
*:..
. . .
...
,. .. . . . .
. . I
.
-.. -. - . .
.:pM;s;-;.
.'. .
.
~~b]f;$l&j,Test
(..;;:?.+
.
.:.::;
..:..
.
.%
.. . .
....
% .
.-.
,
. . .
-5.;
.i,...r:.
:......
.L?~."
.. p t . : :
$+>.
, .
.......
@@pe~ride
.........
Public String tostki&.
..
.
.
..
.. :;.!.
. . .
Return string;
., ?.
.. . .. ...
...
A ,
. .
.. . ... . ... .. .
.
. . , . >, ..: ;
:
.
. . . . : ,: . .
Ti
.:
..
.:.
..
.. . . .
.
. ..
.. ::......:.=:
.i;.<.>.. : : . ;
.. ., . . .:.
.
,. ., .
..
080-41310124
,
:7
. .
.. . ... . .... ..
, .
..,
" q..
;. ..'. as:,:,
'
..
,- '.
:
.:-
. .. ..
<?..*:s,?
.,,
.;:$::, .s.t<::
.
+ .*b.~J>i s s>: r..3
:j ...: :
.::*.;
,., ,:;
.,
:.
;.::sl.:?i-.hibemate-3. 2'g:j
ar afid
dent jar files.
....
,.. \......depen
;:i3&c$$tggg2#.+;i:;:;.>t>.
x
. . . . ' . .. '*
.-;.E"'.Q~cle
4' rype &jO&&JAR &le ;:
. . . . . .>?
.
.*
..
,*+.
-..L
.I:::.
t:- .
L,;
....
c:.,:
. ,:.....
.
.;
>.
. . .
........
<.
. .
'.
... . .i.
. . ... .. . .. ,.
"i
'
5..!,
,:
.?
,.
t..
clas;' A; ' . .
.
. . .
.
{
Bdiprecated
String test()
..
.. . . . . .
{
Sy~tern,out,~rin't~lri(~~i~ricated
method:');
..
.
. .
.$.
,.
.:. .. . . . .
..
. , >..
Class Manager
{
Public static void main(String args[]) .:,
{
A a 1=new A();
A 1.test();
:
Lara Technologies
080-41310121
,'
i.<i!
>.,"::::
.;.!+
.......
,Icsr..-..
.w,c.
........:*.i?.Y.
..:.
.....:. .:
<:.A
. . .
..: ...
.,:.. .$
-:.
::.
3:.,i::.
:
,%
..
....
3 .......
....
. <. .!!.,. .t:. y..
. .,. ,. . .
f '25 :,;, $
:
'
y . ~<....>.
5 ; .::. :..,.%-. >
.:.
.... ,.
.:.
I
class Test
{
@TestAnnotation(count=l 00, date=02-02-2009)
I
Example: hibernate prograrne with usage of annotations.
.
import j avax.persistance~~.co1umn;
import javax:persistance.entity;
import javax.persi&&e.ld;
::. ...
import~javax.p~rst$~~e.jable;
. . . ).:
. . . _ _. . . .
.
'
.,
.:
..- .:,
public vai'~'~setPersoriId(String
.... ,:, .
persodd) {
..
this:dersonId
..
= persodd;
,'.
5 ; .?
..A<
j
.
.
.
:
.
.
.
>
Lars Technologies
pub!ic class
{
Public static void ma@(S;@ng.pgs[])
.
..
.
'
. ,
. : . :. .. ._.. .,.
...
, ,'
'i.
&~:::hotation~o~fi~wation
,
ac=new An$tatioqConfi.gurati~nO;
.......
. . _ :. _
~":ac.configure@,,
.....
...
'.;-session~actor~
sfac.build~ession~actory~;,~:
.
Session ssssion=sf.openSession();
. . . .
T
.:,.!
.,
<.
::.r:.. :.
::
:.
...
. .,.a< .
..'!-.
..
,;
. . .....
. .:..:...: .
>$.
,,
.... ,$<,
.....
. .-
. ....
.=:.. .! 1....,.:..
.: .; -. ..
..>:,.:.:
.......
.
..
'..
. . . . . .
:
:. . . :, . ,F . :.:., . .. . -..
.. .. . . . . . . . .
. .>
.
.:.$.;
.:-:, .
x.,.....
:.. . .
-;.
.
.:.,:..,
;.,
:,..?:.:,.;:i.'~::L
..::.3
.!.,:.,...
'
'a
~.
'
.. ...
'
.-. ..;:
.,
...
:.
,:.......
. . 9 .
,}
.
'
<,.
.. . >.
. . .
.,.,: >'<.&.....
.
:'I.,
.A
<
... .-
..........
.. ....,..-.
....
,-, ..
.,:A:.
,.:. . ;,*.
!>
. . ..
. '.aa:.'. , ..
. . .. ..
:
%
.?.;:..
..
$.
......
... .. .;. ........
. ,'*.>.. . .
:...
-. .'<,$;.<.
...
. : .. .>
.. . .:.
I
. ....
. . . . .
I
..,
. .'. .. '
+/
'
..
. . <..
'
'
. . . , :.
'
. . . I . .
. . ..
A
.-..
.
:.J
Lara Technoiogics
I_'
- :
:ih .......
:.. ? .
.:r
.. . .. ..- >:.; ,
.r; ,.;-.;....... . . . ",..z.
:;..
..
..........
.>?, !. . .7 :
...:.. <?.;":.
......
;, ,)?.., . . . . . . . . . .. .......
...
.
.
.
.
.
.
.
. ;;..
. . . . . ,:. .........
... ;.,:. . : :. . . . . . .
.. :
'
:..
...L.', n..:
.
.,,.:
.,
.*
. . . . . . .>.. . ::.. . . :.
. . .. .. . ; ..
. ,
: . v
.:
...
;<:. .'.
...
:...".
. .' .
j.;
,.
080-41310124
.....
,:: .. . .
.. .:....
.:
.'t.:
..i.
,.:
......
. . . .:..
..........
:. .
.. ... ..
......
. .
.-.-.;.,+
.. .... <
..-.; ..-....
:.::;+.-
..
..I. . . . . .
...........
.
,
Ssssion;fush();
Sessiop.close();
..
,y
..
. ..: : ..
..I
. .:-,,
. .. ..+..:;;?>
....: .. .&
,. .?.; . :..
.
. .. .,..
.
.
i? *
. .. .
. ._ .:
.. .. . ..
<
; , ;.. ..: ,. . . . .
..
.
i
i.'.:
-:~:i.r!
..I' .Session.getTransaction.c.ornrnif();
. .
.
.
,
.,., :
. . .:.
Scssiofi.save(pZ);
..
. .
......
L.i.S?
.._
::.+.:?.
'
, ,
..
*.
'
.::..>,:.. .':
.-
. c.. :..,-,.
,.,.y
:
:.-.:
. . . .: . . . .,:,:.....:.
&a$..
. .........
. .....
. . ...;.*".<+
. . ..>.;
.
,
,
d
& .....
.
. : 'h.*...
.A*,?.
,.
-..-.
-..
.
.?.
..;.:<:,
. . .>9.
. . . :.
. ...:,.>.::-,
. . . .. .. .. .. . .
...........
.i r..
s.:.
.;).,,L...
. . . . .
' . .
.
;,;
7 '.i
;:,
..
..
;,
.: . :...:
. - . ..
. . ... . . .
. . ......
.-
$
;:
Sessioll.beginTransaction();
....
s:. ..
::";?s;..:. >.:.....,,;
.?,;.i.
?$:!.
,::.p
-.
. x
..:,<$; 3 .
..
,z
v:.-;
. . . :..
. ..
. .,:.. .
.,
. ::.
..'
: = *..:
.:.:.
. ,.
'
.: .
......<...' .
.:
?$.:
;: ;: !
..*.....,..&... .
'<,
,;
. ,, . ..;
,:
..
...;.:?
(:
;%:?
.....
..
.~%-
..?..
xi
*<t. :
' *-
w.;
. . ,.
>
:
+
..>
,>
. ,
. .
+4,
,:..
.:
?. :
,& ?@
*>!Sk
?>
.i
. . ..
.*.
. !:.
y...
. . . ..
..' , c. , . .. .
. . .a . . .:. .. ... ..
": ;
; ' ,.,.
2) Seeond~evelcache:
*. ..
:;::.we
..,
will not get-this
.,-.:
cache irnplcitly. We have to setexplicifly.
.,.
-.
.ti:;:.
22.:
.It:s. t ~ r:. e the
s pe~skfkint
objects inside a SessionFactory.
. ."4'
.,, :;
:,t;p ".;
?
;%
,': I X
It is:;$iJagable
fofiag
the
users
in
the
sade~application.
!>
.T?;*~.,
., .;a
. 9.
Thre;&j'?e . . .. .. ~. e. v. e *.~ ......*
l ~ p p ~ n ~ stechnics
o u r c ef 2 k h .i.*e. v i n g this caclie,,
. . ,. ..
I E
~: ;: ' . .. ... ~. . . .
~
~
& . .. ..:.; ~
. .~_
......
. . .... .
~ . z T*.
'
. * :,..
' . :... . .
2. OpenSymphony ~ ~ ~ ~
c
i-..!
l
i
&
i
,
,
. .: .., .
.
3. swamcache. ::.,
. . .. .
;;:. . . .
. . . . . ........ . .. . .
...
4. JbossCac!~e. : .:':
.. i.,
Eli bcrnate s u p p o r t s i ,above
~
..
mentone&k@&fi
$ou@e cache technics.
Ws have to set explicitly;in the config file b':{&&tibning the cache
.
. .. .. '.. .....
technic 'fype.
Cache technic type can be m$$(fkd
. _ ,!
by choosing a suitable class f?om a
....;: . $ $ , ..
.;,;. ;!,,; . ;;'!
package ~ r g . h i b e m a t e . ~ a ~ h'ie:,j
,:
(;
. p,;v:
5i
yG.
."
, -,
>
,.x
:.,
:t.\
',
:
;
.,
F,
.:
-2;
[...
. .
L;
:..
,.
.:
, .
. . .
....
>:.
080-4 1310124
.~
..I.-
-:
. . .
.
:.
..,
,
+:
....:..
......
..-V .. .-.
.:,
;G
r
<;. ' .
.i.
.'..,,..:
..........
.
..
....
<.
."I,
:..,.f'
.x:
....:......+
L.
.?
......
..a:.
..
,:
...>..I'
: .,
:.:>>&.
..
3
..
-..
:.
.:
.
..
:./
...
: s~aa!qo$ua$s!sJad
,-
<&adold />
a y 3 e 3 ~ g -qa~ ~ 3 - a ~ e ~ u a q ! q ~ 8 l o
Faqs
.. . ... .
. .. .
...
I) What is O m ?
..
<
. -.
..
.:.j:; 3;:.
;. .:. .
ORM is ~bY@it:~elational
Mapping which transforms objectsin Java,
-.
. n c ~the
. ,::.which is b a s e d " ~ $ ~ ~ Q ~ ~ z into
~ !table
e , of databases atidfor
. . .
this
:-. .!<>-!.:s'.-,.
.. ........ ;.:py::;
. :::,s.:t
\*..
she
mappings of objects and tables i n.
:::..:r.;$gt~;;;reads.the
..
. . . .
:..= metadat'dS@~;$&lias:gll
:< 2 : .
.daGga<$e....'. .<, ;'::.
......
:,
<:.:.
.i:;
.-
.:
.+....
,....M.?.
,-,:
?..*-*
. .,
. ....
;
.x
';
$
>:.,4
,.., ,..
! . .
2) What
:
j5
,bs,
:'
...
. . . . . .
'
. . ...
. .. . .
.: ..
.+,;
;-
..<.
.......
,.
..:.
.:
. . . . ., . .. .
$jFe
'
::; 3i.<::
..,. '. :. ;
.-.
...........
,.-:
:. :. ;
$.::,,
i,,:
.>.
'
. . .
.~;:...
..
.>.<:. . ',..
. .* ;
.: ; .:
'
. m solution eomprisks:of?
....
3) ~ l &.f:_.,;.'j:".
d l o ..e san O
.
...
,. F...
,
.;.;. . .;.. ..;. :
. .
. . <
:'-...
...' .- . :: 5: :
!.., ..k. :,. ,: .
. .....
!
.
..
5):
It should h a v e : i a n : *f($ljer.forming
.~, . .
basic CRUD (Create, Read Update,
Delete) operatioris;on obje~t~,.~-f?,persistent
.>., .?.:
.
classes
1:
,.
.../ .
..
%.
'
:, .
..>.:
... .
:.
.. .
2
. ....
;.,,
. . 2,.
.:,.:.
.. ...
.
. ,:> :.,
..:
'
:. .,i . , : .
Lara Technologies
080-41310124
,.
. ...
..:
11.
. .
.. . ~
.-
.,
.?.
<...
.:
'
. :.
..,,:
:,
.
.
. . . . .
.. .j. l. .
I
*..
-.
111.
. . .. .. .
.'I
. :...
,;
5 : -. .
.:;@
..:
, .
..
:.
.,
:,< .....
?l
.I.,
.
.
. '.
"
-""
;5-'---:-
.-
.,
?
..
.:-'.>.:'
~:>..:-b.,<:.
.t.:.ij-..:...q..
......
,,s..p*<~-;
<..>
..
-S .,iji3..2....Sr
.. . . . . ,.
. .
. .< . ,.k........
.;? .
,,y .*
:
.
:;:.:;
.>
%-
..
;t
'S
..;
.
.
:.
..
1 .
.
..>::
,
..
.:,
'
;,.;
.,:
, ;.,. :uL..y:
...
....
7
;-.:;:"j;
.>,.
'
....
..?.<.I..
:
,.:."
.. .. ..
.... -..: ,
. ,:.,
. .- ;
.' . .:,
..
e:
;>?
?.>. .
..?.,
.":
A
- :>
.!.C.
+. 1
?.
<..
'
.
..
. .
+,,; .:
.a,.5
. . . .,$
. . +,%
>.
' ......
F. ..
?.
..-
.3
'.:":.
".;%G:U;:
$.:,.".!.1.
.,.
The application.is;,designed
around an object model. i;h@'SQL
. . . . . . .. . . .
i-7 . ..::-.
- ....
<"dC is acneraied
,.., :;..........-....
at
.
.
.
6.Cifd.
.
.
.......
.
.
.... fun:e:;
.
And
.
the
association
bew&&$3,,
I.,;
........
,.,
.y ..:.
.. .. ..... . . . . . . .,:?
...
..;..s,-.-,
..;ii&!{:objects is s u p ~ ~ ~ ~. d ~ b y , .:;:,f i e ~ : . D
~
r
s
3
.
~
t
&
and
n
m
qucnes,
~ ~ h ~....n ~ m ,
----.,
.,<; ;.
.. 5. +L: t,g.,:
...
..<<?are..specified
using &';obje~~~~@~nt~dd:&x;p>ession
+.. a . . . . .la,.-.. .,R?r:+i
.... .language. %e2:ijg:z
;I-=.'. I...:
.
.':is best suj ted for medium-sized appq j~h@djj@@h
-r 4 :?S: .. $some c o m p l e ~.;..3?-.$ ~ $ $ ~
.. ....
+.,,,fh.+.
trahsactions: Used when the mapping exc~&X@&~different
...
'.Xi .fi>:;?
.
.
.,.:gdatabase products at a time.
i..e:.?
......
. . . -.
....
-: ..,..
...
;
.,
,
,
. .
.
1
.,...
r.r;,.L.
1Y.
q.r
. -.%
- .;. . .1,.
:!:
' '
;c:il.:...c....
..
.
-.
,:::..:
..
. . . ..,>-.1.'
...
>.
:? :,i ' ::
.
... :..:
. ... :..
...
..
.......
.:. ....
.. ..,...:
. .
. ...........
. . . .. .
. .. . . . \ .
......
.. ..!. ',......- .
,r
Improved productivity
o High-level object-oriented API
o Less Java code to write
o No SQL to write
Improved performance
'
o Sophisiicated caching
:
;;..
o Lazy loading
.
. .
Eager loading':
. . . . .
Impr?ved maintaidability
...
o A lot less code to write
1mpr.ovedportability
,
. . . .
"
>,
, .I :
.
,?.
.*
..
.
. . . 5.
5'
'
I . .
i.
ORM-@yework
.,.,.
generates database-specific SQL for you
.
.<
....
~nswe$2_:,
There are many'benefits from these. oljt$&hich
: . .. . . .. .
following Gethe
.
most
important
one.
...
..
. . .. . .. ...
. . .
i.
the
. .
...:.: r..'
<
...
~roti&%iiviiy
Hibernate reduces the burden of develbper
by
:;&$ .*.
. .:,i,);.,.$,s:;;:<::
+, :<.: :'.$ : . .
!> +:.
. . .y.,.%...
.$.:;?-p.,$
. ,.>, ;,gc-j,
. .+.;:p:;+ . . . .
-..:
.......
.:.:.,. k...
>,
-.-., <&;;;
providingpvcb;
of
. the
. .hctionality
. '.*. ..
and let the deve1oper;fo
....
.:.52:.. .
. . .. . ....
.
.. .
:
,
:r:y
c
o
n
c
e
n
t
r
a
t
e
:
~
~
f
l62iGs
i
~
~
~
S
1
~
.
. . - ;,:$.+.:$
.
,+; ::'
: _ . . . , , .2-'
....
.
.
.
..
,*:
,....
.
. ,. .. ...
.
. .. . ... .....
. ., .,
11
,:.?.Maintainability
-t'
i
~
g
!
:
&
b
~
f
j
3
~
~
~
,
&
most
G
i
d
of
~
the
~
.
. - ..
?;{.,;:.?.
.
,
'
v
<<:
,*:
.
.
.
:
k
:
..*.
::
.
.
.
.
..... .
.
-.. . . . .
': fii@tiona]ity, the LOC for.&e alj$&$i&n will be reduced and 'it:
:.
is-easy:.
,. to-maintain.By automated object/relatfqnal persistence
. ..
.... .: .
.
it eventeduces
the LOC.
. . .
...
. ._
....
. ..
... .~. 1 1 . Perfqrmance - Hand-coded persistence providedgreater
<., . ;....
?,5!5
;;:,
<.ti
perfoq.mce
.,. ,..,
than automated one.
..
But this is not tfule'.:all
.....
the
.......
:.:, ...
- ;:i..tim~s.i.B&in
..
. .;*. . .:. ..... hibernate, it provid&smore optimizati&!fiat
. .
;works
:ill thetime there by increasing the performarize.
,i.+ . If it is
. ..
autoni8;ed pd<siitence
. . then
.
i.t still incriasesthe perfbifhance.
.;.. :;.
g
iv.
vendor ,indepknd&n&&
-Irrespective of the3.$$fesent.
types
of
databges-that are there, hibernate
...
providesamuch easier way
. . . . .
to deveidp a cross platform application.
...
,.;:. . .
< ,. .
. -:\
.,z...,.,
:$'-%.,
.'4:.
#,.,
-,,,
.&'
'
.I.
. -..
A.~.;S;-~>,:
.:.
:
.
I
..:
, :> ..,>
I...
3;.
..
.;
:.
.<
I....
.;;
.;r...,z
'
,?
. ,-.
:-
. .
'
:i
. .
?.:...:
i * . .
.. . . .
..
):
....
. .
.i:-:..:.
. .
' -
:..
.
: >.
' ".
..
5--+*;1
. .
:,.
, .
. . . . .
...
.,
-
I.
.: '
...
..
,.
'
.
I
;r..'
1: .
\
,
.
. . .
:-
2..
' 1
2.
. .. .
--
7..
6 ) What is Hibernate?
Lara Technologies
....
>
-.
.....
-.
.......
\.
:..,7+-
..<a:I.
;L:.
.......
.
.... .<iC.'
:.;,?C.-
.. ,: ....
....
,.. '.'
i..!'
.v .+ . .&:*
:>,,..,
. .,
..
.?:
:.*a
>.<>
.>.L: >.
2->$
>.
. .
,2..ii8.::
-:i
:.,
<i
..:i
~:,',<.
.<...iF',I: .:c2*
? ::
. . . . . ..,...
.
(
......;... .; .
1.
-.;:
. ... .. ...:
>:
i
'
i:
.:
(.
...-. >"
;:,;@;;:,? ,..,...*
:.. .. ..,:4:c,
..r:.j, l$'i::A,,
1 . . ,.:.'I zr:.?.,,
Use
.,, . . % . K , . . ,
.;;. ,. '.$.;.-..
. ,. . . . . . ,:-. . . .
.:.
<.
.....
.. .. .. . .
i.
., ....
.
.....
.
:5,..fi.;
'
:'
-A:,
?.
:' .Y
3 .3%.
?.:..
'.fi..,$ :?
.;
. .>
:?r
; .. ..: . . .
. . . .. .. . . .. . ., .. ..:::;.. .+,. ?....,:A>. . .?
. ...
.
. ..:;. . <:.,. . . .""
. .. ..z..: .x:.,'i; ;
:
:
; ...;,: < . . .
. '......
. .^': .. 4):
.
.
"
3. :"
:.:<.:. ?
. . ,..:. . ..... j,;,ii I
>
,. ; T , .
:<*,:
-.l, .... _ . : ;
. . P. ..
!.; :
""
key
'
;
~
?.
..
. ..,,...... :
:
:
.
:..-....:<
,; .: %
, .,,,
4.
'
',
.."E*..
J+y, <., . .
. :,.. .. .
. . . .;. .. .
,....,
c.:?
, .- ,
....%
>:..
: $ ~ , ~ y +.,- .. !:
. . . . ..*. 5. .;. .;
.-
. . . ..,*
.*
.?'&
,; ..', a?,
(
.:;,;! :f?ey,,.-
. . ,,.a&<:+>.;
. - l . . . C.\,C 1 . <
. .
...
$
a
:
&
;
:
.,
;
.
::$;rrnfirernent
......
:
. . . .:':.
-...:I
l,
JavaBeans
. :.
2.
.;
, .:..:,.,
I/
&
::
.;.., r.., *
.. ?. .....
. ...
. ;.
.:,:. .,.,,7.
.:
....
z+..ik:A',
i@?exsistence
class should not be final,...
.-,....>..,.
...ih;.:..
*-*
<.+.
.
'3 'i.
C
<..
.wa.?..
:
$.%
.,\$$
,;.-
-.
,ll.ki
.,
_
.
$@ j;:.
ti\-;.
I.,
..-. .
.?
' '
,,:-+
'
. ..Ah.,
. ,.
:...
: ,.:<::.!2$
. .*-I-j>:.
-::
........
?.,r . .
.:.
-...,..
.;.....
.:.:..a .
.;z:.v :
?.
'
>
Lars Technologies
?,$.:;
,.
:... ;:.*::
:',:;!
.. ..,,.( ..:.
....
'
. . a, ,?........I.......
.,)
< :.$
.!.;<:,.7 ;. :*. .... '.>!,c
.. I . .., .. '
? :; $
......
-
,
,
A:,.
, . +.$.;;Al4;~:.?).
.: ,:.,:..
.,:
:,
-.
.:,<.:>;2::
:.
.....
.
.
.
.
.
.
. . !;..'::.:
.-; '<..;:;i:$:;:.
.<;: . :
:
L*..
::.
..; ?,..j,:
1;-.,.' ...:...
;,, :: .;3;:*:.+:e.>&
:,>.
.
. . .. .<. ...
. . ,. .
...;... ...<,... ; ...
. . .. . . .
-:
;,..-1:>*,y:.:
5$@3:::
..%:.
.-.,
r .;:
.... , .
_. .. . . . . .
...
:!.::;j,,:!.
..T~+Y-
Ya.2.
..
,: ,
.LC. L
<.
. .
....
.?.
?.:
''1
.. ;: :.,,,.:,<.?
.z...,. . .
,.
, ?%
:. . 5 ' .
.,>.A&.
;
<<.,?.
< T"
.$I
,, ..
:
,
.
.....
:<
"
*.
.......
,'
. . . . . .
. .
'
. ..: . .
..
.,
Business Layer
a
Life Cycle
?l..
'
.- :......
"
Persistence Layer
.%
%.
. .
....
!<
Classes
. .. .. .
.......
. . . . . ..
. .. .. ?
.:.,, >i;: .F.y :..!. <:.,
;.;',:.?!c$<+:,y; .. ;:; ;
. h,:.
.: .
.. .-ST',
. . . .:.:. :' .2.* -,
.cfl
1
I
E I
Session
SessionFactory
......
. . . .
. . ..7L
.....
Transaction
.:%
... . . .
.,,. . .
..
?.
a
Configuratio
..
....
'1. T
......
r
,,
;,:
, , ; .
,.
. . :
, .
'
.
JDBC
With JDBC, developer has to write
code to map an object model's data
representation to a relational data
model and its corresponding database
Lara Technologies
's:
c .
Hibernate
'
schema.
.
::
1.
, ...
:.I:
-.
'
.
... *.,: yv. .....
.., ..*y+:::.:.
. ~ . ~ ~ ~
... :,h..&
>
: ,..,,*
~......
:-,:*,a>,
c .,,r
2
:,..:I:
..
*........
JDBC sup~otisbnl
y native.
Languag&@QL).
..>..:.
Developer fia$. to find out ther$
efficient way$o access database, i.e.
to select effeciive query from a
number o f
to
. . . perform same
::.
'
::-.:. j-abernate
.,.
provide's transparent
....:$$::i;,,:;:
.......
......
.+..
>
..
(.s'
'i : i
. . ..
. ..
., .. .,..
. .
.:,
..I
.;f:
.... . . . . ..
ij..
....
. .....
:?:.: . . ..:.... .. . . :.:. . . . .!,,<.
.
\...
.
.,.:
.L.,
,%
..!&.:
'
.,
.
. .:-:
.
. . L
.
,.
..,:,
..
. .,
;.,
:,.
...k.,7k.
'
:
.
, .?
v .:.
..&.
....
.i.
i
'
-!%.
:, ,
:
.
T
*
,
%
.
........
-y:~i..".:,
<
.,?
..:
'
;;,
..
..;.;--
C ! ' . ,
..
7 y . . . . . ~
,,:.;
.. . . .
.-;
~ i b e r n a kieduces lines of c d e by
With JDBC, it js deye]opei?$i!2.:..
,,
responsibility to handle J D B C .<. . ~ ~maintaining
~ J ~ ~ object-table mapping
set and convert it to Java objects ;:.:.:.,:';;.itself
and returns result to a~plication
..........
through code to use this persistent l::i$in;forin
of Java objects. It relieves
!d.. .,;*, **::-.
.: .:.:
data in application. So with JDBC, ?;$p$ogrammer from manual handling of
mapping between Java objects and ..{.$$ersistent data, hence reducfng the
development time and maintenance
database tables is done manually.
c .,
...- .
...
.
.:;!y .
2 ,
::.<.gi.:y: ,
i
;.
'1.
r.-&
Lara Technologies
1
..'?
., .
.
.
.
.
. .
.:
..
A..
,.
";
.
.. >. .. .
: .:.
:.
.'
. . . .'
...
. ..
...
f:;$-+:
?
:;
...
. -.. ., .
..
..,
.
. . .. . ..
..
- .
..
.
.; .:
.,.
.;.... .. . . . - .
....
. .... .,
.. ... & :
. . . ..
:. ..
+.
:G
L<:
+ :
...
. . . .s
L:
3'
..
. :
.c .:+
.$:>a.
$.- .-
.5 i:
'
tt.,tz
i . .-;.I.
. . . 7.;
.>.. p: , <
.>. .I
>
q<><..
,:,.!?,.
< ,
-.*-,
::
.i'!F,
1:
...
. " r :cF'
..
... )
. .. . ... . .
.
,.
. .:
r
.......
I.
.
.
..
'
-:.
il.
. ./I
.<.,
. .
.2<
<
...........
. ,:. ..... ..,. . . . .
:
i::..;
. . . . ..
..::.?..:.,
.... !.
,..;.:
$: .
. ......
.'.?-. , ,>
.-<..: -;,:,.:.
..
.........
-.
,,L..........- . . i l . .
G
: . .. . .
.
.,.
..
, :..
.- ..;.r.,
. . . ..
. . ............
..I.
,
..:. :
: ;
,
. . . . . .j ..
; ....;. g.:?, ,j;..:
.:;<.
-i:. 4 .?.......
........
:- .
. ,,,. ::..- ..
;'
r.:.
. .'
,.':
: x: ,:.<: .:;
: ,,: :,:>,.
'i.,
. .:
I.
.... i..
..,.: . . .? .
POJO stands for Plain Old $ava Objects. These are just basic JavaBeans
that have defined setter and getter methods for all the properties that are
there in that bean. Besides theyican alsohave some business logic related
to that property. Hibernate applicati6hs%vorks efficiently with POJOS
rather then simple java classes.
12)What is object/rela tional mapping metadata?
Lara Technologies
080-41310124
136
.
.
.-
, .:I.....
.....
.,
Hibernate simplifies:
.
.
. . ..
..
>
.
.
.
.
.
'%. "
...
.
-.
<:...
......
.
-...
.....
<
.,! ? :
..
,.
,.;I::>;!
,.
r<
<-.
>..
..
!>:x'?
..I,:..
:.>::
..
,::;
: :. ..
?.!
.!.::#;
""I?..
,:,,, ;:
:
'.
;. "?.
*
...
. ; .r%..
;
.,,;
::
;.
_,.
:?.;< !
-1.
?..
..
>
,-*
.t:;:
a.:;
,:\..:
?,
.>,
?!
. ;.\: <.
7-
:>
"
.:c...:. . -..,:....:.. .. .. .. .
-.
,,;:
:.?:..\.::>.:
-3'
'"
......
'. ..
.< . .,>,., 9 :,
<.
. .?.
-,
.:.,
......
.... ..,--,
si :
13'.
". .I
+
. .:
.
.
.-
::
:
:
.,..* ,.. :, ..
*..:
.> .,.:;.*..; . ..:,*-.,
. :. :.,: -,,,..::
'.'
85:!22 1 ) ; ~. ,~%;,<.:$g:\
h a tis the file e~ten$$@;~.ou
$se
for hibernate mapping fil&2j;:s:.;,
:.,.
.......
. . . . .... .
.;
. . ;.. .\ .. .
.
.
.;. .
.:
.........
.......
;..
.
:....
:.....
:
.
.,.
"~.
:+;j.$gj<;,
p;;;;>.:::.s
$:.
,
:
..f.
!
;
:
;
;
:
;
.
.....
...
.-..; ,
.<..,. ...
".
_
. . ...... .x
.;,. m l
The n&@i5fthe
file
s
j
l
o
u
l
d
'
~
~
~
~
i
k
e
~
;
~
s
~
:
,
.
~
.
f
j
~
~
n
n
~
e
~
h
b
m
...
.......*,..*
... .*....
.:. .,r .
'
, ;. .
;
.
".,,.*. - < . ..
. ...
.
.,:
.:
,.
,tt..::: ...
".<.
..>. ;..
. .. . .
. ...a.
..;
,.
.
....
.. . .
...
.:.
. .. . I%.+,
:. ...;i. .
... ..,>-:...
'..".:,:%
"G.;?
> z , ,<""
.!?.;,
,
.
.
. . .. .. .
15) Whatpthe
,.!,!.
:..:.
peed
.
for.:Hibernate
....
xml m&iRing
file?
(.,
'
. .. ..... .. . .
..
..
...
-*;.$%:.
.
. .....
. .
.$&.'>,
.$&;&/<"
. . .
. .. ...,...I... .
. .
. . . .. .
?
.. ..
. .. . .
.:. .
.;..?.
L.,
..7.:..:<5
..,,
: !.,!..
* ...
'
..';i:;.
:..
,;.:,
, , .: :,+
..
'
~.
Lara Technologies
'L
. ..,
. ..
:'.
:.
. . . .
.....
....
:...
..
. . .
.,.
...
.......
>
. .
. . .. : .
.-.. .. . . .
..,:..
.,.e
..r..,
.....:.
.. .
i.
..
c,bz.i.c
I % i
. T.'...,,*$,'
. ..
,.
:,,.,..!..
?' .
.-
.
:
.~
'
>.
J..:
. x
.$.;>:.;:,
. ...
....
........
.
*,;.,*: r7
18) HOW willyou configure ~ i k rvi..n a t e ?
* y;:? y .;
The configuration
.
files hibernate.cfg.xm1 (or hibernate.pr6~;.&ies)
- . . and
mapping files *.hbm.xml are used by the Configuration c~ass'%b:;t$~iate
.
(i.e.
+:...,
. and bootstrap hibernate)
.:$...-.i:kppcd
,
. the SessionFactory, which ht@&deates
vi;.;;-.
: configure
. .
:. . the ;igg>.,.
n instances. Sessl$in;;mstances. ere-tbsprimwy
interface f~d;&i.,~
......e;;?...;:,
., .+'! . ,..:
;;<;*$*, 2 ',-':~*:p:<,,~:
.. :':
:.
,-..:-.. ...
....!?::;,...e .ys:$:;i.2< 2
..v
.-..
.,..
perslsf&i3&
service.
:
A
:
!
.
+
:
v.:
......%;:-; ...--a ,:....
.,,-:
.::.,
.
,&::
;
,.,
....
-+gg3,;
:
.....
....::.
-..
..-..,.,.;. ...,.. ;;;:-.+,;:
.; $!.
......
-<-q&:
...
....*.&y&
-.!<.;:.;.>.:;~j;L~%. :fi;:;,~$~".'.
&<. . ;;.:.k
. L..
...ti7.:
".*$
..; ,."
$;'
, .rr.,c.r;j$..'-,
,
r.....
:c :.
<,;:. .-, ' i d b '
.>.?+%>,g..
hibemat$c fg.xnll (a]tematively can use -hl~~@,ate~~li~$A~fiies):
These two -'%&;g$:i;.:> ;.:;
: ?: .,+
.,
..........
.:,..!.::.
.:
es are used to configure the hibernate sevice (coriii&6$@n:.driver
:, * .:
class,
ection URL, connection username, connection passii0d, :dialect etc). If ?:ij.'.....
...
"
both gigs are present in the classpath then hibernate .cfg.x&l'?fIlioverrides
?,??:z$\
. ......
.7-,;,
....
the s ~ @ sfaund in the hibemate.properties.file.
.:,:.
:.. .
...
-.qggi:.
: .,
'. .
,':,.:
. '.'
;$$,. .:. .!
~ a ~ ~+,, i... n(*/hbm.xm&
~ l e s These files are &6gt!p map persist&@objects
.?-.:
.. -..
to a relatiorialdatabase.
.
.
~ ~.:%
i ~ ~:i3.':,lF2.,iebz.. b e s t . ~ rto
a c&&e;each
t i c.......
e
objec~dian
. . . .:..
. . ..
. . . .-;
. . ...*.
. :. ,:
...
individual mapping Jile (i.e'~ifiAj5$$ig5fi1e
.!
,
per
class)
beca~~e_stomg:large
.......
...>&i"
..
number of persistent classes intciC8fi+pi@pppigg
file can bk@iffcult to manage
i:$3 :I
and maintain. The nai~~ing,conventio~~~~~~t.o~u~e.
.:'..: . . . .the
. same name as the
;.&>:< .: .: persistent (POJO) class na&.
..:.=.
..., ..... or exam&
Accppn~.cllSs
...
;..
will have a
mapping file nanied r'~ccounfi$bm.xml.
...*::.... .
Alternatively hibernitte amotatiofis
can be used as pal? of your deisisfent
... ,..
... clnss code instead of the *.hbrn.wml
.........
..
files.
.....;.*.
".>c:
$:k:s!::j:,..
.s~.2.:.+,2..t <.
;<:
:.
;.
,%..
<f.:r2:
..,.
f!
. .. ... .. : .
.-
...
. . .
,.-,=*:
:;
:,;.-
":
J,-
c:2y;.+
I1~,,i:l,.:.i';i;"'
..
;.;
.',:
'$
<. ;
.:. . .
v:. s ;
.<> :
>.,,
:.
..
'I
'9
\;
::.:%
::
.-
7.r;
,,. &
:?.:
&.$
;.::
>..
>
:.:.
;a
.-. .
?&:...
...
!;ycz,;
.:,-
.G,
': ' s .
......
. .
5,.
.: c:, ,:,..;,<
u. :>
...
.',.
.;:*;,.
' . . . j ! .a:...,
...........
. ,. .. ..
........
....
.,
.,:*.-
Lara Technologies
'
..:,.
?.
. ,
2.L'.
.?.
,
:
:.>
e,-::.:
.c
*:?.
-.
,.:. .,.
..,
=.
h ttp://javaeasytoall.com
Programmatic configuration
,.
t;
,... .
,
;
_. ... ./
:.
;.
XML configuration (hiberna t5e'.;;cfg . xml)
.'i
'..
.:
. - .
..
i .
2
, .,
.i. .
. . . . .
Method chain&&$?.
. 5x
a p r o g r h i n g technipue.:that
is supported by many
(;. '.
.;: ,.u.
;*,.&.*%
ae&.:,,s:
is i b s readable when compared to actual java
hibernate .inPe'&a~@~This
e+-.. code. &fi;it is not riii&tory to use this fo&;t. Look
" how a
sessio&ktory
-.
<.
is ~reat6d'~hen
we use
-- --- ..if.c!-.&+-*,.;, $?+?.
1.
i.
2"
.-p.>..i.
--
. ....; :i;.
..
. ....
.
. .
.. ~. . .. . .
':
A
:
.
'
.:
'
..
. . . :... . .
... j
s.
_.
..
* First w@$&.d
., :rs.F to write Java domain obj'$<tf(beans with setter and
.:.
....$
.:. .:.
,. . :.,.
,?-.,<'I
: %.
y .>
..,?&?*.,
getter). ~ eF,zsi
@ r,r i a b l e sshould be same as databa~e~c~lumns.
..........
-;.. . . .
. .:ii.;i..zwfite
-..
hbm&s.
where we
java class to table &d..database
+?
.:
tz*
. .
. .
.-'-9
.cc$lurnns
to
3
-;&,class
variables.
r
.
~
. .
. ......
. ..,: :...
.,
'
.*.2.:-+
,i
'
Ji?.j.
G;!+:
. ;.
'
:.<:;,+.
@+pi;%
.-'!
>$?.?@
.:
,:
..
.>.
<..,t..
Exampl&f
. . . . ,.
. .-
*, .x.+:.
'.
..
.. .. :.
. .
..
s;'?
:;. t !.:';-?
, . jv
:.ti.. ;.
jjG;;..;,
.
.;
. : .. ..
. . .
,""'
..
>?
..Ck..+7
:.
<hibernate-maming>-:Jc,, ;
::: .
::.
. :;:;.,$ ,,\
<
.
A,l"~;.
<c I ass name-~+.oo.p.
...
.:. ,test.Use~::-tab:le="user">
"-;j;i!; ,,
.' _
<property col'lmpk,,USER
..-....,.J;
-~ ~ ~ @
. . . '. .l . e n ~ t h5"
="25
. .:" ..
:.:., ..p:jJ,...,
name="userNamg -not-null="trukk::type4ava .Iang: stringt'/>
<property c o l u m n = " . ~-~s ~~ ~~ ~ ~ 0 R d " ( l i n ~5"t b = " 2 5
name="user~asswor&!,
n&:null="trueV
.
.
typ&=ttjava.lang.~tring"b
;.
</class>
.. .. .. . .
.:
</hi bemate-mapping>
: <
:
. .L
. ot
,. ~.?-,l::.:;
.',A
I.?..I.
.+
..>
.L
,.
. )
..
: .~
'.
i2.6:.
. . ~.
. . . . . . .s
. :.
<,<,
ii.
iii.
:
...
.. .. .
> .
:r...
/..:..
. . .. .
"description" type="stringU>
="DESCRIPTIONn/>
>.
..::.
:'
-.. - .
..r.i9..3
-<:c
.+A+r.
...
...
.. .., -.-
. ..
...
.,..
.. \T.$'*;>
'
......
. .
. .. .7.
:<i:j::~
.'*;. ?.......
;:;:' ,*.::
.-'
L.*.L,
i:
,
.. . .
..
'3;,2$;.5"%...
. : ..
.. .:..
.
....,,.:.....
?<, .?:
;*, .,;
...........
. . .
.!',
.,;*. ,!.,;.
:..::.
:.'
,.: .:
Controlling insert-
Lara Technologies
080-41310124
14 1
5
Y
, : . ; >.<.
:..
,;a..,
. .>
ease
;\
.i
>.
..
.
..
.
.
'T
-+ ....... . . ..:.:."'
....
'
,:-
. . ..
...
7 .
. :.
>
..A.
'5
. <.
.. . : j
-_
;'
:.-:.
:: t.
. .:
.. .. .........
. :, .>
. .:. :. . .
. .".,. ..:; .
I:,. ....
,< .* ::; ?
:>
. ......
S
,. .:
i
.:
..
.. .'.
<
::
.....
~ .~.:. b x ~ ~ ainterface
e t o r- ~
used tb ereat&pidxies
...
ConnbtionProvider
interface
- used fori'JDBC..connection
....
.'., ,.........
,,,,.
.....
manegement
. ..
.
~ r a n,z:&
t
)
~
~
~
a
c
t
interface
~
r
=
y
Used
for
trans&ibn
T: ;:
.
'::
:
manageMent
.
,
~ r a :.;,n .+&
:.), i t i ~interface
n..
- Used for transaction rnana&&ent
Tra$$h%ii~$ManagementGoku
.
...
.:*..;.
..,.,
P'herface - used'*,...
: :_._.
....
?:.*'..:.
,.,'A:!:...
,.
.
trdri$kction
managehent,
._. .. . . ..
.
_
.. :. . ;. ..
;,, ::.. . . . . ' . . . . . . .
~ a h e e b t e r f a c -prb,vid$s
e
caching techniques and%trategies
~ a c h e ~ f o ; i d e interfacer
same 3s Cache interface
~ l a s s ~ e r s i s tinterface
er
- p r o v i d e s ; ~strategies
~~
Iden tifierGenera.t,or
interface - ~ s s :for'primary
d
key
:
. 'i.
....
generation
.
Dialect abstract cla<s::- provides SQL support
:,:
,.
.. .
..
..
?:.?
.
,. ..... .-.
.
;
..,.:%
<;::~-.;.:~:;$7)What
are t h e:.. \. . . ~. . . ~ ,,.t: :I. &. .~ . $ i ~that
~ !are
i ~there
t ~ in
~ hi
f b&.n+;tg?
~ ~ ~ ~
c..
......
. . . ., ..:
7 .
!,V
... . .::.
. . . . .;.i.:.. ..:. . .... . . . .. .?...,...
-i
, ..
-.:*.
.,... >..
........
. .
..,:..
:?:.<
...
. . ..>. .
:I++.
.,."\.,, x
-.$
,
,-,
39;
'
~%,.
'
..?.
.... .. .
. :..
.
i.
. ..
,
. .
... .-.
: .:A,f:
.<.I:.
.
'
:i
,L'....
. .- .
. -. . .
.2.;
..-,<;
':'
!+
c...:.
:A
..::.
..i
"
>. .
,.
.?.I'
. ,,,; %?
2-
,.LV*.
.;
.,I
!:
..
.
..
There are many benefits fiom these. Out of which the following are the
most important one.
Lara Technologies
080-41310124
142
J .
...
A,i.. .:
...
). :.
.-
!.:7j
,:.
:...
r..-5
.. ,
...
,i,
.-
a
r.z..'
x.!
.T,.~*
qy:.,
.:F!>;
.<: ,>:.,;.
,l.-.'L.
......
....
I.....
2..
r.
.
...
.
Y.,
.....
,
-.
.<.
$;
.<'L1-
".;
...
..........
,
...g.*!)L-$,
.........
3
....
..",
+.a:.
.;f--Y.
..*
,.?;.A
,,
,?:??>
...
-,
...........
; . *:..$.
i.
..
., -:
I.*.P ,<
.t*<g,.,2
k..
.?
*+$;.:+:
,29) what
e-..+:.;: '.i
.?&. .
......
.
:> *.
. . . . . . .,
. ---..*.>.
is SesSionFaetor-y?
. :. .
:7..;.z:*.
<.;;,.
..
.....
,;?
.; . : ; : ,:. :,
+, . ;: ;.
:;. ?i;,.
; .
;i'
..:
'.w :j . :::
A . 12.
. :..<
,.: .: 1: .
C
'
. . .... .. ..: . .
i..
. ::
.?h:
:$&:
c
'
4..
. . ,. .
i .
. . . . . . . . .
. . ,. . ,;?.< ;,;.-.
. .
-:. . . .. ,.
2..
....
-;,
'(T'.
.
. .:.
. ,.
,
....
:$~i??$i.on~actor~
.is a$ interface from which the applicati gefbSession
~nstandes
5v.,3!
%a+session~aciorycaches generate SQL statements &d;other
mappMg%netada$a
.-.: .+.+x,;..,
that hibernate uses at runtime. Typically one:.&tance
.
of
Sessioi@&dory at.startup is created for the whole. application. . . .
n.G.x>
-....-.
... .
..
.%...t'.<:<.:...
. . . .
1.
.
.
... :,...
...
.. <.
.
:j
...
'.,
. ,..
. -",.
....
.!.., ..
i. . :
i
. i
.<!
.!v
.....
.v ....... ,.
s':.*:!,..;!,2.>
.. ... -..
.
...
.
..
..
I
-==
I ~ o n f i ~ ution
r a cfg = new Confrgu$atiov();
I =...&..
-.:
...?.
. .. . ...
..
, ,
1 . 1 .
.>At,
~ ~ b ..::
r i f i .:.g: ~
......
h ,-~ r nll)f
: . ,'.'
~rnl
1 cfg.add~esource("rnyij-stance/~
I cfg.setPropertics( ~ y s t k r n . ~ e t ~ r o p e r t 1;.
i e s:.~.!:.'j
- :. . .
1
j SessionFaetory sessions~=~fg.buildSessionFactory0;
. .....
...;. ;!:. c.,
..
.!,
..
.,
.<..+
--
........
1I
r......
.f
-.
Lara Technologies
. ..:,,; : . ,
.......
.. ,. .>. . ;
..
. ...
..:
. . . ,. . .
,
. . .. .
>,
. .
.
.. . . . .
.....
'
--i.
.*.:
.(
% "'.
!! ,..
'
.
i'
:.
:
. :..
,;
..
..
:.,. ;\ .,& 3
;.;.....
. ... .. .
.
j 6, ..?+
.<.. . .;<,> .!
.$, $5 ><;
9.
..
L.;:'.;,;,.;.;?+
'
.; :
.!,. >
<;
c.
-.
.. .. i. .
...
. .%.
:.
(
;
....
;...?.s?.
?:,:,,-
.,
..
. .. . .. . ..
,:?
. .
c.;
............ !.;+*:
'
!.:+
. .,.. .
;.
+::
<:
... .2,:<; .; .. p;;,;+. .>.. :. '2
*, :.:.<:
,*. .
,
.,
.<*.
h' ;..a
.
. . .
.
, . ;.:
.-
<
.,
.:*,
.:..<;..,
...
*.$S
.5
-:
i
;.
..I
SC'J"
....
.. , .*
.;:.
.. .:
. .... ..
. .
.. .
:. . . . .,. .. ,,
. .:.,.
. . . . ..., .
,. .
.....
.<
,....
<
.
,..?
.
i . .
-.
&,\
<,.
....
Wraps a JDB"@connection
...< c.:! ;
+-, ,.:.ap
.
5. ..
$::.
F < :. ,.>~ forT$ansaction
&
~
~
;.'
,2; ;.:;
H0f&i$
.
. :mandatoq-(first-level)
..
cache of pqsistent
objects'afed
when
..
z..:j?{a.":
:.
+:
.:?:,#!:
navi&j*giae
bbj$k@*aRh
or looking up *Gfects by identlfi@
'-. +.'
~~.
;.,?'
.,*
T,;
,<
.-
.. .
:;@ :.
.k
..
$,;
,<..<-.,
:I ; . .
F
::;'
; :,y,
. ....>, ..'
.,:-.
.,
.,
? . . , )
;{
....
:..:. '.i
.::..:;~ b7>:$.
,..=
;...
.:
;.. ;.:
+,,.?..'%
.?.$
:&. ;+,<,
.:,i:?
.. :
2.'.
t p ; y .>.
-.
*$?
.&:;
,.?..:;:
>
.
>
,,
1.
i ..;...
.:
._.
;,
......
,.
'a.
+.
. ... P'.
I.3,
...
t .
..: . . .
. ;.: .
,..;
:
.?.
. ,.:
d!.:
. .: ,..:!. . . ..
, :
.. fi'.,
: . .
;g
.>a
!:
, a ......
. .d. . . . .;;; .:
..... . . . . '):;.
.
.
,.. .;. . . . : I.*.
,:
;. : :: <. ,,:.-
. .. ..
,,
. . .
..i
e.
.-.
;' : ,
...
. ..
.
i.
.,
'
-. .
.I.
.'
.-
>
,. "
.-.~
.!
....
....
....
:.
..
,
Lara Technologies
SessionFactory sessionFactory =
con,figuration.buildSessionFactory();
,. .:. ?
..
.:-.
. .
.....
I!:
..:".
..,>
..,:2.';.
.,. < .<d~.?
,-B..
p.::;l?z ..":...
,
:.
.: ,
;
; ;:
,...
..
'
. .:. ..
.: . .; .. .. .
:: 'i .;'.
. . ,.. . ...
,..
.
:.
.
, .:..
.. .
. .c<e.t.e.
A.
r:.:,l-'."
......
C:
2.
. ,..
. I
. .
..
>'
.
.
'
d
*
.
.
'-:.
, ,. ..
. . .. .... ... ...
.:;.
..:.
..
.... .
:<
' :Execute
...
,. .
.. :,.
Java objedts:.
y:,
.,. y?.:;
,t 2
T$@k.>'.::
;:
<+
;,$
:>
-.:,
,.>>..
:$*:&-.
c;.:
&'{'?;.
:.,
..
.....
. . %
.!:. .
:-.
*..
2,
..
. .
....--.:;.
... :, . .:
....
,...
::;-.:Jy7.
':-
....
.!,<..-.!
..
.
:.
...... ~i%<~.".:,t,'<:;
.' ..;.. .
. ' .-";
> .
',.>.
,.-,.,h..
<
:.,:
. . . ? .?
..:, .
. . . . . . .: "
..!..r
:_
.:.:
. . .
<.
...
-.
t.
?.<
s..
. . . >;>.. : . . . . . . .
. -. . ..
.
... .
..
-!
.
--..::.,...[...... . . . . . . .
:':'. . . . .
,",
:.:
'
..
<.;".,ir.
....
..,a
.... . . . .
?
:.:
.:
Lara Technologies
. .
5;
,:'
,
.
:
:?
:,.
. ., .
.-..
:;?.~.:+s;~,>,
:'"><.
;. .
. . . . . ;'>
.,. . . . . . . . . : . , -. :. ;* .
. :::+. :; >?.
....,,,(,
;
;
,:.?<
.. :,..; .. <. k:?:.:
..il.
.":t!e;::
.. .
;...:$'..':1_
.
. .:*..7:.::.,.>.,
. . .
!
:
.
'
::
-: ...
<
. . - ......
-:-_',:.%$,..
Y..'.
. . . .. .
.>:,.
<s
*;:<{,
':
sL!
p:y;.;
,
.:
.:
.:' .
. .. . .. .
..-..:
...
*
..
.?$..:
::
'&;
....
..
. ..
.. :
...
..
....
. .
>; . .A
.
..
.;
.>...,
;:;.:...
:$
::,;:-.,.:,! .?$.$:;.:.?;,,>
,
: .; y...: , .:';.?:$
.. - .:.
... . .:
. ... >
.,,:, ,
,,,.:.
:
: .'
. . * . :..!.>:
. . .
. !. .,,.., ? . - : : . . : ... .,:z<,.x,
> s,. :..\ 1: ;: :. i..;:.
,
..L:
/
.:
. . '.. .
;...
.......
.'I
. . . . .
+.; ...:.*
~$;
- .72.
;,.:- ...:
z;,., 1sr...
; . ;. .,
.
.:I---
.,:
,: ,t;
;@ig,;.,;
:3 ;..:;
--~~:-.:.*.~:$.~:>$.&,~;~.~
$;:;
$..,.;:$x:;.,>\
::;
': 3 <
. . > - .....
..A
:?.
:.
;?:-.: .
:+
...
'-
*.:i
.2>::.
-. .: '.,<
<.:;+..;:.;
,
>
.
<.:
..-:. ', . .
.>
._
Transient;Objects:
. . .2
., :-k
..,.
. .;
. . . .
classes
. .. . . .
'.,.?;:.,.:.., .
.-.
.
,:
!,
....., ,.
.;->.
.. .. .. .. .
1.
.
.. ... . .
.: ....
I
....
:-..:>s..
::
>. ,
.:.:.>
. '
...
.. ...-.
.!I..
.... ..
1,.
. ..-...
.,<;:. .;pe
>.
&tiched ~ b - ~ ~:. t. . ~ : , ,
. .. .
;.'.:$!.%,
....'
'
3..
?
.?It
: 4 ::
,..ah*,
gy.:! .;,
>:.
;:>.;:
. .
..-
( / . :
-';
:J ;
-.,:$:
:,.
'
. . . ..,
,: !,-:
D41i2
. .. . . . .
...
.r
<'
.-.
Cons: Working with detached objects is quite cumbersome and using Data
Transfer Object for the presentation layers
is better than using detached
Lara Technoiogies
Persistent Object:
Persistent Objcct is the ~bject:~thiit
. . . .,. isin session currently. It has a
representation in the database and.&k.i$6htifier
.;.. .;.
.:. ...
value and might have been
...'. :.
,>+&$@$Y
...
saved or loaded.
.;, ;! .S.;~?)
* %..,
..
.
.:
.,
,,it,,:
>:.:
. .pi
. . .
.:.
.,l.
'
<./-
.'.<&,
,: ,.,2.<:-
2xg5;.
.?~:,.-.C~P:~~~
l.; ...,...';.r'.
;+aa.z
;,
cx
,>.;,;.~3..:j.,;
- . + . . . ,T
! : ;::.j,.+.
j~~.
...
,?,<,
>.., ,.:
.<..:.
.
.....
,.
.'.
......
: . . . ::...
'.,:.. .
;.
.-.
..
,;: .
'C,
.......
i_ ,
'. qj,
. !,
<
: ."F
:-:.
-.
I,.
''
.-
.?
5.-!.
uses identifier
or
*iii.l
Sk
.,?
';
.?
'
<#.
....
_I.
....
.;!%
.-,. ..
..
:., .
i.
,.
?
.'.
. , , .. ,.. :.<
,.: ' . . \ - c
:..
!,
::y
;:&
..
,.
%.=. :.
.
...
. . ..>
. . ... . .
. . T.
.:; .? r
&; ......
5
,>
-.. .....
.
<..
.
.
.
i.
;.,
....
,..
...
.
<'.
.li.T<.
.'
2%:
.....
J..
..<
y;; ;
:
::
..
...;'.:x,.
.
. .
.
::ti..
'"""
'C
'"&BG2>&&&E&%:;
3
:
,
c
........:
. ." <$-e>
c*
.,+ - 3".: .k..-<&< -;..,,?;
!:;
.$.is
;.
-.
.,,..
;.:-; .
. . ::.>,21;$,.$,
. .,..:>
..', ,,:?:.
<,
:.
..,
..<*..$+:;..,
... 2...:,
.:<<;,::.-p::>.'..
,:.*,
. :. .:\
...:.
.>< .. <.:.:,;
....
. .:
.. .. ... .. ...l .
. . .~~
. .........
..
:-
i-
'T
'.>,.. :?'.+
*:.:.:.
,'
.. .. .
'.
. .. .. ..
.
.
.
T
'?.
..?.,
<.
;..;.:.
: :>
?..:;<:.
45) H6Wdo.
%+;:: you. define
sequence generatc%;primary
key in @%&ate?
. . . .
,:.
,
. .?
-4:.
<:.
,
.
*;
....
i$;@s><
.:
..,
i::
<.,.:
Fz.y.y:d..::.';
..\.,. ,..,;
.
.:>.,
. . .. .. . ,,.
.>.
"
?.>,,-
... ,
. .
<, so.
%&&i
$i:*;
., .=,<<.
;-
.
I
,;
v
L;; .
,/
.>.
1 ..?:,.,
j,,.?
-:
.v r
?';
...,
......<
LC;.
-., :..
. ..: .>,
..
Lars Technologies
.;
.x +
G
,+
;&.:
::>.:...::9 :, ...:.,..
7 ,! ::
, :..
.......: ; :..
- .
'
.<*.A
..&:
4$.?2
.-;
-? ..i.
>.,.
:,: + . i
'
....-,
- --..-.
. .,
-*2.:->
......
-*
...f ..'..,.'S .. ":
... .. .,. ..
. .
y:!;=.*r.
......-.
:.. ....
i
;
,,,,,
. .
:.<,
-,
.*;.;i...A'*
.,"
,
,%
:;:,..v?.::L
7 : ;
., ,
Z?
p.:
L$.*+$rz&
..
%.I
:.::..
. ....t:..,
s i '.i2ih~..\'
...
~:~,.~.:~~@~:+<L
>@g;z:.
... :I. In Hibedhie configuration file s e t . ~ i f ~ ~ $-.....w4
...:
EM" ::;::>%,:$$P
.*:x.~ . .. . y:.. .
. . .:..
i:.::
:<propertv name="show-s q l l l > t r u e < ~ ~ ;.::,.r ~ p e ~ $ ~ + y >
. .
:..
?,!
i'
>
..
....
waiit to see tkeii~ibernate
.... ;::: .44)'H;3.0u
>.,:$p$v+G w. .......generated SQL statemeoth?o&'::,
. - ..,..
..<.!G
:.,
. .<
. . <09&GyT
* .,.".'.\"
:yk.;:rz,y>,:v$... .;:.'Qq$$?;;i
.;
.
.
...?..
consoi,e,,~wb
.
...a t shou Id we d:~~44~i:~i$~+~:B~g.TG:~
: ~;,
2
,
.,.... .".
:.:.: ..
., . ,;.,;,* ;+.&..:p'"-k
;j$."
.$ ?:;,
;e >:;,
.-
.:;,i..
. . . .'I
i r .-.
.-:* .;+;
'<, ..; >.
?.<:.,
:
; "?.;?:"'
.,
~-.:
. .
,
.:.
.
. .
.
.>
..
...,... .. ... . .....?
. .. ,.
An implici~associationjoin
-Ax ~rdinaryj'oin~in
the FROM clause
A fetch join in the FROM clause.
.
..
.. .,;"
. .>'
,. .,+. +.,
!...:.
. . . .+
:. : . .- .. , .
. . .
. . . .. .
.,
A theta-&&
..
join in the m R .E clause..
"
. .
.
48) ~ x ~ l a i & r ~ r i . t eAPI
rja
. . .
.:$
<.
:.
....
.. . .
.; . ;.
;:..
......
..: >$.<..
. .
.
:,:
.?
. .. . .
;.
:..
,i
( ..
,. ..
'z
; ..
., :
t .
:.
.
.
:..
. .
. , . .. .
. ; I
,.
$e&
...
?.
%.
*,
'. I
I.,.';
.:.
. . /. . .
..,,.
,.+. . ?
>
. .?>
,b?
+..
.?
.
,.%
. :.
:,,:
c.
. : . . . . . . . . : ..: .;
._..
. . . . .
.i.-
.,;.
..
.
:,. .
..
.. :.. ...
..
..:
c
.
.:
:.
'
'
./.
::.,
Named SQL queries are defined in the mapping xrnl document and called
wherever required.
Example:
Lara Technologies
. . ..
. ..
....
;
.
:.:::;50)
How do you invoice Stored
Procedures?
.
......
.....".
.
.
.
".
;.
" : .
. .
.
., .:..:i_.:.
< . .. ..
. . . . -.. ., .
. .. ,
<. .,.7, :
.;..
I- 'T
.: ,.41,
. . >::, ,.
-7:.
.' .
-1
'1..
'
. .
:.,I
..
.
.
............
;>...,
:.
. . :. .. .
, .,
>
.:.
'..
;.
...
,........
.:..>;....
, . .:,
,.;:
.,.;>.
:".:
:. . . .
. . . -:
_.*l,
Lara Technologies
080-41310123
''
<:
':
> .*.
.:
. . . .. . .
..
get'():'method
will return null if
...
the uni;luk:id
.. . . is not found in the
database.
7
.
1oad ( ) just returns a proxy by default
g e t ( ) will hit theidatabase
:;..
. ..-.
and da&b2se y:25': behit .il~tilthz
..
,
immediately.
. . . .e:... .?,.
proxy is first invoked.
b.5
...........
.
. .
.
1. .
: -...
,:;?. .*s~.:.
.
i
A:.:::.,
..?.+.
: .
1:.
..
.-:.,:. . . .... .,.
. . . .
.. . ,.
. . . . .
. . 3 .1:
5 ..,;
.!?
,<.~
.
'
''
.,..- . i>;."
.
:7:
.<<. .
$ :.. ,
. .-
::
....
.
::-:
4 :*.
..
53) ' m a t i s the diff&ed6&:bktrv<;,band
...
. . . merge
. . . . .,.
and update ?
.....,: 2. g.;.
Use u p d a t e ( ) if you are)&$e that::the:&&kibn
does not contain an aha*.
.. . . . .. . . .
'
'..
persistent instance
...
with the same iddntifi&i+qjd
:> : . . . m&$e
. . . .
( ) if you want to - ;:: j
',.
...
merge your modifications
..~.
.
at any time withdut c o e i i d-. e i a t b,. ~of the state of ;:,. .. . ,. :. .. : i
......
6 i?,
;'
,-:. 7.:; . ..the
session
. ..: .
,, .. ..;.k
... ...:.
.... ,_
<.
:. . .:, ..
,.. .. ..__
>>
:
. . . .. .
.,,.
......
. .
. . :
....
. ...
. -..,.. . .:
:,,
"
, :5.
.rx..
,L
>
..
;..
.&
.......
'
. . . . .>
'
. .
-.
.
*
i
. .
ii*
..i
.,$,a
...
. . ...:... ...
. . .
... . .
.?
Bag
<.
Map
55) What are managed aSsociations and hibernate associations?
Lara Technologies
080-41310124
.. '-..!<:.., ....'>
:
. :.
;..
.. : .;:, z :
.L
'.'
!.
.l
. . . . ..:
:
.,.
.-
<:
..I
Inverse='..true(false"
. . .
.... - ......:'.
..
~.
.. .<... ....
,
...
..,, ., ....
.
.-
.
. . . . . ..
.-
-.
'
..,;
,:::.
i..
.. ,:>,
;
'
.
'
k
'
,;:;::q-<.,
.+<:>A:G&~,%=
;. Cr1tepaasi.a silnpii fi ed ~i
<,.j<,
:
0
...%,.
:
i:
"
.....
;<+:
...
.:
.......
C . . , ; p .._>
. . .
:.,............
=::. y:.Ls;.:.
.; ..:g..::
.: .::
fir;reui&ng.
entities
by composing Cnte@?$;;.
...c:. ...........
s,:.<
.<:::;;
:..:,:;
. . . .
.
.:. . . .
* ..
objec&;nis, is a very convenient
.
. ..~
. . .~
. ..=. p
....
. .~
. .,.
. .o
_. ..!for
a. . .c. . h.-functionality
....
like isear&$<.;
... ..;
,.!-.:,..i-~,.
r.
..-.,
,Gscreens
$&re
there
is
a
variable
n~fi~e$~p$~on&tions.Jo
be
placed
upon
ae:~c;;:
,?{:,;!;,~
' ~*,
...:
. . .
.:?<!;:;.
2 . 2;-:i%,x
$:<:?.:
- ,.:: :..! ., .
.-.:
<F
-'.; ....... *....
. 3:q &:>
."><
... ,.. . .
. .:.
...........
..
.,
... .- - .
$....z<y.
:,result
set.
,
: L<<i.< >!:.g ..
.<<"...
. .. .. . . . . . .
.I
s.">,
,:
'.?.-:".I'.
- . '.........
- I ;
. . . . . .->
. .......".!.........
<..."
P.. ,-.,.
- .:
:
,
.....
,.:>
.,
<
. . ,. :>
, . .
,.
*r..+.
-..<
;..+
6-.>i.
<k ., 3>,*.-.
,
<!
,,,
..i.
v , < - . .
%.,<.
>.<....c
,-
..3
<
?<
;"
:
;:-, ,?rr&.
.*
:
;
-.7. . . .!:.?
.*;,%:,.?:$%*
:\
. . ., . ...
.
.........y?..y!.ycy+?
?
.,*:
::. , . ., ., - ..
:
.,.*k..
C $.:.-!..7.
.<...I.,'.*
:
D,
. . . . . .
.............
>.,<<
, ..: $ 4..
$+*r.pt,
'1.
.4:;?\",
.
*"d. f .
i r.:..r1..
, x.
:.:.
.. . ..~.
.
. . .
&id$'
'
~I:;'
~~
-$::!.!.3QL
.
should be generated at runtime and contain only
columns
'kwKosevalues have. changed
.:.,.'.
d y n a r n i & i n s e r t ( d e f a u. l. t.s , ~false):
~
Specifie~thztINSEF&&QL
.:-.
:;
:
.@
.
should be generated at runt~me&d:contain
.
only the '~:@~urn?.~~~y&~$@values
., . ~. ..,
....*::<,,... ;-;,z+i*+.
!: .: ,. ,. ..->,: .,...
.
are not null.
++:.:: ..?m+;i.::>.
,.
>..
._:)
..:.d.?.
?
,
?::. p.
..
.
;i.
*.<: ;:,'.'.
.!?. . .
. ,.
L...?
<.,.,Z
:
;.
r.,<. ,:
7 .
..: . ,
*;:.
...
. . : . .""~m':;.i.::..
-,;:.?.;p
:'
.. :, y ,.>;I; ;,. .;
,
..5...
s!._.:.;...". .i
:. ;j. :.:.
. ..: .:. .
!:.
. -.
v.+
'(
',; :
:
.'.
~'
:*2;.?:,:2
'
. . . .
r,.,:..
'
,-.:..........
..,..,. . -: ..
.,: . :, .:,; .: . .
. . . ."..
. . . . . :.
,,:t
..
..<
.> r.: - .. ;
.,....
!'
r....
:'
..,.:',,.;
>;
*-;J:'.;..,
.,
,.;...:
Lara Technologies
...
+,
; ;<.
.I
~~
.-
;*:it.
.:
,..
.:
...
...
>.
....
"..
.. ,.-;:...
"
.! ', .
.,-.... :. . ..
I
. . . .. ... ...
61) What,-Jd~~yo&~&h:by
lazy fetching?
.
. ,. .. . .
>:,>,
,,;
' . .. . , .
.
. ....
....
..
. .,
..
, .,;,,F *
;.
It's a fetchf$2~strate~~
used by Hibernate to constraiiit'the
>.. ..;. retrieval
.of
... ,,:T +,:,
associated dv&cts
_ ,..
when applicat.ionjust needs the objects$&
doesn't need
.. . .. . :.<<.:.
. . .... + .:
to navigate thdT&sociation.
.
,.:.
;;.
.:.
<
. . . .> !::..
,
I
: '1.
?.* .; '.:. ,.
:.;:.,'
r.: :::::,
;.>.- >,:. .':,
. . . . . . .
...
. .
.......
.
...
,. . .. .. .;..
..,..
.... .
: :+;..i62);:What
is ta b l ..~ ~bclass,
~ ~ table
~ ~ per
~ class
s u hierarchy and!ia@le..per
; . :.
.
.
.
....
.:!;.:. .: , .:.
, ..;. >
.........
.... .,,>.
;;.;:.;.
. . . . .
. . .... . . .
:!:r ....:
-t!cong~,ete.
:. . :L-- class
.:.....
. . .
.
"
.
.
>
.
..
.f
..
:.?2,.>,
:.
. ,.
. . . ..
....
. .
<.
.::
.
..
.....
:
:
<
: . . ..
.. :. .. . .
. .
',<
. ... .
.,. ,!
... . ..
~_
a.
.. . . . .
...
. .. .. . . . ..
. .:.. . . . ..,:.
..,.. .
. ....: I
..'. .
......
... .
.;.
.z ... ..
. :
bas&$,
. .
. .........
.;". ....
..it
These'& aib.;i.&ritance
. . '
".
*%
c. !
. . .
. :.;
'
.:
;; i
:
:.;.. .;. ..: . ;.. . .. fi ,; . . . . . . . .
*.
. . . .. . . . .. .
. . .
. ; ;
..
.
. . . ?!. .......
;::,:,
;x<,
.
. . . :...
.,><."<>.;
.?!.:
:.... .
..,:,
.$..
..
'
: ..;.
.
. . . . . ... . .
I
...
....
....
..:
-Maay to one
If you retrieve book then publisher is also retrieved with it.
\
080-31310121
152
..
I
'
--II
<c,:,>pB.y.: n-.n:+=kiw~<';:.
.-...-
..
:...
.:
..
. :. ;..
..::<<..::
.:..
ii
.-.
..:i
'
..
..p~,>p~;;'
l-,.~ir-1;~=-post:3~~;le
'f..
.:~ * ~ , ~ - . ,-,:{-I-:
~ ~ : ;2=.1iit; >
l-,GIlt
: :1::1ty Kt:
:-
. . .
..'-I-.
.. :...
,.,.
.... <.
. ..
,. .
.....
. . . .
. '
-,
*;
..?.
67) w'hat$S?ihe
...
difference b & ~ e e ~.. $. .d ~ t e d .:. :. . a f i d < ~ r collection
d , e r e d in > : ? ? ::
.
. . .
hibernati?'.'
. . . .
.
.
.
. .
%/
,x.
- ;
col1ection:ivs.
. .. order collection :?o:r$ed
<, *
.....
.:+*:.;+<
.......
i.
::I
?.:
.* .:........;..
., >.
...
.,,(,
-.,* .
...*...:..
....
1..
'!.
?
.: ...
...-:.;,.,.-I,'
il>
',:
.
...
c. .:. .
: .. :.
<..
:..:....... . . .
'
. ;<: :.
=: .> *.:
1. :.. ....
.*? ,'
z,
-.
-.,:.-._
.-.
. I:..
..
,:;
.. ?: .
:.
.. . . .
. S,.('
...
.:,
order co1lectiCfp
:sorted cd!ec,tion
. . ,.
....:
..
.....
. . I
..
., . -......Y. ;.:
,:..:..;
. . ....
.
, .
A sorted &$]ection$i
a
collection by :util.&hg: sorting
.:
features provided?hy,
.. . .. .,.the Java
.'. .... ....
collections fiam&ho$k. TIle sorting i
occurs in the.mem&;:$f,~~lviwhichrunning Hi bemate, aft&the data
being read fi-om database using java
comparator.
order colle&$~fi;yissorti,r$a
. .
. .
collection by ipeb$Fj6ngfhe order-by
clause_forsorting this collection when
t e a : . . . -. ;.
.;
%
%
,..,!'.
IJaraTechnologies
.,,
- ..,
... ,
.; . . .
%
.
,1
., . ... :
:; ;
,;
..;. . .. ..
'
"
. .
.
:
. ... ..
. .. .. . 'L !?
. . . .... .;:c
:.......
:,
:.?:.
..
..: .
. .
'..
,.I.:-;.,:..
;*::$- '.>;::"
":., :.
.,2:.
....
;
....
.4
. .::
>-:;x:::$>:
,,
.,c.
..+.:<:.... .,).
.<.
..............
....
........
. . ::..
. .... . '
.....
i
?
.
2,
. . . .
.:
..
,..
,,.. . . :...:
...
.;
,k.t?
:.:. "
<.
,
....
.
. . . .
'
..
r,
-.i.,
.>.,.?
?
:,.,
.:; 2;.
.:
..*.;.,t
.
;.
:,
i:.;,l.
. I :g$:.:.between persistent
,
. % . .
..
1t's a mapping done in bbernategenexalJy td-Ghowthe relation
....
.;,. 'Cr .
'. .:<a
.
.
...,&?."
,:k:;i ,,
.
>j+s;
.....;El
.,r^...
l:........
:I;. .::I 1 :,
.-.;":.v.
,.:..'+:.s:..
.
. $ ;;, >.+;
- . 2%
.<..~.~~i<:;,~;.~.,;:~'~~i~.;_.~:
.... /:.. ..
.% .,: :.,.~y..<.;>?
,:.
:.
.,&
.1
.? :
; !>i-,z,:
.;->.,
::
5 .;
";y.
..
.... :.:
....
. ,'
i
u.
?*:?.
. ..<.+:.
.......I
;.A
,:
.:t:..
..'.
.
.
c'.-!.
.:.;;:2.:;:;:;
:;.r.,:
,.s
:.
,,:.'s7,;.."
.
.... .:
_:.:i.
... .:. . . . .
. c:..." ...... .
........
.,...
-. --..,.:
:.a<:
.:i;;:;j3:.:i.3
;.>.. ,
. . . .............
.;: [. :.
$
:.:.
.$'.>::<.
<
..
'.
..A
...... .
? ,;:.;
...
..- .y. . 1.
. ...
.< .
.- >
1;.
.-?;:.!
........
.$
:.
.*,!<<?<
.L:
;,?:I.;
.,,.,! : ...'.*c :
.:: ?
1.:.
.-.
'
:-
.:,
....
,.;:
.:<;:.
. . . . .
.;,:*.,I;;;
.
:..
?in
?..$.;:;
.;.:$
.;.
>. .
:.
. a Ur
. . .
interfaces or i~ientifiei$&~erties
g;:
::+ ...&,
Required to define an empty;constructor
.;:.
+".,..:
Sharedreferences~~otsu~~o&e$~
"*.. +
-,.. . ..- ,:,... .
.
::.
;.
,.$.
,2. a
Example:
.* ':
'
: !.!
.:!;..
. ..%.
J'?. . . . . . . .
.
I;
. . . . . . .
,; ,. ;;:i..;.
.. .-..;.
.. . .
.. ..>.;,.-. .
68) What is inheritance mapping strategy? (OR) What are the different
approaches to represent an inheritance hierarchy?
It's a typical strategy to map "wholelobject graph" which are related to each
other through inheritance. We use hese inheritance mapping strategies
,:.:;,
.: ..,:
. ,..: .. . . . .
..
';
.:
ableper per
- subclass 3. Table per concrete
... .
ll
,a
.
:.. . . . . ..
,g41:2${7.s
an inheritance mapping strategy in which a sing1e table is created.for all
.:..~;gd"s;.:;;.
.:;.. % ..,
,.~,..,~~$@&%%~lasses.in
the inheritante hierarchy
and map it accordingly in our mapping.
.....-...,.
).~~;57g;,:~g<G..?,
s?3;.s2:;*i:+%.
,Q
YE?:::
...
,
......
.,A
...
:>L:;@&.::..
.::;,!&& &5;.:
..:;
%
;:.
.:
i . ;:$,!:.
.
.,?
'
,.a
......
,
...
...:i..
...:...
":. . . ..: . .. .
:;. . ..,i:v;::.:
. . . . . .:
. ..
:.....:. .> .:
?.
,..
.
: <
...,>. ..,:*:.:
..
: . .;,I .- .;.. 1
.
r;. . . . .
c.;:.2 2. .,.- .
.
fil~~~~;$:;;$~g,i
,..,g
<. .....
-..
,
. . .7.-:_
;:-.I'
:.
. ,
:.
:
... :
,..,
.-.
.... ........:
. .. ~
7.
';,,,?.'.
L
ret&dais?
.i.-
. , .
..... .: . . . .
. . .
. .
..,
. -..:.I.
;yi.
.:.' ,
. .:
.:. .. . .. ..
:. . ...
..
.. . .">
i
-..
:5.
1. .......
inhe~-itance.mappin~
strategy in which we c~.ea_tg
[able k ~ ienih
<,..
ebq$~ete
... +... ..:-.ri
c!s.-::< and.inap it accordingly in our mapping fi1ein:Tava side.
.. , .:
..........
Disadvmtage
.-z.ia::t;-:4+
is that we could repeat the same properties in theconcrete
........
.....
classe'$~$$luding inherited properties.
...
<;.$i.:<,<,
of the
..,.T$$$i,.one
..,: .: -......
..+' ,
.,.
-:.r...,{. , :..
q;:'..::.
'
'. ,
SV. .i:
5 . ..
. ...i...:
_:
..: .. .. ... ,
::,.Zs:?.
:-;..-
. :\. .%
.
:.
..>
. . . . .,,:?
.
.: .
J+,:".. .:
>
.% . .
. ;.:
. . . . . .
.: !
$
I
.
......
..!<'I
::.
Lara Technologies
080-41310124
...
. ,..-