what so mapping?
‘The process of linking DB tables with java lasas(80 /Entiy/
‘Model clases) and Db table cols withthe Properties of
«lasses and having synchorinization b/w them i called
Ide spring abe persnstence loge
SQL Queries based persisinace logic
= Soitis Db s/w dependent.
=> Changing DB s/w in the middle of
coco development or productions complex
mere “> Chaging Db s/w in Development to
Testing => UAT => Productionis
‘complex.
=> synchrozation means the modification done in
objects of java asses will reflect to Db tables records
acacia Tocvercome problem use o-r mapping
Persistence logiegiven by PA speciation
and implemented through ORM F/ws like
hibernate itbatiselipse link and et.
‘Hen class/Mode| class/80 cassava bear)
public class Employee
private int eno;
private string enamé
sun Me
private String eadd; {PA specification |
fos econ dines
Iter 8 ont a forormappingpesstence
fimplover-tab (ab table) Ce
tp ename ead eps ~~
' “Ane Aes vendor MRR) vend Vendors
Java App fr . ce hibernate ‘ibatis
(ormapoing weitence / ee
(lezmaeping weston / 65 leamesn zag pho) form tw) est orm Ta (orm ta]
Citas 7 ai
ine
Pr
sia o
hiberante App/_ —
notes: O-r Mapping persstnece lle objects based
Itbatis App/ Eclipse lnk App/ =)
persistnece logic without using ay SQL Queries. So this
Persistence logic Db s/w indepednent Persistence logic
Persistence loge portable across the multiple Db s/ws
Java App
‘o-rmapping peraptence logic aon
‘pitisnot another 0- framework
‘ORNeramewo}k (ike Hibenrtte)
itis spring module proving abstraction on multiple ORM
frameworks like hibernate, itBatis and etc. to simplity objects
based O-r mapping Persitnece log.
at supplies multiple Template classes like HibernateTemplate
‘oplinkTemplate and etc. to avoid boller plate code of or
‘mapping persistence logic
Cor)
Plain Hibernate Code (Appi)
2) Create Configuration object (To activate Hb t/w)
1b] Create Session factory object {Common logics)
6) Create Sesion obj
4) begin Tx
€)_Persistnece operation code] App specific
logic
‘commit froliback Tx {Common [Th# code that repeats across the multiple applications
1) close session /session factory objects| “pny” either with nochange or wit change iclled boilerplate
code]
note:: Commlogis = boilerplate code.-
/ORM code having integration with hibernate) ‘o-r mapping Persistghe logic
{2} Create/injec HibernateTemplate class obj
b) Perform Persinece operations
Spring core Spring
te)
note:: No_boilerplace code problema.Spring ORM Advantages
{a) Avoids boilter plate code by supplying Template classes
(b) Common exce
n handling (we need not to handle ORM f/w specific exceptions
we need to catch and handle the common DataAccessExcepion)
note: spring jdbc, spring orm , spring data modules throw common exceptions
like DataAccessException class hierarchy classes
(c) Persistence logic is portable acorss the multiple Db s/ws.. and Entity classes are portable across the
multiple ORM frameworks
(4) Common Transai
n Management support
{e) Common single row methods calls (given by JPA) and
common JPQL (Java Persitence Query Language)
and etc.
Spring with Hibernate
=>it says write B.logic in spring and write peristence logic in hibernate by taking the
support of the Spring ORM supplied HibernateTemplate class
given basaed Template method Design Pattern which say
that it defines a algorithm where super class/common class
will take care commong logic by leaving specific logics
to sub classes/developers to supply.
DA class
ibernateTemplate obj
|—>SessionFactory object (dependent) (SessionFactory obj multiple services of hibernate)
120s fein Sourcesbiect)
|—> Entity classes with annotations/ mapping files
| cohtnernate poles raeay "RR fold)
dialect (capable generating SQL queries)
2.show_sql (to see SOL queries on the console)
3.hbm2ddl.auto :: validate(default),create, update(best), create-drop
creates the db tables if they are not
4 available, uses them if ther are alredy
approaches availabe. alters them by adding new
cols if neccessary.
(a) Using xml driven cfgs (Cfg both user-defined and pre-defined classes using xml)
) Using xml +annotation driven gs user-define calsses using annotations
and pre-defined clases using xml)
A) 100% Code driven efgs (user-defined classes uisng annotations and
pre-defined classes using @Bean methods in @Configuration class)
we can develop Spring ORM Apps.
Ma Using spring boot cfgs (User-defind classes using annotation and
prefined classes using @Bean methods if at all there
are not coming through auto configuration)
impotant spring Annotations for layered Apps
@Component ~>To make java class as spring bean with no specialties
@Service —>To make java class as sprign bean cum service class
(support TxMgmt)
@Controller —-> To make java class as spring bean cum controller
@Repository —-> To make java class as spring bean cum DAO class
(With Exception
transalation support)
Entity classes with Annotations
Annotations in Entity classes
Basic Annotations
@Entity (JPA) (mandatory) a) JPA Annotations
@Table (JPA) (optional) b) Hibernate Annotations
@id (JPA) (mandatory) ¢) Java Config annotataions (JSE JE modules)
@Colulmn (JPA) (optional) d) Third party annotations
@Type (HB) (optional)
if Entity class name is matching with DB table name and Entity properites are
matching with Db table col names then placing @Table, @Column annotaitons
optional... if want to use dynamic schema generation (db tables generations }
itis remcomanded to place them to controll on type, length , unique and etc..
details..
@tntity To supprt Disk caching of
@Table(name="STUDENT") o. HB second Level cache
public class Student implements Serializable{
eid .
a) —>makes this property related colum as.
‘@Typettypesine") pk column .. while creating Db table..
private Integer sno;
‘@Colurnn(name="SNAME" length="20" nullable-false)
@Type(type="string")
pxleste Sri suaene, ‘To support java numberic data type ranges while creating
‘@Colurnn(name="SADD" length="20") length attribute values will not be
@Typeltype="string")
private String sad; in Db table creation., but string cots the length will be
@Column(name="AVG") ratazeeds
eTypeltype="tloat”)
priv Float ave; (The above operations will not take class. if u are working
getters && getters already available Db tables)
beans >
Factory Bean that gives HB SessionFactory obj
_>7 as Resultant obj based on the given injected
Ter cuit cciaiay igs = values like DS, HB Properties ane tc...
Internal cache of IOC containe!
( > kos inal
com.nt.entity.Student sesfact SessionFac
template Hibernate
org hibernate. dialect. Oracle 10gDialecttrue
libernate Template class cfg—>
Bulk Operations in Hiberante
erate Query Languaeé{/sPaL (Java Perssitence Query language)
b) Native SQL
c) Criteria API
To execute HQL/JPQL Select Queries we use find(), findXxx() methods on
HiberanteTemplate class object.. similary for non-select queries use bulkUpdate(-) method
SQL ---> DB s/w dependent Queries --> writtern by using Db tablename, col names.
HQL/JPQL --> DB s/w independent queries -->Written Using Entity class name and
its Properties
HB dialect converts HQL/JPQL queries into underlying DB s/w SQL queries..
=>upto hb 5.1 HOL/JPQL supports both named(:) and positional params(?) in the
HQL/JPQL queries.. but from hiberaten 5.2 we have support only for named Parameters..
So most of the find() findXxx() that supports positional params are deprecated in
HiberanteTemplate class.. (they did this work in support to spring data jpa )
ListDTOs List Object (List Entities)eto of Crushepestont)
1. ental oty)
“Sar meta pertornr sve ob operation if given vale recorded present
inthe 08 abi oterwise kw prom vate span. Spring Ota ther
sepente up| method we ned tue al) the elo epee aber peraton
eoverise
cn regteremploye(empoyecOT to {
‘eam aioe
Letoyee entre tmpoyet: ‘is meting ream eniy bet,
‘ants copyrapertit, representing the record ht
Tse emotene Inert
‘Pteypoye erty -empRapo soley)
tatu ery sth") “i mat nena Ha
‘ium empropesoelaiyhgstna); serene) rato over he ear ot
) ‘kinret metodo update eco
2c nt rics nin)
"Sir Becher ocr: Tose recht dh abl, stead of exacting “het SOL
“Quer fr mattis by aig mre network ound" wecang for batch ero where
‘plese SL ucts with ferent aes wi e hg Sle batch and at bth wl Be se
{be stoncetoredce network ound op aed De! Be ee
escort servo, poup regain a et eran mone ne
esa Toren 10 eke etc. ner sa quer for 10 tes having ferent
‘ahr ow by ona neato ute tener fn spp ands foro
‘we do sma operon by sang htc narton heater wb ado
pic nt vesermpoyeesroplstcempoye0TO> 0}
“eae OTe Ura neddlseveecoghetmplyeesruplstefren Enolyee0TF ana 900001,
{Uscemplyee> sence ew Enplyeet TO oa 2000.0,
‘rotors et employs sede 7000
Chloe eny-ew Employes 7 ,
BeanticopyPaprieea ey
bemoan ‘Strout pitino eve ai re vA testing
Tse enone
sci, ORE emer
estore ved ects
‘nee ey te
‘een ay ae
‘sty eth gto:
’
Mineo
a 5) po ne ou)
erie ee nce op
‘eoveride Sem ot pr trloyees outer getmplyeestount
uc ong ettpopescout {
iceman
‘ta empep com
1
4) pub boon xs 0
‘ech whee eerd seve nat boson he en ae,
5 patie voi dle)
‘cre econ etd en th en Hae.
ade ncn Ap
‘eoverice a
sting meen ‘sytmcueprntniece emowatmplyseb 2:
lpn exis) (
masepa derby
‘et te” employee dled";
,
ret ie” employee nt dete
,
0 {oon yet
‘aptonl op gen fom Jes
ade ani case ‘cs camaro ingore, ond
——— eal chec wate te ob
pb OponlmpleyeeDIO> getempoyetiit i} {hat espe Ths canbe Maleking wit Uhrig the hone
ioe orate ‘oting halrotracpton
pene np “
a
ct o90T0-Getntemend;
ffopt Present fe
he nooo
ese ery
Ente yp ge
Employee to-new Empoyeotop; | Sedetmeten ape
eawtscapyroperesersysok | OptonakenpoysebTO> oTOnrve.gttmpoyety22}
monoptonst of oporolnresend)
=—_—_— System piti'22 employee cr "spo gt
inet syemet tn spoyn nt foue”Wong th pay
‘ibe ney)
les ne
"aenlqumentneptn-Incaa the pen ny
ran terran sym pence
ny pain ab
Termeni
en
sont
atures at oe ode’
,
Vinaoos
‘ie wis coe ti tanner ds
Sossamon, | Pt ne
"Senumectncepton- cae thee emer on tem
pc nla
Pac bic eoro> eine —
‘he ase ‘Sa guns aston up
heres etaenpoye or Spm Ae
pile Seingraeetneoyeesyonetrs-enploeenr>iaroy( | SABE
‘icone tovorebtrter
et
Uinta sine Ane, ‘Semaitinteiara.semenenlyeycverntais. foe Ee TO26.
Sororrsensost ew EmlyeTO27,
Coyne roe : ‘ew Eman:
: ‘ception
X ‘Stem au pte Polen bul delto
‘ere poy oe te (|v
Wines
‘Stes tances te pe with en tomer ae eto eer ee tne fo ee
"Snymectneptn cae the en eo fs
(onan css
‘i eNOS employee i ———
Vsesenatens sven gooey of 5.2620) eaesptem.e-nth
“ectnlye> erent empep aye
,
itera way ting Prsten lg nSg Ota P6
13) Usng rect mets of REAR ncaa itor, PagngAndSoringteposory and et.
1) Ging taba ced Repnay merce [stopwatch
9 ie gan maton ing a | re
5) ity Ooney metas f ur Roonay each Newest] TAS
1) byciie Saupe or any oprons
1) ing state ayn rts (reap cla) | See gw,
1) ig Sar» oucaying meta sr Rpaoy ee oh
"as fates peor nssn Open to | on
atecsrngpeten,
‘Shiba of Cusp and Soe ter peostory
‘Chetan revived ASC mE
‘ete meetin cer rag pasion ating rip es
‘nrsbypcs band onthe en pee Pesce]
rte ueooo
p= ee
‘op cont avg patna orig
2. puntata fasion)
Yly cord hong Sarin nde ASUS
ue tmplyeoTo> ehltmrensing rope tea)
‘arco
carson ton spncenewortdorin Sot Orecion ASC
tet
saresor bon spncenewortadomin Sot Srecton 15am
[eect taal) enpap fon
Ye tase
eeepiyes ncten(uecglyes)enphapntndancsennysororeion sc pope | Teter te:
Soxbyionorecton Sone
‘seco hae sna ‘nS oe onion tc tn,
TpoyeroTo tow Emly tTO0;
Ecmseconrepeere 23)
Iroronadlanh
»
em tO ate
, Twat ease
le gto sae ota ptam ov-pinth: