容器管理论文中英文资料外文翻译文献_第1页
容器管理论文中英文资料外文翻译文献_第2页
容器管理论文中英文资料外文翻译文献_第3页
容器管理论文中英文资料外文翻译文献_第4页
容器管理论文中英文资料外文翻译文献_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

容器管理论文中英文资料外文翻译文献PAGEPAGE13容器管理论文中英文资料外文翻译文献WritingContainer-ManagedPersistentEntityBeansContainer-ManagedFieldsAcontainer-managedpersistententitybeanallowsthecontainertohandlesomeorallofitsdataaccesslogic.RatherthancodingJDBCorSQL/Joperationsinyourbeanclass,yourcontainerimplicitlyperformsalldatabaseoperationsbehindthescenes.Withcontainer-managedpersistence,youmustmakesomeofyourentitybeanclass’sfieldspublicsothatthecontainercansetthefieldswhenitperformsdatabaseoperationsonbehalfofyourbean.Thefieldsthatyouwanttobepersistentarecalledcontainer-managedfields.Youdon’thavetoworryaboutsettingthesefields—theEJBcontainerwillautomaticallymanipulatethemforyoubehindthesceneswhenitperformsstorageoperations.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.PrimaryKeyClassAswithbean-managedpersistence,container-managedpersistencedictatesthatyourprimarykeyclassmustbeserializable.BecausetheEJBcontainerwillworkwithyourprimarykey,therearenewrestrictionsforhowyouwriteyourprimarykeyclass.Themostimportantrestrictionisthatthefieldsyouhaveinyourprimarykeymustcomefromthecontainer-managedfieldsofyourentitybean,whichwedescribedpreviously.ThisrestrictionallowstheEJBcontainertoset,aswellasextract,yourentitybean’sprimarykeyfields.Forexample,takeourprimarykeyclassfromourChapter8’sbankaccount:Thisisavalidprimarykeyclassforcontainer-managedpersistencebecauseit’sserializableandbecauseitspublicfieldscomefromourbeanclass’scontainer-managedfields.ImplementationGuidelinesforContainer-ManagedPersistenceThemethodimplementationsofyourentitybeansshouldbedifferentforcontainer-managedpersistententities.Nolongerareyoucontrollingtheroutinepersistentoperationsofyourbeans,andsomanyofthemethodscanbeleftempty—thecontainerwilldoitforyou.Table9.1isasummaryofwhatyoushouldimplementineachmethod,assumingyourentitybean’spersistenceiscontainermanaged.Takeaquickglanceatthechartfornow.Asyoucanseefromthetable,manyofthedatabase-intensiveoperationshavebeenreducedinscopesignificantly.Youshouldreferbacktothechartwhenreadingthroughthecodeinthischapterorwhenprogrammingyourownentitybeanclasses.Theorderofmethodslistedveryroughlymodelstheflowofcontrolofanentitybeaninstance’slifecyclethatwesawattheendofChapter7.Container-ManagedPersistenceExample:AProductLineLet’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.javaOurremoteinterfaceisspecifiedbyProduct.java,showninSource9.1.OurremoteinterfaceisverysimilartoChapter8’sbankaccountremoteinterface.Ithasmethodstomodifytheentitybeaninstance’sfieldsandthrowsremoteexceptionstoindicatesystem-levelerrors.ProductHome.javaNext,wehavetheproduct’shomeinterface,ProductHome.java,presentedinSource9.2Ourhomeinterfacedefinesasinglecreate()methodtocreateanewproductinthedatabase.ItreturnsaProductEJBobjectsotheclientcanmanipulatetheentitybeandataandthrowsajavax.ejb.CreateExceptiontoindicateanapplication-levelproblem.Wealsoexposeallsortsoffindermethodstofindexistingproducts.SomeofthefindersreturnasingleEJBobject,whileothersreturnajava.util.EnumerationofmultipleEJBobjects.Thisisneededifthefindermethodsfindmorethanonematchingobject.NotethatfindByPrimaryKey()shouldneverreturnanenumerationbecauseprimarykeysmustbeunique.ProductPK.javaOurprimarykeyclassisdefinedbyProductPK.java,showninSource9.3.AswithourBankAccount,ourprimarykeyisasimplestring.Andaswe’vefoundout,therearerestrictionsforwhatourprimarykeycanbe.Ourprimarykeyfieldsarecomingfromthecontainer-managedfieldsoftheentitybeanclass,asisrequiredwithcontainer-managedpersistence.Inparticular,ourprimarykeyrepresentstheIDstringofaproduct(suchasaproductSKUnumber).ProductBean.javaNext,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.javaOurclientcodeisasimplesuiteoftestcasestotryoutourbean,asshowninSource9.5.WeperformaJNDIlookuptoacquirethehomeobjectandcreatesomeentitybeandata.Wethentryoutacoupleoffindermethods.Wecanloopthroughthefinders’returnednumerationsandcallbusinessmethodsoneachEJBobject.WethendestroyalltheEJBobjectswecreatedinafinally{}clause.TheDeploymentDescriptorWenowneedtowriteourdeploymentdescriptor.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.RunningtheClientProgramToruntheclientprogram,typeacommandsimilartothefollowing(dependingonwhatyourEJBcontainerJavaNamingandDirectoryInterface,orJNDI,initializationparametersare):TheinitializationparametersarerequiredbyJNDItofindthehomeobject,aswelearnedinChapter4.Server-SideOutputWhenyouruntheclient,youshouldseesomethingsimilartothefollowingontheserverside.Notethatyourparticularoutputmayvary,duetovariancesinEJBcontainerbehavior.Wecreatedanumberofnewproductsinourclientcode.Foreachnewproduct,ourEJBcontainercreatedadedicatedbeaninstance.Itdidn’thavetodothis—itcouldhavepassivated/activatedthesamebeanandswitchedcontextbetweenclients.Whencreatingabean,ourcontainerfirstcallednewInstance(),followedbysetEntityContext(),whichgotthebeanintothepool.ItthencalledejbCreate(),setupthedatabasedata,boundthebeantoanEJBobject,andfinallycalledejbPostCreate()—allasexpected.Itthenservicedafewbusinesscalls,instantiatedafewnewbeans,andoccasionallysynchronizedthebeanswiththeunderlyingdatabase.Client-SideOutputFortheclientside,aftercreatingsomeproducts,weperformedafindforallproductsthatcost$200.Indeed,multipleentitybeanswerereturnedinourenumeration,asisshownbelow:PromisesandRealities:Bean-ManagedPersistenceversusContainer-ManagedPersistenceNowthatyou’veseenbothbean-managedandcontainer-managedpersistententitybeans,youmustbeconvincedthatcontainer-managedbeansarethewaytogo.AllthatJDBCcodewaseliminatedfromourbeanclass,savingussignificantdevelopmenttime.However,thechoicebetweencontainer-andbean-managedpersistenceisnotnecessarilyclear-cut.Bothbean-managedandcontainer-managedbeanshavevirtues.Container-managedpersistencemaypromisealot,butitscurrentmanifestationfailstodeliveronnumerouscounts,aboutwhichyoumustbeinformed.Letuslookatthreepromises,andtherealitiesofthosepromises,forcontainer-managedpersistence.Promise:Container-ManagedPersistenceReducesCodeIfyoutelltheEJBcontaineracoupleofthingsaboutyourbean,container-managedpersistencecanperformalldataaccesslogicforyou.Thisreducesthesizeofyourbeantremendously—nomoreJDBCcodeinyourbeans—whichreducesoveralldevelopmenttime.RealityDependingonyourcontainer,youstillmayneedtowritepersistentcodewithcontainer-managedbeans.Thiscouldbegoingthroughaseriesofwizardstospecifyhowyourentitybeansmaptoanunderlyingstore.Youalsoneedtospecifythelogicbehindyourfindermethods.Thedifferenceisthatyourdataaccesslogicisnowspecifieddeclaratively,ratherthanbeingwritteninJava.Thisdoessignificantlyreduceyourcodesize,however,andhasthenicefeaturethatyoucanmigratetonewdatabaseschemasveryquicklywithoutchanginganysourcecode.Anothercodebenefitrarelymentionedisthatyourcontainercanbeverysmartabouthowitcachesentitybeanstateinmemory.AdvancedEJBcontainersshipwithasharedobjectcache,whichstoresentitybeandatainmemoryacrosstransactions.Usingasharedobjectcache,thecontainercanavoidunnecessaryejbLoad/ejbStorecalls,whichincreasestransactionalthroughputexponentially.Notethatyoucancacheentitybeanstateinmemoryusingbean-managedpersistenceaswell,buttheburdenofdoingthisfallsonyou.Promise:Container-ManagedPersistenceReducesBugsOnebenefitofcontainer-managedpersistenceisthatiteliminatesmanyofthebugsthatoccurinadeployment—mostlyduetobuggyJDBCcode.TheproblemwithJDBCcodeisthatit’snot“type-safe.”Youcan’tdetectwhetheritwillworkatcompiletime—yourJDBCstatementsaresimplestringsthatcanberesolvedonlyatruntime.Bywayofcomparison,withcontainer-managedpersistencetheEJBcontainerhasbeenwrittenbyadatabaseprofessionalwhosesolejobistomakesurethegenerateddatabasecallsare,ingeneral,accurate.Plus,ifyouasauserhavespecifiedanerror(perhapsyoumisnamedacontainer-managedfieldinthedeploymentdescriptor),youcandetecterrorsatcompiletime.ThewayyoudetecttheseerrorsisbyrunningyourEJBcontainertools,which,ifthey’reanygood,shoulduseJavaReflectionortheequivalenttofigureoutwhetheryourdeploymentdescriptordoesindeedmaptoyourcontainer-managedfields.Itcanalsocheckthingssuchaswhetheryourprimarykeyfieldsareasubsetofyourcontainer-managedfields.

编写容器:管理持久性实体Bean容器管理作用域一个容器管理的持久性实体Bean,让容器处理一些或所有的数据访问逻辑。与编码使用JDBC或SQL/J操作你的Bean类不同,你的容器在后台执行了所有的数据库操作。使用容器管理持久化,你必须在你的实体Bean类中做出一些的公有访问的作用域,使容器在它执行数据库操作时你的Bean能够设置访问各个作用域。你想使用持久性操作的作用域,被称作容器管理作用域。你不必担心如何设置这些作用域——当EJB容器执行存储操作时,EJB容器将自动在后台为你操纵这些作用域。一个容器管理作用域的约束,是每个你想使用容器管理的作用域必须遵循Java对象序列化的规则(我们在附录A中完整的说明了这些规则)。这意味着原始类型,如double型和布尔型,以及serializable类中的主键类或EJB处理其他实体Bean时,可以使用容器管理作用域。举例来说,以下是我们在第8章中我们的银行帐户实体Bean类的部分代码:使用容器管理持久化,容器能够为你在后台持续上述每个作用域。当保存你的Bean的实例对象的作用域时,容器负责为这些作用域的值来检查你的Bean的实例。当向你的Bean的实例对象中载入数据时,容器设置这些作用域。这很可能是因为每一个作用域都被声明为public。当然,你还必须告知容器应该操控哪些作用域。你在你的Bean的部署描述符中详细说明这一点。EJB容器会检查部署描述符,断定操控你的实体Bean的哪个作用域。注意:不是Bean内的所有作用域都必须由容器管理。你可以手动的从一个次级来源中引入数据,或者你有合适的作用域。EJB容器在持久化操作时会自动通知你的Bean类,允许你来管理这些作用域的操作。主键类与Bean管理的持久化相同,容器管理持久化指令时你的主键类必须被serializable。由于EJB容器会操作你的主键,你如何写自己的主键类也有了新的限制。最主要的限制是你主键中的作用域必须来自已经声明的由容器管理的你的实体Bean的作用域。我们前面所述。这项限制允许EJB容器设置,以及提取,你的实体Bean的主键作用域。举例来说,我们第8章的银行账户的主键类:这是一个容器管理持久化的有效的主键类,因为它是可序列化的,并且是从我们Bean类的容器管理作用域而来的公有的作用域。为容器管理持久化执行准则你的实体Bean执行方法应该与容器管理持久性实体不同。不再是你为你的Bean控制程序持久化操作,有许多方法可以为空——容器会为你完成这些方法。表9.1是你在每个方法里应该实现什么的一个概要,假定你的实体Bean的持久性,是由容器管理的。现在快速扫视图9.1。正如你从表中所看到的,许多数据库加强器操作在范围上已明显降低。,当你读完这一章中的代码或当你编写你自己的实体Ban类时,你应该参考着对照表9.1。该方法的顺序非常概略模型的列出了我们曾在第7章末尾看到的一个实体Bean实例的控制流的生命周期。容器管理持久化举例:一个生产线让我们来看一个在实际中容器管理持久化的快速演示,应用这一概念的一个产品线。如果你为一家制造某种产品的公司工作,你的公司的生产线是一组由你的公司生产的产品。例如,如果你是一个家电公司,你可能会生产洗碗机,炉灶和烘干机。如果你是一个电脑硬件公司,你可能会生产内存,硬盘和处理器。我们将要使用一个实体Bean模仿一个普通的产品,并使用容器管理持久化。我们的生产线的对象模型,详见图9.1。让我们来看看每个我们必须为我们的实体Bean构成创建的文件。Product.javaProduct.java指定了我们的远程接口,见源代码9.1。我们的远程接口与第八章中的银行账户中的远程接口十分相似。它拥有修改实体Bean实例作用域并抛出接口异常来显示系统级的错误的方法。ProductHome.java接下来,我们分析产品的主接口,ProductHome.java,见源代码9.2。我们的主接口定义了一个单一的create()方法在数据库中创建一个新产品。它返回一个产品的EJB对象,使客户端可以操纵实体Bean数据,并抛出一个javax.ejb.CreateException显示一个应用级问题。我们还给出各种搜索方法来查找现有的产品。一些查找方法返回一个单一的EJB对象,而另一些返回一个java.util.Enumeration来枚举多个EJB对象。如果搜索方法找到一个以上匹配的对象这个方法是非常必要的。注意findByPrimaryKey()方法应该永远不会返回一个枚举,因为主键必须是唯一的。ProductPK.java我们的主键类由ProductPK.java定义,见源代码9.3。在我们的银行账户中,我们的主键是一个简单的字符串。正如我们设定的我们对我们的主键的值有相应的限制。我们的主键作用域来自由容器管理的作用域的实体Bean类,并作为容器管理的持久化单元的必要部分。特别是,我们的主键代表一个产品的ID字符串(如一个产品SKU编号)。ProductBean.java接下来,我们来看我们容器管理实体Bean的执行,ProductBean.java,见源代码9.4。这个Bean比我们的银行账户的例子复杂得多。我们已经定义了许多搜索方法,并且我们有四个持久性作用域。然而尽管我们已经添加这一切的复杂性,我们的Bean却是我们的银行账户Bean大小的40%。这是一个对代码的复杂性了不起的减少。因为我们的Bean中没有数据库代码,我们减少了在我们的Bean中出现bug的可能,这应归于用户错误处理与JDBC的代码。这样可以节省巨大的开发和测试时间。我们有4个容器管理的作用域,都是public作用范围。这样容器可以操纵他们。我们的ejbCreate()方法向已通过进入系统的客户端参数,简单地设定我们的容器管理作用域。EJB容器将提取这些作用域,并为我们从数据库中提取数据。注意,我们ejbCreate()方法并不返回一个主键,因为EJB容器已经为我们执行了这样的操作。我们Bean中其余的只是空的方法和注释。这些几乎没有逻辑可言。我们的Bean类只是数据与一些存取器方法。Client.java我们的客户端代码是一套简单测试案例,用来测试我们的Bean,见源代码9.5。我们执行一个JNDI查找获得有效的对象并创造一些实体Bean数据。然后,我们尝试一系列的搜索方法。我们可以通过循环调用搜索方法返回编号并可以对每个EJB的对象调用业务方法。最后,我们在我们创造的finally{}子句中销毁所有EJB对象。部署描述符我们现在需要编写我们的部署描述符。除了界定标准实体Bean作用域,我们现在还需要通知容器我们公共的容器管理作用域。部署描述符列在表9.2中。注意,我们不再有任何JDBC的明确应用性能,因为我们已经向容器具体化数据库的所有行为。除上述部署描述符,我们需要告诉容器到底履行持久化操作。这是一个容器管理持久化的交替使用——你依然需要定义持久化规则,而不只是把他们添加到你使用JDBC或SQL/J编写的Bean中。如果你使用的是关系数据存储,你需要严格定义如何把你的实体Bean的公共作用域映射到数据库。因此,我们必须定义一系列的关系对象映射到实体。这些实体把实体Bean作用域映射到关系型数据库的字段。该EJB容器(在此我们使用的是BEA公司的WebLogic)在向数据库存储或从数据库中

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论