Java Persistence API Framework_v2

download Java Persistence API Framework_v2

of 29

Transcript of Java Persistence API Framework_v2

  • 8/13/2019 Java Persistence API Framework_v2

    1/29

    Persistence Framework

    Persistenceframework

  • 8/13/2019 Java Persistence API Framework_v2

    2/29

    Persistence Framework

    2

    Objectives

    Understanding Database Encapsulation Advantages and Disadvantages of Database

    Encapsulation Implementation Strategies Persistence Framework JPA API Best Practices

  • 8/13/2019 Java Persistence API Framework_v2

    3/29

    Persistence Framework

    Encapsulation

    !e term encapsulationis often usedinterc!angeabl" wit! information !iding# it ist!e !iding of design decisionsin a computerprogram t!at are most likel" to c!ange# t!usprotecting ot!er parts of t!e program fromc!ange if t!e design decision is c!anged$

    !e protection involves providing a stableinterface$ In modern programming

    languages# t!e principle of information !idingmanifests itself in a number of wa"s#including encapsulation %given t!eseparation of concerns& and pol"morp!ism$

  • 8/13/2019 Java Persistence API Framework_v2

    4/29

    Persistence Framework

    Encapsulation on Database

    !e implication of encapsulation is t!at we can buildcomponents and c!ange it later wit!out affect ot!ercomponents wit!in t!e s"stem as long as t!e interfaceto t!at component did not c!ange$

    Database is t!e place for information storage# we canalso !ave various encapsulation arc!itectures# andimplementation strategies so t!at we can decouple t!ecode wit! t!e database s"stem including t!eir p!"sicalsc!emas from business code $

    a t"pical arc!itectural la"ering strateg" for buildingbusiness applications w!ic! includes a databaseencapsulation la"er %also known as a persistence la"eror simpl" a data la"er&$

  • 8/13/2019 Java Persistence API Framework_v2

    5/29

    Persistence Framework

    Starting from '(((# developers figured out#t!at accessing relational databases using)ust*directl" JDB+ is a rat!er dirt"*ugl" wa"$

    !e differences in ob)ect,oriented design andrelational databases got tooobvious$

    a number of pro)ects including opensource!ave been set up to solve t!is problem in amore elegant wa"

    -ibernate# +astor# +a"enne# .JB#or/ue#D.DS#oplink# iBatis# AD. and etc

    ob)ect,oriented databases were at t!at timealread" more or less p!ased out$

    A Brief History of Persistence

    http://www.hibernate.org/http://www.castor.org/http://incubator.apache.org/cayenne/http://db.apache.org/ojb/http://db.apache.org/torque/http://www.enhydra.org/tech/dods/index.htmlhttp://www.oracle.com/technology/products/ias/toplink/index.htmlhttp://ibatis.apache.org/http://ibatis.apache.org/http://www.oracle.com/technology/products/ias/toplink/index.htmlhttp://www.enhydra.org/tech/dods/index.htmlhttp://db.apache.org/torque/http://db.apache.org/ojb/http://incubator.apache.org/cayenne/http://www.castor.org/http://www.hibernate.org/
  • 8/13/2019 Java Persistence API Framework_v2

    6/29

    Persistence Framework

    Database Encapsulation Befits

    It reduces t!e coupling of ob)ect and databaseso t!at increasing abilit" to evolve eit!er one$

    It implements all data,related code in oneplace$ It make t!e programmers )ob easier$

    It gives "ou a common place to implementdata,oriented business rules$

    It allow to takes advantage of specificdatabase features# increasing applicationperformance

    It allows application programmers to focus ont!e business problem and DBA%s& can focus ont!e database$ %separation of responsibilities&

  • 8/13/2019 Java Persistence API Framework_v2

    7/29Persistence Framework

    Database encapsulation disadvantages

    !ere are potentiall" several disadvantages ofdatabase encapsulation0

    Increase t!e Investment$

    !e" often re/uire reasonabl" clean1apping$

    Provide not enoug! control over databaseaccess$

  • 8/13/2019 Java Persistence API Framework_v2

    8/29Persistence Framework

    Implementation Strategies ForDatabase Enca sulation La ers

    Just 2uer"0 business ob)ects access datasources directl" b" submitting Structure2uer" 3anguage %S23& or .b)ect 2uer"3anguage %.23& code to t!e database

    Data access ob)ects%DA. it encapsulate t!edatabase access logic re/uired of businessob)ects# It also !ave industr",standardapproac!es suc! as Java Data .b)ect %JD.&

    and Active4 Data .b)ect %AD.&$

  • 8/13/2019 Java Persistence API Framework_v2

    9/29Persistence Framework

    Implementation Strategies ForDatabase Enca sulation La ers

    Persistence framework*tools0 It encapsulatesdatabase access from business ob)ects$ Insteadof writing code to implement t!e Databaseaccess logic # it re/uire to define meta data t!at

    represents t!e mappings$ Data Service0 It is an operation offered b" a

    component entit" or application t!at can beinvoked b" ot!er entities# for e5ample$

    > +ommon .b)ect 6e/uest Broker Arc!itecture %+.6BA&> +ustomer Information +ontrol S"stem %+I+S& ransaction> Distributed +omponent .b)ect 1odel %D+.1&> Electronic data interc!ange %EDI&> Stored procedures

    > 7eb Services

  • 8/13/2019 Java Persistence API Framework_v2

    10/29Persistence Framework

    Wat is te issue!Some applications ma" not take advantage oft!e encapsulation la"er%s& and some ma" not#t!e" !ave to access data directl" to t!eDatabase $

    legac" applications cannot access*not allow tot!e database encapsulation $

    rework some of "our legac" applications to uset!e database encapsulation la"er cannot or

    will not performed*wanted$!ere are some tec!nologies# suc! as a bulk

    load facilit" or a reporting framework# t!atre/uire direct access to t!e database sc!ema$

  • 8/13/2019 Java Persistence API Framework_v2

    11/29Persistence Framework

    Note

    !ere are still !ave values to implementdatabase encapsulation because we

    can reduce coupling of ob)ect and DBand t!erefore reducing developmentcosts and maintenance effort$

  • 8/13/2019 Java Persistence API Framework_v2

    12/29Persistence Framework

    General Persistence Framework features

    , 7rite S23automaticall"

    , +reate Joins for -as,one 6elations!ips

    , Aggregate Sub/ueries

    , Support Paging

    , Allow for 8ersioning

    , +ascading operation

    , -andle +olumnAliases

    , Support In!eritance

    , 8alue .b)ectssupport

    , +ac!ing mec!anism

  • 8/13/2019 Java Persistence API Framework_v2

    13/29Persistence Framework

    "ava Persistence #e$uirements

    Simplification of t!e persistence model> Elimination of deplo"ment descriptor

    3ig!t,weig!t persistence model> In terms of programming and deplo"ment model as

    well as runtime performance

    estabilit" outside of t!e containers> +reate test clients t!at would use entities in a non,

    managed environment

    Domain modelling t!roug! in!eritance andpol"morp!ism

    .b)ect*6elational %.*6& mapping

    E5tensive uer in a abilities

  • 8/13/2019 Java Persistence API Framework_v2

    14/29Persistence Framework

    JPA Primary Features

    P.J.,based persistence model Plain .ld Java .b)ects

    Supports traditional .,. modelling concepts

    In!eritance# pol"morp!ism# encapsulation# etc$ Standard abstract relational /uer" language

    Java Persistence 2uer" 3anguage Standard .*6 mapping metadata

    Using Java SE 9 Annotations and*or 413 Portabilit" across providers %implementations&

    oplink# -ibernate# :odo# .penJPA# Eclipse3ink

  • 8/13/2019 Java Persistence API Framework_v2

    15/29Persistence Framework

    Java Persistence P! "JP# Java Persistence API %JPA& provides P.J. %Plain .ld Java .b)ect&

    standard and ob)ect relational mapping %.6 mapping& for datapersistence among applications$

    Persistence# w!ic! deals wit! storing and retrieving of applicationdata# can now be programmed wit! Java Persistence API starting

    from EJB ;$( as a result of JS6 ''($ Java Persistence API !as borrowed man" of t!e concepts and

    standards from leading persistence frameworks like oplink %from.racle& and -ibernate %from JBoss&$ .ne of t!e great benefits of JPAis t!at it is an independent API and can nicel" integrate wit! J'EEas well as J'SE applications$

  • 8/13/2019 Java Persistence API Framework_v2

    16/29Persistence Framework

    Principles on ow to use "P%

    !e JPA is a specification# JPA providerimplementations !ave to keep t!ePortabilit"$ Even for t!e provider,specificfunctionalit"# t!e" s!ould be easil" toidentif" and isolated$

    JPA,based code s!ould usuall" work inbot! Java SE and Java EE environment$

    JPA code s!ould be portable acrossdifferent datasources$ If database,specific features must be used# t!e"s!ould be isolated in configuration files

    rat!er t!an tainting t!e code$

  • 8/13/2019 Java Persistence API Framework_v2

    17/29Persistence Framework

    Principles on ow to use "P%

    Self,e5planator" code and annotationsare preferable to comments t!at arenecessar" to e5plain convoluted code$

    !e best practices generall" re/uire ver"little e5tra effort to implement andprovide long,term maintenance benefitsat little or no cost in initial development

    time Service Provider Interface %SPI& for

    container*persistence providerplugabilit"$

  • 8/13/2019 Java Persistence API Framework_v2

    18/29Persistence Framework

    "ava Persistence %PI &est Practices

    Standard SQL for Database if required

    Effectively Use Database-SpecificFeatures

    Select a suitable Layer for PA !odeLayer

    Favor Self-Describin" !ode over#etadata and !o$$ents

    Define %a$in" !onventions to &ave a#ore-'eadable !ode

    !onsider usin" Sprin" fra$e(or) toPA

  • 8/13/2019 Java Persistence API Framework_v2

    19/29Persistence Framework

    "ava Persistence %PI &est Practices

    !arefully !onsider and Select a#etadata !onfi"uration Strate"y

    Favor !onventions over E*ceptions

    Access Fields 'at&er +&an Properties,#ore freedo$

    Favor .E$bedded/d for !o$posite0eys

    Use 1nly E*act Datatypes for Pri$ary0eys,usin" inte"er

    Usin" Standard PA !ode

    Effectively Use /$ple$entation-Specific

    Features

  • 8/13/2019 Java Persistence API Framework_v2

    20/29Persistence Framework

    Java Persistence P! Best Practices

    6eplacing an eager loading strateg" wit!a la

  • 8/13/2019 Java Persistence API Framework_v2

    21/29Persistence Framework

    FuturesJPA 2.0. The focus is on refining, fixing,and improving portability. Some possiblene features of JPA 2.0!

    "etter validation support, e.g.

    #$ength%&', #(ax%2' )mproved JP*$ support

    +riteria AP) support

    Toplin and -iberate already have this.

    The -ash(ap lie hints %e.g. logging,caching' to be standardised

    toplin.logging.level

    toplin.cache.type.default

  • 8/13/2019 Java Persistence API Framework_v2

    22/29

    Persistence Framework

    JP $%& proposed features1ore fle5ible modeling

    Embeddable class support , embed a class into anot!erclass wit!out mapping t!at class directl" to a table$Instead it=s stored >embedded> inside anot!er class as acolumn in t!e table$

    .rdered 3ists ? !e ordering applied to a /uer" to bepersisted in t!e database$ From t!e database perspective#!ardwiring ordering in t!e database is done b" defining aninde5 column$

    Access "pe , t!e process of defining w!et!er t!e provider

    uses fields or properties to access t!e elements of a class$+urrentl" a single access t"pe applies to complete entit"!ierarc!"$ !e proposed c!ange is to allow "ou to specif"t!is on a per,class basis#

    @Access"pe%P6.PE6&annotation on an entit" class$

  • 8/13/2019 Java Persistence API Framework_v2

    23/29

    Persistence Framework

    JP $%& proposed features

    E5panded .*6 1appping Functionalit" Unidrectional one,to,man"

    relations!ips using foreign ke"ou reall" don=t want bot! ends of

    one,to,man" relations!ip to !ave toknow about eac! ot!er$ oda" "ou!ave to use a )oin table$ !is c!angewould allow "ou to specif" t!erelations!ip on one side onl"$

  • 8/13/2019 Java Persistence API Framework_v2

    24/29

    Persistence Framework

    JP $%& proposed features

    2ueries Some current limitations0

    SE3E+ clause too constrained$ .nl" supports aggregatefunctions

    Some unnecessar" restrictions on parameter usage , w!at

    about collections 2ueries are alwa"s pol"morp!ic

    D"namic /ueries currentl" entail string construction$ !is isburdensome on t!e appaction to build t!is$ +riteria APIs allow>node,wise> /uer" construction$

    +riteria2uer" c/ C em$create+riteria2uer"%+ustomer$class&

    $add%6estrictions$e/%>status># >preferred>&

    $add%6estrictions$e/%># >('(>&

    !ere are alread" e5isting criteria APIs and e5pressionlanguage APIs available , -ibernate# .JB# +a"enne# op3ink#etc$

  • 8/13/2019 Java Persistence API Framework_v2

    25/29

    Persistence Framework

    JP $%& proposed features Standardi

  • 8/13/2019 Java Persistence API Framework_v2

    26/29

    Persistence Framework

    JP $%& proposed features E5tended persistence conte5t

    Allow entities to remain managed across multiple serial transactions$Application,managed is alwa"s e5tended$ It is up to t!e developer tomanage t!e lifec"cle of t!e persistence conte5t$ It is also up to t!eapplication to manage t!e association between a JA transaction andpersistence conte5t$

    .ne wa" to solve t!e issue is made t!e Entit"1anager detects it is in atransaction and registers wit! JA for notifications$ But t!eEntit"1anager is not alwa"s created at t!e beginning of a transaction#or "ou ma" !ave multiple active transactions# so t!e ot!er wa" to dot!is is to register t!e Entit"1anager wit! a transaction usingem$)oinransaction%&$ +ontainer,managed transactions is for Java EEenvironments# and provides a muc! easier solution$

    !ere is an open issue wit! conversational state# passivation andclustering$ Basicall" it is not well defined w!at !appens w!enconversational state contains an Entit"1anager and gets passivatedand reconstituted# particularl" on anot!er instance of a cluster$ JPA '$(will tr" to fi5 t!is$

  • 8/13/2019 Java Persistence API Framework_v2

    27/29

    Persistence Framework

    JP $%& proposed features

    8alidation JS6 ;(; on Bean 8alidation defines a

    metadata model and API for validation# for

    general use in SE and EE$ !e" want toleverage t!is in JPA but it depends on timing$Some e5ample validations include

    @6e/uired# @3engt!%ma5C9& @1a5%'( etc$!e" are also looking at supporting user,definedvalidation# e$g$ @Ade/uatel"+ompensated$7!en "ou define t!ese validation annotations#t!e provider can automaticall" c!eck t!at t!eseconstraints are met$

  • 8/13/2019 Java Persistence API Framework_v2

    28/29

    Persistence Framework

    J'( )*+ and (!

    JS6 ;0 Java1 Persistence '$( is now inProposed Final Draft !ttp0**www$)cp$org*en*)sr*detailidC;

    Eclipse Persistence Services Pro)ect%Eclipse3ink& is going to be t!e referenceimplementation of JPA '$($ !ttp0**www$eclipse$org*eclipselink*

    http://www.jcp.org/en/jsr/detail?id=317http://www.jcp.org/en/jsr/detail?id=317
  • 8/13/2019 Java Persistence API Framework_v2

    29/29

    Persistence Framework

    #esources

    Klassfis! persistence !omepage

    !ttps0**glassfis!$dev$)ava$net*)avaee9*persistence

    Persistence support page

    !ttps0**glassfis!$dev$)ava$net*)avaee9*persistence*entit"persistence,support$!tml

    Blog on using persistence in 7eb applications

    !ttp0**weblogs$)ava$net*blog*ss';*arc!ive*'((9

    *'*usingL)avaLpers$!tml Blog on sc!ema generation

    !ttp0**blogs$sun$com*roller*page*)ava'dbInKlassFis!MautomaticLtableLgenerationLfeatureLin