You are on page 1of 21

Gii thiu Hibernate Framework : Hibernate l 1 framework hay th vin gip d dng lu tr cc !

i t"ng #ava v $%&'( )* th+c hin vic g,n k-t cc !i t"ng #ava.l cc l/p thu0c ph1n model c2a 3ng d4ng g5i l model cla667 v/i cc !i t"ng trong &ataba6e nh 8able9 :rocedure9 ;iew(( v cc <uan h gia cc !i t"ng( )* tch 3ng d4ng ra thnh = thnh ph1n l thnh ph1n databa6e v ch>ng tr?nh ch@y 3ng d4ng( Ahi 6B d4ng Hibernate chng ta chC c1n <uan tDm -n cc model cla66 v Hibernate cho phEp th+c hin cc cDu truy vFn9 hay thao tc d liu.Gn6ert9 Hpdate9 &elete7 tr+c ti-p trIn cc !i t"ng Jodel( Kin trc Hibernate

Tnh nng chnh ca Hibernate 1( Knh L@.Japping7 !i t"ng model v 8able trong &ataba6e MN Jap m0t !i t"ng Jodel $la66 v/i 1 bOng trong &ataba6e th? Hibernate 6B d4ng m0t file Lml N mP tO cch th3c Japping hoQc c* thN dRng kS php Tnotation trong Uava V N Jap tr+c ti-p( =( 8Snh per6i6tence Hibernate cung cFp ph>ng th3c th+c hin vic Jap cc l/p Uava Lu!ng &ataba6e m0t cch trong 6u!t v Wi hXi cc l/p Uava ny c* $on6tructor khPng tham 6! v cc thu0c

tSnh khPng "c Y d@ng :ublic trong vi trZng h"p Qc bit c1n vi-t l@i hm Ha6h$ode v e<ual6( Hibernate h[ tr" vic truy vFn d liu theo nh*m.$ollection79 t\p h"p.%et79 danh 6ch.'i6t7 rFt t!t( ]( )gPn ng truy vFn Hibernate cho phEp ngZi dRng th+c hin cDu truy vFn ngay trIn !i t"ng m khPng phOi vi-t l@i cDu %^'( Hibernate cWn cung cFp mooi#t b0 $riteria ^uerie6 h[ tr" vic th+c hin truy vFn theo h/ng !i t"ng _( AhO n`ng t>ng thSch Hibernate h1u nh "c h[ tr" trong tFt cO cc aramework nh %pring9 %truct9 U%a.Uava %erver aace7 X !"ng # $ng !%ng Hibernate &'n gi(n trong )*+ )nte,,i./c #( $reate new :ro#ect tb aile trIn menu( 8Sch ch5n nh h?nh 6au aini6h

./c 0( 8b menu 8ool6 &ata%ource6c

$h5n &ataba6e N k-t n!i n-u cha c* th? thIm &ataba6e bdng cch click v biNu t"ng th1m v ch5n &e &ata %ource( Hin lIn h?nh 6au

1 ( ( = ( (

] ( ( _ ( (

V ( (

$c b/c th+c hin nh 6auf $h5n tab &ataba6e o e/c 1f ASch ch5n th vin #ar cho U&e$ &river aile6f o2!bc#342ar hoQc c,a55e5#046i7 o e/c =f $h5n U&e$ &river $la66 orac,e42!bc4!ri8er49rac,e*i8er nh h?nh vg

o e/c ]f 8hi-t l\p chu[i &ataba6e Hh' k-t n!i -n &ataba6e( ;S d4f -!bc:orac,e:thin::;<404;<40:#=0#:giaotie7 o tr!ng AiNm tra Lem k-t n!i &ataba6e c* thnh cPng hay khPng j ch5n )-u khPng thnh cPng hky Lem l@i gi trl &ataba6e Hh' $h5n tab %chema6 m 8able6 o 8Sch ch5n %chema no * m ta mu!n 6B d4ng9 chnng h@n 6chema GT)+?T@ nh h?nh bIn d/i .17 hoQc .=7 -!bc:orac,e:thin:gtie7tn>gtie7tn:;<404;<40:#=0#:giaotie7

e/c _9Vf )h\p H6er name9 v :a66word c2a H6er M`ng nh\p &ataba6e

$h if )-u ta thi-t l\p chu[i &ataba6e Hh' nh cch .=7 th? e/c _9V ta c* thN N

o ASch ch5n &ata%ource c2a pro#ect

N load cc table6 c2a 6chema ny vo trong

$h5n tab $on6ole ch5n %^' &ialect t>ng 3ng v/i lo@i &ataba6e nh h?nh d/i

ASch ch5n oA N hon thnh t@o &ata%ource9 6g chuyNn 6ang e/c ] ./c ; ch5n :ackage ch3a cc file 6inh ra

./c 3 ch5n cc 8able v cc $olumn6 c1n map v ch5n cc peneration %etting6( $u!i cRng oA 1 =

8Sch ch5n cc bOng c1n map

$ch 6B d4ng q1r l t@o thIm m0t &ata%ource m/i ch3a cc 8able6 q=r l t@o thIm m0t package m/i N ch3a cc file mapping do tool t@o ra ASch ch5n ASch ch5n ASch ch5n N hefe6h 8able6 N 6elect all 8able6 c1n penarate ra file mapping N de6elect all 8able6

ABu hCnh hibernate ;4<


$c file 6g "c peneration lf s hibernate(cfg(Lml s $c file model $la66 hay per6i6tent $la66 t>ng 3ng v/i cc 8able6 ch3a trong &ata%ource( ;S d4f %ervertntity(cla66 s aile maping u(hbm(Lml )0i dung file cFu h?nh hibernate4cDg4Em, nh 6auf vjLml ver6ionwx1(yx encodingwxutfz{xj| v}&o$8~:t hibernatezconfiguration :He'G$ zHibernateHibernate $onfiguration &8& ](yt) httpfhibernate(6ourceforge(nethibernatezconfigurationz](y(dtd| vhibernatezconfiguration| v6e66ionzfactory| <!-- Database connection settings --> vproperty namewconnection(drivercla66|oracle(#dbc(driver(oracle&rivervproperty| vproperty namewconnection(url|#dbcforaclefthinf]y(=(]y(=f1V=1fgiaotiepvproperty| vproperty namewconnection(u6ername|gtieptnvproperty| vproperty namewconnection(pa66word|gtieptnvproperty| <!-- JDBC connection pool (use the built-in) --> vproperty namewconnection(pool6ie|1vproperty| <!-- SQL dialect --> vproperty namewdialect|org(hibernate(dialect(oracle&ialectvproperty| <!-- Enable Hibe nate!s auto"atic session conte#t "anage"ent --> vproperty namewcurrent6e66ionconteLtcla66|threadvproperty| <!-- Disable the second-le$el cache --> vproperty namewcache(providercla66|org(hibernate(cache()o$ache:rovidervproperty| <!-- Echo all e#ecuted SQL to stdout --> vproperty namew6how6<l|truevproperty|

<!-- D op and e-c eate the database sche"a on sta tup --> vproperty namewhbm=ddl(auto|updatevproperty| <!-- C%c &ile "apping s' d(ng t ong DataSou ce -- > vmapping re6ourcewnetc6etechJapping%erver(hbm(Lml| cccc v6e66ionzfactory| vhibernatezconfiguration| )0i dung file mapping Fer8er4hbm4Em, nh 6auf vjLml ver6ionw1(yj| v}&o$8~:t hibernatezmapping :He'G$ zHibernateHibernate Japping &8& ](yt) httpfhibernate(6ourceforge(nethibernatezmappingz](y(dtd| vhibernatezmapping| vcla66 namewnet(c6etech(%ervertntity tablew%th;th 6chemawp8Gt:8)| vid namewid| v}zz vgenerator cla66w6e<uence| vparam namew6e<uence|6<dm6ervervparam| vgenerator| zz| vcolumn namewG& 6<lztypewvarchar= lengthw=y notznullwtrue| vid| vproperty namewma| vcolumn namewJT 6<lztypewvarchar= lengthw]y notznullwtrue| vproperty| vproperty namewten| vcolumn namew8t) 6<lztypewvarchar= lengthwVy| vproperty| vproperty namewmo8a| vcolumn namewJo8T 6<lztypewvarchar= lengthw=yy| vproperty| vproperty namewhieu'uc| vcolumn namewHGtH'H$ 6<lztypewvarchar= lengthw1 notznullwtrue|

vproperty| vproperty namewtrang8hai| vcolumn namew8hT)p8HTG 6<lztypewvarchar= lengthw1 notz nullwtrue| vproperty| vproperty namewipTddre66| vcolumn namewG:T&&ht%% 6<lztypewvarchar= lengthw1yy| vproperty| vproperty namewu6er)ame| vcolumn namewH%th)TJt 6<lztypewvarchar= lengthwVy| vproperty| vproperty namewpa66ord| vcolumn namew:T%%oh& 6<lztypewvarchar= lengthwVy| vproperty| vproperty namewngay8ao| vcolumn namew)pT~8To 6<lztypewdate lengthw notznullwtrue| vproperty| vproperty namewnguoi8ao| vcolumn namew)pHoG8To 6<lztypewvarchar= lengthw]y notz nullwtrue| vproperty| vproperty namewngay%ua$uoi| vcolumn namew)pT~%HT$HoG 6<lztypewdate lengthw| vproperty| vproperty namewnguoi%ua$uoi| vcolumn namew)pHoG%HT$HoG 6<lztypewvarchar= lengthw]y| vproperty| vproperty namewnode8ruyen)hanGd| vcolumn namew)o&t8hH~t))HT)G& 6<lztypewvarchar= lengthw=y| vproperty|

vcla66| vhibernatezmapping| )0i dung aile model $la66 public cla66 %ervertntity private %tring id public %tring getGd.7 return id public void 6etGd.%tring id7 thi6(id w id private %tring ma cccccc ccccc

Th"c thi cGc ,nh 5H, I5e,ectJ in5ertJ u7!ateK tr1n Tab,eJ LiewJ gMi ?roce!ureJ Function
1( TNo HibernateOti,4c,a55 N khYi t@o m0t !i t"ng 6e66ionaactory Om nh\n vic k-t n!i v/i &ataba6e v th+c hin cc giao dlch v/i &ataba6e package net(c6etech(Helper import org(hibernate(%e66ionaactory import org(hibernate(cfg($onfiguration public cla66 HibernateHtil private 6tatic final %e66ionaactory 6e66ionaactory w build%e66ionaactory.7 private 6tatic %e66ionaactory build%e66ionaactory.7 try $reate the %e66ionaactory from hibernate(cfg(Lml return new $onfiguration.7(configure.7(build%e66ionaactory.7 catch .8hrowable eL7 Jake 6ure you log the eLception9 a6 it might be 6wallowed %y6tem(err(println.Gnitial %e66ionaactory creation failed( s eL7 throw new tLceptionGnGnitialiertrror.eL7 public 6tatic %e66ionaactory get%e66ionaactory.7

return 6e66ionaactory 04 TNo Pain4c,a55 &Q Te5t $ng !%ng ;4 AGc cGch &Q Huer mRt tab,e MN %t't$8 m0t bOng c* ] cch th+c hin o &Rng hm create^uery.7 final %e66ion 6e66ion w HibernateHtil(get%e66ionaactory.7(open%e66ion.7 Suer <uery w 6e66ion(create^uery.Drom %ervertntity7 Ti5t re6ult w <uery(li6t.7 o &Rng hm create$riteria .7 Ariteria criteria w 6e66ion(create$riteria.%ervertntity(c,a557 criteria(add.he6triction6(e<.i7U!!re559]y(=(]y(=77 Ti5t re6ult w criteria(li6t.7 o &Rng hm create%^'^uery.7 Dy l cch th3c 6B d4ng )ative %^' Suer <uery w 6e66ion(create%^'^uery.%t't$8 6vr(u from %th;th 6vr where 6vr(G:T&&ht%% w j7(addtntity.6vr9%ervertntity( c,a557 <uery(6et%tring.y9]y(=(]y(=7 Ti5t re6ult w <uery(li6t.7 MN truyn tham 6! 6B d4ng j vS d4f Suer <uery Ftring 6tr w %t't$8 u ahoJ %th;th Htht G:T&&ht%% like j <uery w 6e66ion(create%^'^uery.6tr7(addtntity.%ervertntity(c,a557(6et%tring.y9 ]y(=(]y(=7 Ti5t re6ult w <uery(li6t.7 'u if MN thIm rng bu0c trong cDu lnh %t't$8 6au mnh Htht 6B d4ng method 6tatic l/p Ve5triction5 WXeHIFtring 7ro7ert @ameJ 9b2ect 8a,ueK WX,tIFtring 7ro7ert @ameJ 9b2ect 8a,ueK WXgtIFtring 7ro7ert @ameJ 9b2ect 8a,ueK WXe?ro7ert IFtring 7ro7ert @ameJ Ftring other?ro7ert @ameK

c( ;S d4 criteria(add.he6triction6(e<.i7U!!re559]y(=(]y(=77 c* ngha 6g th+c hin <uery %t't$8 u ahoJ %th;th Htht G:T&&ht%% w ]y(=(]y(= Ve5triction5 a ra cc iu kin N th+c hin truy vFn nh l like9 l/n h>n hoQc bdng9 nhX h>n hoQc bdng9 n!i bOng9 orc((( $h if 1( ^uery trIn ;iew th+c hin t>ng t+ nh trIn 8able =( Ahi th+c hin Gn6ert th? Hibernate khPng thN th+c hin c 1 lc nhiu row .khoOng vi tr`m ngh?n row7 c1n phDn ra cc kh!i.k thu\t eatch proce66ing7 N lm vic v/i tbng kh!i c* kSch c nhY h>n( ;S d4 f trong file hibernate(cfg(Lml property hibernate42!bc4batchX5i6e 0< t3c l c cho tbng l1n lm vic l =y bOn ghi( MN G)%th8 m0t dWng vo bOng th+c hin nh 6auf ;S d4 v th+c hin in6ert trIn tablef ))th*c hi+n ,nse t - b.n ghi private boolean in6erthecord%erver.Fer8er+ntit 6erver7 boolean 6ucce66 w fal6e 'i6t li6t %e66ion 6e66ion w HibernateHtil(get%e66ionaactory.7(get$urrent%e66ion.7 6e66ion(begin8ran6action.7 session.save(5er8er); 6e66ion(get8ran6action.7(commit.7 return 6ucce66 MN &t't8t m0t dWng trong bOng th+c hin nh 6auf ;S d4 1 method th+c hin deletef private boolean deletehecord%erver.%ervertntity 6erver7 boolean 6ucce66 w fal6e 'i6t li6t %e66ion 6e66ion w HibernateHtil(get%e66ionaactory.7(get$urrent%e66ion.7 6e66ion(begin8ran6action.7 $riteria criteria w 6e66ion(create$riteria.%ervertntity(cla667 if .i6)ullorelank.6erver( getGpTddre66.777

criteria(add.he6triction6(like.ipTddre669 6erver(getGpTddre66.79 JatchJode(tT$877 if .i6)ullorelank.6erver(get8en.777 criteria(add.he6triction6(like.ten9 6erver(get8en.79 JatchJode(tT$877 li6t w criteria(li6t.7 for .int i w y i v li6t(6ie.7 iss7 session.delete("ServerEntity", (ServerEntity) list.get(i)); 6ucce66 w true 6e66ion(get8ran6action.7(commit.7 return 6ucce66 MN H:&T8t m0t dWng trong bOng th+c hin nh 6auf ;S d4 v th+c hin update trIn tablef ))th*c hi+n /pdate - b.n ghi0 tha" s1 2c t u34n l5 bi6n 21i t78ng se $e private boolean updatehecord%erver.Fer8er+ntit 6erver7 boolean 6ucce66 w fal6e 'i6t li6t %e66ion 6e66ion w HibernateHtil(get%e66ionaactory.7(get$urrent%e66ion.7 6e66ion(begin8ran6action.7 session.update("ServerEntity ", server); 6e66ion(get8ran6action.7(commit.7 return 6ucce66 ;4 GMi Function or ?roce!ure 8@o 1 function hoQc th2 t4c khPng c* tham bi-n truyn vo nh vS d4 6au create or replace function getall6erver return %~%hta$Hh%oh T% cur %~%hta$Hh%oh begin open cur for 6elect u from 6erver return cur end

create or replace procedure procget6erver T% cur %~%hta$Hh%oh begin open cur for 6elect u from 6erver ccccccc(( end cFu h?nh trong file mapping nh 6auf v6<lz<uery namewgetTll%erver callablewtrue| vreturn alia6wev cla66wnet(c6etech(model( %ervertntity| vreturnzproperty namewid columnwG&| vreturnzproperty namewma columnwJT| vreturnzproperty namewten columnw8t)| cc( vreturn| j w getall6erver <!-- goi ha" -- > <!-- call procget6erver goi th9 t(c :h;ng tha" s1 -- > v6<lz<uery| goi trong code #ava nh 6auf <uery w 6e66ion4get@ame!Suer .getTll%erver7 Ti5t re6ult w <uery(li6t.7 YZi 8i Dunction ho[c th t%c c\ tham bin tru ]n 8^o nh/f create or replace function get6erver.ma%erver in varchar=7 return %~%hta$Hh%oh T% cur %~%hta$Hh%oh begin open cur for 6elect u from 6erver where ma w ma%erver return cur end create or replace procedure procget6erver .cur out %~%hta$Hh%oh7 T% begin open cur for 6elect u from 6erver end

cFu h?nh trong file mappingf v6<lz<uery namewget%erver callablewtrue| vreturn alia6w6vr cla66wnet(c6etech(model(%ervertntity | vreturnzproperty namewid columnwG&| vreturnzproperty namewma columnwJT| vreturnzproperty namewten columnw8t)| vreturn| j w call get6erver.j7<!-- goi ha" co tha" so -- > v6<lz<uery| v6<lz<uery namew get%erver= callablewtrue | vreturn alia6w6vr cla66wnet(c6etech(model( %ervertntity | vreturnzproperty namewid columnwG&| vreturnzproperty namewma columnwJT| vreturnzproperty namewten columnw8t)| ccccc(( vreturn| call procget6erver .j7 <!-- goi thu tuc co tha" so -- > v6<lz<uery| T/u _f khi dRng th v6<lz<uery| dRng N g5i hm hay th2 t4c th? u phOi thi-t l\p Tttribute ca,,ab,ewtrue p5i trong code #ava Suer <uery <uery w 6e66ion(get@ame!Suer .get%erver7 <uery(6et:arameter.y9]y(=(]y(=7 Ti5t re6ult w <uery(li6t.7 HoQc Suer <uery <uery w 6e66ion(get@ame!Suer .get%erver=7 Ti5t re6ult w <uery(li6t.7

%B d4ng 6e<uence 6inh ra Gd Ahai bo 6B d4ng nh 6au f vid namewid typewlong columnwper6onid| vgenerator cla66w6e<uence| vparam namew6e<uence|tIn c2a 6e<uencevparam| vgenerator| vid|

F" giZng 8^ khGc nhau gi`a Hibernate 8^ P bati5


o o o o Hirbernate v P bati5 u l th vin open%ource $Fu trc = framework ny gi!ng nhauf cc file config9 mapping $O = framework ny u c* h[ tr" cc Tnnotation Hibernate "c cc G&t for #ava h[ tr" trong khi P bati5 th? cha9 chSnh v? th- m l\p tr?nh dRng Hibernate tin l"i h>n

You might also like