Escolar Documentos
Profissional Documentos
Cultura Documentos
: / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
3.3.2.GA
Co p yri gh t © 20 0 4 R ed H at M i ddl ew are, L L C.
L egal N o ti c e
J u n e 24 , 20 0 9
P refac e
1 . F eedbac k
1 . T u to ri al
1 .1 . P art 1 - T h e fi rst H i bern ate A p p l i c ati o n
1 .1 .1 . S etu p
1 .1 .2. T h e fi rst c l ass
1 .1 .3 . T h e m ap p i n g fi l e
1 .1 .4 . H i bern ate c o n fi gu rati o n
1 .1 .5 . B u i l di n g w i th M av en
1 .1 .6 . S tartu p an d h el p ers
1 .1 .7 . L o adi n g an d sto ri n g o bj ec ts
1 .2. P art 2 - M ap p i n g asso c i ati o n s
1 .2.1 . M ap p i n g th e P erso n c l ass
1 .2.2. A u n i di rec ti o n al S et- based asso c i ati o n
1 .2.3 . W o rk i n g th e asso c i ati o n
1 .2.4 . Co l l ec ti o n o f v al u es
1 .2.5 . B i - di rec ti o n al asso c i ati o n s
1 .2.6 . W o rk i n g bi - di rec ti o n al l i n k s
1 .3 . P art 3 - T h e E v en tM an ager w eb ap p l i c ati o n
1 .3 .1 . W ri ti n g th e basi c serv l et
1 .3 .2. P ro c essi n g an d ren deri n g
1 .3 .3 . D ep l o yi n g an d testi n g
1 .4 . S u m m ary
2. A rc h i tec tu re
2.1 . O v erv i ew
2.2. I n stan c e states
2.3 . J M X I n tegrati o n
2.4 . J CA S u p p o rt
2.5 . Co n tex tu al sessi o n s
3 . Co n fi gu rati o n
1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 0 .3 . L o adi n g an o bj ec t
1 0 .4 . Q u eryi n g
1 0 .4 .1 . E x ec u ti n g q u eri es
1 0 .4 .2. F i l teri n g c o l l ec ti o n s
1 0 .4 .3 . Cri teri a q u eri es
1 0 .4 .4 . Q u eri es i n n ati v e S Q L
1 0 .5 . M o di fyi n g p ersi sten t o bj ec ts
1 0 .6 . M o di fyi n g detac h ed o bj ec ts
1 0 .7 . A u to m ati c state detec ti o n
1 0 .8 . D el eti n g p ersi sten t o bj ec ts
1 0 .9 . R ep l i c ati n g o bj ec t betw een tw o di fferen t datasto res
1 0 .1 0 . F l u sh i n g th e S essi o n
1 0 .1 1 . T ran si ti v e p ersi sten c e
1 0 .1 2. U si n g m etadata
1 1 . T ran sac ti o n s an d Co n c u rren c y
1 1 .1 . S essi o n an d tran sac ti o n sc o p es
1 1 .1 .1 . U n i t o f w o rk
1 1 .1 .2. L o n g c o n v ersati o n s
1 1 .1 .3 . Co n si deri n g o bj ec t i den ti ty
1 1 .1 .4 . Co m m o n i ssu es
1 1 .2. D atabase tran sac ti o n dem arc ati o n
1 1 .2.1 . N o n - m an aged en v i ro n m en t
1 1 .2.2. U si n g J T A
1 1 .2.3 . E x c ep ti o n h an dl i n g
1 1 .2.4 . T ran sac ti o n ti m eo u t
1 1 .3 . O p ti m i sti c c o n c u rren c y c o n tro l
1 1 .3 .1 . A p p l i c ati o n v ersi o n c h ec k i n g
1 1 .3 .2. E x ten ded sessi o n an d au to m ati c v ersi o n i n g
1 1 .3 .3 . D etac h ed o bj ec ts an d au to m ati c v ersi o n i n g
1 1 .3 .4 . Cu sto m i z i n g au to m ati c v ersi o n i n g
1 1 .4 . P essi m i sti c l o c k i n g
1 1 .5 . Co n n ec ti o n rel ease m o des
1 2. I n terc ep to rs an d ev en ts
1 2.1 . I n terc ep to rs
1 2.2. E v en t system
1 2.3 . H i bern ate dec l arati v e sec u ri ty
1 3 . B atc h p ro c essi n g
1 3 .1 . B atc h i n serts
1 3 .2. B atc h u p dates
1 3 .3 . T h e S tatel essS essi o n i n terfac e
1 3 .4 . D M L - styl e o p erati o n s
1 4 .H Q L : T h e H i bern ate Q u ery L an gu age
1 4 .1 . Case S en si ti v i ty
1 4 .2. T h e fro m c l au se
1 4 .3 . A sso c i ati o n s an d j o i n s
1 4 .4 . F o rm s o f j o i n syn tax
1 4 .5 . R eferri n g to i den ti fi er p ro p erty
1 4 .6 . T h e sel ec t c l au se
1 4 .7 . A ggregate fu n c ti o n s
1 4 .8 . P o l ym o rp h i c q u eri es
1 4 .9 . T h e w h ere c l au se
1 4 .1 0 . E x p ressi o n s
1 4 .1 1 . T h e o rder by c l au se
1 4 .1 2. T h e gro u p by c l au se
1 4 .1 3 . S u bq u eri es
1 4 .1 4 . H Q L ex am p l es
1 4 .1 5 . B u l k u p date an d del ete
1 4 .1 6 . T i p s & T ri c k s
1 4 .1 7 . Co m p o n en ts
4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
20 . T o o l set G u i de
20 .1 . A u to m ati c sc h em a gen erati o n
20 .1 .1 . Cu sto m i z i n g th e sc h em a
20 .1 .2. R u n n i n g th e to o l
20 .1 .3 . P ro p erti es
20 .1 .4 . U si n g A n t
20 .1 .5 . I n c rem en tal sc h em a u p dates
20 .1 .6 . U si n g A n t fo r i n c rem en tal sc h em a u p dates
20 .1 .7 . S c h em a v al i dati o n
20 .1 .8 . U si n g A n t fo r sc h em a v al i dati o n
21 . E x am p l e: P aren t/Ch i l d
21 .1 . A n o te abo u t c o l l ec ti o n s
21 .2. B i di rec ti o n al o n e- to - m an y
21 .3 . Casc adi n g l i fe c yc l e
21 .4 . Casc ades an d u ns av ed- v al u e
21 .5 . Co n c l u si o n
22. E x am p l e: W ebl o g A p p l i c ati o n
22.1 . P ersi sten t Cl asses
22.2. H i bern ate M ap p i n gs
22.3 . H i bern ate Co de
23 . E x am p l e: V ari o u s M ap p i n gs
23 .1 . E m p l o yer/E m p l o yee
23 .2. A u th o r/W o rk
23 .3 . Cu sto m er/O rder/P ro du c t
23 .4 . M i sc el l an eo u s ex am p l e m ap p i n gs
23 .4 .1 . " T yp ed" o n e- to - o n e asso c i ati o n
23 .4 .2. Co m p o si te k ey ex am p l e
23 .4 .3 . M an y- to - m an y w i th sh ared c o m p o si te k ey attri bu te
23 .4 .4 . Co n ten t based di sc ri m i n ati o n
23 .4 .5 . A sso c i ati o n s o n al tern ate k eys
24 . B est P rac ti c es
25 . D atabase P o rtabi l i ty Co n si derati o n s
25 .1 . P o rtabi l i ty B asi c s
25 .2. D i al ec t
25 .3 . D i al ec t reso l u ti o n
25 .4 . I den ti fi er gen erati o n
25 .5 . D atabase fu n c ti o n s
25 .6 . T yp e m ap p i n gs
R eferen c es
P reface
W o rk i n g w i th o bj ec t- o ri en ted so ftw are an d a rel ati o n al database c an be c u m berso m e an d ti m e c o n su m i n g i n
to day' s en terp ri se en v i ro n m en ts. H i bern ate i s an O bj ec t/R el ati o n al M ap p i n g to o l fo r J av a en v i ro n m en ts. T h e
term O bj ec t/R el ati o n al M ap p i n g (O R M ) refers to th e tec h n i q u e o f m ap p i n g a data rep resen tati o n fro m an
o bj ec t m o del to a rel ati o n al data m o del w i th a S Q L - based sc h em a.
H i bern ate n o t o n l y tak es c are o f th e m ap p i n g fro m J av a c l asses to database tabl es (an d fro m J av a data
typ es to S Q L data typ es), bu t al so p ro v i des data q u ery an d retri ev al fac i l i ti es. I t c an al so si gn i fi c an tl y redu c e
dev el o p m en t ti m e o th erw i se sp en t w i th m an u al data h an dl i n g i n S Q L an d J D B C.
H i bern ate' s go al i s to rel i ev e th e dev el o p er fro m 9 5 p erc en t o f c o m m o n data p ersi sten c e rel ated
p ro gram m i n g task s. H i bern ate m ay n o t be th e best so l u ti o n fo r data- c en tri c ap p l i c ati o n s th at o n l y u se
sto red- p ro c edu res to i m p l em en t th e bu si n ess l o gi c i n th e database, i t i s m o st u sefu l w i th o bj ec t- o ri en ted
do m ai n m o del s an d bu si n ess l o gi c i n th e J av a- based m i ddl e- ti er. H o w ev er, H i bern ate c an c ertai n l y h el p yo u
to rem o v e o r en c ap su l ate v en do r- sp ec i fi c S Q L c o de an d w i l l h el p w i th th e c o m m o n task o f resu l t set
tran sl ati o n fro m a tabu l ar rep resen tati o n to a grap h o f o bj ec ts.
6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 . F eed back
U se H i bern ate J I R A to rep o rt erro rs o r req u est en h ac em en ts to th i s do c u m en tati o n .
C h ap ter 1 . T utorial
1 .1 . P art 1 - T h e fi rst H i bern ate A p p l i c ati o n
1 .1 .1 . S etu p
1 .1 .2. T h e fi rst c l ass
1 .1 .3 . T h e m ap p i n g fi l e
1 .1 .4 . H i bern ate c o n fi gu rati o n
1 .1 .5 . B u i l di n g w i th M av en
1 .1 .6 . S tartu p an d h el p ers
1 .1 .7 . L o adi n g an d sto ri n g o bj ec ts
1 .2. P art 2 - M ap p i n g asso c i ati o n s
1 .2.1 . M ap p i n g th e P erso n c l ass
1 .2.2. A u n i di rec ti o n al S et- based asso c i ati o n
1 .2.3 . W o rk i n g th e asso c i ati o n
1 .2.4 . Co l l ec ti o n o f v al u es
1 .2.5 . B i - di rec ti o n al asso c i ati o n s
1 .2.6 . W o rk i n g bi - di rec ti o n al l i n k s
1 .3 . P art 3 - T h e E v en tM an ager w eb ap p l i c ati o n
1 .3 .1 . W ri ti n g th e basi c serv l et
1 .3 .2. P ro c essi n g an d ren deri n g
1 .3 .3 . D ep l o yi n g an d testi n g
1 .4 . S u m m ary
I n ten ded fo r n ew u sers, th i s c h ap ter p ro v i des an step - by- step i n tro du c ti o n to H i bern ate, starti n g w i th a
si m p l e ap p l i c ati o n u si n g an i n - m em o ry database. T h e tu to ri al i s based o n an earl i er tu to ri al dev el o p ed by
7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Important
T h i s tu to ri al ex p ec ts th e u ser h av e k n o w l edge o f bo th J av a an d S Q L . I f yo u h av e a l i m i ted
k n o w l edge o f J A V A o r S Q L , i t i s adv i sed th at yo u start w i th a go o d i n tro du c ti o n to th at
tec h n o l o gy p ri o r to attem p ti n g to l earn H i bern ate.
N ote
T h e di stri bu ti o n c o n tai n s an o th er ex am p l e ap p l i c ati o n u n der th e tutorial/eg p ro j ec t so u rc e
di rec to ry.
N ote
A l th o u gh yo u c an u se w h atev er database yo u feel c o m fo rtabl e u si n g, w e w i l l u se H S Q L D B (an
i n - m em o ry, J av a database) to av o i d desc ri bi n g i n stal l ati o n /setu p o f an y p arti c u l ar database
serv ers.
1 . 1 . 1 . S e tu p
8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< pro j ect x ml ns =" h ttp: / / mav en. apach e. o rg/ P O M / 4. 0. 0"
x ml ns : x s i=" h ttp: / / w w w . w 3. o rg/ 2 001/ X M L Sch ema- ins tance"
x s i: s ch emaL o catio n=" h ttp: / / mav en. apach e. o rg/ P O M / 4. 0. 0 h ttp: / / mav en. apach e. o rg/ x s d/ mav en- 4. 0. 0. x s d" >
< b u il d>
< ! - - w e do nt w ant th e v ers io n to b e part o f th e generated w ar f il e name - - >
< f inal Name>$ {artif actI d}< / f inal Name>
< / b u il d>
< ! - - H ib ernate giv es yo u a ch o ice o f b yteco de pro v iders b etw een cgl ib and j av as s is t - - >
< dependency>
< gro u pI d>j av as s is t< / gro u pI d>
< artif actI d>j av as s is t< / artif actI d>
< / dependency>
< / dependencies >
T ip
I t i s n o t a req u i rem en t to u se M av en . I f yo u w i sh to u se so m eth i n g el se to bu i l d th i s tu to i al
(su c h as A n t), th e l ayo u t w i l l rem ai n th e sam e. T h e o n l y c h an ge i s th at yo u w i l l n eed to
m an u al l y ac c o u n t fo r al l th e n eeded dep en den c i es. I f yo u u se so m eth i n g l i k e I v y p ro v i di n g
tran si ti v e dep en den c y m an agem en t yo u w o u l d sti l l u se th e dep en den c i es m en ti o n ed bel o w .
O th erw i se, yo u ' d n eed to grab all dep en den c i es, bo th ex p l i c i t an d tran si ti v e, an d add th em to
th e p ro j ec t' s c l assp ath . I f w o rk i n g fro m th e H i bern ate di stri bu ti o n bu n dl e, th i s w o u l d m ean
h ibern ate3 . j ar, al l arti fac ts i n th e lib/req uired di rec to ry an d al l fi l es fro m ei th er th e
lib/by tec od e/c g lib o r lib/by tec od e/j av assist di rec to ry; addi ti o n al l y yo u w i l l n eed bo th th e
serv l et- ap i j ar an d o n e o f th e sl f4 j l o ggi n g bac k en ds.
S av e th i s fi l e as p om . x m l i n th e p ro j ec t ro o t di rec to ry.
1 .1 .2 . T h e f i rs t c l as s
9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
pu b l ic cl as s E v ent {
priv ate L o ng id;
pu b l ic E v ent( ) {}
pu b l ic L o ng getI d( ) {
retu rn id;
}
T h i s c l ass u ses stan dard J av aB ean n am i n g c o n v en ti o n s fo r p ro p erty getter an d setter m eth o ds, as w el l as
p ri v ate v i si bi l i ty fo r th e fi el ds. A l th o u gh th i s i s th e rec o m m en ded desi gn , i t i s n o t req u i red. H i bern ate c an
al so ac c ess fi el ds di rec tl y, th e ben efi t o f ac c esso r m eth o ds i s ro bu stn ess fo r refac to ri n g.
T h e n o - argu m en t c o n stru c to r i s a req u i rem en t fo r al l p ersi sten t c l asses; H i bern ate h as to c reate o bj ec ts fo r
yo u , u si n g J av a R efl ec ti o n . T h e c o n stru c to r c an be p ri v ate, h o w ev er p ac k age o r p u bl i c v i si bi l i ty i s req u i red
fo r ru n ti m e p ro x y gen erati o n an d effi c i en t data retri ev al w i th o u t bytec o de i n stru m en tati o n .
1 .1 .3 . T h e mappi ng f ile
1 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
H i bern ate n eeds to k n o w h o w to l o ad an d sto re o bj ec ts o f th e p ersi sten t c l ass. T h i s i s w h ere th e H i bern ate
m ap p i n g fi l e c o m es i n to p l ay. T h e m ap p i n g fi l e tel l s H i bern ate w h at tabl e i n th e database i t h as to ac c ess,
an d w h at c o l u m n s i n th at tabl e i t sh o u l d u se.
T h e basi c stru c tu re o f a m ap p i n g fi l e l o o k s l i k e th i s:
< h ib ernate- mapping pack age=" o rg. h ib ernate. tu to rial . do main" >
[ ...]
< / h ib ernate- mapping>
Important
W e w ill o m i t th e D T D dec l arati o n in fu tu re ex am p l es to sh o rten th e c o de. I t i s, o f c o u rse, n o t
o p ti o n al .
B etw een th e tw o h ib ernate- mapping tags, i n c l u de a cl as s el em en t. A l l p ersi sten t en ti ty c l asses (agai n , th ere
m i gh t be dep en den t c l asses l ater o n , w h i c h are n o t fi rst- c l ass en ti ti es) n eed a m ap p i n g to a tabl e i n th e S Q L
database:
< h ib ernate- mapping pack age=" o rg. h ib ernate. tu to rial . do main" >
< / cl as s >
< h ib ernate- mapping pack age=" o rg. h ib ernate. tu to rial . do main" >
1 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T ip
nativ e i s n o l o n ger c o n si der th e best strategy i n term s o f p o rtabi l i ty. fo r fu rth er di sc u ssi o n , see
S ec ti o n 25 .4 , “ I den ti fi er gen erati o n ”
L astl y, w e n eed to tel l H i bern ate abo u t th e rem ai n i n g en ti ty c l ass p ro p erti es. B y defau l t, n o p ro p erti es o f th e
c l ass are c o n si dered p ersi sten t:
< h ib ernate- mapping pack age=" o rg. h ib ernate. tu to rial . do main" >
N ote
W h y do es th e date p ro p erty m ap p i n g i n c l u de th e co l u mn attri bu te, bu t th e titl e do es n o t?
W i th o u t th e co l u mn attri bu te, H i bern ate by defau l t u ses th e p ro p erty n am e as th e c o l u m n
n am e. T h i s w o rk s fo r titl e, h o w ev er, date i s a reserv ed k eyw o rd i n m o st databases so yo u w i l l
n eed to m ap i t to a di fferen t n am e.
T ip
H i bern ate m ak es th i s m ap p i n g typ e determ i n ati o n u si n g refl ec ti o n w h en th e m ap p i n g fi l es are
p ro c essed. T h i s c an tak e ti m e an d reso u rc es, so i f startu p p erfo rm an c e i s i m p o rtan t yo u sh o u l d
c o n si der ex p l i c i tl y defi n i n g th e typ e to u se.
1 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
N ote
W e do th i s do th at th e data rem ai n s betw een ru n s.
W e w ill u ti l i z e th e M av en ex ec p l u gi n to l au n c h th e H S Q L D B serv er by ru n n i n g:
m v n ex ec : j av a - D ex ec . m ain C lass= " org . h sq ld b. S erv er" - D ex ec . arg s= " - d atabase. 0 f ile: targ et/d ata/tutorial" Y o u
w i l l see i t start u p an d bi n d to a T CP /I P so c k et; th i s i s w h ere o u r ap p l i c ati o n w i l l c o n n ec t l ater. I f yo u w an t to
start w i th a fresh database du ri n g th i s tu to ri al , sh u tdo w n H S Q L D B , del ete al l fi l es i n th e targ et/d ata
di rec to ry, an d start H S Q L D B agai n .
C au ti on
T h e bu i l t- i n H i bern ate c o n n ec ti o n p o o l i s i n n o w ay i n ten ded fo r p ro du c ti o n u se. I t l ac k s sev eral
featu res fo u n d o n an y dec en t c o n n ec ti o n p o o l .
1 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< mapping res o u rce=" o rg/ h ib ernate/ tu to rial / do main/ E v ent. h b m. x ml " / >
N ote
N o ti c e th at th i s c o n fi gu rati o n fi l e sp ec i fi es a di fferen t D T D
Y o u c o n fi gu re H i bern ate' s Ses s io nF acto ry. S essi o n F ac to ry i s a gl o bal fac to ry resp o n si bl e fo r a p arti c u l ar
database. I f yo u h av e sev eral databases, fo r easi er startu p yo u sh o u l d u se sev eral < s es s io n- f acto ry>
c o n fi gu rati o n s i n sev eral c o n fi gu rati o n fi l es.
T ip
I n m o st c ases, H i bern ate i s abl e to p ro p erl y determ i n e w h i c h di al ec t to u se. S ee S ec ti o n 25 .3 ,
“ D i al ec t reso l u ti o n ” fo r m o re i n fo rm ati o n .
H i bern ate' s au to m ati c sessi o n m an agem en t fo r p ersi sten c e c o n tex ts i s p arti c u l arl y u sefu l i n th i s c o n tex t. T h e
o p ti o n tu rn s o n au to m ati c gen erati o n o f database sc h em as di rec tl y i n to th e database. T h i s c an
h b m2 ddl . au to
1 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 . 1 . 5 . B u i l d i ng w i th M av e n
1 . 1 . 6 . S tartu p and h e l pe rs
1 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
pu b l ic cl as s H ib ernateU til {
priv ate s tatic f inal Ses s io nF acto ry s es s io nF acto ry = b u il dSes s io nF acto ry( );
W e are n o w ready to start do i n g so m e real w o rj k w i th H i bern ate. L et' s start by w ri ti n g an E v entM anager c l ass
w i th a main( ) m eth o d:
1 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
impo rt j av a. u til . *;
pu b l ic cl as s E v entM anager {
priv ate v o id createA ndSto reE v ent( String titl e, D ate th eD ate) {
Ses s io n s es s io n = H ib ernateU til . getSes s io nF acto ry( ). getC u rrentSes s io n( );
s es s io n. b eginT rans actio n( );
A
o rg.h i bern ate.S essi o n i s desi gn ed to rep resen t a si n gl e u n i t o f w o rk (a si n gl e atm o i c p i ec e o f w o rk to be
p erfo rm ed). F o r n o w w e w i l l k eep th i n gs si m p l e an d assu m e a o n e- to - o n e gran u l ari ty betw een a H i bern ate
o rg.h i bern ate.S essi o n an d a database tran sac ti o n . T o sh i el d o u r c o de fro m th e ac tu al u n derl yi n g tran sac ti o n
system w e u se th e H i bern ate o rg. h ib ernate. T rans actio n A P I . I n th i s p arti c u l ar c ase w e are u si n g J D B C- based
tran sac ti o n al sem an ti c s, bu t i t c o u l d al so ru n w i th J T A .
Important
H i bern ate o ffers th ree m eth o ds o f c u rren t sessi o n trac k i n g. T h e " th read" based m eth o d i s n o t
i n ten ded fo r p ro du c ti o n u se; i t i s m erel y u sefu l fo r p ro to typ i n g an d tu to ri al s su c h as th i s o n e.
Cu rren t sessi o n trac k i n g i s di sc u ssed i n m o re detai l l ater o n .
A o rg.h i bern ate.S essi o n begi n s w h en th e fi rst c al l to getC u rrentSes s io n( ) i s m ade fo r th e c u rren t th read. I t i s
th en bo u n d by H i bern ate to th e c u rren t th read. W h en th e tran sac ti o n en ds, ei th er th ro u gh c o m m i t o r
ro l l bac k , H i bern ate au to m ati c al l y u n bi n ds th e o rg.h i bern ate.S essi o n fro m th e th read an d c l o ses i t fo r yo u . I f
1 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
S ee Ch ap ter 1 1 , Trans ac tions and C onc urre nc y fo r m o re i n fo rm ati o n abo u t tran sac ti o n h an dl i n g an d
dem arc ati o n . T h e p rev i o u s ex am p l e al so sk i p p ed an y erro r h an dl i n g an d ro l l bac k .
N ote
Y o u m ay n eed to p erfo rm m v n c om p ile fi rst.
H ere, w e are u si n g a H i bern ate Q u ery L an gu age (H Q L ) q u ery to l o ad al l ex i sti n g E v ent o bj ec ts fro m th e
database. H i bern ate w i l l gen erate th e ap p ro p ri ate S Q L , sen d i t to th e database an d p o p u l ate E v ent o bj ec ts
w i th th e data. Y o u c an c reate m o re c o m p l ex q u eri es w i th H Q L . S ee Ch ap ter 1 4 , H Q L : Th e H ib e rnate Q ue ry
L ang uag e fo r m o re i n fo rm ati o n .
N o w w e c an c al l o u r n ew fu n c ti o n al i ty, agai n u si n g th e M av en ex ec p l u gi n :
m v n ex ec : j av a - D ex ec . m ain C lass= " org . h ibern ate. tutorial. E v en tM an ag er" - D ex ec . arg s= " list"
1 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 . 2 . 1 . M appi ng th e P e rs on c l as s
pu b l ic cl as s P ers o n {
pu b l ic P ers o n( ) {}
< h ib ernate- mapping pack age=" o rg. h ib ernate. tu to rial . do main" >
Create an asso c i ati o n betw een th ese tw o en ti ti es. P erso n s c an p arti c i p ate i n ev en ts, an d ev en ts h av e
p arti c i p an ts. T h e desi gn q u esti o n s yo u h av e to deal w i th are: di rec ti o n al i ty, m u l ti p l i c i ty, an d c o l l ec ti o n
beh av i o r.
1 .2 .2 . A u ni d i re c ti onal S e t- b as e d as s oc i ati on
1 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
pu b l ic cl as s P ers o n {
< / cl as s >
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
| | | | _ _ _ _ _ _ _ _ _ _ _ _ _
| E V E NT S | | P E R SO N_ E V E NT | | |
| _ _ _ _ _ _ _ _ _ _ _ _ _ | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | | P E R SO N |
| | | | | _ _ _ _ _ _ _ _ _ _ _ _ _ |
| *E V E NT _ I D | < - - > | *E V E NT _ I D | | |
| E V E NT _ D A T E | | *P E R SO N_ I D | < - - > | *P E R SO N_ I D |
| T I T L E | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | | A G E |
| _ _ _ _ _ _ _ _ _ _ _ _ _ | | F I R ST NA M E |
| L A ST NA M E |
| _ _ _ _ _ _ _ _ _ _ _ _ _ |
1 .2 .3 . W ork i ng th e as s oc i ati on
2 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
/ / E nd o f f irs t u nit o f w o rk
aP ers o n. getE v ents ( ). add( anE v ent); / / aP ers o n ( and its co l l ectio n) is detach ed
T h i s i s n o t m u c h u se i n o u r ex am p l e, bu t i t i s an i m p o rtan t c o n c ep t yo u c an i n c o rp o rate i n to yo u r o w n
ap p l i c ati o n . Co m p l ete th i s ex erc i se by addi n g a n ew ac ti o n to th e m ai n m eth o d o f th e E v entM anager an d c al l i t
fro m th e c o m m an d l i n e. I f yo u n eed th e i den ti fi ers o f a p erso n an d an ev en t - th e s av e( ) m eth o d retu rn s i t
(yo u m i gh t h av e to m o di fy so m e o f th e p rev i o u s m eth o ds to retu rn th at i den ti fi er):
2 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 . 2 . 4 . C ol l e c ti on of v al u e s
L et' s add a c o l l ec ti o n o f em ai l addresses to th e P ers o n en ti ty. T h i s w i l l be rep resen ted as a j av a. u til . Set o f
j av a. l ang. String i n stan c es:
T h e m ap p i n g o f th i s Set i s as fo l l o w s:
H ere i s th e u p dated sc h em a:
2 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
| | | | _ _ _ _ _ _ _ _ _ _ _ _ _
| E V E NT S | | P E R SO N_ E V E NT | | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
| _ _ _ _ _ _ _ _ _ _ _ _ _ | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | | P E R SO N | | |
| | | | | _ _ _ _ _ _ _ _ _ _ _ _ _ | | P E R SO N_ E M A I L _ A D D R |
| *E V E NT _ I D | < - - > | *E V E NT _ I D | | | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| E V E NT _ D A T E | | *P E R SO N_ I D | < - - > | *P E R SO N_ I D | < - - >| *P E R SO N_ I D |
| T I T L E | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | | A G E | | *E M A I L _ A D D R |
| _ _ _ _ _ _ _ _ _ _ _ _ _ | | F I R ST NA M E | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| L A ST NA M E |
| _ _ _ _ _ _ _ _ _ _ _ _ _ |
T h i s ti m e w e di d n o t u se a f e tc h q u ery to i n i ti al i z e th e c o l l ec ti o n . M o n i to r th e S Q L l o g an d try to o p ti m i z e th i s
w i th an eager fetc h .
N ote
A rel ati o n al database i s m o re fl ex i bl e th an a n etw o rk p ro gram m i n g l an gu age, i n th at i t do es n o t
n eed a n av i gati o n di rec ti o n ; data c an be v i ew ed an d retri ev ed i n an y p o ssi bl e w ay.
2 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< s et name=" participants " tab l e=" P E R SO N_ E V E NT " inv ers e=" tru e" >
< k ey co l u mn=" E V E NT _ I D " / >
< many- to - many co l u mn=" P E R SO N_ I D " cl as s =" ev ents . P ers o n" / >
< / s et>
1 .2 .6 . W ork i ng b i - d i re c ti onal l i nk s
M an y dev el o p ers p ro gram defen si v el y an d c reate l i n k m an agem en t m eth o ds to c o rrec tl y set bo th si des (fo r
ex am p l e, i n P ers o n):
2 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 .3 .1 . W ri ti ng th e b as i c s e rv l e t
F i rst w e n eed c reate o u r basi c p ro c essi n g serv l et. S i n c e o u r serv l et o n l y h an dl es H T T P G E T req u ests, w e w i l l
o n ly im p l em en t th e do G et( ) m eth o d:
/ / I mpo rts
Simpl eD ateF o rmat dateF o rmatter = new Simpl eD ateF o rmat( " dd. M M . yyyy" );
try {
/ / B egin u nit o f w o rk
H ib ernateU til . getSes s io nF acto ry( ). getC u rrentSes s io n( ). b eginT rans actio n( );
/ / E nd u nit o f w o rk
H ib ernateU til . getSes s io nF acto ry( ). getC u rrentSes s io n( ). getT rans actio n( ). co mmit( );
}
catch ( E x ceptio n ex ) {
H ib ernateU til . getSes s io nF acto ry( ). getC u rrentSes s io n( ). getT rans actio n( ). ro l l b ack ( );
if ( Serv l etE x ceptio n. cl as s . is I ns tance( ex ) ) {
th ro w ( Serv l etE x ceptio n ) ex ;
}
el s e {
th ro w new Serv l etE x ceptio n( ex );
}
}
}
D o not u se a n ew H i bern ate Ses s io n fo r ev ery database o p erati o n . U se o n e H i bern ate Ses s io n th at i s sc o p ed
to th e w h o l e req u est. U se getC u rrentSes s io n( ), so th at i t i s au to m ati c al l y bo u n d to th e c u rren t J av a th read.
2 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 . 3 . 2 . P roc e s s i ng and re nd e ri ng
/ / W rite H T M L h eader
P rintW riter o u t = res po ns e. getW riter( );
o u t. printl n( " < h tml >< h ead>< titl e>E v ent M anager< / titl e>< / h ead>< b o dy>" );
/ / H andl e actio ns
if ( " s to re" . eq u al s ( req u es t. getP arameter( " actio n" )) ) {
String ev entT itl e = req u es t. getP arameter( " ev entT itl e" );
String ev entD ate = req u es t. getP arameter( " ev entD ate" );
/ / P rint page
printE v entF o rm( o u t);
l is tE v ents ( o u t, dateF o rmatter);
/ / W rite H T M L f o o ter
o u t. printl n( " < / b o dy>< / h tml >" );
o u t. f l u s h ( );
o u t. cl o s e( );
2 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
priv ate v o id l is tE v ents ( P rintW riter o u t, Simpl eD ateF o rmat dateF o rmatter) {
pro tected v o id createA ndSto reE v ent( String titl e, D ate th eD ate) {
E v ent th eE v ent = new E v ent( );
th eE v ent. s etT itl e( titl e);
th eE v ent. s etD ate( th eD ate);
1 . 3 . 3 . D e pl oy i ng and te s ti ng
2 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
N ote
I f yo u do n o t h av e T o m c at i n stal l ed, do w n l o ad i t fro m h ttp ://to m c at.ap ac h e.o rg/ an d fo l l o w th e
i n stal l ati o n i n stru c ti o n s. O u r ap p l i c ati o n req u i res n o c h an ges to th e stan dard T o m c at
c o n fi gu rati o n .
1 . 4 . S ummary
T h i s tu to ri al c o v ered th e basi c s o f w ri ti n g a si m p l e stan dal o n e H i bern ate ap p l i c ati o n an d a sm al l w eb
ap p l i c ati o n . M o re tu to ri al s are av ai l abl e fro m th e H i bern ate w ebsi te.
2 . 1 . O v erv iew
T h e di agram bel o w p ro v i des a h i gh - l ev el v i ew o f th e H i bern ate arc h i tec tu re:
2 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h e " c o m p reh en si v e" arc h i tec tu re abstrac ts th e ap p l i c ati o n aw ay fro m th e u n derl yi n g J D B C/J T A A P I s an d
al l o w s H i bern ate to m an age th e detai l s.
P ersi sten t o bj ec ts an d c o l l ec ti o n s
S h o rt- l i v ed, si n gl e th readed o bj ec ts c o n tai n i n g p ersi sten t state an d bu si n ess fu n c ti o n . T h ese c an be
o rdi n ary J av aB ean s/P O J O s. T h ey are asso c i ated w i th ex ac tl y o n e Ses s io n. O n c e th e Ses s io n i s c l o sed, th ey
w i l l be detac h ed an d free to u se i n an y ap p l i c ati o n l ayer (fo r ex am p l e, di rec tl y as data tran sfer o bj ec ts to
an d fro m p resen tati o n ).
T ran si en t an d detac h ed o bj ec ts an d c o l l ec ti o n s
I n stan c es o f p ersi sten t c l asses th at are n o t c u rren tl y asso c i ated w i th a Ses s io n. T h ey m ay h av e been
i n stan ti ated by th e ap p l i c ati o n an d n o t yet p ersi sted, o r th ey m ay h av e been i n stan ti ated by a c l o sed
Ses s io n.
E x te ns ion I nte rf ac e s
H i bern ate o ffers a ran ge o f o p ti o n al ex ten si o n i n terfac es yo u c an i m p l em en t to c u sto m i z e th e beh av i o r o f
yo u r p ersi sten c e l ayer. S ee th e A P I do c u m en tati o n fo r detai l s.
G i v en a " m i n i m al " arc h i tec tu re, th e ap p l i c ati o n byp asses th e T rans actio n/T rans actio nF acto ry an d/o r
C o nnectio nP ro v ider A P I s to c o m m u n i c ate w i th J T A o r J D B C di rec tl y.
2 . 2 . I ns tance s tates
A n i n stan c e o f a p ersi sten t c l ass c an be i n o n e o f th ree di fferen t states. T h ese states are defi n ed i n rel ati o n to
a p e rs is te nc e c onte x t. T h e H i bern ate Ses s io n o bj ec t i s th e p ersi sten c e c o n tex t. T h e th ree di fferen t states are
as fo l l o w s:
tran si en t
T h e i n stan c e i s n o t asso c i ated w i th an y p ersi sten c e c o n tex t. I t h as n o p ersi sten t i den ti ty o r p ri m ary k ey
2 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
v al u e.
p ersi sten t
T h e i n stan c e i s c u rren tl y asso c i ated w i th a p ersi sten c e c o n tex t. I t h as a p ersi sten t i den ti ty (p ri m ary k ey
v al u e) an d c an h av e a c o rresp o n di n g ro w i n th e database. F o r a p arti c u l ar p ersi sten c e c o n tex t, H i bern ate
g uarante e s th at p ersi sten t i den ti ty i s eq u i v al en t to J av a i den ti ty i n rel ati o n to th e i n - m em o ry l o c ati o n o f
th e o bj ec t.
detac h ed
T h e i n stan c e w as o n c e asso c i ated w i th a p ersi sten c e c o n tex t, bu t th at c o n tex t w as c l o sed, o r th e i n stan c e
w as seri al i z ed to an o th er p ro c ess. I t h as a p ersi sten t i den ti ty an d c an h av e a c o rresp o n di n g ro w i n th e
database. F o r detac h ed i n stan c es, H i bern ate do es n o t gu aran tee th e rel ati o n sh i p betw een p ersi sten t
i den ti ty an d J av a i den ti ty.
2 .3 . J M X I nteg ration
J M X i s th e J 2E E stan dard fo r th e m an agem en t o f J av a c o m p o n en ts. H i bern ate c an be m an aged v i a a J M X
stan dard serv i c e. A N M B ean i m p l em en tati o n i s p ro v i ded i n th e di stri bu ti o n : o rg. h ib ernate. j mx . H ib ernateServ ice .
H A R d e p loy m e nt: th e H i bern ate J M X serv i c e i s dep l o yed u si n g a J B o ss serv i c e dep l o ym en t desc ri p to r i n
an E A R an d/o r S A R fi l e, as i t su p p o rts al l th e u su al c o n fi gu rati o n o p ti o n s o f a H i bern ate Ses s io nF acto ry.
H o w ev er, yo u sti l l n eed to n am e al l yo u r m ap p i n g fi l es i n th e dep l o ym en t desc ri p to r. I f yo u u se th e
o p ti o n al H A R dep l o ym en t, J B o ss w i l l au to m ati c al l y detec t al l m ap p i n g fi l es i n yo u r H A R fi l e.
2 .4 . J C A S up p ort
H i bern ate c an al so be c o n fi gu red as a J CA c o n n ec to r. P l ease see th e w ebsi te fo r m o re i n fo rm ati o n . P l ease
n o te, h o w ev er, th at at th i s stage H i bern ate J CA su p p o rt i s u n der dev el o p m en t.
S tarti n g w i th v ersi o n 3 .0 .1 , H i bern ate added th e Ses s io nF acto ry. getC u rrentSes s io n( ) m eth o d. I n i ti al l y, th i s
assu m ed u sage o f J T A tran sac ti o n s, w h ere th e J T A tran sac ti o n defi n ed bo th th e sc o p e an d c o n tex t o f a c u rren t
sessi o n . G i v en th e m atu ri ty o f th e n u m ero u s stan d- al o n e J T A T rans actio nM anager i m p l em en tati o n s, m o st, i f
n o t al l , ap p l i c ati o n s sh o u l d be u si n g J T A tran sac ti o n m an agem en t, w h eth er o r n o t th ey are dep l o yed i n to a
J 2 E E c o n tai n er. B ased o n th at, th e J T A - based c o n tex tu al sessi o n s are al l yo u n eed to u se.
H o w ev er, as o f v ersi o n 3 .1 , th e p ro c essi n g beh i n d Ses s io nF acto ry. getC u rrentSes s io n( ) i s n o w p l u ggabl e. T o th at
en d, a n ew ex ten si o n i n terfac e, o rg. h ib ernate. co ntex t. C u rrentSes s io nC o ntex t , an d a n ew c o n fi gu rati o n
3 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
o rg. h ib ernate. co ntex t. J T A Ses s io nC o ntex t : c u rren t sessi o n s are trac k ed an d sc o p ed by a J T A tran sac ti o n . T h e
p ro c essi n g h ere i s ex ac tl y th e sam e as i n th e o l der J T A - o n l y ap p ro ac h . S ee th e J av ado c s fo r detai l s.
o rg. h ib ernate. co ntex t. T h readL o cal Ses s io nC o ntex t :c u rren t sessi o n s are trac k ed by th read o f ex ec u ti o n . S ee
th e J av ado c s fo r detai l s.
o rg. h ib ernate. co ntex t. M anagedSes s io nC o ntex t : c u rren t sessi o n s are trac k ed by th read o f ex ec u ti o n .
H o w ev er, yo u are resp o n si bl e to bi n d an d u n bi n d a Ses s io n i n stan c e w i th stati c m eth o ds o n th i s c l ass: i t
do es n o t o p en , fl u sh , o r c l o se a Ses s io n.
T h e fi rst tw o i m p l em en tati o n s p ro v i de a " o n e sessi o n - o n e database tran sac ti o n " p ro gram m i n g m o del . T h i s
i s al so al so k n o w n an d u sed as s e s s ion- p e r- re q ue s t. T h e begi n n i n g an d en d o f a H i bern ate sessi o n i s defi n ed
by th e du rati o n o f a database tran sac ti o n . I f yo u u se p ro gram m ati c tran sac ti o n dem arc ati o n i n p l ai n J S E
w i th o u t J T A , yo u are adv i sed to u se th e H i bern ate T rans actio n A P I to h i de th e u n derl yi n g tran sac ti o n system
fro m yo u r c o de. I f yo u u se J T A , yo u c an u ti l i z e th e J T A i n terfac es to dem arc ate tran sac ti o n s. I f yo u ex ec u te
i n an E J B c o n tai n er th at su p p o rts CM T , tran sac ti o n bo u n dari es are defi n ed dec l arati v el y an d yo u do n o t n eed
an y tran sac ti o n o r sessi o n dem arc ati o n o p erati o n s i n yo u r c o de. R efer to Ch ap ter 1 1 , Trans ac tions and
C onc urre nc y fo r m o re i n fo rm ati o n an d c o de ex am p l es.
3 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Y o u c an o btai n a o rg. h ib ernate. cf g. C o nf igu ratio n i n stan c e by i n stan ti ati n g i t di rec tl y an d sp ec i fyi n g X M L m ap p i n g
do c u m en ts. I f th e m ap p i n g fi l es are i n th e c l assp ath , u se addR es o u rce( ). F o r ex am p l e:
H i bern ate do es al l o w yo u r ap p l i c ati o n to i n stan ti ate m o re th an o n e o rg. h ib ernate. Ses s io nF acto ry. T h i s i s u sefu l
i f yo u are u si n g m o re th an o n e database.
3 . 3 . J DB C connections
I t i s adv i sabl e to h av e th e o rg. h ib ernate. Ses s io nF acto ry c reate an d p o o l J D B C c o n n ec ti o n s fo r yo u . I f yo u tak e
3 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
3 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
W arni ng
S om e of th e s e p rop e rtie s are " s y s te m - le v e l" only . S ystem - l ev el p ro p erti es c an be set o n l y v i a
j av a - D pro perty=v al u e o r h ibern ate. p rop erties. T h ey c annot be set by th e o th er tec h n i q u es desc ri bed
abo v e.
3 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
e.g . tru e | f al s e
h i bern ate.j dbc .u se_ get_ gen erated_ k eys retri ev e n ati v el y gen erated k eys after i n sert. R eq u i res J D B C3 +
dri v er an d J R E 1 .4 + , set to fal se i f yo u r dri v er h as p ro bl em s w i th
3 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
e.g . tru e| f al s e
T h e c l assn am e o f a c u sto m
o rg. h ib ernate. co nnectio n. C o nnectio nP ro v ider w h i c h p ro v i des J D B C
h i bern ate.c o n n ec ti o n .p ro v i der_ c l ass c o n n ec ti o n s to H i bern ate.
e.g . 1, 2 , 4, 8
E n abl es au to c o m m i t fo r J D B C p o o l ed c o n n ec ti o n s (i t i s n o t
h i bern ate.c o n n ec ti o n .au to c o m m it
rec o m m en ded). e.g . tru e | f al s e
S p ec i fi es w h en H i bern ate sh o u l d rel ease J D B C c o n n ec ti o n s. B y
defau l t, a J D B C c o n n ec ti o n i s h el d u n ti l th e sessi o n i s ex p l i c i tl y
c l o sed o r di sc o n n ec ted. F o r an ap p l i c ati o n serv er J T A
dataso u rc e, u se af ter_ s tatement to aggressi v el y rel ease
c o n n ec ti o n s after ev ery J D B C c al l . F o r a n o n - J T A c o n n ec ti o n , i t
o ften m ak es sen se to rel ease th e c o n n ec ti o n at th e en d o f eac h
tran sac ti o n , by u si n g af ter_ trans actio n. au to w ill c h o o se
af ter_ s tatement fo r th e J T A an d CM T tran sac ti o n strategi es an d
af ter_ trans actio n fo r th e J D B C tran sac ti o n strategy.
h i bern ate.c o n n ec ti o n .rel ease_ m o de
e.g . au to (defau l t) | o n_ cl o s e | af ter_ trans actio n | af ter_ s tatement
3 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
h ib ernate. cach e. regio n_ pref ix A p refi x to u se fo r sec o n d- l ev el c ac h e regi o n n am es. e.g . pref ix
F o rc es H i bern ate to sto re data i n th e sec o n d- l ev el c ac h e i n a m o re
h ib ernate. cach e. u s e_ s tru ctu red_ entries
h u m an - fri en dl y fo rm at. e.g . tru e| f al s e
e.g . tru e | f al s e
e.g . tru e | f al s e
3 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
e.g . tru e | f al s e
3 .4 .1 . S Q L D i al e c ts
A l w ays set th e h ib ernate. dial ect p ro p erty to th e c o rrec t o rg. h ib ernate. dial ect. D ial ect su bc l ass fo r yo u r database.
I f yo u sp ec i fy a di al ec t, H i bern ate w i l l u se sen si bl e defau l ts fo r so m e o f th e o th er p ro p erti es l i sted abo v e.
T h ism ean s th at yo u w i l l n o t h av e to sp ec i fy th em m an u al l y.
R DBM S Dialec t
O rac l e (an y v ersi o n ) o rg. h ib ernate. dial ect. O racl eD ial ect
S ybase A n yw h ere o rg. h ib ernate. dial ect. Syb as eA nyw h ereD ial ect
3 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
R DBM S Dialec t
F ro n tB ase o rg. h ib ernate. dial ect. F ro ntb as eD ial ect
3 . 4 . 2 . O u te r J oi n F e tc h i ng
I f yo u r database su p p o rts A N S I , O rac l e o r S ybase styl e o u ter j o i n s, oute r j oin f e tc h ing w i l l o ften i n c rease
p erfo rm an c e by l i m i ti n g th e n u m ber o f ro u n d tri p s to an d fro m th e database. T h i s i s, h o w ev er, at th e c o st o f
p o ssi bl y m o re w o rk p erfo rm ed by th e database i tsel f. O u ter j o i n fetc h i n g al l o w s a w h o l e grap h o f o bj ec ts
c o n n ec ted by m an y- to - o n e, o n e- to - m an y, m an y- to - m an y an d o n e- to - o n e asso c i ati o n s to be retri ev ed i n a
si n gl e S Q L SE L E C T .
O u ter j o i n fetc h i n g c an be di sabl ed g lob ally by setti n g th e p ro p erty h ib ernate. max _ f etch _ depth to 0. A setti n g o f
1 o r h i gh er en abl es o u ter j o i n fetc h i n g fo r o n e- to - o n e an d m an y- to - o n e asso c i ati o n s th at h av e been m ap p ed
w i th f etch =" j o in" .
3 . 4 . 4 . S e c ond - l e v e l and q u e ry c ac h e
3 . 4 . 5 . Q u e ry L ang u ag e S u b s ti tu ti on
T h i s w o u l d al l o w yo u to ren am e th e S Q L L O W E R fu n c ti o n .
3 . 4 . 6 . H i b e rnate s tati s ti c s
I f yo u en abl e h ib ernate. generate_ s tatis tics , H i bern ate ex p o ses a n u m ber o f m etri c s th at are u sefu l w h en
tu n i n g a ru n n i n g system v i a Ses s io nF acto ry. getStatis tics ( ). H i bern ate c an ev en be c o n fi gu red to ex p o se th ese
stati sti c s v i a J M X . R ead th e J av ado c o f th e i n terfac es i n o rg. h ib ernate. s tats fo r m o re i n fo rm ati o n .
3 . 5 . L og g ing
H i bern ate u ti l i z es S i m p l e L o ggi n g F ac ade fo r J av a (S L F 4 J ) i n o rder to l o g v ari o u s system ev en ts. S L F 4 J c an
di rec t yo u r l o ggi n g o u tp u t to sev eral l o ggi n g fram ew o rk s (N O P , S i m p l e, l o g4 j v ersi o n 1 .2, J D K 1 .4 l o ggi n g,
J CL o r l o gbac k ) dep en di n g o n yo u r c h o sen bi n di n g. I n o rder to setu p l o ggi n g yo u w i l l n eed slf 4 j - ap i. j ar i n
yo u r c l assp ath to geth er w i th th e j ar fi l e fo r yo u r p referred bi n di n g - slf 4 j - log 4 j 1 2 . j ar i n th e c ase o f L o g4 J . S ee
th e S L F 4 J do c u m en tati o n fo r m o re detai l . T o u se L o g4 j yo u w i l l al so n eed to p l ac e a log 4 j . p rop erties fi l e i n
3 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
yo u r c l assp ath . A n ex am p l e p ro p erti es fi l e i s di stri bu ted w i th H i bern ate i n th e s rc/ di rec to ry.
3 . 6 . I mp l ementing a NamingStrategy
T h e i n terfac e o rg. h ib ernate. cf g. NamingStrategy al l o w s yo u to sp ec i fy a " n am i n g stan dard" fo r database o bj ec ts
an d sc h em a el em en ts.
Y o u c an p ro v i de ru l es fo r au to m ati c al l y gen erati n g database i den ti fi ers fro m J av a i den ti fi ers o r fo r p ro c essi n g
" l o gi c al " c o l u m n an d tabl e n am es gi v en i n th e m ap p i n g fi l e i n to " p h ysi c al " tabl e an d c o l u m n n am es. T h i s
featu re h el p s redu c e th e v erbo si ty o f th e m ap p i n g do c u m en t, el i m i n ati n g rep eti ti v e n o i se (T B L _ p refi x es, fo r
ex am p l e). T h e defau l t strategy u sed by H i bern ate i s q u i te m i n i m al .
4 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Ses s io nF acto ry s f = new C o nf igu ratio n( ). co nf igu re( ). b u il dSes s io nF acto ry( );
A utom atic J N D I b ind ing : H i bern ate c an bi n d i ts Ses s io nF acto ry to J N D I after startu p .
4 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
D ep en di n g o n yo u r en v i ro n m en t, yo u m i gh t h av e to set th e c o n fi gu rati o n o p ti o n
h ib ernate. co nnectio n. aggres s iv e_ rel eas e to tru e i f yo u r ap p l i c ati o n serv er sh o w s " c o n n ec ti o n c o n tai n m en t"
ex c ep ti o n s.
T h e H i bern ate Ses s io n A P I i s i n dep en den t o f an y tran sac ti o n dem arc ati o n system i n yo u r arc h i tec tu re. I f yo u
l et H i bern ate u se J D B C di rec tl y th ro u gh a c o n n ec ti o n p o o l , yo u c an begi n an d en d yo u r tran sac ti o n s by c al l i n g
th e J D B C A P I . I f yo u ru n i n a J 2E E ap p l i c ati o n serv er, yo u m i gh t w an t to u se bean - m an aged tran sac ti o n s an d
c al l th e J T A A P I an d U s erT rans actio n w h en n eeded.
del egates to c o n tai n er- m an aged tran sac ti o n s i f an ex i sti n g tran sac ti o n i s u n derw ay i n th i s c o n tex t (fo r
ex am p l e, E J B sessi o n bean m eth o d). O th erw i se, a n ew tran sac ti o n i s started an d bean - m an aged
tran sac ti o n s are u sed.
S o m e featu res i n H i bern ate (i .e., th e sec o n d l ev el c ac h e, Co n tex tu al S essi o n s w i th J T A , etc .) req u i re ac c ess
to th e J T A T rans actio nM anager i n a m an aged en v i ro n m en t. I n an ap p l i c ati o n serv er, si n c e J 2E E do es n o t
stan dardi z e a si n gl e m ec h an i sm , yo u h av e to sp ec i fy h o w H i bern ate sh o u l d o btai n a referen c e to th e
T rans actio nM anager:
4 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
3 . 8 . 2 . J N D I- b ou nd SessionFactory
T h e easi est w ay to h an dl e Ses s io ns an d tran sac ti o n s i s H i bern ate' s au to m ati c " c u rren t" Ses s io n m an agem en t.
F o r a di sc u ssi o n o f c o n tex tu al sessi o n s see S ec ti o n 2.5 , “ Co n tex tu al sessi o n s” . U si n g th e " j ta" sessi o n c o n tex t,
i f th ere i s n o H i bern ate Ses s io n asso c i ated w i th th e c u rren t J T A tran sac ti o n , o n e w i l l be started an d asso c i ated
w i th th at J T A tran sac ti o n th e fi rst ti m e yo u c al l s es s io nF acto ry. getC u rrentSes s io n( ). T h e Ses s io ns retri ev ed v i a
getC u rrentSes s io n( ) i n th e" j ta" c o n tex t are set to au to m ati c al l y fl u sh befo re th e tran sac ti o n c o m p l etes, c l o se
after th e tran sac ti o n c o m p l etes, an d aggressi v el y rel ease J D B C c o n n ec ti o n s after eac h statem en t. T h i s al l o w s
th e Ses s io ns to be m an aged by th e l i fe c yc l e o f th e J T A tran sac ti o n to w h i c h i t i s asso c i ated, k eep i n g u ser
c o de c l ean o f su c h m an agem en t c o n c ern s. Y o u r c o de c an ei th er u se J T A p ro gram m ati c al l y th ro u gh
U s erT rans actio n, o r (rec o m m en ded fo r p o rtabl e c o de) u se th e H i bern ate T rans actio n A P I to set tran sac ti o n
bo u n dari es. I f yo u ru n i n an E J B c o n tai n er, dec l arati v e tran sac ti o n dem arc ati o n w i th CM T i s p referred.
3 .8 .4 . J M X d e pl oy me nt
H i bern ate i s di stri bu ted w i th o rg. h ib ernate. j mx . H ib ernateServ ice fo r dep l o ym en t o n an ap p l i c ati o n serv er w i th
J M X c ap abi l i ti es, su c h as J B o ss A S . T h e ac tu al dep l o ym en t an d c o n fi gu rati o n i s v en do r- sp ec i fi c . H ere i s an
ex am p l e j b o s s - s erv ice. x ml fo r J B o ss 4 .0 .x :
4 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< / mb ean>
4 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
4 .1 . A s imp l e P O J O ex amp l e
M o st J av a ap p l i c ati o n s req u i re a p ersi sten t c l ass rep resen ti n g fel i n es. F o r ex am p l e:
4 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
pu b l ic cl as s C at {
priv ate L o ng id; / / identif ier
pu b l ic C o l o r getC o l o r( ) {
retu rn co l o r;
}
v o id s etC o l o r( C o l o r co l o r) {
th is . co l o r = co l o r;
}
v o id s etSex ( ch ar s ex ) {
th is . s ex =s ex ;
}
pu b l ic ch ar getSex ( ) {
retu rn s ex ;
}
4 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T ran si ti v e reattac h m en t fo r detac h ed o bj ec ts (c asc ade u p date o r c asc ade m erge) - see S ec ti o n 1 0 .1 1 ,
“ T ran si ti v e p ersi sten c e”
Ses s io n. s av eO rU pdate( )
Ses s io n. merge( )
A c en tral featu re o f H i bern ate, p rox ie s , dep en ds u p o n th e p ersi sten t c l ass bei n g ei th er n o n - fi n al , o r th e
im p l em en tati o n o f an i n terfac e th at dec l ares al l p u bl i c m eth o ds.
C at dec l ares ac c esso r m eth o ds fo r al l i ts p ersi sten t fi el ds. M an y o th er O R M to o l s di rec tl y p ersi st i n stan c e
v ari abl es. I t i s better to p ro v i de an i n di rec ti o n betw een th e rel ati o n al sc h em a an d i n tern al data stru c tu res o f
th e c l ass. B y defau l t, H i bern ate p ersi sts J av aB ean s styl e p ro p erti es an d rec o gn i z es m eth o d n am es o f th e
fo rm getF o o , is F o o an d s etF o o . I f req u i red, yo u c an sw i tc h to di rec t fi el d ac c ess fo r p arti c u l ar p ro p erti es.
P ro p erti es n eed not be dec l ared p u bl i c - H i bern ate c an p ersi st a p ro p erty w i th a defau l t, pro tected o r priv ate
get / set p ai r.
4 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
pu b l ic String getName( ) {
retu rn name;
}
pro tected v o id s etName( String name) {
th is . name=name;
}
}
4 . 3 . I mp l ementing eq u al s ( ) and h as h C o d e( )
Y o u h av e to o v erri de th e eq u al s ( ) an d h as h C o de( ) m eth o ds i f yo u :
i n ten d to p u t i n stan c es o f p ersi sten t c l asses i n a Set (th e rec o m m en ded w ay to rep resen t m an y- v al u ed
asso c i ati o n s); and
i n ten d to u se reattac h m en t o f detac h ed i n stan c es
4 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
pu b l ic cl as s C at {
...
pu b l ic b o o l ean eq u al s ( O b j ect o th er) {
if ( th is == o th er) retu rn tru e;
if ( ! ( o th er ins tanceo f C at) ) retu rn f al s e;
retu rn tru e;
}
pu b l ic int h as h C o de( ) {
int res u l t;
res u l t = getM o th er( ). h as h C o de( );
res u l t = 2 9 * res u l t + getL itterI d( );
retu rn res u l t;
}
4 . 4 . Dy namic mod el s
N ote
Th e f ollow ing f e ature s are c urre ntly c ons id e re d e x p e rim e ntal and m ay c h ang e in th e ne ar
f uture .
4 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< / cl as s >
E v en th o u gh asso c i ati o n s are dec l ared u si n g target c l ass n am es, th e target typ e o f asso c i ati o n s c an al so be a
dyn am i c en ti ty i n stead o f a P O J O .
5 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Ses s io n s = o penSes s io n( );
T rans actio n tx = s . b eginT rans actio n( );
Ses s io n s = o penSes s io n( );
/ / C reate a cu s to mer
M ap dav id = new H as h M ap( );
dav id. pu t( " name" , " D av id" );
/ / L ink b o th
dav id. pu t( " o rganiz atio n" , f o o b ar);
/ / Sav e b o th
s . s av e( " C u s to mer" , dav id);
s . s av e( " O rganiz atio n" , f o o b ar);
tx . co mmit( );
s . cl o s e( );
/ / C reate a cu s to mer
M ap dav id = new H as h M ap( );
dav id. pu t( " name" , " D av id" );
dynamicSes s io n. s av e( " C u s to mer" , dav id);
...
dynamicSes s io n. f l u s h ( );
dynamicSes s io n. cl o s e( )
...
/ / C o ntinu e o n po j o Ses s io n
4 . 5 . T up l iz ers
o rg. h ib ernate. tu pl e. T u pl iz er, an d i ts su b- i n terfac es, are resp o n si bl e fo r m an agi n g a p arti c u l ar rep resen tati o n o f
a p i ec e o f data gi v en th at rep resen tati o n ' s o rg. h ib ernate. E ntityM o de . I f a gi v en p i ec e o f data i s th o u gh t o f as a
data stru c tu re, th en a tu p l i z er i s th e th i n g th at k n o w s h o w to c reate su c h a data stru c tu re an d h o w to ex trac t
v al u es fro m an d i n j ec t v al u es i n to su c h a data stru c tu re. F o r ex am p l e, fo r th e P O J O en ti ty m o de, th e
c o rresp o n di n g tu p l i z er k n o w s h o w c reate th e P O J O th ro u gh i ts c o n stru c to r. I t al so k n o w s h o w to ac c ess th e
P O J O p ro p erti es u si n g th e defi n ed p ro p erty ac c esso rs.
T h ere are tw o h i gh - l ev el typ es o f T u p l i z ers, rep resen ted by th e o rg. h ib ernate. tu pl e. entity. E ntityT u pl iz er an d
o rg. h ib ernate. tu pl e. co mpo nent. C o mpo nentT u pl iz er i n terfac es. E ntityT u pl iz ers are resp o n si bl e fo r m an agi n g th e
abo v e m en ti o n ed c o n trac ts i n regards to en ti ti es, w h i l e C o mpo nentT u pl iz ers do th e sam e fo r c o m p o n en ts.
5 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
5 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
/ **
*A v ery triv ial J D K P ro x y I nv o catio nH andl er impl ementatio n w h ere w e pro x y an interf ace as
* th e do main mo del and s impl y s to re pers is tent s tate in an internal M ap. T h is is an ex tremel y
* triv ial ex ampl e meant o nl y f o r il l u s tratio n.
*/
pu b l ic f inal cl as s D ataP ro x yH andl er impl ements I nv o catio nH andl er {
priv ate String entityName;
priv ate H as h M ap data = new H as h M ap( );
pu b l ic H as h M ap getD ata( ) {
retu rn data;
}
}
/ **
*
*/
pu b l ic cl as s P ro x yH el per {
pu b l ic s tatic String ex tractE ntityName( O b j ect o b j ect) {
/ / O u r cu s to m j av a. l ang. ref l ect. P ro x y ins tances actu al l y b u ndl e
/ / th eir appro priate entity name, s o w e s impl y ex tract it f ro m th ere
/ / if th is repres ents o ne o f o u r pro x ies ; o th erw is e, w e retu rn nu l l
if ( P ro x y. is P ro x yC l as s ( o b j ect. getC l as s ( ) ) ) {
I nv o catio nH andl er h andl er = P ro x y. getI nv o catio nH andl er( o b j ect );
if ( D ataP ro x yH andl er. cl as s . is A s s ignab l eF ro m( h andl er. getC l as s ( ) ) ) {
D ataP ro x yH andl er myH andl er = ( D ataP ro x yH andl er ) h andl er;
retu rn myH andl er. getE ntityName( );
}
}
retu rn nu l l ;
}
5 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2. R egi ster i t w i th th e o rg. h ib ernate. impl . Ses s io nF acto ryI mpl (w h i c h i s th e i m p l em en tati o n c l ass fo r
o rg. h ib ernate. Ses s io nF acto ry) u si n g th e regis terE ntityNameR es o l v er m eth o d.
C h ap ter 5 . B as ic O / R M ap p ing
5 .1 . M ap p i n g dec l arati o n
5 .1 .1 . D o c typ e
5 .1 .2. H i bern ate- m ap p i n g
5 .1 .3 . Cl ass
5 .1 .4 . i d
5 .1 .5 . E n h an c ed i den ti fi er gen erato rs
5 .1 .6 . I den ti fi er gen erato r o p ti m i z ati o n
5 .1 .7 . c o m p o si te- i d
5 .1 .8 . D i sc ri m i n ato r
5 .1 .9 . V ersi o n (o p ti o n al )
5 .1 .1 0 . T i m estam p (o p ti o n al )
5 .1 .1 1 . P ro p erty
5 .1 .1 2. M an y- to - o n e
5 .1 .1 3 . O n e- to - o n e
5 .1 .1 4 . N atu ral - i d
5 .1 .1 5 . Co m p o n en t an d dyn am i c - c o m p o n en t
5 .1 .1 6 . P ro p erti es
5 .1 .1 7 . S u bc l ass
5 .1 .1 8 . J o i n ed- su bc l ass
5 .1 .1 9 . U n i o n - su bc l ass
5 .1 .20 . J o i n
5 .1 .21 . K ey
5 .1 .22. Co l u m n an d fo rm u l a el em en ts
5 .1 .23 . I m p o rt
5 .1 .24 . A n y
5 .2. H i bern ate typ es
5 .2.1 . E n ti ti es an d v al u es
5 .2.2. B asi c v al u e typ es
5 .2.3 . Cu sto m v al u e typ es
5 .3 . M ap p i n g a c l ass m o re th an o n c e
5 .4 . S Q L q u o ted i den ti fi ers
5 .5 . M etadata al tern ati v es
5 .5 .1 . U si n g X D o c l et m ark u p
5 .5 .2. U si n g J D K 5 .0 A n n o tati o n s
5 .6 . G en erated p ro p erti es
5 .7 . A u x i l i ary database o bj ec ts
H ere i s an ex am p l e m ap p i n g:
5 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< / s u b cl as s >
< / cl as s >
5 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
5 . 1 . 1 . D oc ty pe
5.1.1.1. EntityResolver
H i bern ate w i l l fi rst attem p t to reso l v e D T D s i n i ts c l assp ath . I t do es th i s i s by regi steri n g a c u sto m
o rg. x ml . s ax . E ntityR es o l v er i m p l em en tati o n w i th th e S A X R eader i t u ses to read i n th e x m l fi l es. T h i s c u sto m
E ntityR es o l v er rec o gn i z es tw o di fferen t system I d n am esp ac es:
T h e fo l l o w i n g i s an ex am p l e o f u ti l i z i n g u ser n am esp ac i n g:
5 . 1 . 2 . H i b e rnate - mappi ng
T h i s el em en t h as sev eral o p ti o n al attri bu tes. T h e s ch ema an d catal o g attri bu tes sp ec i fy th at tabl es referred to
i n th i s m ap p i n g bel o n g to th e n am ed sc h em a an d/o r c atal o g. I f th ey are sp ec i fi ed, tabl en am es w i l l be
q u al i fi ed by th e gi v en sc h em a an d c atal o g n am es. I f th ey are m i ssi n g, tabl en am es w i l l be u n q u al i fi ed. T h e
def au l t- cas cade attri bu te sp ec i fi es w h at c asc ade styl e sh o u l d be assu m ed fo r p ro p erti es an d c o l l ec ti o n s th at do
n o t sp ec i fy a cas cade attri bu te. B y defau l t, th e au to - impo rt attri bu te al l o w s yo u to u se u n q u al i fi ed c l ass n am es
i n th e q u ery l an gu age.
5 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
s ch ema (o p ti o n al ): th e n am e o f a database sc h em a.
T h e h ib ernate- mapping el em en t al l o w s yo u to n est sev eral p ersi sten t < cl as s > m ap p i n gs, as sh o w n abo v e. I t i s,
h o w ev er, go o d p rac ti c e (an d ex p ec ted by so m e to o l s) to m ap o n l y a si n gl e p ersi sten t c l ass, o r a si n gl e c l ass
h i erarc h y, i n o n e m ap p i n g fi l e an d n am e i t after th e p ersi sten t su p erc l ass. F o r ex am p l e, C at. h b m. x ml ,
D o g. h b m. x ml , o r i f u si n g i n h eri tan c e, A nimal . h b m. x ml .
5 . 1 . 3 . C l as s
< cl as s
name=" C l as s Name"
tab l e=" tab l eName"
dis criminato r- v al u e=" dis criminato r_ v al u e"
mu tab l e=" tru e| f al s e"
s ch ema=" o w ner"
catal o g=" catal o g"
pro x y=" P ro x yI nterf ace"
dynamic- u pdate=" tru e| f al s e"
dynamic- ins ert=" tru e| f al s e"
s el ect- b ef o re- u pdate=" tru e| f al s e"
po l ymo rph is m=" impl icit| ex pl icit"
w h ere=" arb itrary s q l w h ere co nditio n"
pers is ter=" P ers is terC l as s "
b atch - s iz e=" N"
o ptimis tic- l o ck =" no ne| v ers io n| dirty| al l "
l az y=" tru e| f al s e"
entity- name=" E ntityName"
ch eck =" arb itrary s q l ch eck co nditio n"
ro w id=" ro w id"
s u b s el ect=" SQ L ex pres s io n"
ab s tract=" tru e| f al s e"
no de=" el ement- name"
/ >
5 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
I m m u tabl e c l asses, mu tab l e=" f al s e" , c an n o t be u p dated o r del eted by th e ap p l i c ati o n . T h i s al l o w s H i bern ate to
m ak e so m e m i n o r p erfo rm an c e o p ti m i z ati o n s.
T h e o p ti o n al pro x y attri bu te en abl es l az y i n i ti al i z ati o n o f p ersi sten t i n stan c es o f th e c l ass. H i bern ate w i l l
i n i ti al l y retu rn CG L I B p ro x i es th at i m p l em en t th e n am ed i n terfac e. T h e p ersi sten t o bj ec t w i l l l o ad w h en a
m eth o d o f th e p ro x y i s i n v o k ed. S ee " I n i ti al i z i n g c o l l ec ti o n s an d p ro x i es" bel o w .
5 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
m ap p ed i n si de th i s < cl as s > dec l arati o n as a < s u b cl as s > o r < j o ined- s u b cl as s >. F o r m o st p u rp o ses, th e defau l t
po l ymo rph is m=" impl icit" i s ap p ro p ri ate. E x p l i c i t p o l ym o rp h i sm i s u sefu l w h en tw o di fferen t c l asses are m ap p ed
to th e sam e tabl e T h i s al l o w s a " l i gh tw ei gh t" c l ass th at c o n tai n s a su bset o f th e tabl e c o l u m n s.
T h e pers is ter attri bu te l ets yo u c u sto m i z e th e p ersi sten c e strategy u sed fo r th e c l ass. Y o u c an , fo r ex am p l e,
sp ec i fy yo u r o w n su bc l ass o f o rg. h ib ernate. pers is ter. E ntityP ers is ter, o r yo u c an ev en p ro v i de a c o m p l etel y n ew
im p l em en tati o n o f th e i n terfac e o rg. h ib ernate. pers is ter. C l as s P ers is ter th at i m p l em en ts, fo r ex am p l e,
p ersi sten c e v i a sto red p ro c edu re c al l s, seri al i z ati o n to fl at fi l es o r L D A P . S ee o rg. h ib ernate. tes t. C u s to mP ers is ter
fo r a si m p l e ex am p l e o f " p ersi sten c e" to a H as h tab l e.
T h e dynamic- u pdate an d dynamic- ins ert setti n gs are n o t i n h eri ted by su bc l asses, so th ey c an al so be sp ec i fi ed
o n th e < s u b cl as s > o r < j o ined- s u b cl as s > el em en ts. A l th o u gh th ese setti n gs c an i n c rease p erfo rm an c e i n so m e
c ases, th ey c an ac tu al l y dec rease p erfo rm an c e i n o th ers.
U se o f s el ect- b ef o re- u pdate w i l l u su al l y dec rease p erfo rm an c e. I t i s u sefu l to p rev en t a database u p date
tri gger bei n g c al l ed u n n ec essari l y i f yo u reattac h a grap h o f detac h ed i n stan c es to a Ses s io n.
al l : c h ec k al l c o l u m n s
dirty: c h ec k th e c h an ged c o l u m n s, al l o w i n g so m e c o n c u rren t u p dates
no ne: do n o t u se o p ti m i sti c l o c k i n g
T h ere i s n o di fferen c e betw een a v i ew an d a base tabl e fo r a H i bern ate m ap p i n g. T h i s i s tran sp aren t at th e
database l ev el , al th o u gh so m e D B M S do n o t su p p o rt v i ew s p ro p erl y, esp ec i al l y w i th u p dates. S o m eti m es yo u
w an t to u se a v i ew , bu t yo u c an n o t c reate o n e i n th e database (i .e. w i th a l egac y sc h em a). I n th i s c ase, yo u
c an m ap an i m m u tabl e an d read- o n l y en ti ty to a gi v en S Q L su bsel ec t ex p ressi o n :
5 .1 .4 . id
5 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< id
name=" pro pertyName"
type=" typename"
co l u mn=" co l u mn_ name"
u ns av ed- v al u e=" nu l l | any| no ne| u ndef ined| id_ v al u e"
acces s =" f iel d| pro perty| C l as s Name" >
no de=" el ement- name| @ attrib u te- name| el ement/ @ attrib u te| . "
T h ere i s an al tern ati v e < co mpo s ite- id> dec l arati o n th at al l o w s ac c ess to l egac y data w i th c o m p o si te k eys. I ts
u se i s stro n gl y di sc o u raged fo r an yth i n g el se.
T h e o p ti o n al < generato r> c h i l d el em en t n am es a J av a c l ass u sed to gen erate u n i q u e i den ti fi ers fo r i n stan c es
o f th e p ersi sten t c l ass. I f an y p aram eters are req u i red to c o n fi gu re o r i n i ti al i z e th e gen erato r i n stan c e, th ey
are p assed u si n g th e < param> el em en t.
A l l gen erato rs i m p l em en t th e i n terfac e o rg. h ib ernate. id. I dentif ierG enerato r. T h i s i s a v ery si m p l e i n terfac e.
S o m e ap p l i c ati o n s c an c h o o se to p ro v i de th ei r o w n sp ec i al i z ed i m p l em en tati o n s, h o w ev er, H i bern ate
p ro v i des a ran ge o f bu i l t- i n i m p l em en tati o n s. T h e sh o rtc u t n am es fo r th e bu i l t- i n gen erato rs are as fo l l o w s:
increment
gen erates i den ti fi ers o f typ e l o ng, s h o rt o r int th at are u n i q u e o n l y w h en n o o th er p ro c ess i s i n serti n g data
i n to th e sam e tabl e. D o not us e in a c lus te r.
identity
s eq u ence
h il o
u ses a h i /l o al go ri th m to effi c i en tl y gen erate i den ti fi ers o f typ e l o ng, s h o rt o r int , gi v en a tabl e an d c o l u m n
(by defau l t h ib ernate_ u niq u e_ k ey an d nex t_ h i resp ec ti v el y) as a so u rc e o f h i v al u es. T h e h i /l o al go ri th m
6 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
s eq h il o
u ses a h i /l o al go ri th m to effi c i en tl y gen erate i den ti fi ers o f typ e l o ng, s h o rt o r int , gi v en a n am ed database
seq u en c e.
u u id
u ses a 1 28 - bi t U U I D al go ri th m to gen erate i den ti fi ers o f typ e stri n g th at are u n i q u e w i th i n a n etw o rk (th e
I P address i s u sed). T h e U U I D i s en c o ded as a stri n g o f 3 2 h ex adec i m al di gi ts i n l en gth .
gu id
nativ e
as s igned
s el ect
f o reign
s eq u ence- identity
5.1.4 .2 . H i/ lo a lg orith m
5.1.4 .3 . U U I D a lg orith m
6 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< id name=" id" type=" l o ng" co l u mn=" pers o n_ id" u ns av ed- v al u e=" 0" >
< generato r cl as s =" identity" / >
< / id>
T h e as s igned gen erato r m ak es H i bern ate u se u ns av ed- v al u e=" u ndef ined" . T h i s fo rc es H i bern ate to go to th e
database to determ i n e i f an i n stan c e i s tran si en t o r detac h ed, u n l ess th ere i s a v ersi o n o r ti m estam p
p ro p erty, o r yo u defi n e I ntercepto r. is U ns av ed( ).
S tarti n g w i th rel ease 3 .2.3 , th ere are 2 n ew gen erato rs w h i c h rep resen t a re- th i n k i n g o f 2 di fferen t asp ec ts
o f i den ti fi er gen erati o n . T h e fi rst asp ec t i s database p o rtabi l i ty; th e sec o n d i s o p ti m i z ati o n O p ti m i z ati o n
m ean s th at yo u do n o t h av e to q u ery th e database fo r ev ery req u est fo r a n ew i den ti fi er v al u e. T h ese tw o
n ew gen erato rs are i n ten ded to tak e th e p l ac e o f so m e o f th e n am ed gen erato rs desc ri bed abo v e, starti n g in
3 .3 .x . H o w ev er, th ey are i n c l u ded i n th e c u rren t rel eases an d c an be referen c ed by F Q N .
T h e fi rst o f th ese n ew gen erato rs i s o rg. h ib ernate. id. enh anced. Seq u enceStyl eG enerato r w h i c h i s i n ten ded, fi rstl y,
as a rep l ac em en t fo r th e s eq u ence gen erato r an d, sec o n dl y, as a better p o rtabi l i ty gen erato r th an nativ e. T h i s
6 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
i s bec au se nativ e gen eral l y c h o o ses betw een identity an d s eq u ence w h i c h h av e l argel y di fferen t sem an ti c s th at
c an c au se su btl e i ssu es i n ap p l i c ati o n s eyei n g p o rtabi l i ty. o rg. h ib ernate. id. enh anced. Seq u enceStyl eG enerato r,
h o w ev er, ac h i ev es p o rtabi l i ty i n a di fferen t m an n er. I t c h o o ses betw een a tabl e o r a seq u en c e i n th e
database to sto re i ts i n c rem en ti n g v al u es, dep en di n g o n th e c ap abi l i ti es o f th e di al ec t bei n g u sed. T h e
di fferen c e betw een th i s an d nativ e i s th at tabl e- based an d seq u en c e- based sto rage h av e th e sam e ex ac t
sem an ti c . I n fac t, seq u en c es are ex ac tl y w h at H i bern ate tri es to em u l ate w i th i ts tabl e- based gen erato rs.
T h i s gen erato r h as a n u m ber o f c o n fi gu rati o n p aram eters:
T h e sec o n d o f th ese n ew gen erato rs i s o rg. h ib ernate. id. enh anced. T ab l eG enerato r, w h i c h i s i n ten ded, fi rstl y, as a
rep l ac em en t fo r th e tab l e gen erato r, ev en th o u gh it ac tu al l y fu n c ti o n s m u c h m o re lik e
o rg. h ib ernate. id. M u l tipl eH iL o P erT ab l eG enerato r, an d sec o n dl y, as a re- i m p l em en tati o n o f
o rg. h ib ernate. id. M u l tipl eH iL o P erT ab l eG enerato r th at u ti l i z es th e n o ti o n o f p l u ggabl e o p ti m i z ers. E ssen ti al l y th i s
gen erato r defi n es a tabl e c ap abl e o f h o l di n g a n u m ber o f di fferen t i n c rem en t v al u es si m u l tan eo u sl y by u si n g
m u l ti p l e di sti n c tl y k eyed ro w s. T h i s gen erato r h as a n u m ber o f c o n fi gu rati o n p aram eters:
6 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
5 . 1 . 7 . c ompos i te - i d
< k ey- pro perty name=" pro pertyName" type=" typename" co l u mn=" co l u mn_ name" / >
< k ey- many- to - o ne name=" pro pertyName cl as s =" C l as s Name" co l u mn=" co l u mn_ name" / >
......
< / co mpo s ite- id>
< co mpo s ite- id cl as s =" M edicareI d" mapped=" tru e" >
< k ey- pro perty name=" medicareNu mb er" / >
< k ey- pro perty name=" dependent" / >
< / co mpo s ite- id>
6 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
5 . 1 . 8 . D i s c ri mi nator
T h e < dis criminato r> el em en t i s req u i red fo r p o l ym o rp h i c p ersi sten c e u si n g th e tabl e- p er- c l ass- h i erarc h y
m
ap p i n g strategy. I t dec l ares a di sc ri m i n ato r c o l u m n o f th e tabl e. T h e di sc ri m i n ato r c o l u m n c o n tai n s m ark er
v al u es th at tel l th e p ersi sten c e l ayer w h at su bc l ass to i n stan ti ate fo r a p arti c u l ar ro w . A restri c ted set o f
typ es c an be u sed: s tring, ch aracter, integer, b yte, s h o rt , b o o l ean, yes _ no , tru e_ f al s e .
co l u mn (o p ti o n al - defau l ts to cl as s ): th e n am e o f th e di sc ri m i n ato r c o l u m n .
T h e f o rmu l a attri bu te al l o w s yo u to dec l are an arbi trary S Q L ex p ressi o n th at w i l l be u sed to ev al u ate th e typ e
o f a ro w . F o r ex am p l e:
5 . 1 . 9 . V e rs i on ( opti onal )
T h e < v ers io n> el em en t i s o p ti o n al an d i n di c ates th at th e tabl e c o n tai n s v ersi o n ed data. T h i s i s p arti c u l arl y
u sefu l i f yo u p l an to u se long trans ac tions . S ee bel o w fo r m o re i n fo rm ati o n :
< v ers io n
co l u mn=" v ers io n_ co l u mn"
name=" pro pertyName"
type=" typename"
acces s =" f iel d| pro perty| C l as s Name"
u ns av ed- v al u e=" nu l l | negativ e| u ndef ined"
generated=" nev er| al w ays "
ins ert=" tru e| f al s e"
no de=" el ement- name| @ attrib u te- name| el ement/ @ attrib u te| . "
/ >
6 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
acces s (o p ti o n al - defau l ts to pro perty): th e strategy H i bern ate u ses to ac c ess th e p ro p erty v al u e.
V ersi o n n u m bers c an be o f H i bern ate typ e l o ng , integer, s h o rt , times tamp o r cal endar.
name : th e n am e o f a J av aB ean s styl e p ro p erty o f J av a typ e D ate o r T imes tamp o f th e p ersi sten t c l ass.
acces s (o p ti o n al - defau l ts to pro perty): th e strategy H i bern ate u ses fo r ac c essi n g th e p ro p erty v al u e.
6 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
N ote
< T imes tamp> i s eq u i v al en t to < v ers io n type=" times tamp" >. A n d < times tamp s o u rce=" db " > i s
eq u i v al en t to < v ers io n type=" db times tamp" >
5 . 1 . 1 1 . P rope rty
T h e < pro perty> el em en t dec l ares a p ersi sten t J av aB ean styl e p ro p erty o f th e c l ass.
ty p e nam e c o u l d be:
6 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
integer, s tring, ch aracter, date, times tamp, f l o at, b inary, s erial iz ab l e, o b j ect, b l o b etc .
A p o w erfu l featu re i s deri v ed p ro p erti es. T h ese p ro p erti es are by defi n i ti o n read- o n l y. T h e p ro p erty v al u e i s
c o m p u ted at l o ad ti m e. Y o u dec l are th e c o m p u tati o n as an S Q L ex p ressi o n . T h i s th en tran sl ates to a SE L E C T
c l au se su bq u ery i n th e S Q L q u ery th at l o ads an i n stan c e:
A n o rdi n ary asso c i ati o n to an o th er p ersi sten t c l ass i s dec l ared u si n g a many- to - o ne el em en t. T h e rel ati o n al
m o del i s a m an y- to - o n e asso c i ati o n ; a fo rei gn k ey i n o n e tabl e i s referen c i n g th e p ri m ary k ey c o l u m n (s) o f
th e target tabl e.
< many- to - o ne
name=" pro pertyName"
co l u mn=" co l u mn_ name"
cl as s =" C l as s Name"
cas cade=" cas cade_ s tyl e"
f etch =" j o in| s el ect"
u pdate=" tru e| f al s e"
ins ert=" tru e| f al s e"
pro perty- ref =" pro pertyNameF ro mA s s o ciatedC l as s "
acces s =" f iel d| pro perty| C l as s Name"
u niq u e=" tru e| f al s e"
no t- nu l l =" tru e| f al s e"
o ptimis tic- l o ck =" tru e| f al s e"
l az y=" pro x y| no - pro x y| f al s e"
no t- f o u nd=" igno re| ex ceptio n"
entity- name=" E ntityName"
f o rmu l a=" arb itrary SQ L ex pres s io n"
no de=" el ement- name| @ attrib u te- name| el ement/ @ attrib u te| . "
emb ed- x ml =" tru e| f al s e"
index =" index _ name"
u niq u e_ k ey=" u niq u e_ k ey_ id"
f o reign- k ey=" f o reign_ k ey_ name"
/ >
6 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
name : th e n am e o f th e p ro p erty.
< many- to - o ne name=" pro du ct" cl as s =" P ro du ct" co l u mn=" P R O D U C T _ I D " / >
T h e pro perty- ref attri bu te sh o u l d o n l y be u sed fo r m ap p i n g l egac y data w h ere a fo rei gn k ey refers to a u n i q u e
k ey o f th e asso c i ated tabl e o th er th an th e p ri m ary k ey. T h i s i s a c o m p l i c ated an d c o n fu si n g rel ati o n al m o del .
F o r ex am p l e, i f th e P ro du ct c l ass h ad a u n i q u e seri al n u m ber th at i s n o t th e p ri m ary k ey. T h e u niq u e attri bu te
c o n tro l s H i bern ate' s D D L gen erati o n w i th th e S c h em aE x p o rt to o l .
< pro perty name=" s erial Nu mb er" u niq u e=" tru e" type=" s tring" co l u mn=" SE R I A L _ NU M B E R " / >
< many- to - o ne name=" pro du ct" pro perty- ref =" s erial Nu mb er" co l u mn=" P R O D U C T _ SE R I A L _ NU M B E R " / >
T h i s i s n o t en c o u raged, h o w ev er.
6 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< many- to - o ne name=" o w ner" pro perty- ref =" identity. s s n" co l u mn=" O W NE R _ SSN" / >
5 . 1 . 1 3 . O ne - to- one
< o ne- to - o ne
name=" pro pertyName"
cl as s =" C l as s Name"
cas cade=" cas cade_ s tyl e"
co ns trained=" tru e| f al s e"
f etch =" j o in| s el ect"
pro perty- ref =" pro pertyNameF ro mA s s o ciatedC l as s "
acces s =" f iel d| pro perty| C l as s Name"
f o rmu l a=" any SQ L ex pres s io n"
l az y=" pro x y| no - pro x y| f al s e"
entity- name=" E ntityName"
no de=" el ement- name| @ attrib u te- name| el ement/ @ attrib u te| . "
emb ed- x ml =" tru e| f al s e"
f o reign- k ey=" f o reign_ k ey_ name"
/ >
name : th e n am e o f th e p ro p erty.
P ri m ary k ey asso c i ati o n s do n o t n eed an ex tra tabl e c o l u m n . I f tw o ro w s are rel ated by th e asso c i ati o n , th en
th e tw o tabl e ro w s sh are th e sam e p ri m ary k ey v al u e. T o rel ate tw o o bj ec ts by a p ri m ary k ey asso c i ati o n ,
en su re th at th ey are assi gn ed th e sam e i den ti fi er v al u e.
7 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< o ne- to - o ne name=" empl o yee" cl as s =" E mpl o yee" co ns trained=" tru e" / >
A n ew l y sav ed i n stan c e o f P ers o n i s assi gn ed th e sam e p ri m ary k ey v al u e as th e E mpl o yee i n stan c e referred
w i th th e empl o yee p ro p erty o f th at P ers o n.
A l tern ati v el y, a fo rei gn k ey w i th a u n i q u e c o n strai n t, fro m E mpl o yee to P ers o n, c an be ex p ressed as:
< many- to - o ne name=" pers o n" cl as s =" P ers o n" co l u mn=" P E R SO N_ I D " u niq u e=" tru e" / >
< o ne- to - o ne name=" empl o yee" cl as s =" E mpl o yee" pro perty- ref =" pers o n" / >
5 . 1 . 1 4 . N atu ral - i d
A l th o u gh w e rec o m m en d th e u se o f su rro gate k eys as p ri m ary k eys, yo u sh o u l d try to i den ti fy n atu ral k eys
fo r al l en ti ti es. A n atu ral k ey i s a p ro p erty o r c o m bi n ati o n o f p ro p erti es th at i s u n i q u e an d n o n - n u l l . I t i s al so
i m m u tabl e. M ap th e p ro p erti es o f th e n atu ral k ey i n si de th e < natu ral - id> el em en t. H i bern ate w i l l gen erate
th e n ec essary u n i q u e k ey an d n u l l abi l i ty c o n strai n ts an d, as a resu l t, yo u r m ap p i n g w i l l be m o re
sel f- do c u m en ti n g.
7 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
name : th e n am e o f th e p ro p erty.
u pdate : do th e m ap p ed c o l u m n s ap p ear i n S Q L U P D A T E s ?
acces s (o p ti o n al - defau l ts to pro perty): th e strategy H i bern ate u ses fo r ac c essi n g th e p ro p erty v al u e.
5 . 1 . 1 6 . P rope rti e s
7 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
u pdate : do th e m ap p ed c o l u m n s ap p ear i n S Q L U P D A T E s ?
...
< pro perties name=" name"
u niq u e=" tru e" u pdate=" f al s e" >
< pro perty name=" f irs tName" / >
< pro perty name=" initial " / >
< pro perty name=" l as tName" / >
< / pro perties >
< / cl as s >
5 . 1 . 1 7 . S u b c l as s
P o l ym o rp h i c p ersi sten c e req u i res th e dec l arati o n o f eac h su bc l ass o f th e ro o t p ersi sten t c l ass. F o r th e tabl e-
p er- c l ass- h i erarc h y m ap p i n g strategy, th e < s u b cl as s > dec l arati o n i s u sed. F o r ex am p l e:
< s u b cl as s
name=" C l as s Name"
dis criminato r- v al u e=" dis criminato r_ v al u e"
pro x y=" P ro x yI nterf ace"
l az y=" tru e| f al s e"
dynamic- u pdate=" tru e| f al s e"
dynamic- ins ert=" tru e| f al s e"
entity- name=" E ntityName"
no de=" el ement- name"
ex tends =" Su percl as s Name" >
7 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
E ac h su bc l ass dec l ares i ts o w n p ersi sten t p ro p erti es an d su bc l asses. < v ers io n> an d < id> p ro p erti es are
assu m ed to be i n h eri ted fro m th e ro o t c l ass. E ac h su bc l ass i n a h i erarc h y m u st defi n e a u n i q u e
dis criminato r- v al u e. I f th i s i s n o t sp ec i fi ed, th e fu l l y q u al i fi ed J av a c l ass n am e i s u sed.
5 . 1 . 1 8 . J oi ne d - s u b c l as s
< j o ined- s u b cl as s
name=" C l as s Name"
tab l e=" tab l ename"
pro x y=" P ro x yI nterf ace"
l az y=" tru e| f al s e"
dynamic- u pdate=" tru e| f al s e"
dynamic- ins ert=" tru e| f al s e"
s ch ema=" s ch ema"
catal o g=" catal o g"
ex tends =" Su percl as s Name"
pers is ter=" C l as s Name"
s u b s el ect=" SQ L ex pres s io n"
entity- name=" E ntityName"
no de=" el ement- name" >
< k ey . . . . >
7 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
5 . 1 . 1 9 . U ni on- s u b c l as s
7 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< u nio n- s u b cl as s
name=" C l as s Name"
tab l e=" tab l ename"
pro x y=" P ro x yI nterf ace"
l az y=" tru e| f al s e"
dynamic- u pdate=" tru e| f al s e"
dynamic- ins ert=" tru e| f al s e"
s ch ema=" s ch ema"
catal o g=" catal o g"
ex tends =" Su percl as s Name"
ab s tract=" tru e| f al s e"
pers is ter=" C l as s Name"
s u b s el ect=" SQ L ex pres s io n"
entity- name=" E ntityName"
no de=" el ement- name" >
5 . 1 . 2 0 . J oi n
< j o in
tab l e=" tab l ename"
s ch ema=" o w ner"
catal o g=" catal o g"
f etch =" j o in| s el ect"
inv ers e=" tru e| f al s e"
o ptio nal =" tru e| f al s e" >
< k ey . . . / >
tab l e: th e n am e o f th e j o i n ed tabl e.
7 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
inv ers e (o p ti o n al - defau l ts to f al s e ): i f en abl ed, H i bern ate w i l l n o t i n sert o r u p date th e p ro p erti es
defi n ed by th i s j o i n .
o ptio nal (o p ti o n al - defau l ts to f al s e ): i f en abl ed, H i bern ate w i l l i n sert a ro w o n l y i f th e p ro p erti es
defi n ed by th i s j o i n are n o n - n u l l . I t w i l l al w ays u se an o u ter j o i n to retri ev e th e p ro p erti es.
T h i s featu re i s o ften o n l y u sefu l fo r l egac y data m o del s. W e rec o m m en d few er tabl es th an c l asses an d a
fi n e- grai n ed do m ai n m o del . H o w ev er, i t i s u sefu l fo r sw i tc h i n g betw een i n h eri tan c e m ap p i n g strategi es i n a
si n gl e h i erarc h y, as ex p l ai n ed l ater.
5 .1 .2 1 . K e y
< k ey
co l u mn=" co l u mnname"
o n- del ete=" no actio n| cas cade"
pro perty- ref =" pro pertyName"
no t- nu l l =" tru e| f al s e"
u pdate=" tru e| f al s e"
u niq u e=" tru e| f al s e"
/ >
7 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< co l u mn
name=" co l u mn_ name"
l ength =" N"
precis io n=" N"
s cal e=" N"
no t- nu l l =" tru e| f al s e"
u niq u e=" tru e| f al s e"
u niq u e- k ey=" mu l tico l u mn_ u niq u e_ k ey_ name"
index =" index _ name"
s q l - type=" s q l _ type_ name"
ch eck =" SQ L ex pres s io n"
def au l t=" SQ L ex pres s io n" / >
5 . 1 . 2 3 . Import
< impo rt cl as s =" j av a. l ang. O b j ect" rename=" U niv ers e" / >
< impo rt
cl as s =" C l as s Name"
rename=" Sh o rtName"
/ >
cl as s : th e fu l l y q u al i fi ed c l ass n am e o f an y J av a c l ass.
5 . 1 . 2 4 . A ny
7 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< any name=" b eing" id- type=" l o ng" meta- type=" s tring" >
< meta- v al u e v al u e=" T B L _ A NI M A L " cl as s =" A nimal " / >
< meta- v al u e v al u e=" T B L _ H U M A N" cl as s =" H u man" / >
< meta- v al u e v al u e=" T B L _ A L I E N" cl as s =" A l ien" / >
< co l u mn name=" tab l e_ name" / >
< co l u mn name=" id" / >
< / any>
< any
name=" pro pertyName"
id- type=" idtypename"
meta- type=" metatypename"
cas cade=" cas cade_ s tyl e"
acces s =" f iel d| pro perty| C l as s Name"
o ptimis tic- l o ck =" tru e| f al s e"
>
< meta- v al u e . . . / >
< meta- v al u e . . . / >
.....
< co l u mn . . . . / >
< co l u mn . . . . / >
.....
< / any>
name : th e p ro p erty n am e.
acces s (o p ti o n al - defau l ts to pro perty): th e strategy H i bern ate u ses fo r ac c essi n g th e p ro p erty v al u e.
5 . 2 . Hibernate ty p es
5 . 2 . 1 . E nti ti e s and v al u e s
A n en ti ty' s p ersi sten t state c o n si sts o f referen c es to o th er en ti ti es an d i n stan c es o f v alue typ es. V al u es are
p ri m i ti v es: c o l l ec ti o n s (n o t w h at i s i n si de a c o l l ec ti o n ), c o m p o n en ts an d c ertai n i m m u tabl e o bj ec ts. U n l i k e
en ti ti es, v al u es i n p arti c u l ar c o l l ec ti o n s an d c o m p o n en ts, are p ersi sted an d del eted by reac h abi l i ty. S i n c e
v al u e o bj ec ts an d p ri m i ti v es are p ersi sted an d del eted al o n g w i th th ei r c o n tai n i n g en ti ty, th ey c an n o t be
i n dep en den tl y v ersi o n ed. V al u es h av e n o i n dep en den t i den ti ty, so th ey c an n o t be sh ared by tw o en ti ti es o r
c o l l ec ti o n s.
7 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
5 . 2 . 2 . B as i c v al u e ty pe s
T h e bu i l t- i n b as ic m ap p ing ty p e s c an be ro u gh l y c atego ri z ed i n to th e fo l l o w i n g:
s tring
cl as s
b inary
tex t
M ap s l o n g J av a stri n gs to a S Q L C L O B o r T E X T typ e.
s erial iz ab l e
M ap s seri al i z abl e J av a typ es to an ap p ro p ri ate S Q L bi n ary typ e. Y o u c an al so i n di c ate th e H i bern ate typ e
s erial iz ab l e w i th
th e n am e o f a seri al i z abl e J av a c l ass o r i n terfac e th at do es n o t defau l t to a basi c typ e.
cl o b , b l o b
8 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
imm_ date, imm_ time, imm_ times tamp, imm_ cal endar, imm_ cal endar_ date, imm_ s erial iz ab l e, imm_ b inary
5 . 2 . 3 . C u s tom v al u e ty pe s
T o i m p l em en t a c u sto m typ e, i m p l em en t ei th er o rg. h ib ernate. U s erT ype o r o rg. h ib ernate. C o mpo s iteU s erT ype an d
dec l are p ro p erti es u si n g th e fu l l y q u al i fi ed c l assn am e o f th e typ e. V i ew o rg. h ib ernate. tes t. D o u b l eStringT ype to
see th e k i n d o f th i n gs th at are p o ssi bl e.
< pro perty name=" tw o Strings " type=" o rg. h ib ernate. tes t. D o u b l eStringT ype" >
< co l u mn name=" f irs t_ s tring" / >
< co l u mn name=" s eco nd_ s tring" / >
< / pro perty>
T h e C o mpo s iteU s erT ype , E nh ancedU s erT ype , U s erC o l l ectio nT ype , an d U s erV ers io nT ype i n terfac es p ro v i de su p p o rt
fo r m o re sp ec i al i z ed u ses.
< typedef cl as s =" co m. myco mpany. u s ertypes . D ef au l tV al u eI ntegerT ype" name=" def au l t_ z ero " >
< param name=" def au l t" >0< / param>
< / typedef >
< pro perty name=" prio rity" type=" def au l t_ z ero " / >
8 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
I t i s al so p o ssi bl e to o v erri de th e p aram eters su p p l i ed i n a typ edef o n a c ase- by- c ase basi s by u si n g typ e
p aram eters o n th e p ro p erty m ap p i n g.
5 . 4 . S Q L q uoted id entifiers
Y o u c an fo rc e H i bern ate to q u o te an i den ti fi er i n th e gen erated S Q L by en c l o si n g th e tabl e o r c o l u m n n am e i n
bac k ti c k s i n th e m ap p i n g do c u m en t. H i bern ate w i l l u se th e c o rrec t q u o tati o n styl e fo r th e S Q L D ial ect . T h i s i s
u su al l y do u bl e q u o tes, bu t th e S Q L S erv er u ses brac k ets an d M yS Q L u ses bac k ti c k s.
< cl as s name=" L ineI tem" tab l e=" ` L ine I tem` " >
< id name=" id" co l u mn=" ` I tem I d` " / >< generato r cl as s =" as s igned" / >< / id>
< pro perty name=" itemNu mb er" co l u mn=" ` I tem # ` " / >
...
< / cl as s >
5 . 5 . 1 . U s i ng X D oc l e t mark u p
8 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
8 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
/ **
* @ h ib ernate. cl as s
* tab l e=" C A T S"
*/
pu b l ic cl as s C at {
priv ate L o ng id; / / identif ier
priv ate D ate b irth date;
priv ate C at mo th er;
priv ate Set k ittens
priv ate C o l o r co l o r;
priv ate ch ar s ex ;
priv ate f l o at w eigh t;
/ *
*@ h ib ernate. id
* generato r- cl as s =" nativ e"
* co l u mn=" C A T _ I D "
*/
pu b l ic L o ng getI d( ) {
retu rn id;
}
priv ate v o id s etI d( L o ng id) {
th is . id=id;
}
/ **
*@ h ib ernate. many- to - o ne
* co l u mn=" P A R E NT _ I D "
*/
pu b l ic C at getM o th er( ) {
retu rn mo th er;
}
v o id s etM o th er( C at mo th er) {
th is . mo th er = mo th er;
}
/ **
*@ h ib ernate. pro perty
* co l u mn=" B I R T H _ D A T E "
*/
pu b l ic D ate getB irth date( ) {
retu rn b irth date;
}
v o id s etB irth date( D ate date) {
b irth date = date;
}
/ **
*@ h ib ernate. pro perty
* co l u mn=" W E I G H T "
*/
pu b l ic f l o at getW eigh t( ) {
retu rn w eigh t;
}
v o id s etW eigh t( f l o at w eigh t) {
th is . w eigh t = w eigh t;
}
8 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
5 . 5 . 2 . U s i ng J D K 5 .0 A nnotati ons
@ I d;
L o ng id;
@ T rans ient
I nteger age;
@ E mb edded
priv ate A ddres s h o meA ddres s ;
N ote
S u p p o rt fo r J D K 5 .0 A n n o tati o n s (an d J S R - 220 ) i s c u rren tl y u n der dev el o p m en t. P l ease refer to
th e H i bern ate A n n o tati o n s m o du l e fo r m o re detai l s.
ins ert : th e gi v en p ro p erty v al u e i s gen erated o n i n sert, bu t i s n o t regen erated o n su bseq u en t u p dates.
P ro p erti es l i k e c reated- date fal l i n to th i s c atego ry. E v en th o u gh v ersi o n an d ti m estam p p ro p erti es c an be
m ark ed as gen erated, th i s o p ti o n i s n o t av ai l abl e.
8 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h e fi rst m o de i s to ex p l i c i tl y l i st th e CR E A T E an d D R O P c o m m an ds i n th e m ap p i n g fi l e:
8 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
6 .4 . Co l l ec ti o n ex am p l es
pu b l ic cl as s P ro du ct {
priv ate String s erial Nu mb er;
priv ate Set parts = new H as h Set( );
T h e p ersi sten t c o l l ec ti o n s i n j ec ted by H i bern ate beh av e l i k e H as h M ap, H as h Set , T reeM ap, T reeSet o r A rrayL is t ,
dep en di n g o n th e i n terfac e typ e.
T ip
T h ere are q u i te a ran ge o f m ap p i n gs th at c an be gen erated fo r c o l l ec ti o n s th at c o v er m an y
c o m m o n rel ati o n al m o del s. W e su ggest yo u ex p eri m en t w i th th e sc h em a gen erati o n to o l so
th at yo u u n derstan d h o w v ari o u s m ap p i n g dec l arati o n s tran sl ate to database tabl es.
8 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
A p art fro m < s et>, th ere i s al so < l is t>, < map>, < b ag>, < array> an d < primitiv e- array> m ap p i n g el em en ts. T h e
< map> el em en t i s rep resen tati v e:
< map
name=" pro pertyName"
tab l e=" tab l e_ name"
s ch ema=" s ch ema_ name"
l az y=" tru e| ex tra| f al s e"
inv ers e=" tru e| f al s e"
cas cade=" al l | no ne| s av e- u pdate| del ete| al l - del ete- o rph an| del ete- o rph an"
s o rt=" u ns o rted| natu ral | co mparato rC l as s "
o rder- b y=" co l u mn_ name as c| des c"
w h ere=" arb itrary s q l w h ere co nditio n"
f etch =" j o in| s el ect| s u b s el ect"
b atch - s iz e=" N"
acces s =" f iel d| pro perty| C l as s Name"
o ptimis tic- l o ck =" tru e| f al s e"
mu tab l e=" tru e| f al s e"
no de=" el ement- name| . "
emb ed- x ml =" tru e| f al s e"
>
< k ey . . . . / >
< map- k ey . . . . / >
< el ement . . . . / >
< / map>
name : th e c o l l ec ti o n p ro p erty n am e
8 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
6 . 2 . 1 . C ol l e c ti on f ore i g n k e y s
T h e fo rei gn k ey c o n strai n t c an u se O N D E L E T E C A SC A D E .
< k ey co l u mn=" pro du ctSerial Nu mb er" o n- del ete=" cas cade" / >
6 . 2 . 2 . C ol l e c ti on e l e me nts
Co l l ec ti o n s c an c o n tai n al m o st an y o th er H i bern ate typ e, i n c l u di n g: basi c typ es, c u sto m typ es, c o m p o n en ts
an d referen c es to o th er en ti ti es. T h i s i s an i m p o rtan t di sti n c ti o n . A n o bj ec t i n a c o l l ec ti o n m i gh t be h an dl ed
w i th " v al u e" sem an ti c s (i ts l i fe c yc l e fu l l y dep en ds o n th e c o l l ec ti o n o w n er), o r i t m i gh t be a referen c e to
an o th er en ti ty w i th i ts o w n l i fe c yc l e. I n th e l atter c ase, o n l y th e " l i n k " betw een th e tw o o bj ec ts i s c o n si dered
to be a state h el d by th e c o l l ec ti o n .
6 . 2 . 3 . Ind e x e d c ol l e c ti ons
< l is t- index
co l u mn=" co l u mn_ name"
b as e=" 0| 1| . . . " / >
8 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< map- k ey
co l u mn=" co l u mn_ name"
f o rmu l a=" any SQ L ex pres s io n"
type=" type_ name"
no de=" @ attrib u te- name"
l ength =" N" / >
co l u mn (o p ti o n al ): th e n am e o f th e c o l u m n h o l di n g th e c o l l ec ti o n i n dex v al u es.
A n y c o l l ec ti o n o f v al u es o r m an y- to - m an y asso c i ati o n s req u i res a dedi c ated c olle c tion tab le w i th a fo rei gn
k ey c o l u m n o r c o l u m n s, c olle c tion e le m e nt c olum n o r c o l u m n s, an d p o ssi bl y an i n dex c o l u m n o r c o l u m n s.
< el ement
co l u mn=" co l u mn_ name"
f o rmu l a=" any SQ L ex pres s io n"
type=" typename"
l ength =" L "
precis io n=" P "
s cal e=" S"
no t- nu l l =" tru e| f al s e"
u niq u e=" tru e| f al s e"
no de=" el ement- name"
/ >
co l u mn (o p ti o n al ): th e n am e o f th e c o l u m n h o l di n g th e c o l l ec ti o n el em en t v al u es.
9 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
co l u mn (o p ti o n al ): th e n am e o f th e el em en t fo rei gn k ey c o l u m n .
f etch (o p ti o n al - defau l ts to j o in): en abl es o u ter- j o i n o r seq u en ti al sel ec t fetc h i n g fo r th i s asso c i ati o n .
T h i s i s a sp ec i al c ase; fo r fu l l eager fetc h i n g i n a si n gl e SE L E C T o f an en ti ty an d i ts m an y- to - m an y
rel ati o n sh i p s to o th er en ti ti es, yo u w o u l d en abl e j o in fetc h i n g,n o t o n l y o f th e c o l l ec ti o n i tsel f, bu t al so
w i th th i s attri bu te o n th e < many- to - many> n ested el em en t.
u niq u e (o p ti o n al ): en abl es th e D D L gen erati o n o f a u n i q u e c o n strai n t fo r th e fo rei gn - k ey c o l u m n . T h i s
m ak es th e asso c i ati o n m u l ti p l i c i ty effec ti v el y o n e- to - m an y.
no t- f o u nd (o p ti o n al - defau l ts to ex ceptio n): sp ec i fi es h o w fo rei gn k eys th at referen c e m i ssi n g ro w s w i l l
be h an dl ed: igno re w i l l treat a m i ssi n g ro w as a n u l l asso c i ati o n .
entity- name (o p ti o n al ): th e en ti ty n am e o f th e asso c i ated c l ass, as an al tern ati v e to cl as s .
< s et name=" names " tab l e=" pers o n_ names " >
< k ey co l u mn=" pers o n_ id" / >
< el ement co l u mn=" pers o n_ name" type=" s tring" / >
< / s et>
9 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
A n asso c i ati o n fro m P ro du ct to P art req u i res th e ex i sten c e o f a fo rei gn k ey c o l u m n an d p o ssi bl y an i n dex
c o l u m n to th e P art tabl e. A < o ne- to - many> tag i n di c ates th at th i s i s a o n e- to - m an y asso c i ati o n .
W arni ng
I f th e fo rei gn k ey c o l u m n o f a < o ne- to - many> asso c i ati o n i s dec l ared NO T NU L L , yo u m u st
dec l are th e < k ey> m ap p i n g no t- nu l l =" tru e" o r us e a b id ire c tional as s oc iation w i th th e c o l l ec ti o n
m ap p i n g m ark ed inv ers e=" tru e" . S ee th e di sc u ssi o n o f bi di rec ti o n al asso c i ati o n s l ater i n th i s
c h ap ter fo r m o re i n fo rm ati o n .
9 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
N o ti c e th e u se o f a fo rm u l a- based i n dex :
6 . 3 . 1 . S orte d c ol l e c ti ons
< map name=" h o l idays " s o rt=" my. cu s to m. H o l idayC o mparato r" >
< k ey co l u mn=" year_ id" / >
< map- k ey co l u mn=" h o l _ name" type=" s tring" / >
< el ement co l u mn=" h o l _ date" type=" date" / >
< / map>
< s et name=" al ias es " tab l e=" pers o n_ al ias es " o rder- b y=" l o w er( name) as c" >
< k ey co l u mn=" pers o n" / >
< el ement co l u mn=" name" type=" s tring" / >
< / s et>
< map name=" h o l idays " o rder- b y=" h o l _ date, h o l _ name" >
< k ey co l u mn=" year_ id" / >
< map- k ey co l u mn=" h o l _ name" type=" s tring" / >
< el ement co l u mn=" h o l _ date type=" date" / >
< / map>
N ote
T h e v al u e o f th e o rder- b y attri bu te i s an S Q L o rderi n g, n o t an H Q L o rderi n g.
s o rtedU s ers = s . createF il ter( gro u p. getU s ers ( ), " o rder b y th is . name" ). l is t( );
9 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
A b id ire c tional as s oc iation al l o w s n av i gati o n fro m bo th " en ds" o f th e asso c i ati o n . T w o k i n ds o f bi di rec ti o n al
asso c i ati o n are su p p o rted:
o n e- to - m an y
set o r bag v al u ed at o n e en d an d si n gl e- v al u ed at th e o th er
m an y- to - m an y
set o r bag v al u ed at bo th en ds
Ch an ges m ade o n l y to th e i n v erse en d o f th e asso c i ati o n are not p ersi sted. T h i s m ean s th at H i bern ate h as
tw o rep resen tati o n s i n m em o ry fo r ev ery bi di rec ti o n al asso c i ati o n : o n e l i n k fro m A to B an d an o th er l i n k
fro m B to A . T h i s i s easi er to u n derstan d i f yo u th i n k abo u t th e J av a o bj ec t m o del an d h o w a m an y- to - m an y
rel ati o n sh i p i n J av ai s c reated:
catego ry. getI tems ( ). add( item); / / T h e catego ry no w " k no w s " ab o u t th e rel atio ns h ip
item. getC atego ries ( ). add( catego ry); / / T h e item no w " k no w s " ab o u t th e rel atio ns h ip
9 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
M ap p i n g o n e en d o f an asso c i ati o n w i th inv ers e=" tru e" do es n o t affec t th e o p erati o n o f c asc ades as th ese are
o rth o go n al c o n c ep ts.
A bi di rec ti o n al asso c i ati o n w h ere o n e en d i s rep resen ted as a < l is t> o r < map>, req u i res sp ec i al c o n si derati o n .
I f th ere i s a p ro p erty o f th e c h i l d c l ass th at m ap s to th e i n dex c o lu m n yo u c an u se inv ers e=" tru e" o n th e
c o l l ec ti o n m ap p i n g:
9 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
9 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
A n < idb ag> h as a syn th eti c i d gen erato r, j u st l i k e an en ti ty c l ass. A di fferen t su rro gate k ey i s assi gn ed to
eac h c o l l ec ti o n ro w . H i bern ate do es n o t, h o w ev er, p ro v i de an y m ec h an i sm fo r di sc o v eri n g th e su rro gate k ey
v al u e o f a p arti c u l ar ro w .
T h e u p date p erfo rm an c e o f an < idb ag> su p ersedes a regu l ar < b ag>. H i bern ate c an l o c ate i n di v i du al ro w s
effi c i en tl y an d u p date o r del ete th em i n di v i du al l y, si m i l ar to a l i st, m ap o r set.
I n th e c u rren t i m p l em en tati o n , th e nativ e i den ti fi er gen erati o n strategy i s n o t su p p o rted fo r < idb ag>
c o l l ec ti o n i den ti fi ers.
6 . 4 . C ol l ection ex amp l es
T h i s sec ti o n c o v ers c o l l ec ti o n ex am p l es.
pu b l ic cl as s P arent {
priv ate l o ng id;
priv ate Set ch il dren;
....
....
}
9 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h i s m ap s to th e fo l l o w i n g tabl e defi n i ti o n s:
N o ti c e th e NO T NU L L c o n strai n t:
9 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
A l tern ati v el y, i f th i s asso c i ati o n m u st be u n i di rec ti o n al yo u c an dec l are th e NO T NU L L c o n strai n t o n th e < k ey>
m ap p i n g:
T abl e defi n i ti o n s:
9 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
7 . 1 . I ntrod uction
A sso c i ati o n m ap p i n gs are o ften th e m o st di ffi c u l t th i n g to i m p l em en t c o rrec tl y. I n th i s sec ti o n w e ex am i n e
so m e c an o n i c al c ases o n e by o n e, starti n g w i th u n i di rec ti o n al m ap p i n gs an d th en bi di rec ti o n al c ases. W e w i l l
u se P ers o n an d A ddres s i n al l th e ex am p l es.
A unid ire c tional m any - to- one as s oc iation i s th e m o st c o m m o n k i n d o f u n i di rec ti o n al asso c i ati o n .
1 0 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
7 . 2 . 2 . O ne - to- one
A unid ire c tional one - to- one as s oc iation on a f ore ig n k e y i s al m o st i den ti c al . T h e o n l y di fferen c e i s th e c o l u m n
u n i q u e c o n strai n t.
create tab l e P ers o n ( pers o nI d b igint no t nu l l primary k ey, addres s I d b igint no t nu l l u niq u e )
create tab l e A ddres s ( addres s I d b igint no t nu l l primary k ey )
A unid ire c tional one - to- one as s oc iation on a p rim ary k e y u su al l y u ses a sp ec i al i d gen erato r I n th i s ex am p l e,
h o w ev er, w e h av e rev ersed th e di rec ti o n o f th e asso c i ati o n :
7 . 2 . 3 . O ne - to- many
A unid ire c tional one - to- m any as s oc iation on a f ore ig n k e y i s an u n u su al c ase, an d i s n o t rec o m m en ded.
1 0 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
7 . 3 . 1 . O ne - to- many
A unid ire c tional one - to- m any as s oc iation on a j oin tab le i s th e p referred o p ti o n . S p ec i fyi n g u niq u e=" tru e" ,
c h an ges th e m u l ti p l i c i ty fro m m an y- to - m an y to o n e- to - m an y.
A unid ire c tional m any - to- one as s oc iation on a j oin tab le is c o m m o n w h en th e asso c i ati o n i s o p ti o n al . F o r
ex am p l e:
1 0 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
7 . 3 . 3 . O ne - to- one
A unid ire c tional one - to- one as s oc iation on a j oin tab le i s p o ssi bl e, bu t ex trem el y u n u su al .
1 0 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
7 . 4 . B id irectional as s ociations
1 0 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
7 . 4 . 2 . O ne - to- one
create tab l e P ers o n ( pers o nI d b igint no t nu l l primary k ey, addres s I d b igint no t nu l l u niq u e )
create tab l e A ddres s ( addres s I d b igint no t nu l l primary k ey )
A b id ire c tional one - to- one as s oc iation on a p rim ary k e y u ses th e sp ec i al i d gen erato r:
1 0 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h e fo l l o w i n g i s an ex am p l e o f a b id ire c tional one - to- m any as s oc iation on a j oin tab le . T h e inv ers e=" tru e" c an
go o n ei th er en d o f th e asso c i ati o n , o n th e c o l l ec ti o n , o r o n th e j o i n .
1 0 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
7 . 5 . 2 . one to one
A b id ire c tional one - to- one as s oc iation on a j oin tab le i s p o ssi bl e, bu t ex trem el y u n u su al .
1 0 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
I n a m o re c o m p l ex ex am p l e, i m agi n e th at th e asso c i ati o n betw een E mpl o yee an d O rganiz atio n i s m ai n tai n ed i n
an E mpl o yment tabl e fu l l o f h i sto ri c al em p l o ym en t data. A n asso c i ati o n to th e em p l o yee' s m os t re c e nt
em p l o yer, th e o n e w i th th e m o st rec en t s tartD ate, c o u l d be m ap p ed i n th e fo l l o w i n g w ay:
1 0 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< j o in>
< k ey co l u mn=" empl o yeeI d" / >
< s u b s el ect>
s el ect empl o yeeI d, o rgI d
f ro m E mpl o yments
gro u p b y o rgI d
h av ing s tartD ate = max ( s tartD ate)
< / s u b s el ect>
< many- to - o ne name=" mo s tR ecentE mpl o yer"
cl as s =" O rganiz atio n"
co l u mn=" o rgI d" / >
< / j o in>
T h e n o ti o n o f a c om p one nt i s re- u sed i n sev eral di fferen t c o n tex ts an d p u rp o ses th ro u gh o u t H i bern ate.
1 0 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
pu b l ic cl as s P ers o n {
priv ate j av a. u til . D ate b irth day;
priv ate Name name;
priv ate String k ey;
pu b l ic String getK ey( ) {
retu rn k ey;
}
priv ate v o id s etK ey( String k ey) {
th is . k ey=k ey;
}
pu b l ic j av a. u til . D ate getB irth day( ) {
retu rn b irth day;
}
pu b l ic v o id s etB irth day( j av a. u til . D ate b irth day) {
th is . b irth day = b irth day;
}
pu b l ic Name getName( ) {
retu rn name;
}
pu b l ic v o id s etName( Name name) {
th is . name = name;
}
......
......
}
pu b l ic cl as s Name {
ch ar initial ;
String f irs t;
String l as t;
pu b l ic String getF irs t( ) {
retu rn f irs t;
}
v o id s etF irs t( String f irs t) {
th is . f irs t = f irs t;
}
pu b l ic String getL as t( ) {
retu rn l as t;
}
v o id s etL as t( String l as t) {
th is . l as t = l as t;
}
pu b l ic ch ar getI nitial ( ) {
retu rn initial ;
}
v o id s etI nitial ( ch ar initial ) {
th is . initial = initial ;
}
}
O u r H i bern ate m ap p i n g w o u l d l o o k l i k e th i s:
1 1 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< cl as s name=" eg. P ers o n" tab l e=" pers o n" >
< id name=" K ey" co l u mn=" pid" type=" s tring" >
< generato r cl as s =" u u id" / >
< / id>
< pro perty name=" b irth day" type=" date" / >
< co mpo nent name=" Name" cl as s =" eg. Name" > < ! - - cl as s attrib u te o ptio nal - - >
< pro perty name=" initial " / >
< pro perty name=" f irs t" / >
< pro perty name=" l as t" / >
< / co mpo nent>
< / cl as s >
< cl as s name=" eg. P ers o n" tab l e=" pers o n" >
< id name=" K ey" co l u mn=" pid" type=" s tring" >
< generato r cl as s =" u u id" / >
< / id>
< pro perty name=" b irth day" type=" date" / >
< co mpo nent name=" Name" cl as s =" eg. Name" u niq u e=" tru e" >
< parent name=" namedP ers o n" / > < ! - - ref erence b ack to th e P ers o n - - >
< pro perty name=" initial " / >
< pro perty name=" f irs t" / >
< pro perty name=" l as t" / >
< / co mpo nent>
< / cl as s >
< s et name=" s o meNames " tab l e=" s o me_ names " l az y=" tru e" >
< k ey co l u mn=" id" / >
< co mpo s ite- el ement cl as s =" eg. Name" > < ! - - cl as s attrib u te req u ired - - >
< pro perty name=" initial " / >
< pro perty name=" f irs t" / >
< pro perty name=" l as t" / >
< / co mpo s ite- el ement>
< / s et>
Important
I f yo u defi n e a Set o f c o m p o si te el em en ts, i t i s i m p o rtan t to i m p l em en t eq u al s ( ) an d h as h C o de( )
c o rrec tl y.
1 1 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Co m p o si te el em en ts c an c o n tai n c o m p o n en ts bu t n o t c o l l ec ti o n s. I f yo u r c o m p o si te el em en t c o n tai n s
c o m p o n en ts, u se th e < nes ted- co mpo s ite- el ement> tag. T h i s c ase i s a c o l l ec ti o n o f c o m p o n en ts w h i c h
th em sel v es h av e c o m p o n en ts. Y o u m ay w an t to c o n si der i f a o n e- to - m an y asso c i ati o n i s m o re ap p ro p ri ate.
R em o del th e c o m p o si te el em en t as an en ti ty, bu t be aw are th at ev en th o u gh th e J av a m o del i s th e sam e,
th e rel ati o n al m o del an d p ersi sten c e sem an ti c s are sti l l sl i gh tl y di fferen t.
I t m u st i m p l em en t j av a. io . Serial iz ab l e .
1 1 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
N ote
I n H i bern ate3 , al th o u gh th e sec o n d req u i rem en t i s n o t an abso l u tel y h ard req u i rem en t o f
H i bern ate, i t i s rec o m m en ded.
U se th e < co mpo s ite- id> tag, w i th n ested < k ey- pro perty> el em en ts, i n p l ac e o f th e u su al < id> dec l arati o n . F o r
ex am p l e, th e O rderL ine c l ass h as a p ri m ary k ey th at dep en ds u p o n th e (c o m p o si te) p ri m ary k ey o f O rder.
< co mpo s ite- id name=" id" cl as s =" O rderL ineI d" >
< k ey- pro perty name=" l ineI d" / >
< k ey- pro perty name=" o rderI d" / >
< k ey- pro perty name=" cu s to merI d" / >
< / co mpo s ite- id>
< / cl as s >
T ip
T h e co l u mn el em en t i s an al tern ati v e to th e co l u mn attri bu te ev eryw h ere. U si n g th e co l u mn
el em en t j u st gi v es m o re dec l arati o n o p ti o n s, w h i c h are m o stl y u sefu l w h en u ti l i z i n g h b m2 ddl
1 1 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< s et name=" o rderL ines " inv ers e=" tru e" >
< k ey>
< co l u mn name=" o rderI d" / >
< co l u mn name=" cu s to merI d" / >
< / k ey>
< o ne- to - many cl as s =" O rderL ine" / >
< / s et>
< dynamic- co mpo nent name=" u s erA ttrib u tes " >
< pro perty name=" f o o " co l u mn=" F O O " type=" s tring" / >
< pro perty name=" b ar" co l u mn=" B A R " type=" integer" / >
< many- to - o ne name=" b az " cl as s =" B az " co l u mn=" B A Z _ I D " / >
< / dynamic- co mpo nent>
T h e sem an ti c s o f a < dynamic- co mpo nent> m ap p i n g are i den ti c al to < co mpo nent>. T h e adv an tage o f th i s k i n d o f
m ap p i n g i s th e abi l i ty to determ i n e th e ac tu al p ro p erti es o f th e bean at dep l o ym en t ti m e j u st by edi ti n g th e
m ap p i n g do c u m en t. R u n ti m e m an i p u l ati o n o f th e m ap p i n g do c u m en t i s al so p o ssi bl e, u si n g a D O M p arser.
Y o u c an al so ac c ess, an d c h an ge, H i bern ate' s c o n fi gu rati o n - ti m e m etam o del v i a th e C o nf igu ratio n o bj ec t.
1 1 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
9 .1 . T h e th ree strategi es
9 .1 .1 . T abl e p er c l ass h i erarc h y
9 .1 .2. T abl e p er su bc l ass
9 .1 .3 . T abl e p er su bc l ass: u si n g a di sc ri m i n ato r
9 .1 .4 . M i x i n g tabl e p er c l ass h i erarc h y w i th tabl e p er su bc l ass
9 .1 .5 . T abl e p er c o n c rete c l ass
9 .1 .6 . T abl e p er c o n c rete c l ass u si n g i m p l i c i t p o l ym o rp h i sm
9 .1 .7 . M i x i n g i m p l i c i t p o l ym o rp h i sm w i th o th er i n h eri tan c e m ap p i n gs
9 .2. L i m i tati o n s
i m p l i c i t p o l ym o rp h i sm
9 . 1 . 1 . T ab l e pe r c l as s h i e rarc h y
1 1 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
9 . 1 . 2 . T ab l e pe r s u b c l as s
A tabl e p er su bc l ass m ap p i n g l o o k s l i k e th i s:
F o u r tabl es are req u i red. T h e th ree su bc l ass tabl es h av e p ri m ary k ey asso c i ati o n s to th e su p erc l ass tabl e so
th e rel ati o n al m o del i s ac tu al l y a o n e- to - o n e asso c i ati o n .
9 . 1 . 3 . T ab l e pe r s u b c l as s : u s i ng a d i s c ri mi nator
1 1 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h e o p ti o n al f etch =" s el ect" dec l arati o n tel l s H i bern ate n o t to fetc h th e C h eq u eP ayment su bc l ass data u si n g an
o u ter j o i n w h en q u eryi n g th e su p erc l ass.
1 1 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
9 . 1 . 5 . T ab l e pe r c onc re te c l as s
T h ere are tw o w ays w e c an m ap th e tabl e p er c o n c rete c l ass strategy. F i rst, yo u c an u se < u nio n- s u b cl as s >.
I f yo u r su p erc l ass i s abstrac t, m ap i t w i th ab s tract=" tru e" . I f i t i s n o t abstrac t, an addi ti o n al tabl e (i t defau l ts
to P A Y M E NT i n th e ex am p l e abo v e), i s n eeded to h o l d i n stan c es o f th e su p erc l ass.
A n al tern ati v e ap p ro ac h i s to m ak e u se o f i m p l i c i t p o l ym o rp h i sm :
1 1 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< any name=" payment" meta- type=" s tring" id- type=" l o ng" >
< meta- v al u e v al u e=" C R E D I T " cl as s =" C reditC ardP ayment" / >
< meta- v al u e v al u e=" C A SH " cl as s =" C as h P ayment" / >
< meta- v al u e v al u e=" C H E Q U E " cl as s =" C h eq u eP ayment" / >
< co l u mn name=" P A Y M E NT _ C L A SS" / >
< co l u mn name=" P A Y M E NT _ I D " / >
< / any>
1 1 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< cl as s name=" No nel ectro nicT rans actio n" tab l e=" NO NE L E C T R O NI C _ T X N" >
< id name=" id" type=" l o ng" co l u mn=" T X N_ I D " >
< generato r cl as s =" nativ e" / >
< / id>
...
< j o ined- s u b cl as s name=" C as h P ayment" tab l e=" C A SH _ P A Y M E NT " >
< k ey co l u mn=" P A Y M E NT _ I D " / >
< pro perty name=" amo u nt" co l u mn=" C A SH _ A M O U NT " / >
...
< / j o ined- s u b cl as s >
< j o ined- s u b cl as s name=" C h eq u eP ayment" tab l e=" C H E Q U E _ P A Y M E NT " >
< k ey co l u mn=" P A Y M E NT _ I D " / >
< pro perty name=" amo u nt" co l u mn=" C H E Q U E _ A M O U NT " / >
...
< / j o ined- s u b cl as s >
< / cl as s >
9 . 2 . L imitations
T h ere are l i m i tati o n s to th e " i m p l i c i t p o l ym o rp h i sm " ap p ro ac h to th e tabl e p er c o n c rete- c l ass m ap p i n g
strategy. T h ere are so m ew h at l ess restri c ti v e l i m i tati o n s to < u nio n- s u b cl as s > m ap p i n gs.
tabl e p er
< many- to - o ne> < o ne- to - o ne> < o ne- to - many> < many- to - many> s . get( P ayment. cl as s , id)
c l ass- h i erarc h y
tabl e p er
< many- to - o ne> < o ne- to - o ne> < o ne- to - many> < many- to - many> s . get( P ayment. cl as s , id)
su bc l ass
tabl e p er < o ne- to - many>
c o n c rete- c l ass (fo r
< many- to - o ne> < o ne- to - o ne> < many- to - many> s . get( P ayment. cl as s , id)
(u n i o n - inv ers e=" tru e"
su bc l ass) o n l y)
tabl e p er
c o n c rete c l ass
< any> not s up p orte d not s up p orte d < many- to - any> s . createC riteria( P ayment. cl as s ). add( R es trictio ns . idE q
(i m p l i c i t
1 2 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
p o l ym o rp h i sm )
I n o th er w o rds, H i bern ate ap p l i c ati o n dev el o p ers sh o u l d al w ays th i n k abo u t th e s tate o f th ei r o bj ec ts, an d
n o t n ec essari l y abo u t th e ex ec u ti o n o f S Q L statem en ts. T h i s p art i s tak en c are o f by H i bern ate an d i s o n l y
rel ev an t fo r th e ap p l i c ati o n dev el o p er w h en tu n i n g th e p erfo rm an c e o f th e system .
W e w i l l n o w di sc u ss th e states an d state tran si ti o n s (an d th e H i bern ate m eth o ds th at tri gger a tran si ti o n ) i n
m o re detai l .
1 2 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
I f C at h as a gen erated i den ti fi er, th e i den ti fi er i s gen erated an d assi gn ed to th e cat w h en s av e( ) i s c al l ed. I f
C at h as an as s igned i den ti fi er, o r a c o m p o si te k ey, th e i den ti fi er sh o u l d be assi gn ed to th e cat i n stan c e befo re
c al l i n g s av e( ). Y o u c an al so u se pers is t( ) i n stead o f s av e( ), w i th th e sem an ti c s defi n ed i n th e E J B 3 earl y draft.
1 2 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Y o u c an ev en l o ad an o bj ec t u si n g an S Q L SE L E C T ... F O R U P D A T E , u si n g a L o ck M o de. S ee th e A P I
do c u m en tati o n fo r m o re i n fo rm ati o n .
s es s . s av e( cat);
s es s . f l u s h ( ); / / f o rce th e SQ L I NSE R T
s es s . ref res h ( cat); / / re- read th e s tate ( af ter th e trigger ex ecu tes )
1 0 . 4 . Q uery ing
I f yo u do n o t k n o w th e i den ti fi ers o f th e o bj ec ts yo u are l o o k i n g fo r, yo u n eed a q u ery. H i bern ate su p p o rts an
easy- to - u se bu t p o w erfu l o bj ec t o ri en ted q u ery l an gu age (H Q L ). F o r p ro gram m ati c q u ery c reati o n , H i bern ate
su p p o rts a so p h i sti c ated Cri teri a an d E x am p l e q u ery featu re (Q B C an d Q B E ). Y o u c an al so ex p ress yo u r
q u ery i n th e n ati v e S Q L o f yo u r database, w i th o p ti o n al su p p o rt fro m H i bern ate fo r resu l t set c o n v ersi o n i n to
o bj ec ts.
1 0 . 4 . 1 . E x e c u ti ng q u e ri e s
H Q L an d n ati v e S Q L q u eri es are rep resen ted w i th an i n stan c e o f o rg. h ib ernate. Q u ery. T h i s i n terfac e o ffers
m eth o ds fo r p aram eter bi n di n g, resu l t set h an dl i n g, an d fo r th e ex ec u ti o n o f th e ac tu al q u ery. Y o u al w ays
o btai n a Q u ery u si n g th e c u rren t Ses s io n:
1 2 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
/ / f etch ids
I terato r iter = s es s . createQ u ery( " f ro m eg. Q u x q o rder b y q . l ik el ines s " ). iterate( );
w h il e ( iter. h as Nex t( ) ) {
Q u x q u x = ( Q u x ) iter. nex t( ); / / f etch th e o b j ect
/ / s o meth ing w e co u l dnt ex pres s in th e q u ery
if ( q u x . cal cu l ateC o mpl icatedA l go rith m( ) ) {
/ / del ete th e cu rrent ins tance
iter. remo v e( );
/ / do nt need to pro ces s th e res t
b reak ;
}
}
1 2 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
w h il e ( res u l ts . h as Nex t( ) ) {
O b j ect[ ] ro w = ( O b j ect[ ] ) res u l ts . nex t( );
C o l o r type = ( C o l o r) ro w [ 0] ;
D ate o l des t = ( D ate) ro w [ 1] ;
I nteger co u nt = ( I nteger) ro w [ 2 ] ;
.....
}
1 2 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
/ / named parameter l is t
L is t names = new A rrayL is t( );
names . add( " I z i" );
names . add( " F ritz " );
Q u ery q = s es s . createQ u ery( " f ro m D o mes ticC at cat w h ere cat. name in ( : names L is t)" );
q . s etP arameterL is t( " names L is t" , names );
L is t cats = q . l is t( );
Q u ery q = s es s . createQ u ery( " s el ect cat. name, cat f ro m D o mes ticC at cat " +
" o rder b y cat. name" );
Scro l l ab l eR es u l ts cats = q . s cro l l ( );
if ( cats . f irs t( ) ) {
}
cats . cl o s e( )
1 2 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 0 . 4 . 2 . F i l te ri ng c ol l e c ti ons
1 0 . 4 . 3 . C ri te ri a q u e ri e s
1 2 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h e C riteria an d th e asso c i ated E x ampl e A P I are di sc u ssed i n m o re detai l i n Ch ap ter 1 5 , C rite ria Q ue rie s .
1 0 . 4 . 4 . Q u e ri e s i n nati v e S Q L
D o mes ticC at cat = ( D o mes ticC at) s es s . l o ad( C at. cl as s , new L o ng( 6 9 ) );
cat. s etName( " P K " );
s es s . f l u s h ( ); / / ch anges to cat are au to matical l y detected and pers is ted
Important
H i bern ate do es n o t o ffer i ts o w n A P I fo r di rec t ex ec u ti o n o f U P D A T E o r D E L E T E statem en ts.
H i bern ate i s a s tate m anag e m e nt serv i c e, yo u do n o t h av e to th i n k in s tate m e nts to u se i t.
J D B C i s a p erfec t A P I fo r ex ec u ti n g S Q L statem en ts, yo u c an get a J D B C C o nnectio n at an y ti m e
by c al l i n g s es s io n. co nnectio n( ). F u rth erm o re, th e n o ti o n o f m ass o p erati o n s c o n fl i c ts w i th
o bj ec t/rel ati o n al m ap p i n g fo r o n l i n e tran sac ti o n p ro c essi n g- o ri en ted ap p l i c ati o n s. F u tu re
v ersi o n s o f H i bern ate c an , h o w ev er, p ro v i de sp ec i al m ass o p erati o n fu n c ti o n s. S ee Ch ap ter 1 3 ,
B atc h p roc e s s ing fo r so m e p o ssi bl e batc h o p erati o n tri c k s.
1 2 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
/ / in th e f irs t s es s io n
C at cat = ( C at) f irs tSes s io n. l o ad( C at. cl as s , catI d);
C at po tential M ate = new C at( );
f irs tSes s io n. s av e( po tential M ate);
/ / l ater, in a new s es s io n
s eco ndSes s io n. u pdate( cat); / / u pdate cat
s eco ndSes s io n. u pdate( mate); / / u pdate mate
I f th e C at w i th i den ti fi er catI d h ad al ready been l o aded by s eco ndSes s io n w h en th e ap p l i c ati o n tri ed to reattac h
i t, an ex c ep ti o n w o u l d h av e been th ro w n .
U se u pdate( ) i f yo u are c ertai n th at th e sessi o n do es n o t c o n tai n an al ready p ersi sten t i n stan c e w i th th e sam e
i den ti fi er. U se merge( ) i f yo u w an t to m erge yo u r m o di fi c ati o n s at an y ti m e w i th o u t c o n si derati o n o f th e state
o f th e sessi o n . I n o th er w o rds, u pdate( ) i s u su al l y th e fi rst m eth o d yo u w o u l d c al l i n a fresh sessi o n , en su ri n g
th at th e reattac h m en t o f yo u r detac h ed i n stan c es i s th e fi rst o p erati o n th at i s ex ec u ted.
/ / j u s t reas s o ciate:
s es s . l o ck ( f ritz , L o ck M o de. NO NE );
/ / do a v ers io n ch eck , th en reas s o ciate:
s es s . l o ck ( iz i, L o ck M o de. R E A D );
/ / do a v ers io n ch eck , u s ing SE L E C T . . . F O R U P D A T E , th en reas s o ciate:
s es s . l o ck ( pk , L o ck M o de. U P G R A D E );
/ / in th e f irs t s es s io n
C at cat = ( C at) f irs tSes s io n. l o ad( C at. cl as s , catI D );
/ / l ater, in a new s es s io n
s eco ndSes s io n. s av eO rU pdate( cat); / / u pdate ex is ting s tate ( cat h as a no n- nu l l id)
s eco ndSes s io n. s av eO rU pdate( mate); / / s av e th e new ins tance ( mate h as a nu l l id)
1 2 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
s av eO rU pdate( ) do es th e fo l l o w i n g:
i f th e o bj ec t i s v ersi o n ed by a < v ers io n> o r < times tamp>, an d th e v ersi o n p ro p erty v al u e i s th e sam e
v al u e assi gn ed to a n ew l y i n stan ti ated o bj ec t, s av e( ) i t
o th erw i se u pdate( ) th e o bj ec t
i f th ere i s a p ersi sten t i n stan c e w i th th e sam e i den ti fi er c u rren tl y asso c i ated w i th th e sessi o n , c o p y th e
state o f th e gi v en o bj ec t o n to th e p ersi sten t i n stan c e
i f th ere i s n o p ersi sten t i n stan c e c u rren tl y asso c i ated w i th th e sessi o n , try to l o ad i t fro m th e database, o r
c reate a n ew p ersi sten t i n stan c e
th e p ersi sten t i n stan c e i s retu rn ed
th e gi v en i n stan c e do es n o t bec o m e asso c i ated w i th th e sessi o n , i t rem ai n s detac h ed
1 3 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
U sec ases fo r th i s featu re i n c l u de rec o n c i l i n g data en tered i n to di fferen t database i n stan c es, u p gradi n g system
c o n fi gu rati o n i n fo rm ati o n du ri n g p ro du c t u p grades, ro l l i n g bac k c h an ges m ade du ri n g n o n - A CI D tran sac ti o n s
an d m o re.
1 0 . 1 0 . F l us h ing th e S es s ion
S o m eti m es th e Ses s io n w i l l ex ec u te th e S Q L statem en ts n eeded to syn c h ro n i z e th e J D B C c o n n ec ti o n ' s state
w i th th e state o f o bj ec ts h el d i n m em o ry. T h i s p ro c ess, c al l ed f lus h , o c c u rs by defau l t at th e fo l l o w i n g p o i n ts:
befo re so m e q u ery ex ec u ti o n s
fro m o rg. h ib ernate. T rans actio n. co mmit( )
fro m Ses s io n. f l u s h ( )
2. al l en ti ty u p dates
3 . al l c o l l ec ti o n del eti o n s
4 . al l c o l l ec ti o n el em en t del eti o n s, u p dates an d i n serti o n s
5 . al l c o l l ec ti o n i n serti o n s
6 . al l en ti ty del eti o n s i n th e sam e o rder th e c o rresp o n di n g o bj ec ts w ere del eted u si n g Ses s io n. del ete( )
1 3 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
s es s = s f . o penSes s io n( );
T rans actio n tx = s es s . b eginT rans actio n( );
s es s . s etF l u s h M o de( F l u s h M o de. C O M M I T ); / / al l o w q u eries to retu rn s tal e s tate
/ / ch ange to iz i is no t f l u s h ed!
...
tx . co mmit( ); / / f l u s h o ccu rs
s es s . cl o s e( );
I f th e c h i l dren i n a p aren t/c h i l d rel ati o n sh i p w o u l d be v al u e typ ed (e.g. a c o l l ec ti o n o f addresses o r stri n gs),
th ei r l i fe c yc l e w o u l d dep en d o n th e p aren t an d n o fu rth er ac ti o n w o u l d be req u i red fo r c o n v en i en t
" c asc adi n g" o f state c h an ges. W h en th e p aren t i s sav ed, th e v al u e- typ ed c h i l d o bj ec ts are sav ed an d w h en
th e p aren t i s del eted, th e c h i l dren w i l l be del eted, etc . T h i s w o rk s fo r o p erati o n s su c h as th e rem o v al o f a
c h i l d fro m th e c o l l ec ti o n . S i n c e v al u e- typ ed o bj ec ts c an n o t h av e sh ared referen c es, H i bern ate w i l l detec t th i s
an d del ete th e c h i l d fro m th e database.
< o ne- to - o ne name=" pers o n" cas cade=" pers is t" / >
< o ne- to - o ne name=" pers o n" cas cade=" pers is t, del ete, l o ck " / >
A sp ec i al c asc ade styl e, del ete- o rph an, ap p l i es o n l y to o n e- to - m an y asso c i ati o n s, an d i n di c ates th at th e
del ete( ) o p erati o n sh o u l d be ap p l i ed to an y c h i l d o bj ec t th at i s rem o v ed fro m th e asso c i ati o n .
R ec o m m en dati o n s:
I t do es n o t u su al l y m ak e sen se to en abl e c asc ade o n a < many- to - o ne> o r < many- to - many> asso c i ati o n .
Casc ade i s o ften u sefu l fo r < o ne- to - o ne> an d < o ne- to - many> asso c i ati o n s.
1 3 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
F u rth erm o re, a m ere referen c e to a c h i l d fro m a p ersi sten t p aren t w i l l resu l t i n sav e/u p date o f th e c h i l d. T h i s
m etap h o r i s i n c o m p l ete, h o w ev er. A c h i l d w h i c h bec o m es u n referen c ed by i ts p aren t i s not au to m ati c al l y
del eted, ex c ep t i n th e c ase o f a < o ne- to - many> asso c i ati o n m ap p ed w i th cas cade=" del ete- o rph an" . T h e p rec i se
sem an ti c s o f c asc adi n g o p erati o n s fo r a p aren t/c h i l d rel ati o n sh i p are as fo l l o w s:
H i bern ate ex p o ses m etadata v i a th e C l as s M etadata an d C o l l ectio nM etadata i n terfac es an d th e T ype h i erarc h y.
I n stan c es o f th e m etadata i n terfac es c an be o btai n ed fro m th e Ses s io nF acto ry.
C at f ritz = . . . . . . ;
C l as s M etadata catM eta = s es s io nf acto ry. getC l as s M etadata( C at. cl as s );
1 3 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h e m o st i m p o rtan t p o i n t abo u t H i bern ate an d c o n c u rren c y c o n tro l i s th at i t i s easy to u n derstan d. H i bern ate
di rec tl y u ses J D B C c o n n ec ti o n s an d J T A reso u rc es w i th o u t addi n g an y addi ti o n al l o c k i n g beh av i o r. I t i s
rec o m m en ded th at yo u sp en d so m e ti m e w i th th e J D B C, A N S I , an d tran sac ti o n i so l ati o n sp ec i fi c ati o n o f yo u r
database m an agem en t system .
T h e di sc u ssi o n o f c o n c u rren c y c o n tro l i n H i bern ate begi n s w i th th e gran u l ari ty o f C o nf igu ratio n, Ses s io nF acto ry,
an d Ses s io n, as w el l as database tran sac ti o n s an d l o n g c o n v ersati o n s.
1 1 . 1 . 1 . U ni t of w ork
1 3 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
tal k i n g abo u t a m o re abstrac t n o ti o n o f a tran sac ti o n . T h e term " bu si n ess tran sac ti o n " i s al so so m eti m es
u sed i n l i eu o f u n i t o f w o rk .
1 3 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
A utom atic V e rs ioning : H i bern ate c an p erfo rm au to m ati c o p ti m i sti c c o n c u rren c y c o n tro l fo r yo u . I t c an
au to m ati c al l y detec t i f a c o n c u rren t m o di fi c ati o n o c c u rred du ri n g u ser th i n k ti m e. Ch ec k fo r th i s at th e
en d o f th e c o n v ersati o n .
D e tac h e d O b j e c ts : i f yo u dec i de to u se th e s e s s ion- p e r- re q ue s t p attern , al l l o aded i n stan c es w i l l be i n th e
detac h ed state du ri n g u ser th i n k ti m e. H i bern ate al l o w s yo u to reattac h th e o bj ec ts an d p ersi st th e
m o di fi c ati o n s. T h e p attern i s c al l ed s e s s ion- p e r- re q ue s t- w ith - d e tac h e d - ob j e c ts . A u to m ati c v ersi o n i n g i s
u sed to i so l ate c o n c u rren t m o di fi c ati o n s.
1 1 . 1 . 3 . C ons i d e ri ng ob j e c t i d e nti ty
A n ap p l i c ati o n c an c o n c u rren tl y ac c ess th e sam e p ersi sten t state i n tw o di fferen t Ses s io ns. H o w ev er, an
i n stan c e o f a p ersi sten t c l ass i s n ev er sh ared betw een tw o Ses s io n i n stan c es. I t i s fo r th i s reaso n th at th ere
are tw o di fferen t n o ti o n s o f i den ti ty:
D atabase I den ti ty
f o o . getI d( ). eq u al s ( b ar. getI d( ) )
J V M I den ti ty
f o o ==b ar
1 3 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 1 . 1 . 4 . C ommon i s s u e s
D o n o t u se th e an ti - p attern s s e s s ion- p e r- us e r- s e s s ion o r s e s s ion- p e r- ap p lic ation (th ere are, h o w ev er, rare
ex c ep ti o n s to th i s ru l e). S o m e o f th e fo l l o w i n g i ssu es m i gh t al so ari se w i th i n th e rec o m m en ded p attern s, so
en su re th at yo u u n derstan d th e i m p l i c ati o n s befo re m ak i n g a desi gn dec i si o n :
H o w ev er, i t i s o ften desi rabl e to k eep yo u r p ersi sten c e l ayer p o rtabl e betw een n o n - m an aged reso u rc e- l o c al
en v i ro n m en ts, an d system s th at c an rel y o n J T A bu t u se B M T i n stead o f CM T . I n bo th c ases u se
p ro gram m ati c tran sac ti o n dem arc ati o n . H i bern ate o ffers a w rap p er A P I c al l ed T rans actio n th at tran sl ates i n to
th e n ati v e tran sac ti o n system o f yo u r dep l o ym en t en v i ro n m en t. T h i s A P I i s ac tu al l y o p ti o n al , bu t w e stro n gl y
en c o u rage i ts u se u n l ess yo u are i n a CM T sessi o n bean .
fl u sh th e sessi o n
c o m m i t th e tran sac ti o n
c l o se th e sessi o n
h an dl e ex c ep ti o n s
W e di sc u ssed F l u sh i n g th e sessi o n earl i er, so w e w i l l n o w h av e a c l o ser l o o k at tran sac ti o n dem arc ati o n an d
ex c ep ti o n h an dl i n g i n bo th m an aged an d n o n - m an aged en v i ro n m en ts.
1 3 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
/ / do s o me w o rk
...
tx . co mmit( );
}
catch ( R u ntimeE x ceptio n e) {
if ( tx ! = nu l l ) tx . ro l l b ack ( );
th ro w e; / / o r dis pl ay erro r mes s age
}
f inal l y {
s es s . cl o s e( );
}
/ / do s o me w o rk
...
Y o u sh o u l d sel ec t o rg. h ib ernate. trans actio n. J D B C T rans actio nF acto ry, w h i c h i s th e defau l t, an d fo r th e sec o n d
ex am p l e sel ec t " th read" as yo u r h ib ernate. cu rrent_ s es s io n_ co ntex t_ cl as s .
1 1 . 2 . 2 . U s i ng J T A
I f yo u r p ersi sten c e l ayer ru n s i n an ap p l i c ati o n serv er (fo r ex am p l e, beh i n d E J B sessi o n bean s), ev ery
dataso u rc e c o n n ec ti o n o btai n ed by H i bern ate w i l l au to m ati c al l y be p art o f th e gl o bal J T A tran sac ti o n . Y o u c an
al so i n stal l a stan dal o n e J T A i m p l em en tati o n an d u se i t w i th o u t E J B . H i bern ate o ffers tw o strategi es fo r J T A
i n tegrati o n .
1 3 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
I f yo u u se bean - m an aged tran sac ti o n s (B M T ), H i bern ate w i l l tel l th e ap p l i c ati o n serv er to start an d en d a
B M T tran sac ti o n i f yo u u se th e T rans actio n A P I . T h e tran sac ti o n m an agem en t c o de i s i den ti c al to th e
n o n - m an aged en v i ro n m en t.
/ / B M T idio m
Ses s io n s es s = f acto ry. o penSes s io n( );
T rans actio n tx = nu l l ;
try {
tx = s es s . b eginT rans actio n( );
/ / do s o me w o rk
...
tx . co mmit( );
}
catch ( R u ntimeE x ceptio n e) {
if ( tx ! = nu l l ) tx . ro l l b ack ( );
th ro w e; / / o r dis pl ay erro r mes s age
}
f inal l y {
s es s . cl o s e( );
}
tx . b egin( );
tx . co mmit( );
}
catch ( R u ntimeE x ceptio n e) {
tx . ro l l b ack ( );
th ro w e; / / o r dis pl ay erro r mes s age
}
W i th CM T , tran sac ti o n dem arc ati o n is c o m p l eted in sessi o n bean dep l o ym en t desc ri p to rs, n o t
p ro gram m ati c al l y. T h e c o de i s redu c ed to :
/ / C M T idio m
Ses s io n s es s = f acto ry. getC u rrentSes s io n( );
/ / do s o me w o rk
...
W h en c o n fi gu ri n g H i bern ate' s tran sac ti o n fac to ry, c h o o se o rg. h ib ernate. trans actio n. J T A T rans actio nF acto ry i f yo u
u se J T A di rec tl y (B M T ), an d o rg. h ib ernate. trans actio n. C M T T rans actio nF acto ry i n a CM T sessi o n bean . R em em ber
to al so set h ib ernate. trans actio n. manager_ l o o k u p_ cl as s . E n su re th at yo u r h ib ernate. cu rrent_ s es s io n_ co ntex t_ cl as s
1 3 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 1 . 2 . 3 . E x c e pti on h and l i ng
1 1 . 2 . 4 . T rans ac ti on ti me ou t
1 4 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
/ / do s o me w o rk
...
1 1 . 3 . 1 . A ppl i c ati on v e rs i on c h e c k i ng
t. co mmit( );
s es s io n. cl o s e( );
1 4 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
N ote
E arl i er v ersi o n s o f H i bern ate req u i red ex p l i c i t di sc o n n ec ti o n an d rec o n n ec ti o n o f a Ses s io n.
T h ese m eth o ds are dep rec ated, as begi n n i n g an d en di n g a tran sac ti o n h as th e sam e effec t.
E ac h i n terac ti o n w i th th e p ersi sten t sto re o c c u rs i n a n ew Ses s io n. H o w ev er, th e sam e p ersi sten t i n stan c es
are reu sed fo r eac h i n terac ti o n w i th th e database. T h e ap p l i c ati o n m an i p u l ates th e state o f detac h ed
i n stan c es o ri gi n al l y l o aded i n an o th er Ses s io n an d th en reattac h es th em u si n g Ses s io n. u pdate( ),
Ses s io n. s av eO rU pdate( ), o r Ses s io n. merge( ).
1 4 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
I n bo th c ases, w i th dedi c ated v ersi o n /ti m estam p c o l u m n s o r w i th a fu l l /di rty fi el d c o m p ari so n , H i bern ate
u ses a si n gl e U P D A T E statem en t, w i th an ap p ro p ri ate W H E R E c l au se, p er en ti ty to ex ec u te th e v ersi o n c h ec k
an d u p date th e i n fo rm ati o n . I f yo u u se tran si ti v e p ersi sten c e to c asc ade reattac h m en t to asso c i ated en ti ti es,
H i bern ate m ay ex ec u te u n n ec essary u p dates. T h i s i s u su al l y n o t a p ro bl em , bu t on up d ate tri ggers i n th e
database m i gh t be ex ec u ted ev en w h en n o c h an ges h av e been m ade to detac h ed i n stan c es. Y o u c an
c u sto m i z e th i s beh av i o r by setti n g s el ect- b ef o re- u pdate=" tru e" i n th e < cl as s > m ap p i n g, fo rc i n g H i bern ate to
SE L E C T th e i n stan c e to en su re th at c h an ges di d o c c u r befo re u p dati n g th e ro w .
L o ck M o de. R E A D i s ac q u i red au to m ati c al l y w h en H i bern ate reads data u n der R ep eatabl e R ead o r
S eri al i z abl e i so l ati o n l ev el . I t c an be re- ac q u i red by ex p l i c i t u ser req u est.
L o ck M o de. NO NE rep resen ts th e absen c e o f a l o c k . A l l o bj ec ts sw i tc h to th i s l o c k m o de at th e en d o f a
1 4 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
A c al l to Ses s io n. l o ck ( ).
I f th e req u ested l o c k m o de i s n o t su p p o rted by th e database, H i bern ate u ses an ap p ro p ri ate al tern ate m o de
i n stead o f th ro w i n g an ex c ep ti o n . T h i s en su res th at ap p l i c ati o n s are p o rtabl e.
A F T E R _ ST A T E M E NT (al so referred to as aggressi v e rel ease): rel eases c o n n ec ti o n s after ev ery statem en t
ex ec u ti o n . T h i s aggressi v e rel easi n g i s sk i p p ed i f th at statem en t l eav es o p en reso u rc es asso c i ated w i th
th e gi v en sessi o n . Cu rren tl y th e o n l y si tu ati o n w h ere th i s o c c u rs i s th ro u gh th e u se o f
o rg. h ib ernate. Scro l l ab l eR es u l ts .
T h e c o n fi gu rati o n p aram eter h ib ernate. co nnectio n. rel eas e_ mo de i s u sed to sp ec i fy w h i c h rel ease m o de to u se.
T h e p o ssi bl e v al u es are as fo l l o w s:
1 4 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 2 . 1 . I ntercep tors
T h e I ntercepto r i n terfac e p ro v i des c al l bac k s fro m th e sessi o n to th e ap p l i c ati o n , al l o w i n g th e ap p l i c ati o n to
i n sp ec t an d/o r m an i p u l ate p ro p erti es o f a p ersi sten t o bj ec t befo re i t i s sav ed, u p dated, del eted o r l o aded.
O n e p o ssi bl e u se fo r th i s i s to trac k au di ti n g i n fo rm ati o n . F o r ex am p l e, th e fo l l o w i n g I ntercepto r au to m ati c al l y
sets th e createT imes tamp w h en an A u ditab l e i s c reated an d u p dates th e l as tU pdateT imes tamp p ro p erty w h en an
A u ditab l e i s u p dated.
1 4 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
impo rt j av a. io . Serial iz ab l e;
impo rt j av a. u til . D ate;
impo rt j av a. u til . I terato r;
1 4 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 2 . 2 . E v ent s y s tem
I f yo u h av e to reac t to p arti c u l ar ev en ts i n yo u r p ersi sten c e l ayer, yo u c an al so u se th e H i bern ate3 e v e nt
arc h i tec tu re. T h e ev en t system c an be u sed i n addi ti o n , o r as a rep l ac em en t, fo r i n terc ep to rs.
T h e l i sten ers sh o u l d be c o n si dered si n gl eto n s. T h i s m ean s th ey are sh ared betw een req u ests, an d sh o u l d n o t
sav e an y state as i n stan c e v ari abl es.
Y o u al so n eed a c o n fi gu rati o n en try tel l i n g H i bern ate to u se th e l i sten er i n addi ti o n to th e defau l t l i sten er:
1 4 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
L i sten ers regi stered dec l arati v el y c an n o t sh are i n stan c es. I f th e sam e c l ass n am e i s u sed i n m u l ti p l e
< l is tener/ > el em en ts, eac h referen c e w i l l resu l t i n a sep arate i n stan c e o f th at c l ass. I f yo u n eed to sh are
l i sten er i n stan c es betw een l i sten er typ es yo u m u st u se th e p ro gram m ati c regi strati o n ap p ro ac h .
< l is tener type=" pre- del ete" cl as s =" o rg. h ib ernate. s ecu re. J A C C P reD el eteE v entL is tener" / >
< l is tener type=" pre- u pdate" cl as s =" o rg. h ib ernate. s ecu re. J A C C P reU pdateE v entL is tener" / >
< l is tener type=" pre- ins ert" cl as s =" o rg. h ib ernate. s ecu re. J A C C P reI ns ertE v entL is tener" / >
< l is tener type=" pre- l o ad" cl as s =" o rg. h ib ernate. s ecu re. J A C C P reL o adE v entL is tener" / >
N o te th at < l is tener type=" . . . " cl as s =" . . . " / > i s sh o rth an d fo r < ev ent type=" . . . " >< l is tener cl as s =" . . . " / >< / ev ent>
w h en th ere i s ex ac tl y o n e l i sten er fo r a p arti c u l ar ev en t typ e.
< grant ro l e=" admin" entity- name=" U s er" actio ns =" ins ert, u pdate, read" / >
< grant ro l e=" s u " entity- name=" U s er" actio ns =" *" / >
1 4 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
h ib ernate. j db c. b atch _ s iz e 2 0
H i bern ate di sabl es i n sert batc h i n g at th e J D B C l ev el tran sp aren tl y i f yo u u se an identity i den ti fi er gen erato r.
tx . co mmit( );
s es s io n. cl o s e( );
1 3 . 2 . B atch up d ates
F o r retri ev i n g an d u p dati n g data, th e sam e i deas ap p l y. I n addi ti o n , yo u n eed to u se s cro l l ( ) to tak e
adv an tage o f serv er- si de c u rso rs fo r q u eri es th at retu rn m an y ro w s o f data.
1 4 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
tx . co mmit( );
s es s io n. cl o s e( );
tx . co mmit( );
s es s io n. cl o s e( );
I n th i s c o de ex am p l e, th e C u s to mer i n stan c es retu rn ed by th e q u ery are i m m edi atel y detac h ed. T h ey are
n ev er asso c i ated w i th an y p ersi sten c e c o n tex t.
T h e ins ert( ), u pdate( ) an d del ete( ) o p erati o n s defi n ed by th e Statel es s Ses s io n i n terfac e are c o n si dered to be
di rec t database ro w - l ev el o p erati o n s. T h ey resu l t i n th e i m m edi ate ex ec u ti o n o f a S Q L I NSE R T , U P D A T E o r
D E L E T E resp ec ti v el y. T h ey h av e di fferen t sem an ti c s to th e s av e( ), s av eO rU pdate( ) an d del ete( ) o p erati o n s
defi n ed by th e Ses s io n i n terfac e.
1 3 . 4 . DM L - s ty l e op erations
A s al ready di sc u ssed, au to m ati c an d tran sp aren t o bj ec t/rel ati o n al m ap p i n g i s c o n c ern ed w i th th e
m an agem en t o f th e o bj ec t state. T h e o bj ec t state i s av ai l abl e i n m em o ry. T h i s m ean s th at m an i p u l ati n g data
1 5 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
di rec tl y i n th e database (u si n g th e S Q L D ata M anipu l atio n L angu age (D M L ) th e statem en ts: I NSE R T , U P D A T E ,
D E L E T E ) w i l l n o t affec t i n - m em o ry state. H o w ev er, H i bern ate p ro v i des m eth o ds fo r bu l k S Q L - styl e D M L
statem en t ex ec u ti o n th at i s p erfo rm ed th ro u gh th e H i bern ate Q u ery L an gu age (H Q L ).
S o m e p o i n ts to n o te:
String h q l U pdate = " u pdate C u s to mer c s et c. name = : new Name w h ere c. name = : o l dName" ;
/ / o r String h q l U pdate = " u pdate C u s to mer s et name = : new Name w h ere name = : o l dName" ;
int u pdatedE ntities = s . createQ u ery( h q l U pdate )
. s etString( " new Name" , new Name )
. s etString( " o l dName" , o l dName )
. ex ecu teU pdate( );
tx . co mmit( );
s es s io n. cl o s e( );
Cu sto m v ersi o n typ es, o rg. h ib ernate. u s ertype. U s erV ers io nT ype , are n o t al l o w ed i n c o n j u n c ti o n w i th a
u pdate v ers io ned statem en t.
1 5 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h e int v al u e retu rn ed by th e Q u ery. ex ecu teU pdate( ) m eth o d i n di c ates th e n u m ber o f en ti ti es effec ted by th e
o p erati o n . T h i s m ay o r m ay n o t c o rrel ate to th e n u m ber o f ro w s effec ted i n th e database. A n H Q L bu l k
o p erati o n m i gh t resu l t i n m u l ti p l e ac tu al S Q L statem en ts bei n g ex ec u ted (fo r j o i n ed- su bc l ass, fo r ex am p l e).
T h e retu rn ed n u m ber i n di c ates th e n u m ber o f ac tu al en ti ti es affec ted by th e statem en t. G o i n g bac k to th e
ex am p l e o f j o i n ed- su bc l ass, a del ete agai n st o n e o f th e su bc l asses m ay ac tu al l y resu l t i n del etes agai n st n o t
ju st th e tabl e to w h i c h th at su bc l ass i s m ap p ed, bu t al so th e " ro o t" tabl e an d p o ten ti al l y j o i n ed- su bc l ass
tabl es fu rth er do w n th e i n h eri tan c e h i erarc h y.
T h e p seu do - syn tax fo r I NSE R T statem en ts i s: I NSE R T I NT O E ntityName pro perties _ l is t s el ect_ s tatement . S o m e
p o i n ts to n o te:
T h e fo l l o w i n g i s an ex am p l e o f an H Q L I NSE R T statem en t ex ec u ti o n :
String h q l I ns ert = " ins ert into D el inq u entA cco u nt ( id, name) s el ect c. id, c. name f ro m C u s to mer c w h ere . . . " ;
int createdE ntities = s . createQ u ery( h q l I ns ert )
. ex ecu teU pdate( );
tx . co mmit( );
s es s io n. cl o s e( );
1 5 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h i s m an u al u ses l o w erc ase H Q L k eyw o rds. S o m e u sers fi n d q u eri es w i th u p p erc ase k eyw o rds m o re
readabl e, bu t th i s c o n v en ti o n i s u n su i tabl e fo r q u eri es em bedded i n J av a c o de.
1 4 . 2 . T h e from cl aus e
T h e si m p l est p o ssi bl e H i bern ate q u ery i s o f th e fo rm :
f ro m eg. C at
f ro m C at
f ro m C at as cat
f ro m C at cat
1 5 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
f ro m F o rmu l a, P arameter
f ro m C at as cat
inner j o in cat. mate as mate
l ef t o u ter j o in cat. k ittens as k itten
inner j o in
l ef t o u ter j o in
righ t o u ter j o in
f u l l j o in (n o t u su al l y u sefu l )
f ro m C at as cat
j o in cat. mate as mate
l ef t j o in cat. k ittens as k itten
f ro m C at as cat
l ef t j o in cat. k ittens as k itten
w ith k itten. b o dyW eigh t > 10. 0
A
" fetc h " j o i n al l o w s asso c i ati o n s o r c o l l ec ti o n s o f v al u es to be i n i ti al i z ed al o n g w i th th ei r p aren t o bj ec ts u si n g
a si n gl e sel ec t. T h i s i s p arti c u l arl y u sefu l i n th e c ase o f a c o l l ec ti o n . I t effec ti v el y o v erri des th e o u ter j o i n an d
l az y dec l arati o n s o f th e m ap p i n g fi l e fo r asso c i ati o n s an d c o l l ec ti o n s. S ee S ec ti o n 1 9 .1 , “ F etc h i n g strategi es”
fo r m o re i n fo rm ati o n .
f ro m C at as cat
inner j o in f etch cat. mate
l ef t j o in f etch cat. k ittens
f ro m C at as cat
inner j o in f etch cat. mate
l ef t j o in f etch cat. k ittens ch il d
l ef t j o in f etch ch il d. k ittens
1 5 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
f ro m D o cu ment do c f etch al l pro perties w h ere l o w er( do c. name) l ik e ' % cats % '
T h e impl icit fo rm do es n o t u se th e j o i n k eyw o rd. I n stead, th e asso c i ati o n s are " dereferen c ed" u si n g
do t- n o tati o n . impl icit j o i n s c an ap p ear i n an y o f th e H Q L c l au ses. impl icit j o i n resu l t i n i n n er j o i n s i n th e
resu l ti n g S Q L statem en t.
Important
P l ease n o te th at, starti n g i n v ersi o n 3 .2.2, th i s h as c h an ged si gn i fi c an tl y. I n p rev i o u s v ersi o n s,
id alw ay s referred to th e i den ti fi er p ro p erty regardl ess o f i ts ac tu al n am e. A ram i fi c ati o n o f th at
dec i si o n w as th at n o n - i den ti fi er p ro p erti es n am ed id c o u l d n ev er be referen c ed i n H i bern ate
q u eri es.
1 4 . 6 . T h e s el ect cl aus e
T h e s el ect c l au se p i c k s w h i c h o bj ec ts an d p ro p erti es to retu rn i n th e q u ery resu l t set. Co n si der th e fo l l o w i n g:
s el ect mate
f ro m C at as cat
inner j o in cat. mate as mate
1 5 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
O r as a L is t :
s el ect max ( b o dyW eigh t) as max , min( b o dyW eigh t) as min, co u nt( *) as n
f ro m C at cat
s el ect new map( max ( b o dyW eigh t) as max , min( b o dyW eigh t) as min, co u nt( *) as n )
f ro m C at cat
s el ect av g( cat. w eigh t), s u m( cat. w eigh t), max ( cat. w eigh t), co u nt( cat)
f ro m C at cat
av g( . . . ), s u m( . . . ), min( . . . ), max ( . . . )
co u nt( *)
1 5 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
s el ect f irs tName| | ' ' | | initial | | ' ' | | u pper( l as tName) f ro m P ers o n
1 4 . 8 . P ol y morp h ic q ueries
A q u ery l i k e:
f ro m C at as cat
f ro m j av a. l ang. O b j ect o
1 4 . 9 . T h e w h ere cl aus e
T h e w h ere c l au se al l o w s yo u to refi n e th e l i st o f i n stan c es retu rn ed. I f n o al i as ex i sts, yo u c an refer to
p ro p erti es by n am e:
T h e fo l l o w i n g q u ery:
s el ect f o o
f ro m F o o f o o , B ar b ar
w h ere f o o . s tartD ate = b ar. date
1 5 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
f ro m F o o fo o
w h ere f o o . b ar. b az . cu s to mer. addres s . city is no t nu l l
1 4 . 1 0 . E x p res s ions
E x p ressi o n s u sed i n th e w h ere c l au se i n c l u de th e fo l l o w i n g:
1 5 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
l o gi c al o p erati o n s and, o r, no t
" S im p l e" c ase, cas e . . . w h en . . . th en . . . el s e . . . end, an d " searc h ed" c ase, cas e w h en . . . th en . . . el s e . . . end
A n y fu n c ti o n o r o p erato r defi n ed by E J B - Q L 3 .0 :
s u b s tring( ), trim( ), l o w er( ), u pper( ), l ength ( ), l o cate( ), ab s ( ), s q rt( ), b it_ l ength ( ), mo d( )
co al es ce( ) an d nu l l if ( )
f ro m D o mes ticC at cat w h ere cat. name b etw een ' A ' and ' B '
f ro m D o mes ticC at cat w h ere cat. name in ( ' F o o ' , ' B ar' , ' B az ' )
T h e n egated fo rm s c an be w ri tten as fo l l o w s:
f ro m D o mes ticC at cat w h ere cat. name no t b etw een ' A ' and ' B '
f ro m D o mes ticC at cat w h ere cat. name no t in ( ' F o o ' , ' B ar' , ' B az ' )
B o o l ean s c an be easi l y u sed i n ex p ressi o n s by dec l ari n g H Q L q u ery su bsti tu ti o n s i n H i bern ate c o n fi gu rati o n :
< pro perty name=" h ib ernate. q u ery. s u b s titu tio ns " >tru e 1, f al s e 0< / pro perty>
Y o u c an test th e si z e o f a c o l l ec ti o n w i th th e sp ec i al p ro p erty s iz e o r th e sp ec i al s iz e( ) fu n c ti o n .
1 5 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
f ro m C al endar cal w h ere max el ement( cal . h o l idays ) > cu rrent_ date
s el ect mo th er f ro m C at as mo th er, C at as k it
w h ere k it in el ements ( f o o . k ittens )
N o te th at th ese c o n stru c ts - s iz e , el ements , indices , minindex , max index , minel ement , max el ement - c an o n l y be
u sed i n th e w h ere c l au se i n H i bern ate3 .
1 6 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
s el ect cu s t
f ro m P ro du ct pro d,
Sto re s to re
inner j o in s to re. cu s to mers cu s t
w h ere pro d. name = ' w idget'
and s to re. l o catio n. name in ( ' M el b o u rne' , ' Sydney' )
and pro d = al l el ements ( cu s t. cu rrentO rder. l ineI tems )
H int: so m eth i n g l i k e
1 4 . 1 1 . T h e ord er by cl aus e
T h e l i st retu rn ed by a q u ery c an be o rdered by an y p ro p erty o f a retu rn ed c l ass o r c o m p o n en ts:
1 4 . 1 2 . T h e g roup by cl aus e
A q u ery th at retu rn s aggregate v al u es c an be gro u p ed by an y p ro p erty o f a retu rn ed c l ass o r c o m p o n en ts:
A h av ing c l au se i s al so al l o w ed.
1 6 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
s el ect cat
f ro m C at cat
j o in cat. k ittens k itten
gro u p b y cat. id, cat. name, cat. o th er, cat. pro perties
h av ing av g( k itten. w eigh t) > 100
o rder b y co u nt( k itten) as c, s u m( k itten. w eigh t) des c
1 4 . 1 3 . S ubq ueries
F o r databases th at su p p o rt su bsel ec ts, H i bern ate su p p o rts su bq u eri es w i th i n q u eri es. A su bq u ery m u st be
su rro u n ded by p aren th eses (o ften by an S Q L aggregate fu n c ti o n c al l ). E v en c o rrel ated su bq u eri es
(su bq u eri es th at refer to an al i as i n th e o u ter q u ery) are al l o w ed.
f ro m C at as f atcat
w h ere f atcat. w eigh t > (
s el ect av g( cat. w eigh t) f ro m D o mes ticC at cat
)
f ro m C at as cat
w h ere no t ex is ts (
f ro m C at as mate w h ere mate. mate = cat
)
s el ect cat. id, ( s el ect max ( k it. w eigh t) f ro m cat. k itten k it)
f ro m C at as cat
1 4 . 1 4 . HQ L ex amp l es
H i bern ate q u eri es c an be q u i te p o w erfu l an d c o m p l ex . I n fac t, th e p o w er o f th e q u ery l an gu age i s o n e o f
H i bern ate' s m ai n stren gth s. T h e fo l l o w i n g ex am p l e q u eri es are si m i l ar to q u eri es th at h av e been u sed o n
rec en t p ro j ec ts. P l ease n o te th at m o st q u eri es yo u w i l l w ri te w i l l be m u c h si m p l er th an th e fo l l o w i n g
ex am p l es.
1 6 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
W h at a m o n ster! A c tu al l y, i n real l i fe, I ' m n o t v ery k een o n su bq u eri es, so m y q u ery w as real l y m o re l i k e
th i s:
1 6 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 4 . 1 6 . T ip s & T rick s
Y o u c an c o u n t th e n u m ber o f q u ery resu l ts w i th o u t retu rn i n g th em :
( ( I nteger) s es s io n. createQ u ery( " s el ect co u nt( *) f ro m . . . . " ). iterate( ). nex t( ) ). intV al u e( )
1 6 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Q u ery q = s . createQ u ery( " f ro m f o o F o o as f o o w h ere f o o . name=: name and f o o . s iz e=: s iz e" );
q . s etP ro perties ( f o o B ean); / / f o o B ean h as getName( ) and getSiz e( )
L is t f o o s = q . l is t( );
Y o u c an fi n d th e si z e o f a c o l l ec ti o n w i th o u t i n i ti al i z i n g i t:
( ( I nteger) s es s io n. createQ u ery( " s el ect co u nt( *) f ro m . . . . " ). iterate( ). nex t( ) ). intV al u e( );
1 4 . 1 7 . C omp onents
Co m p o n en ts c an be u sed si m i l arl y to th e si m p l e v al u e typ es th at are u sed i n H Q L q u eri es. T h ey c an ap p ear
i n th e s el ect c l au se as fo l l o w s:
1 6 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
A n o th er c o m m o n u se o f c o m p o n en ts i s i n ro w v al u e c o n stru c to rs.
f ro m P ers o n p w h ere p. name. f irs t=' J o h n' and p. name. l as t=' J ingl eh eimer- Sch midt'
f ro m P ers o n p w h ere p. name=( ' J o h n' , ' J ingl eh eimer- Sch midt' )
U si n g ro w v al u e co ns tru cto r syn tax c an al so be ben efi c i al w h en u si n g su bq u eri es th at n eed to c o m p are agai n st
m u l ti p l e v al u es:
f ro m C at as cat
w h ere no t ( cat. name, cat. co l o r ) in (
s el ect cat. name, cat. co l o r f ro m D o mes ticC at cat
)
1 6 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
R estri c ti o n s c an be gro u p ed l o gi c al l y.
1 5 . 3 . O rd ering th e res ul ts
Y o u c an o rder th e resu l ts u si n g o rg. h ib ernate. criterio n. O rder.
1 6 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 5 . 4 . A s s ociations
B y n av i gati n g asso c i ati o n s u si n g createC riteria( ) yo u c an sp ec i fy c o n strai n ts u p o n rel ated en ti ti es:
1 6 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 5 . 6 . E x amp l e q ueries
T h e c l ass o rg. h ib ernate. criterio n. E x ampl e al l o w s yo u to c o n stru c t a q u ery c ri teri o n fro m a gi v en i n stan c e.
V ersi o n p ro p erti es, i den ti fi ers an d asso c i ati o n s are i gn o red. B y defau l t, n u l l v al u ed p ro p erti es are ex c l u ded.
1 6 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h ere i s n o ex p l i c i t " gro u p by" n ec essary i n a c ri teri a q u ery. Certai n p ro j ec ti o n typ es are defi n ed to be
g roup ing p roj e c tions , w h i c h al so ap p ear i n th e S Q L gro u p b y c l au se.
1 7 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Ses s io n s es s io n = . . . . ;
T rans actio n tx n = s es s io n. b eginT rans actio n( );
L is t res u l ts = q u ery. getE x ecu tab l eC riteria( s es s io n). s etM ax R es u l ts ( 100). l is t( );
tx n. co mmit( );
s es s io n. cl o s e( );
A D etach edC riteria c an al so be u sed to ex p ress a su bq u ery. Cri teri o n i n stan c es i n v o l v i n g su bq u eri es c an be
o btai n ed v i a Su b q u eries o r P ro perty.
D etach edC riteria av gW eigh tF o rSex = D etach edC riteria. f o rC l as s ( C at. cl as s , " cat2 " )
. s etP ro j ectio n( P ro perty. f o rName( " w eigh t" ). av g( ) )
. add( P ro perty. f o rName( " cat2 . s ex " ). eq P ro perty( " cat. s ex " ) );
s es s io n. createC riteria( C at. cl as s , " cat" )
. add( P ro perty. f o rName( " w eigh t" ). gt( av gW eigh tF o rSex ) )
. l is t( );
1 7 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
C h ap ter 1 6 . N ativ e S Q L
1 6 .1 . U si n g a SQ L Q u ery
1 6 .1 .1 . S c al ar q u eri es
1 6 .1 .2. E n ti ty q u eri es
1 6 .1 .3 . H an dl i n g asso c i ati o n s an d c o l l ec ti o n s
1 6 .1 .4 . R etu rn i n g m u l ti p l e en ti ti es
1 6 .1 .5 . R etu rn i n g n o n - m an aged en ti ti es
1 6 .1 .6 . H an dl i n g i n h eri tan c e
1 6 .1 .7 . P aram eters
1 6 .2. N am ed S Q L q u eri es
1 6 .2.1 . U si n g retu rn - p ro p erty to ex p l i c i tl y sp ec i fy c o l u m n /al i as n am es
1 6 .2.2. U si n g sto red p ro c edu res fo r q u eryi n g
1 6 .3 . Cu sto m S Q L fo r c reate, u p date an d del ete
1 6 .4 . Cu sto m S Q L fo r l o adi n g
H i bern ate3 al l o w s yo u to sp ec i fy h an dw ri tten S Q L , i n c l u di n g sto red p ro c edu res, fo r al l c reate, u p date, del ete,
an d l o ad o p erati o n s.
1 6 . 1 . U s ing a SQ L Q u ery
E x ec u ti o n o f n ati v e S Q L q u eri es i s c o n tro l l ed v i a th e SQ L Q u ery i n terfac e, w h i c h i s o btai n ed by c al l i n g
Ses s io n. createSQ L Q u ery( ). T h e fo l l o w i n g sec ti o n s desc ri be h o w to u se th i s A P I fo r q u eryi n g.
1 6 . 1 . 1 . S c al ar q u e ri e s
1 7 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h i s q u ery sp ec i fi ed:
th e S Q L q u ery stri n g
th e c o l u m n s an d typ es to retu rn
T h i s i s essen ti al l y th e sam e q u ery as befo re, bu t n o w R es u l tSetM etaD ata i s u sed to determ i n e th e typ e o f
N A M E an d B I R T H D A T E , w h ere as th e typ e o f I D i s ex p l i c i tl y sp ec i fi ed.
H o w th e j av a.sq l .T yp es retu rn ed fro m R esu l tS etM etaD ata i s m ap p ed to H i bern ate typ es i s c o n tro l l ed by th e
D i al ec t. I f a sp ec i fi c typ e i s n o t m ap p ed, o r do es n o t resu l t i n th e ex p ec ted typ e, i t i s p o ssi bl e to c u sto m i z e i t
v i a c al l s to regis terH ib ernateT ype i n th e D i al ec t.
1 6 . 1 . 2 . E nti ty q u e ri e s
T h e abo v e q u eri es w ere al l abo u t retu rn i n g sc al ar v al u es, basi c al l y retu rn i n g th e " raw " v al u es fro m th e
resu l tset. T h e fo l l o w i n g sh o w s h o w to get en ti ty o bj ec ts fro m a n ati v e sq l q u ery v i a addE ntity( ).
T h i s q u ery sp ec i fi ed:
th e S Q L q u ery stri n g
th e en ti ty retu rn ed by th e q u ery
1 7 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 6 . 1 . 4 . R e tu rni ng mu l ti pl e e nti ti e s
T h e fo l l o w i n g fo rm i s n o t v u l n erabl e to c o l u m n n am e du p l i c ati o n :
T h i s q u ery sp ec i fi ed:
th e en ti ti es retu rn ed by th e q u ery
T h e { c at.* } an d { m o th er.* } n o tati o n u sed abo v e i s a sh o rth an d fo r " al l p ro p erti es" . A l tern ati v el y, yo u c an
l i st th e c o l u m n s ex p l i c i tl y, bu t ev en i n th i s c ase H i bern ate i n j ec ts th e S Q L c o l u m n al i ases fo r eac h p ro p erty.
T h e p l ac eh o l der fo r a c o l u m n al i as i s j u st th e p ro p erty n am e q u al i fi ed by th e tabl e al i as. I n th e fo l l o w i n g
ex am p l e, yo u retri ev e Cats an d th ei r m o th ers fro m a di fferen t tabl e (c at_ l o g) to th e o n e dec l ared i n th e
m ap p i n g m etadata. Y o u c an ev en u se th e p ro p erty al i ases i n th e w h ere c l au se.
1 7 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
A si m p l e
{[ al ias name] . [ pro pertyname] A _ NA M E as {item. name}
p ro p erty
A c o m p o si te
{[ al ias name] . [ co mpo nentname] . [ pro pertyname] } C U R R E NC Y as {item. amo u nt. cu rrency}, V A L U E as {item. amo u nt. v al u e}
p ro p erty
D i sc ri m i n ato r
{[ al ias name] . cl as s } D I SC as {item. cl as s }
o f an en ti ty
A l l p ro p erti es
{[ al ias name] . *} {item. *}
o f an en ti ty
A c o l l ec ti o n
{[ al ias name] . k ey} O R G I D as {co l l . k ey}
k ey
T h e i d o f an
{[ al ias name] . id} E M P I D as {co l l . id}
c o l l ec ti o n
T h e el em en t
o f an {[ al ias name] . el ement} X I D as {co l l . el ement}
c o l l ec ti o n
p ro p erty o f
th e el em en t
{[ al ias name] . el ement. [ pro pertyname] } NA M E as {co l l . el ement. name}
in th e
c o l l ec ti o n
A l l p ro p erti es
o f th e
{[ al ias name] . el ement. *} {co l l . el ement. *}
el em en t in
th e c o l l ec ti o n
A l l p ro p erti es
o f th e th e {[ al ias name] . *} {co l l . *}
c o l l ec ti o n
I t i s p o ssi bl e to ap p l y a R esu l tT ran sfo rm er to n ati v e S Q L q u eri es, al l o w i n g i t to retu rn n o n - m an aged en ti ti es.
T h i s q u ery sp ec i fi ed:
1 7 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
th e S Q L q u ery stri n g
a resu l t tran sfo rm er
1 6 . 1 . 6 . H and l i ng i nh e ri tanc e
1 6 . 1 . 7 . P arame te rs
1 6 . 2 . N amed S Q L q ueries
N am ed S Q L q u eri es c an be defi n ed i n th e m ap p i n g do c u m en t an d c al l ed i n ex ac tl y th e sam e w ay as a n am ed
H Q L q u ery. I n th i s c ase, yo u do not n eed to c al l addE ntity( ).
T h e < retu rn- j o in> el em en t i s u se to jo in asso c i ati o n s an d th e < l o ad- co l l ectio n> el em en t i s u sed to defi n e
q u eri es w h i c h i n i ti al i z e c o l l ec ti o n s,
1 7 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< s q l - q u ery name=" pers o ns W ith " res u l ts et- ref =" pers o nA ddres s " >
SE L E C T pers o n. NA M E A S {pers o n. name},
pers o n. A G E A S {pers o n. age},
pers o n. SE X A S {pers o n. s ex },
addres s . ST R E E T A S {addres s . s treet},
addres s . C I T Y A S {addres s . city},
addres s . ST A T E A S {addres s . s tate},
addres s . Z I P A S {addres s . z ip}
F R O M P E R SO N pers o n
J O I N A D D R E SS addres s
O N pers o n. I D = addres s . P E R SO N_ I D A ND addres s . T Y P E =' M A I L I NG '
W H E R E pers o n. NA M E L I K E : nameP attern
< / s q l - q u ery>
1 7 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Y o u c an ex p l i c i tl y tel l H i bern ate w h at c o l u m n al i ases to u se w i th < retu rn- pro perty>, i n stead o f u si n g th e
{}- syn tax to l et H i bern ate i n j ec t i ts o w n al i ases.F o r ex am p l e:
< s q l - q u ery name=" o rganiz atio nC u rrentE mpl o yments " >
< retu rn al ias =" emp" cl as s =" E mpl o yment" >
< retu rn- pro perty name=" s al ary" >
< retu rn- co l u mn name=" V A L U E " / >
< retu rn- co l u mn name=" C U R R E NC Y " / >
< / retu rn- pro perty>
< retu rn- pro perty name=" endD ate" co l u mn=" myE ndD ate" / >
< / retu rn>
SE L E C T E M P L O Y E E A S {emp. empl o yee}, E M P L O Y E R A S {emp. empl o yer},
ST A R T D A T E A S {emp. s tartD ate}, E ND D A T E A S {emp. endD ate},
R E G I O NC O D E as {emp. regio nC o de}, E I D A S {emp. id}, V A L U E , C U R R E NC Y
F R O M E M P L O Y M E NT
W H E R E E M P L O Y E R = : id A ND E ND D A T E I S NU L L
O R D E R B Y ST A R T D A T E A SC
< / s q l - q u ery>
I n th i s ex am p l e < retu rn- pro perty> w as u sed i n c o m bi n ati o n w i th th e {}- syn tax fo r i n j ec ti o n . T h i s al l o w s u sers
to c h o o se h o w th ey w an t to refer c o l u m n an d p ro p erti es.
1 6 . 2 . 2 . U s i ng s tore d proc e d u re s f or q u e ry i ng
1 7 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< s q l - q u ery name=" s el ectA l l E mpl o yees _ SP " cal l ab l e=" tru e" >
< retu rn al ias =" emp" cl as s =" E mpl o yment" >
< retu rn- pro perty name=" empl o yee" co l u mn=" E M P L O Y E E " / >
< retu rn- pro perty name=" empl o yer" co l u mn=" E M P L O Y E R " / >
< retu rn- pro perty name=" s tartD ate" co l u mn=" ST A R T D A T E " / >
< retu rn- pro perty name=" endD ate" co l u mn=" E ND D A T E " / >
< retu rn- pro perty name=" regio nC o de" co l u mn=" R E G I O NC O D E " / >
< retu rn- pro perty name=" id" co l u mn=" E I D " / >
< retu rn- pro perty name=" s al ary" >
< retu rn- co l u mn name=" V A L U E " / >
< retu rn- co l u mn name=" C U R R E NC Y " / >
< / retu rn- pro perty>
< / retu rn>
{ ? = cal l s el ectA l l E mpl o yments ( ) }
< / s q l - q u ery>
S to red p ro c edu res c u rren tl y o n l y retu rn sc al ars an d en ti ti es. < retu rn- j o in> an d < l o ad- co l l ectio n> are n o t
su p p o rted.
Y o u c an n o t u se sto red p ro c edu res w i th H i bern ate u n l ess yo u fo l l o w so m e p ro c edu re/fu n c ti o n ru l es. I f th ey do
n o t fo l l o w th o se ru l es th ey are n o t u sabl e w i th H i bern ate. I f yo u sti l l w an t to u se th ese p ro c edu res yo u h av e
to ex ec u te th em v i a s es s io n. co nnectio n( ). T h e ru l es are di fferen t fo r eac h database, si n c e database v en do rs
h av e di fferen t sto red p ro c edu re sem an ti c s/syn tax .
T h e rec o m m en ded c al l fo rm is stan dard S Q L 9 2: { ? = cal l f u nctio nName( < parameters >) } o r
{ ? = cal l pro cedu reName( < parameters >}. N ati v e c al l syn tax i s n o t su p p o rted.
F o r O rac l e th e fo l l o w i n g ru l es ap p l y:
F o r S ybase o r M S S Q L serv er th e fo l l o w i n g ru l es ap p l y:
T h e p ro c edu re m u st retu rn a resu l t set. N o te th at si n c e th ese serv ers c an retu rn m u l ti p l e resu l t sets an d
u p date c o u n ts, H i bern ate w i l l i terate th e resu l ts an d tak e th e fi rst resu l t th at i s a resu l t set as i ts retu rn
v al u e. E v eryth i n g el se w i l l be di sc arded.
I f yo u c an en abl e SE T NO C O U NT O N i n yo u r p ro c edu re i t w i l l p ro babl y be m o re effi c i en t, bu t th i s i s n o t a
req u i rem en t.
1 7 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Y o u c an v i ew th e ex p ec ted o rder by en abl i n g debu g l o ggi n g fo r th e o rg. h ib ernate. pers is ter. entity l ev el . W i th
th i s l ev el en abl ed, H i bern ate w i l l p ri n t o u t th e stati c S Q L th at i s u sed to c reate, u p date, del ete etc . en ti ti es.
T o v i ew th e ex p ec ted seq u en c e, do n o t i n c l u de yo u r c u sto m S Q L i n th e m ap p i n g fi l es, as th i s w i l l o v erri de th e
H i bern ate gen erated stati c S Q L .
T h e sto red p ro c edu res are i n m o st c ases req u i red to retu rn th e n u m ber o f ro w s i n serted, u p dated an d
del eted, as H i bern ate h as so m e ru n ti m e c h ec k s fo r th e su c c ess o f th e statem en t. H i bern ate al w ays regi sters
th e fi rst statem en t p aram eter as a n u m eri c o u tp u t p aram eter fo r th e CU D o p erati o n s:
u pdate P E R SO N
s et
NA M E = u name,
w h ere
I D = u id;
retu rn SQ L % R O W C O U NT ;
E ND u pdateP ers o n;
1 8 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< s et name=" empl o yments " inv ers e=" tru e" >
< k ey/ >
< o ne- to - many cl as s =" E mpl o yment" / >
< l o ader q u ery- ref =" empl o yments " / >
< / s et>
H i bern ate3 p ro v i des an i n n o v ati v e n ew ap p ro ac h to h an dl i n g data w i th " v i si bi l i ty" ru l es. A H ib e rnate f ilte r i s a
gl o bal , n am ed, p aram eteri z ed fi l ter th at c an be en abl ed o r di sabl ed fo r a p arti c u l ar H i bern ate sessi o n .
1 8 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
O r, to a c o l l ec ti o n :
< s et . . . >
< f il ter name=" myF il ter" co nditio n=" : myF il terP aram = M Y _ F I L T E R E D _ C O L U M N" / >
< / s et>
O r, to bo th o r m u l ti p l es o f eac h at th e sam e ti m e.
T h e m eth o ds o n Ses s io n are: enab l eF il ter( String f il terName), getE nab l edF il ter( String f il terName), an d
dis ab l eF il ter( String f il terName). B y defau l t, fi l ters are not en abl ed fo r a gi v en sessi o n . F i l ters m u st be en abl ed
th ro u gh u se o f th e Ses s io n. enab l eF il ter( ) m eth o d, w h i c h retu rn s an i n stan c e o f th e F il ter i n terfac e. I f yo u u sed
th e si m p l e fi l ter defi n ed abo v e, i t w o u l d l o o k l i k e th i s:
s es s io n. enab l eF il ter( " myF il ter" ). s etP arameter( " myF il terP aram" , " s o me- v al u e" );
1 8 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
I n o rder to en su re th at yo u are p ro v i ded w i th c u rren tl y effec ti v e rec o rds, en abl e th e fi l ter o n th e sessi o n
p ri o r to retri ev i n g em p l o yee data:
Ses s io n s es s io n = . . . ;
s es s io n. enab l eF il ter( " ef f ectiv eD ate" ). s etP arameter( " as O f D ate" , new D ate( ));
L is t res u l ts = s es s io n. createQ u ery( " f ro m E mpl o yee as e w h ere e. s al ary > : targetSal ary" )
. s etL o ng( " targetSal ary" , new L o ng( 1000000))
. l is t( );
< f il ter- def name=" myF il ter" co nditio n=" ab c > x yz " >. . . < / f il ter- def >
< f il ter- def name=" myO th erF il ter" >ab c=x yz < / f il ter- def >
C h ap ter 1 8 . X M L M ap p ing
1 8 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 8 .1 . W o rk i n g w i th X M L data
1 8 .1 .1 . S p ec i fyi n g X M L an d c l ass m ap p i n g to geth er
1 8 .1 .2. S p ec i fyi n g o n l y an X M L m ap p i n g
1 8 .2. X M L m ap p i n g m etadata
1 8 .3 . M an i p u l ati n g X M L data
X M L M ap p ing is an e x p e rim e ntal f e ature in H ib e rnate 3 . 0 and is c urre ntly und e r ac tiv e d e v e lop m e nt.
H ere i s an ex am p l e o f m ap p i n g a P O J O an d X M L si m u l tan eo u sl y:
...
< / cl as s >
1 8 . 1 . 2 . S pe c i f y i ng onl y an X M L mappi ng
1 8 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
...
< / cl as s >
F o r c o l l ec ti o n s an d si n gl e v al u ed asso c i ati o n s, th ere i s an addi ti o n al emb ed- x ml attri bu te. I f emb ed- x ml =" tru e" ,
th e defau l t, th e X M L tree fo r th e asso c i ated en ti ty (o r c o l l ec ti o n o f v al u e typ e) w i l l be em bedded di rec tl y i n
th e X M L tree fo r th e en ti ty th at o w n s th e asso c i ati o n . O th erw i se, i f emb ed- x ml =" f al s e" , th en o n l y th e
referen c ed i den ti fi er v al u e w i l l ap p ear i n th e X M L fo r si n gl e p o i n t asso c i ati o n s an d c o l l ec ti o n s w i l l n o t ap p ear
at al l .
D o n o t l eav e emb ed- x ml =" tru e" fo r to o m an y asso c i ati o n s, si n c e X M L do es n o t deal w el l w i th c i rc u l ari ty.
1 8 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
...
< / cl as s >
w o u l d retu rn datasets su c h as th i s:
I f yo u set emb ed- x ml =" tru e" o n th e < o ne- to - many> m ap p i n g, th e data m i gh t l o o k m o re l i k e th i s:
1 8 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
D o cu ment do c = . . . . ;
tx . co mmit( );
s es s io n. cl o s e( );
tx . co mmit( );
s es s io n. cl o s e( );
1 8 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 8 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
su i tabl e fo r l arge c o l l ec ti o n s.
P rox y f e tc h ing : a si n gl e- v al u ed asso c i ati o n i s fetc h ed w h en a m eth o d o th er th an th e i den ti fi er getter i s
i n v o k ed u p o n th e asso c i ated o bj ec t.
" N o- p rox y " f e tc h ing : a si n gl e- v al u ed asso c i ati o n i s fetc h ed w h en th e i n stan c e v ari abl e i s ac c essed.
Co m p ared to p ro x y fetc h i n g, th i s ap p ro ac h i s l ess l az y; th e asso c i ati o n i s fetc h ed ev en w h en o n l y th e
i den ti fi er i s ac c essed. I t i s al so m o re tran sp aren t, si n c e n o p ro x y i s v i si bl e to th e ap p l i c ati o n . T h i s
ap p ro ac h req u i res bu i l dti m e bytec o de i n stru m en tati o n an d i s rarel y n ec essary.
L az y attrib ute f e tc h ing : an attri bu te o r si n gl e v al u ed asso c i ati o n i s fetc h ed w h en th e i n stan c e v ari abl e i s
ac c essed. T h i s ap p ro ac h req u i res bu i l dti m e bytec o de i n stru m en tati o n an d i s rarel y n ec essary.
I f yo u set h ib ernate. def au l t_ b atch _ f etch _ s iz e , H i bern ate w i l l u se th e batc h fetc h o p ti m i z ati o n fo r l az y fetc h i n g.
T h i s o p ti m i z ati o n c an al so be en abl ed at a m o re gran u l ar l ev el .
P l ease be aw are th at ac c ess to a l az y asso c i ati o n o u tsi de o f th e c o n tex t o f an o p en H i bern ate sessi o n w ill
resu l t i n an ex c ep ti o n . F o r ex am p l e:
s = s es s io ns . o penSes s io n( );
T rans actio n tx = s . b eginT rans actio n( );
tx . co mmit( );
s . cl o s e( );
I nteger acces s L ev el = ( I nteger) permis s io ns . get( " acco u nts " ); / / E rro r!
1 9 . 1 . 2 . T u ni ng f e tc h s trate g i e s
1 8 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< many- to - o ne name=" mo th er" cl as s =" C at" f etch =" j o in" / >
1 9 . 1 . 3 . S i ng l e - e nd e d as s oc i ati on prox i e s
A t startu p , H i bern ate3 gen erates p ro x i es by defau l t fo r al l p ersi sten t c l asses an d u ses th em to en abl e l az y
fetc h i n g o f many- to - o ne an d o ne- to - o ne asso c i ati o n s.
1 9 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h ese p ro bl em s are al l du e to fu n dam en tal l i m i tati o n s i n J av a' s si n gl e i n h eri tan c e m o del . T o av o i d th ese
p ro bl em s yo u r p ersi sten t c l asses m u st eac h i m p l em en t an i n terfac e th at dec l ares i ts bu si n ess m eth o ds. Y o u
sh o u l d sp ec i fy th ese i n terfac es i n th e m ap p i n g fi l e w h ere C atI mpl i m p l em en ts th e i n terfac e C at an d
D o mes ticC atI mpl i m p l em en ts th e i n terfac e D o mes ticC at . F o r ex am p l e:
N ote
l is t( ) do es n o t u su al l y retu rn p ro x i es.
B y c h o o si n g l az y=" no - pro x y" i n stead o f th e defau l t l az y=" pro x y" , yo u c an av o i d p ro bl em s asso c i ated w i th
1 9 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
typ ec asti n g. H o w ev er, bu i l dti m e bytec o de i n stru m en tati o n i s req u i red, an d al l o p erati o n s w i l l resu l t i n
i m m edi ate p ro x y i n i ti al i z ati o n .
Y o u c an u se a c o l l ec ti o n fi l ter to get th e si z e o f a c o l l ec ti o n w i th o u t i n i ti al i z i n g i t:
( ( I nteger) s . createF il ter( co l l ectio n, " s el ect co u nt( *)" ). l is t( ). get( 0) ). intV al u e( )
s . createF il ter( l az yC o l l ectio n, " " ). s etF irs tR es u l t( 0). s etM ax R es u l ts ( 10). l is t( );
1 9 . 1 . 5 . U s i ng b atc h f e tc h i ng
1 9 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
i s m ap p ed w i th a p ro x y, l az y=" tru e" . I f yo u n o w i terate th ro u gh al l c ats an d c al l getO w ner( ) o n eac h , H i bern ate
w i l l , by defau l t, ex ec u te 25 SE L E C T statem en ts to retri ev e th e p ro x i ed o w n ers. Y o u c an tu n e th i s beh av i o r by
sp ec i fyi n g a b atch - s iz e i n th e m ap p i n g o f P ers o n:
< cl as s name=" P ers o n" b atch - s iz e=" 10" >. . . < / cl as s >
B atc h fetc h i n g o f c o l l ec ti o n s i s p arti c u l arl y u sefu l i f yo u h av e a n ested tree o f i tem s, i .e. th e typ i c al bi l l - o f-
m ateri al s p attern . H o w ev er, a ne s te d s e t o r a m ate rializ e d p ath m i gh t be a better o p ti o n fo r read- m o stl y
trees.
1 9 . 1 . 6 . U s i ng s u b s e l e c t f e tc h i ng
1 9 . 1 . 7 . U s i ng l az y prope rty f e tc h i ng
L az y p ro p erty l o adi n g req u i res bu i l dti m e bytec o de i n stru m en tati o n . I f yo u r p ersi sten t c l asses are n o t
en h an c ed, H i bern ate w i l l i gn o re l az y p ro p erty setti n gs an d retu rn to i m m edi ate fetc h i n g.
1 9 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< target name=" ins tru ment" depends =" co mpil e" >
< tas k def name=" ins tru ment" cl as s name=" o rg. h ib ernate. to o l . ins tru ment. I ns tru mentT as k " >
< cl as s path path =" $ {j ar. path }" / >
< cl as s path path =" $ {cl as s es . dir}" / >
< cl as s path ref id=" l ib . cl as s . path " / >
< / tas k def >
1 9 . 2 . T h e S econd L ev el C ach e
A H i bern ate Ses s io n i s a tran sac ti o n - l ev el c ac h e o f p ersi sten t data. I t i s p o ssi bl e to c o n fi gu re a c l u ster o r
J V M - l ev el (Ses s io nF acto ry- l ev el ) c ac h e o n a c l ass- by- c l ass an d c o l l ec ti o n - by- c o l l ec ti o n basi s. Y o u c an ev en
p l u g i n a c l u stered c ac h e. B e aw are th at c ac h es are n o t aw are o f c h an ges m ade to th e p ersi sten t sto re by
an o th er ap p l i c ati o n . T h ey c an , h o w ev er, be c o n fi gu red to regu l arl y ex p i re c ac h ed data.
Q u ery
Clu ster
Cac h e Prov id er c lass Ty pe Cac h e
S af e
S u pported
H ash tabl e
(n o t i n ten ded
o rg. h ib ernate. cach e. H as h tab l eC ach eP ro v ider m em o ry yes
fo r p ro du c ti o n
u se)
m em o ry,
E H Cac h e o rg. h ib ernate. cach e. E h C ach eP ro v ider yes
di sk
m em o ry,
O S Cac h e o rg. h ib ernate. cach e. O SC ach eP ro v ider yes
di sk
yes
c l u stered (i p
S w arm Cac h e o rg. h ib ernate. cach e. Sw armC ach eP ro v ider (c l u stered
m u l ti c ast)
i n v al i dati o n )
c l u stered (i p
J B o ss Cac h e yes yes (c l o c k
o rg. h ib ernate. cach e. T reeC ach eP ro v ider m u l ti c ast),
1 .x (rep l i c ati o n ) syn c req .)
tran sac ti o n al
yes
c l u stered (i p
J B o ss Cac h e (rep l i c ati o n yes (c l o c k
o rg. h ib ernate. cach e. j b c2 . J B o s s C ach eR egio nF acto ry m u l ti c ast),
2 o r syn c req .)
tran sac ti o n al
i n v al i dati o n )
1 9 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 9 . 2 . 1 . C ac h e mappi ng s
< cach e
u s age=" trans actio nal | read- w rite| no ns trict- read- w rite| read- o nl y"
regio n=" R egio nName"
incl u de=" al l | no n- l az y"
/ >
u s age (req u i red) sp ec i fi es th e c ac h i n g strategy: trans actio nal , read- w rite , no ns trict- read- w rite o r read- o nl y
A l tern ati v el y, yo u c an sp ec i fy < cl as s - cach e> an d < co l l ectio n- cach e> el em en ts i n h ib ernate. cf g. x ml .
1 9 . 2 . 2 . S trate g y : re ad onl y
< cl as s name=" eg. I mmu tab l e" mu tab l e=" f al s e" >
< cach e u s age=" read- o nl y" / >
....
< / cl as s >
1 9 . 2 . 3 . S trate g y : re ad / w ri te
1 9 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Important
N o n e o f th e c ac h e p ro v i ders su p p o rt al l o f th e c ac h e c o n c u rren c y strategi es.
nonstric t-
Cac h e read - only read - w rite transac tional
read - w rite
H ash tabl e (n o t i n ten ded fo r p ro du c ti o n
yes yes yes
u se)
E H Cac h e yes yes yes
O S Cac h e yes yes yes
S w arm Cac h e yes yes
J B o ss Cac h e 1 .x yes yes
J B o ss Cac h e 2 yes yes
1 9 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 9 . 4 . T h e Q uery C ach e
Q u ery resu l t sets c an al so be c ac h ed. T h i s i s o n l y u sefu l fo r q u eri es th at are ru n freq u en tl y w i th th e sam e
p aram eters. Y o u w i l l fi rst n eed to en abl e th e q u ery c ac h e:
1 9 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 9 . 5 . 1 . T ax onomy
c o l l ec ti o n s o f v al u es
o n e- to - m an y asso c i ati o n s
m an y- to - m an y asso c i ati o n s
i n dex ed c o l l ec ti o n s
sets
bags
A l l i n dex ed c o l l ec ti o n s (m ap s, l i sts, an d arrays) h av e a p ri m ary k ey c o n si sti n g o f th e < k ey> an d < index >
c o l u m n s. I n th i s c ase, c o l l ec ti o n u p dates are ex trem el y effi c i en t. T h e p ri m ary k ey c an be effi c i en tl y i n dex ed
an d a p arti c u l ar ro w c an be effi c i en tl y l o c ated w h en H i bern ate tri es to u p date o r del ete i t.
< idb ag> m ap p i n gs defi n e a su rro gate k ey, so th ey are effi c i en t to u p date. I n fac t, th ey are th e best c ase.
1 9 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
1 9 . 5 . 4 . O ne s h ot d e l e te
1 9 . 6 . M onitoring p erformance
O p ti m i z ati o n i s n o t m u c h u se w i th o u t m o n i to ri n g an d ac c ess to p erfo rm an c e n u m bers. H i bern ate p ro v i des a
fu l l ran ge o f fi gu res abo u t i ts i n tern al o p erati o n s. S tati sti c s i n H i bern ate are av ai l abl e p er Ses s io nF acto ry.
Y o u c an ac c ess Ses s io nF acto ry m etri c s i n tw o w ays. Y o u r fi rst o p ti o n i s to c al l s es s io nF acto ry. getStatis tics ( ) an d
read o r di sp l ay th e Statis tics yo u rsel f.
H i bern ate c an al so u se J M X to p u bl i sh m etri c s i f yo u en abl e th e Statis tics Serv ice M B ean . Y o u c an en abl e a
si n gl e M B ean fo r al l yo u r Ses s io nF acto ry o r o n e p er fac to ry. S ee th e fo l l o w i n g c o de fo r m i n i m al i sti c
c o n fi gu rati o n ex am p l es:
1 9 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
Statis tics Serv ice s tats = new Statis tics Serv ice( ); / / M B ean impl ementatio n
s tats . s etSes s io nF acto ry( s es s io nF acto ry); / / B ind th e s tats to a Ses s io nF acto ry
s erv er. regis terM B ean( s tats , o n); / / R egis ter th e M b ean o n th e s erv er
Statis tics Serv ice s tats = new Statis tics Serv ice( ); / / M B ean impl ementatio n
s erv er. regis terM B ean( s tats , o n); / / R egis ter th e M B ean o n th e s erv er
at ru n ti m e: s f . getStatis tics ( ). s etStatis tics E nab l ed( tru e) o r h ib ernateStats B ean. s etStatis tics E nab l ed( tru e)
S tati sti c s c an be reset p ro gram m ati c al l y u si n g th e cl ear( ) m eth o d. A su m m ary c an be sen t to a l o gger (i n fo
l ev el ) u si n g th e l o gSu mmary( ) m eth o d.
1 9 . 6 . 2 . M e tri c s
M etri c s rel ated to th e gen eral Ses s io n u sage, su c h as n u m ber o f o p en sessi o n s, retri ev ed J D B C
c o n n ec ti o n s, etc .
M etri c s rel ated to th e en ti ti es, c o l l ec ti o n s, q u eri es, an d c ac h es as a w h o l e (ak a gl o bal m etri c s).
D etai l ed m etri c s rel ated to a p arti c u l ar en ti ty, c o l l ec ti o n , q u ery o r c ac h e regi o n .
2 0 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 0 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 0 . 1 . 1 . C u s tomi z i ng th e s c h e ma
< pro perty name=" b al ance" precis io n=" 12 " s cal e=" 2 " / >
< many- to - o ne name=" b ar" co l u mn=" b arI d" no t- nu l l =" tru e" / >
< el ement co l u mn=" s erial Nu mb er" type=" l o ng" no t- nu l l =" tru e" u niq u e=" tru e" / >
< many- to - o ne name=" o rg" co l u mn=" o rgI d" u niq u e- k ey=" O rgE mpl o yeeI d" / >
< pro perty name=" empl o yeeI d" u niq u e- k ey=" O rgE mpl o yee" / >
< many- to - o ne name=" b ar" co l u mn=" b arI d" f o reign- k ey=" F K F o o B ar" / >
< pro perty name=" name" type=" my. cu s to mtypes . Name" / >
< co l u mn name=" l as t" no t- nu l l =" tru e" index =" b ar_ idx " l ength =" 30" / >
< co l u mn name=" f irs t" no t- nu l l =" tru e" index =" b ar_ idx " l ength =" 2 0" / >
< co l u mn name=" initial " / >
< / pro perty>
2 0 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< pro perty name=" credits " type=" integer" ins ert=" f al s e" >
< co l u mn name=" credits " def au l t=" 10" / >
< / pro perty>
< v ers io n name=" v ers io n" type=" integer" ins ert=" f al s e" >
< co l u mn name=" v ers io n" def au l t=" 0" / >
< / pro perty>
< cl as s name=" F o o " tab l e=" f o o s " ch eck =" b ar < 100. 0" >
...
< pro perty name=" b ar" type=" f l o at" / >
< / cl as s >
2 0 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 0 . 1 . 2 . R u nni ng th e tool
j av a - cp h ib e rnate _ c las s p ath s o rg. h ib ernate. to o l . h b m2 ddl . Sch emaE x po rt op tions m ap p ing _ f ile s
C o nf igu ratio n cf g = . . . . ;
new Sch emaE x po rt( cf g). create( f al s e, tru e);
2 0 . 1 . 3 . P rope rti e s
2 0 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 0 . 1 . 4 . U s i ng A nt
< s ch emaex po rt
pro perties =" h ib ernate. pro perties "
q u iet=" no "
tex t=" no "
dro p=" no "
del imiter=" ;"
o u tpu t=" s ch ema- ex po rt. s q l " >
< f il es et dir=" s rc" >
< incl u de name=" **/ *. h b m. x ml " / >
< / f il es et>
< / s ch emaex po rt>
< / target>
T h e Sch emaU pdate to o l w i l l u p date an ex i sti n g sc h em a w i th " i n c rem en tal " c h an ges. T h e Sch emaU pdate
dep en ds u p o n th e J D B C m etadata A P I an d, as su c h , w i l l n o t w o rk w i th al l J D B C dri v ers.
j av a - cp h ib e rnate _ c las s p ath s o rg. h ib ernate. to o l . h b m2 ddl . Sch emaU pdate op tions m ap p ing _ f ile s
C o nf igu ratio n cf g = . . . . ;
new Sch emaU pdate( cf g). ex ecu te( f al s e);
2 0 . 1 . 6 . U s i ng A nt f or i nc re me ntal s c h e ma u pd ate s
2 0 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 0 . 1 . 7 . S c h e ma v al i d ati on
T h e Sch emaV al idato r to o l w i l l v al i date th at th e ex i sti n g database sc h em a " m atc h es" yo u r m ap p i n g do c u m en ts.
T h e Sch emaV al idato r dep en ds h eav i l y u p o n th e J D B C m etadata A P I an d, as su c h , w i l l n o t w o rk w i th al l J D B C
dri v ers. T h i s to o l i s ex trem el y u sefu l fo r testi n g.
j av a - cp h ib e rnate _ c las s p ath s o rg. h ib ernate. to o l . h b m2 ddl . Sch emaV al idato r op tions m ap p ing _ f ile s
C o nf igu ratio n cf g = . . . . ;
new Sch emaV al idato r( cf g). v al idate( );
2 0 . 1 . 8 . U s i ng A nt f or s c h e ma v al i d ati on
2 0 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
21 .1 . A n o te abo u t c o l l ec ti o n s
21 .2. B i di rec ti o n al o n e- to - m an y
21 .3 . Casc adi n g l i fe c yc l e
21 .4 . Casc ades an d u ns av ed- v al u e
21 .5 . Co n c l u si o n
O n e o f th e fi rst th i n gs th at n ew u sers w an t to do w i th H i bern ate i s to m o del a p aren t/c h i l d typ e rel ati o n sh i p .
T h ere are tw o di fferen t ap p ro ac h es to th i s. T h e m o st c o n v en i en t ap p ro ac h , esp ec i al l y fo r n ew u sers, i s to
m o del bo th P arent an d C h il d as en ti ty c l asses w i th a < o ne- to - many> asso c i ati o n fro m P arent to C h il d. T h e
al tern ati v e ap p ro ac h i s to dec l are th e C h il d as a < co mpo s ite- el ement>. T h e defau l t sem an ti c s o f a
o n e- to - m an y asso c i ati o n i n H i bern ate are m u c h l ess c l o se to th e u su al sem an ti c s o f a p aren t/c h i l d
rel ati o n sh i p th an th o se o f a c o m p o si te el em en t m ap p i n g. W e w i l l ex p l ai n h o w to u se a b id ire c tional
one - to- m any as s oc iation w ith c as c ad e s to m o del a p aren t/c h i l d rel ati o n sh i p effi c i en tl y an d el egan tl y.
I f w e w ere to ex ec u te th e fo l l o w i n g c o de:
P arent p = . . . . . ;
C h il d c = new C h il d( );
p. getC h il dren( ). add( c);
s es s io n. s av e( c);
s es s io n. f l u s h ( );
2 0 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< many- to - o ne name=" parent" co l u mn=" parent_ id" no t- nu l l =" tru e" / >
T h e fo l l o w i n g c o de w o u l d be u sed to add a n ew C h il d:
pu b l ic v o id addC h il d( C h il d c) {
c. s etP arent( th is );
ch il dren. add( c);
}
T h e c o de to add a C h il d l o o k s l i k e th i s:
< s et name=" ch il dren" inv ers e=" tru e" cas cade=" al l " >
< k ey co l u mn=" parent_ id" / >
< o ne- to - many cl as s =" C h il d" / >
< / s et>
T h i s si m p l i fi es th e c o de abo v e to :
2 0 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
H o w ev er, th e fo l l o w i n g c o de:
< s et name=" ch il dren" inv ers e=" tru e" cas cade=" al l - del ete- o rph an" >
< k ey co l u mn=" parent_ id" / >
< o ne- to - many cl as s =" C h il d" / >
< / s et>
E v en th o u gh th e c o l l ec ti o n m ap p i n g sp ec i fi es inv ers e=" tru e" , c asc ades are sti l l p ro c essed by i terati n g th e
c o l l ec ti o n el em en ts. I f yo u n eed an o bj ec t be sav ed, del eted o r u p dated by c asc ade, yo u m u st add i t to th e
c o l l ec ti o n . I t i s n o t en o u gh to si m p l y c al l s etP arent( ).
2 1 . 4 . C as cad es and u ns av ed - v al u e
S u p p o se w e l o aded u p a P arent i n o n e Ses s io n, m ade so m e c h an ges i n a U I ac ti o n an d w an ted to p ersi st th ese
c h an ges i n a n ew sessi o n by c al l i n g u pdate( ). T h e P arent w i l l c o n tai n a c o l l ec ti o n o f c h i l dren an d, si n c e th e
c asc adi n g u p date i s en abl ed, H i bern ate n eeds to k n o w w h i c h c h i l dren are n ew l y i n stan ti ated an d w h i c h
rep resen t ex i sti n g ro w s i n th e database. W e w i l l al so assu m e th at bo th P arent an d C h il d h av e gen erated
i den ti fi er p ro p erti es o f typ e L o ng. H i bern ate w i l l u se th e i den ti fi er an d v ersi o n /ti m estam p p ro p erty v al u e to
determ i n e w h i c h o f th e c h i l dren are n ew . (S ee S ec ti o n 1 0 .7 , “ A u to m ati c state detec ti o n ” .) I n H ib e rnate 3 , it is
no long e r ne c e s s ary to s p e c if y an u ns av ed - v alu e e x p lic itly .
2 0 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
T h i s m ay be su i tabl e fo r th e c ase o f a gen erated i den ti fi er, bu t w h at abo u t assi gn ed i den ti fi ers an d c o m p o si te
i den ti fi ers? T h i s i s m o re di ffi c u l t, si n c e H i bern ate c an n o t u se th e i den ti fi er p ro p erty to di sti n gu i sh betw een a
n ew l y i n stan ti ated o bj ec t, w i th an i den ti fi er assi gn ed by th e u ser, an d an o bj ec t l o aded i n a p rev i o u s sessi o n .
I n th i s c ase, H i bern ate w i l l ei th er u se th e ti m estam p o r v ersi o n p ro p erty, o r w i l l ac tu al l y q u ery th e
sec o n d- l ev el c ac h e o r, w o rst c ase, th e database, to see i f th e ro w ex i sts.
2 1 . 5 . C oncl us ion
T h e sec ti o n s w e h av e j u st c o v ered c an be a bi t c o n fu si n g. H o w ev er, i n p rac ti c e, i t al l w o rk s o u t n i c el y. M o st
H i bern ate ap p l i c ati o n s u se th e p aren t/c h i l d p attern i n m an y p l ac es.
2 2 . 1 . P ers is tent C l as s es
T h e p ersi sten t c l asses h ere rep resen t a w ebl o g an d an i tem p o sted i n a w ebl o g. T h ey are to be m o del l ed as a
stan dard p aren t/c h i l d rel ati o n sh i p , bu t w e w i l l u se an o rdered bag, i n stead o f a set:
impo rt j av a. u til . L is t;
pu b l ic cl as s B l o g {
priv ate L o ng _ id;
priv ate String _ name;
priv ate L is t _ items ;
pu b l ic L o ng getI d( ) {
retu rn _ id;
}
pu b l ic L is t getI tems ( ) {
retu rn _ items ;
}
pu b l ic String getName( ) {
retu rn _ name;
}
pu b l ic v o id s etI d( L o ng l o ng1) {
_ id = l o ng1;
}
pu b l ic v o id s etI tems ( L is t l is t) {
_ items = l is t;
}
pu b l ic v o id s etName( String s tring) {
_ name = s tring;
}
}
2 1 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
pu b l ic cl as s B l o gI tem {
priv ate L o ng _ id;
priv ate C al endar _ datetime;
priv ate String _ tex t;
priv ate String _ titl e;
priv ate B l o g _ b l o g;
pu b l ic B l o g getB l o g( ) {
retu rn _ b l o g;
}
pu b l ic C al endar getD atetime( ) {
retu rn _ datetime;
}
pu b l ic L o ng getI d( ) {
retu rn _ id;
}
pu b l ic String getT ex t( ) {
retu rn _ tex t;
}
pu b l ic String getT itl e( ) {
retu rn _ titl e;
}
pu b l ic v o id s etB l o g( B l o g b l o g) {
_ b l o g = b l o g;
}
pu b l ic v o id s etD atetime( C al endar cal endar) {
_ datetime = cal endar;
}
pu b l ic v o id s etI d( L o ng l o ng1) {
_ id = l o ng1;
}
pu b l ic v o id s etT ex t( String s tring) {
_ tex t = s tring;
}
pu b l ic v o id s etT itl e( String s tring) {
_ titl e = s tring;
}
}
2 2 . 2 . Hibernate M ap p ing s
T h e X M L m ap p i n gs are n o w strai gh tfo rw ard. F o r ex am p l e:
2 1 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< cl as s
name=" B l o g"
tab l e=" B L O G S" >
< id
name=" id"
co l u mn=" B L O G _ I D " >
< / id>
< b ag
name=" items "
inv ers e=" tru e"
o rder- b y=" D A T E _ T I M E "
cas cade=" al l " >
< / b ag>
< / cl as s >
2 1 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< cl as s
name=" B l o gI tem"
tab l e=" B L O G _ I T E M S"
dynamic- u pdate=" tru e" >
< id
name=" id"
co l u mn=" B L O G _ I T E M _ I D " >
< / id>
< many- to - o ne
name=" b l o g"
co l u mn=" B L O G _ I D "
no t- nu l l =" tru e" / >
< / cl as s >
2 2 . 3 . Hibernate C od e
T h e fo l l o w i n g c l ass dem o n strates so m e o f th e k i n ds o f th i n gs w e c an do w i th th ese c l asses u si n g H i bern ate:
2 1 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
pu b l ic cl as s B l o gM ain {
B l o g b l o g = new B l o g( );
b l o g. s etName( name);
b l o g. s etI tems ( new A rrayL is t( ) );
Ses s io n s es s io n = _ s es s io ns . o penSes s io n( );
T rans actio n tx = nu l l ;
try {
tx = s es s io n. b eginT rans actio n( );
s es s io n. pers is t( b l o g);
tx . co mmit( );
}
catch ( H ib ernateE x ceptio n h e) {
if ( tx ! =nu l l ) tx . ro l l b ack ( );
th ro w h e;
}
f inal l y {
s es s io n. cl o s e( );
}
retu rn b l o g;
}
2 1 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 3 . 1 . E mp l oy er/ E mp l oy ee
T h e fo l l o w i n g m o del o f th e rel ati o n sh i p betw een E mpl o yer an d E mpl o yee u ses an en ti ty c l ass (E mpl o yment ) to
rep resen t th e asso c i ati o n . Y o u c an do th i s w h en th ere m i gh t be m o re th an o n e p eri o d o f em p l o ym en t fo r th e
sam e tw o p arti es. Co m p o n en ts are u sed to m o del m o n etary v al u es an d em p l o yee n am es.
H ere i s a p o ssi bl e m ap p i n g do c u m en t:
2 1 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< cl as s name=" E mpl o yer" tab l e=" empl o yers " >
< id name=" id" >
< generato r cl as s =" s eq u ence" >
< param name=" s eq u ence" >empl o yer_ id_ s eq < / param>
< / generato r>
< / id>
< pro perty name=" name" / >
< / cl as s >
< cl as s name=" E mpl o yment" tab l e=" empl o yment_ perio ds " >
< many- to - o ne name=" empl o yer" co l u mn=" empl o yer_ id" no t- nu l l =" tru e" / >
< many- to - o ne name=" empl o yee" co l u mn=" empl o yee_ id" no t- nu l l =" tru e" / >
< / cl as s >
< cl as s name=" E mpl o yee" tab l e=" empl o yees " >
< id name=" id" >
< generato r cl as s =" s eq u ence" >
< param name=" s eq u ence" >empl o yee_ id_ s eq < / param>
< / generato r>
< / id>
< pro perty name=" tax f il eNu mb er" / >
< co mpo nent name=" name" cl as s =" Name" >
< pro perty name=" f irs tName" / >
< pro perty name=" initial " / >
< pro perty name=" l as tName" / >
< / co mpo nent>
< / cl as s >
2 1 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 1 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< cl as s name=" W o rk " tab l e=" w o rk s " dis criminato r- v al u e=" W " >
< / cl as s >
< s et name=" w o rk s " tab l e=" au th o r_ w o rk " inv ers e=" tru e" >
< k ey co l u mn=" au th o r_ id" / >
< many- to - many cl as s =" W o rk " co l u mn=" w o rk _ id" / >
< / s et>
< / cl as s >
2 1 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
create tab l e w o rk s (
id B I G I NT no t nu l l generated b y def au l t as identity,
tempo F L O A T ,
genre V A R C H A R ( 2 55),
tex t I NT E G E R ,
titl e V A R C H A R ( 2 55),
type C H A R ( 1) no t nu l l ,
primary k ey ( id)
)
create tab l e au th o r_ w o rk (
au th o r_ id B I G I NT no t nu l l ,
w o rk _ id B I G I NT no t nu l l ,
primary k ey ( w o rk _ id, au th o r_ id)
)
create tab l e au th o rs (
id B I G I NT no t nu l l generated b y def au l t as identity,
al ias V A R C H A R ( 2 55),
primary k ey ( id)
)
al ter tab l e au th o rs
add co ns traint au th o rs F K 0 f o reign k ey ( id) ref erences pers o ns
al ter tab l e au th o r_ w o rk
add co ns traint au th o r_ w o rk F K 0 f o reign k ey ( au th o r_ id) ref erences au th o rs
al ter tab l e au th o r_ w o rk
add co ns traint au th o r_ w o rk F K 1 f o reign k ey ( w o rk _ id) ref erences w o rk s
T h e m ap p i n g do c u m en t w i l l l o o k l i k e th i s:
2 1 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
cu s to mers , o rders , l ine_ items an d pro du cts h o l d c u sto m er, o rder, o rder l i n e i tem an d p ro du c t data resp ec ti v el y.
l ine_ items al so ac ts as an asso c i ati o n tabl e l i n k i n g o rders w i th p ro du c ts.
2 2 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 2 1 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 3 . 4 . 2 . C ompos i te k e y e x ampl e
2 2 2 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< pro perty name=" name" no t- nu l l =" tru e" l ength =" 100" / >
< pro perty name=" addres s " no t- nu l l =" tru e" l ength =" 2 00" / >
< / cl as s >
< cl as s name=" O rder" tab l e=" C u s to merO rder" l az y=" tru e" >
< s ynch ro niz e tab l e=" L ineI tem" / >
< s ynch ro niz e tab l e=" P ro du ct" / >
2 2 3 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 3 . 4 . 4 . C onte nt b as e d d i s c ri mi nati on
2 2 4 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< / cl as s >
2 2 5 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
< pro perty name=" u s erI d" l ength =" 8" / >
< / cl as s >
< pro perty name=" addres s " l ength =" 300" / >
< pro perty name=" z ip" l ength =" 5" / >
< pro perty name=" co u ntry" l ength =" 2 5" / >
< many- to - o ne name=" pers o n" u niq u e=" tru e" no t- nu l l =" tru e" / >
< / cl as s >
< / cl as s >
C h ap ter 2 4 . B es t P ractices
W ri te fi n e- grai n ed c l asses an d m ap th em u si n g < co mpo nent>:
U se an A ddres s c l ass to en c ap su l ate s treet , s u b u rb , s tate, po s tco de. T h i s en c o u rages c o de reu se an d
si m p l i fi es refac to ri n g.
2 2 6 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
P l ac e eac h c l ass m ap p i n g i n i ts o w n fi l e:
D o n o t u se a si n gl e m o n o l i th i c m ap p i n g do c u m en t. M ap co m. eg. F o o in th e fi l e co m/ eg/ F o o . h b m. x ml . T h i s
m ak es sen se, p arti c u l arl y i n a team en v i ro n m en t.
L o ad m ap p i n gs as reso u rc es:
D ep l o y th e m ap p i n gs al o n g w i th th e c l asses th ey m ap .
D o n o t m an age yo u r o w n J D B C c o n n ec ti o n s:
H i bern ate al l o w s th e ap p l i c ati o n to m an age J D B C c o n n ec ti o n s, bu t h i s ap p ro ac h sh o u l d be c o n si dered a
l ast- reso rt. I f yo u c an n o t u se th e bu i l t- i n c o n n ec ti o n p ro v i ders, c o n si der p ro v i di n g yo u r o w n
i m p l em en tati o n o f o rg. h ib ernate. co nnectio n. C o nnectio nP ro v ider.
U se h an d- c o ded J D B C i n bo ttl en ec k s:
I n p erfo rm an c e- c ri ti c al areas o f th e system , so m e k i n ds o f o p erati o n s m i gh t ben efi t fro m di rec t J D B C. D o
n o t assu m e, h o w ev er, th at J D B C i s n ec essari l y faster. P l ease w ai t u n ti l yo u k now so m eth i n g i s a
bo ttl en ec k . I f yo u n eed to u se di rec t J D B C, yo u c an o p en a H i bern ate Ses s io n an d u si n gfi l e:///u sr/sh are
/do c /H T M L /en - U S /i n dex .h tm l th at J D B C c o n n ec ti o n . T h i s w ay yo u c an sti l l u se th e sam e tran sac ti o n
strategy an d u n derl yi n g c o n n ec ti o n p ro v i der.
U n derstan d Ses s io n fl u sh i n g:
S o m eti m es th e S essi o n syn c h ro n i z es i ts p ersi sten t state w i th th e database. P erfo rm an c e w i l l be affec ted
i f th i s p ro c ess o c c u rs to o o ften . Y o u c an so m eti m es m i n i m i z e u n n ec essary fl u sh i n g by di sabl i n g au to m ati c
fl u sh i n g, o r ev en by c h an gi n g th e o rder o f q u eri es an d o th er o p erati o n s w i th i n a p arti c u l ar tran sac ti o n .
2 2 7 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
2 5 . 2 . Dial ect
T h e fi rst lin e o f p o rtabi l i ty fo r H i bern ate is th e di al ec t, w h ic h is a sp ec i al i z ati o n o f th e
2 2 8 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
o rg. h ib ernate. dial ect. D ial ect c o n trac t. A di al ec t en c ap su l ates al l th e di fferen c es i n h o w H i bern ate m u st
c o m m u n i c ate w i th a p arti c u l ar database to ac c o m p l i sh so m e task l i k e getti n g a seq u en c e v al u e o r stru c tu ri n g
a S E L E CT q u ery. H i bern ate bu n dl es a w i de ran ge o f di al ec ts fo r m an y o f th e m o st p o p u l ar databases. I f yo u
fi n d th at yo u r p arti c u l ar database i s n o t am o n g th em , i t i s n o t terri bl y di ffi c u l t to w ri te yo u r o w n .
pu b l ic D ial ect res o l v eD ial ect( D atab as eM etaD ata metaD ata) th ro w s J D B C C o nnectio nE x ceptio n
. T h e basi c c o n trac t h ere i s th at i f th e reso l v er ' u n derstan ds' th e gi v en database m etadata th en i t retu rn s th e
c o rresp o n di n g D i al ec t; i f n o t i t retu rn s n u l l an d th e p ro c ess c o n ti n u es to th e n ex t reso l v er. T h e si gn atu re al so
i den ti fi es o rg. h ib ernate. ex ceptio n. J D B C C o nnectio nE x ceptio n as p o ssi bl y bei n g th ro w n . A
J D B CCo n n ec ti o n E x c ep ti o n h ere i s i n terp reted to i m p l y a " n o n tran si en t" (ak a n o n - rec o v erabl e) c o n n ec ti o n
p ro bl em an d i s u sed to i n di c ate an i m m edi ate sto p to reso l u ti o n attem p ts. A l l o th er ex c ep ti o n s resu l t i n a
w arn i n g an d c o n ti n u i n g o n to th e n ex t reso l v er.
T h e c o o l p art abo u t th ese reso l v ers i s th at u sers c an al so regi ster th ei r o w n c u sto m reso l v ers w h i c h w i l l be
p ro c essed ah ead o f th e bu i l t- i n H i bern ate o n es. T h i s m i gh t be u sefu l i n a n u m ber o f di fferen t si tu ati o n s: i t
al l o w s easy i n tegrati o n fo r au to - detec ti o n o f di al ec ts beyo n d th o se sh i p p ed w i th H I bern ate i tsel f; i t al l o w s
yo u to sp ec i fy to u se a c u sto m di al ec t w h en a p arti c u l ar database i s rec o gn i z ed; etc . T o regi ster o n e o r m o re
reso l v ers, si m p l y sp ec i fy th em (sep erated by c o m m as, tabs o r sp ac es) u si n g th e ' h i bern ate.di al ec t_ reso l v ers'
c o n fi gu rati o n setti n g (see th e D I A L E C T _ R E SO L V E R S c o n stan t o n o rg. h ib ernate. cf g. E nv iro nment ).
2 5 . 4 . I d entifier g eneration
W h en c o n si deri n g p o rtabi l i ty betw een databases, an o th er i m p o rtan t dec i si o n i s sel ec ti n g th e i den ti fi er
gen erati o n stratagy yo u w an t to u se. O ri gi n al l y H i bern ate p ro v i ded th e nativ e gen erato r fo r th i s p u rp o se,
w h i c h w as i n ten ded to sel ec t betw een a s e q ue nc e , id e ntity , o r tab le strategy dep en di n g o n th e c ap abi l i ty o f
th e u n derl yi n g database. H o w ev er, an i n si di o u s i m p l i c ati o n o f th i s ap p ro ac h c o m es abo u t w h en targtetti n g
so m e databases w h i c h su p p o rt id e ntity gen erati o n an d so m e w h i c h do n o t. id e ntity gen erati o n rel i es o n th e
S Q L defi n i ti o n o f an I D E N T I T Y (o r au to - i n c rem en t) c o l u m n to m an age th e i den ti fi er v al u e; i t i s w h at i s k n o w n
as a p o st- i n sert gen erati o n strategy bec au ase th e i n sert m u st ac tu al l y h ap p en befo re w e c an k n o w th e
i den ti fi er v al u e. B ec au se H i bern ate rel i es o n th i s i den ti fi er v al u e to u n i q u el y referen c e en ti ti es w i th i n a
p ersi sten c e c o n tex t i t m u st th en i ssu e th e i n sert i m m edi atel y w h en th e u sers req u ests th e en ti ti y be
asso c i ated w i th th e sessi o n (l i k e v i a sav e() e.g.) regardl ess o f c u rren t tran sac ti o n al sem an ti c s.
N ote
H i bern ate w as c h an ged sl i gh tl y o n c e th e i m p l i c ati o n o f th i s w as better u n dersto o d so th at th e
i n sert i s del ayed i n c ases w h ere th at i s feasi bl e.
S tarti n g w i th v ersi o n 3 .2.3 , H i bern ate c o m es w i th a set o f en h an c ed i den ti fi er gen erato rs targetti n g
p o rtabi l i ty i n a m u c h di fferen t w ay.
2 2 9 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM
HIBERNATE - Relational Persistence for Idiomatic Java h ttp : / / docs. j b oss. org / h ib ernate/ core/ 3 . 3 / reference/ en-U S / h tml_ sing le/
N ote
T h ere are sp ec i fi c al l y 2 bu n dl ed e nh anc e d gen erato rs:
T h e i dea beh i n d th ese gen erato rs i s to p o rt th e ac tu al sem an ti c s o f th e i den ti fer v al u e gen erati o n to th e
di fferen t databases. F o r ex am p l e, th e o rg. h ib ernate. id. enh anced. Seq u enceStyl eG enerato r m i m i c s th e beh av i o r o f
a seq u en c e o n databases w h i c h do n o t su p p o rt seq u en c es by u si n g a tabl e.
2 5 . 5 . Databas e functions
W arni ng
T h i s i s an area i n H i bern ate i n n eed o f i m p ro v em en t. I n term s o f p o rtabi l i ty c o n c ern s, th i s
fu n c ti o n h an dl i n g c u rren tl y w o rk s p retty w el l fro m H Q L ; h o w ev er, i t i s q u i te l ac k i n g i n al l o th er
asp ec ts.
Important
T ec h n i c al l y th i s fu n c ti o n regi strati o n is h an dl ed th ro u gh th e
o rg. h ib ernate. dial ect. f u nctio n. SQ L F u nctio nR egis try c l ass w h i c h i s i n ten ded to al l o w u sers to p ro v i de
c u sto m fu n c ti o n defi n i ti o n s w i th o u t h av i n g to p ro v i de a c u sto m di al ec t. T h i s sp ec i fi c beh av i o r i s
n o t fu l l y c o m p l eted as o f yet.
I t i s so rt o f i m p l em en ted su c h th at u sers c an p ro gram ati c al l y regi ster fu n c ti o n s w i th th e
o rg. h ib ernate. cf g. C o nf igu ratio n an d th o se fu n c ti o n s w i l l be rec o gn i z ed fo r H Q L .
2 5 . 6 . T y p e map p ing s
T h i s sec ti o n sc h edu l ed fo r c o m p l eti o n at a l ater date...
References
[P o E A A ] P atte rns of E nte rp ris e A p p lic ation A rc h ite c ture . 0 - 3 21 - 1 27 4 2- 0 . by M arti n F o w l er. Co p yri gh t © 20 0 3
P earso n E du c ati o n , I n c .. A ddi so n - W esl ey P u bl i sh i n g Co m p an y.
2 3 0 of 2 3 0 1 1 / 1 6 / 2 0 0 9 5 : 1 0 PM