2外文翻译编写容器管理持久性实体Bean.docx

上传人:b****5 文档编号:8195314 上传时间:2023-01-29 格式:DOCX 页数:10 大小:24.62KB
下载 相关 举报
2外文翻译编写容器管理持久性实体Bean.docx_第1页
第1页 / 共10页
2外文翻译编写容器管理持久性实体Bean.docx_第2页
第2页 / 共10页
2外文翻译编写容器管理持久性实体Bean.docx_第3页
第3页 / 共10页
2外文翻译编写容器管理持久性实体Bean.docx_第4页
第4页 / 共10页
2外文翻译编写容器管理持久性实体Bean.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

2外文翻译编写容器管理持久性实体Bean.docx

《2外文翻译编写容器管理持久性实体Bean.docx》由会员分享,可在线阅读,更多相关《2外文翻译编写容器管理持久性实体Bean.docx(10页珍藏版)》请在冰豆网上搜索。

2外文翻译编写容器管理持久性实体Bean.docx

2外文翻译编写容器管理持久性实体Bean

WritingContainer-ManagedPersistentEntityBeans

Container-ManagedFields

Acontainer-managedpersistententitybeanallowsthecontainertohandlesomeorallofitsdataaccesslogic.RatherthancodingJDBCorSQL/Joperationsinyourbeanclass,yourcontainerimplicitlyperformsalldatabaseoperationsbehindthescenes.

Withcontainer-managedpersistence,youmustmakesomeofyourentitybeanclass’sfieldspublicsothatthecontainercansetthefieldswhenitperformsdatabaseoperationsonbehalfofyourbean.Thefieldsthatyouwanttobepersistentarecalledcontainer-managedfields.Youdon’thavetoworryaboutsettingthesefields—theEJBcontainerwillautomaticallymanipulatethemforyou

behindthesceneswhenitperformsstorageoperations.

Onerestrictionofcontainer-managedfieldsisthateveryfieldyouwanttobemanagedbythecontainermustfollowtherulesforJavaobjectserialization(wedescribetheserulesinfullinAppendixA).ThismeansthatprimitivetypessuchasdoublesandBooleans,aswellasserializableclassessuchasprimarykeyclassesorEJBhandlestootherentitybeans,canbecontainer-managedfields.

Forexample,thefollowingisasnippetofcodefromourbankaccountentitybeanclassthatwewroteinChapter8:

Withcontainer-managedpersistence,thecontainercanpersisteachofthesefieldsforyoubehindthescenes.Whensavingyourbeaninstance’sfields,thecontainerisresponsibleforqueryingyourbeaninstanceforthesefieldvalues.Whenloadingdataintoyourbeaninstance,thecontainersetsthesefields.Thisispossiblebecauseeachofthefieldsisdeclaredaspublic.

Ofcourse,youstillmustinformthecontaineraboutwhichfieldsitshouldmanipulate.Youspecifythisinyourbean’sdeploymentdescriptor.TheEJBcontainerwillinspectthedeploymentdescriptortofigureoutwhichofyourentitybean’sfieldstomanipulate.

Notethatnotallfieldswithinthebeanhavetobemanagedbythecontainer.Youmightbepullingdatamanuallyfromasecondarysource,oryoumighthavecalculatedfields.TheEJBcontainerwillautomaticallynotifyyourbeanclassduringpersistentoperations,allowingyoutomanagethesefields.

PrimaryKeyClass

Aswithbean-managedpersistence,container-managedpersistencedictatesthatyourprimarykeyclassmustbeserializable.BecausetheEJBcontainerwillworkwithyourprimarykey,therearenewrestrictionsforhowyouwriteyourprimarykeyclass.Themostimportantrestrictionisthatthefieldsyouhaveinyourprimarykeymustcomefromthecontainer-managedfieldsofyourentitybean,whichwedescribedpreviously.ThisrestrictionallowstheEJBcontainertoset,aswellasextract,yourentitybean’sprimarykeyfields.

Forexample,takeourprimarykeyclassfromourChapter8’sbankaccount:

Thisisavalidprimarykeyclassforcontainer-managedpersistencebecauseit’sserializableandbecauseitspublicfieldscomefromourbeanclass’scontainer-managedfields.

ImplementationGuidelinesforContainer-ManagedPersistence

Themethodimplementationsofyourentitybeansshouldbedifferentforcontainer-managedpersistententities.Nolongerareyoucontrollingtheroutinepersistentoperationsofyourbeans,andsomanyofthemethodscanbeleftempty—thecontainerwilldoitforyou.Table9.1isasummaryofwhatyoushouldimplementineachmethod,assumingyourentitybean’spersistenceiscontainermanaged.Takeaquickglanceatthechartfornow.Asyoucanseefromthetable,manyofthedatabase-intensiveoperationshavebeenreducedinscopesignificantly.Youshouldreferbacktothechartwhenreadingthroughthecodeinthischapterorwhenprogrammingyourownentitybeanclasses.Theorderofmethodslistedveryroughlymodelstheflowofcontrolofanentitybeaninstance’slifecyclethatwesawattheendofChapter7.

Container-ManagedPersistenceExample:

AProductLine

Let’sseeaquickdemonstrationofcontainer-managedpersistenceinaction,appliedtotheconceptofaproductline.

Ifyou’reworkingforaproduct-basedcompany,yourcompany’sproductlineisthesuiteofproductsyourcompanyoffers.Forexample,ifyou’reanappliancecompany,youmightofferadishwasher,astove,andadryer.Ifyou’reacomputerhardwarecompany,youmightoffermemory,harddisks,andprocessors.We’regoingtomodelagenericproductasanentitybeanthatusescontainer-managedpersistence.

TheobjectmodelforourproductlineisdetailedinFigure9.1.

Let’stakealookateachofthefilesthatwemustcreateforourentitybeancomponent.

Product.java

OurremoteinterfaceisspecifiedbyProduct.java,showninSource9.1.

OurremoteinterfaceisverysimilartoChapter8’sbankaccountremoteinterface.Ithasmethodstomodifytheentitybeaninstance’sfieldsandthrowsremoteexceptionstoindicatesystem-levelerrors.

ProductHome.java

Next,wehavetheproduct’shomeinterface,ProductHome.java,presentedinSource9.2

Ourhomeinterfacedefinesasinglecreate()methodtocreateanewproductinthedatabase.ItreturnsaProductEJBobjectsotheclientcanmanipulatetheentitybeandataandthrowsajavax.ejb.CreateExceptiontoindicateanapplication-levelproblem.

Wealsoexposeallsortsoffindermethodstofindexistingproducts.SomeofthefindersreturnasingleEJBobject,whileothersreturnajava.util.EnumerationofmultipleEJBobjects.Thisisneededifthefindermethodsfindmorethanonematchingobject.NotethatfindByPrimaryKey()shouldneverreturnanenumerationbecauseprimarykeysmustbeunique.

ProductPK.java

OurprimarykeyclassisdefinedbyProductPK.java,showninSource9.3.

AswithourBankAccount,ourprimarykeyisasimplestring.Andaswe’vefoundout,therearerestrictionsforwhatourprimarykeycanbe.Ourprimarykeyfieldsarecomingfromthecontainer-managedfieldsoftheentitybeanclass,asisrequiredwithcontainer-managedpersistence.Inparticular,ourprimarykeyrepresentstheIDstringofaproduct(suchasaproductSKUnumber).

ProductBean.java

Next,wehaveourcontainer-managedentitybeanimplementation,ProductBean.java,showninSource9.4.

Thisbeanismorecomplexthanourbankaccountexample.We’vedefinedmanyfindermethods,andwehavefourpersistentfields.Yeteventhoughwe’veaddedallthiscomplexity,ourbeanislessthan40percentofthesizeofourBankAccountbean.Thisisanamazingreductionincodecomplexity.Andbecauseourbeanhasnodatabasecodeinit,wehavereducedthechanceforbugsinourbeanthatwouldbeduetousererrorworkingwithJDBCcode.Thisisahugesavingsindevelopmentandtestingtime.

Wehavefourcontainer-managedfields,allwithpublicscope.They’republicsothatthecontainercanmanipulatethem.OurejbCreate()methodsimplysetsourcontainer-managedfieldstothepassed-inclientparameters.TheEJBcontainerwillextractthosefieldsandsetupthedatabasedataforus.NoticethatourejbCreate()methoddoesnotreturnaprimarykeybecausetheEJBcontainerdoesthatforus.

Therestofourbeanisjustemptymethodsandcomments.There’salmostnologicatall.Ourbeanclassisjustdatawithsomeaccessormethods.

Client.java

Ourclientcodeisasimplesuiteoftestcasestotryoutourbean,asshowninSource9.5.

WeperformaJNDIlookuptoacquirethehomeobjectandcreatesomeentitybeandata.Wethentryoutacoupleoffindermethods.Wecanloopthroughthefinders’returnednumerationsandcallbusinessmethodsoneachEJBobject.WethendestroyalltheEJBobjectswecreatedinafinally{}clause.

TheDeploymentDescriptor

Wenowneedtowriteourdeploymentdescriptor.Inadditiontodefiningthestandardentitybeanfields,wenowneedtoinformthecontaineraboutourpubliccontainer-managedfields.ThedeploymentdescriptorisshowninTable9.2.NoticethatwenolongerhaveanyJDBCapplication-specificpropertiesbecausewe’veexternalizedalldatabaseactivitytothecontainer.

Inadditiontothedeploymentdescriptor,weneedtotellthecontainerexactlyhowtoperformpersistentoperations.Thisisonetrade-offofcontainer-managedpersistence—youstillneedtodeclarepersistentrules,ratherthancodethemintoyourbeanusingJDBCorSQL/J.

Ifyou’reusingarelationaldatastore,you’llneedtodefineexactlyhowyourentitybean’spublicfieldsmaptothatdatabase.Thus,wemustdefineaseriesofobject-relationalmappingentries.Theseentriesmapentitybeanfieldstorelationaldatabasecolumnnames.TheEJBcontainer(inthiscase,BEAWebLogic)willusethismappingwhenstoringorretrievingourcontainer-managedfieldsfromthedatabase.NotethatthisisveryEJBcontainer-specific!

SomeEJBcontainerswillsupportobjectdatabasesandthuswillnothaveamappingintoatwo-dimensionalrelationaldatabase.ConsultyourEJBcontainer’sdocumentationformoreinformation.Ourproductline’spersistententriesforBEA’sWebLogicserverareshowninTable9.3.

Wealsoneedtospecifytheimplementationofourhomeobject’sfindermethods.Thisisalso,unfortunately,proprietaryforeachEJBcontainer.BEAWebLogichasasimplescriptinglanguageforthispurpose.Forexample:

ThecompletescriptisshowninTable9.4.Thecontainerwillimplementthislogic,perhapsusingJDBCorSQL/J.Wheneveraclientwantstoexecuteafindermethodonthehomeobject,thecontainerwillautomaticallyruntheimplementedJDBCorSQL/Jcode.

RunningtheClientProgram

Toruntheclientprogram,typeacommandsimilartothefollowing(dependingonwhatyourEJBcontainerJavaNamingandDirectoryInterface,orJNDI,initializationparametersare):

TheinitializationparametersarerequiredbyJNDItofindthehomeobject,aswelearnedin

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 行政公文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1