Tải bản đầy đủ - 0 (trang)
Chapter 26. Exercises for Chapter 8

Chapter 26. Exercises for Chapter 8

Tải bản đầy đủ - 0trang

26.1Exercise8.1:SimpleEJBQLStatements

Theexercisesinthissectionrevealsomeofthebasicaspectsof

EJBQLprogrammingandfunctionality.You'llexplorebasic

findermethods,ejbSelectmethods,andtheuseoftheIN

operationinEJBQLqueries.



26.1.1StartUpJBoss

IfyoualreadyhaveJBossrunning,thereisnoreasontorestart

it.



26.1.2BuildandDeploytheExamplePrograms

Performthefollowingsteps:

1. Openacommandpromptorshellterminaland

changetotheex08_1directorycreatedbythe

extractionprocess

SettheJAVA_HOMEandJBOSS_HOMEenvironmentvariablesto

pointtowhereyourJDKandJBoss4.0areinstalled.Examples:

Windows:

C:\workbook\ex08_1>setJAVA_HOME=C:\jdk1.4.2

C:\workbook\ex08_1>setJBOSS_HOME=C:\jboss-4.0



Unix:



$exportJAVA_HOME=/usr/local/jdk1.4.2

$exportJBOSS_HOME=/usr/local/jboss-4.0



Addanttoyourexecutionpath.

Windows:

C:\workbook\ex08_1>setPATH=..\ant\bin;%PATH%



Unix:

$exportPATH=../ant/bin:$PATH



Performthebuildbytypingant.

Asinthelastexercise,youwillseetitan.jarrebuilt,copiedto

theJBossdeploydirectory,andredeployedbytheapplication

server.



26.1.3ExaminetheJBoss-SpecificFiles

ThisexerciseintroducesnonewfeaturesinJBoss-specificfiles.

Ifyouthinkyouneedto,reviewExercise6.1ofthisworkbook

tounderstandtheJBoss-specificfilesinthisexample.



26.1.4InitializetheDatabase

Thedatabasetablesforthisexercisewillautomaticallybe



createdinJBoss'sdefaultdatabase,HypersonicSQL,whenthe

EJBJARisdeployed.Toinitializeallthetablesinthisexample,

though,youmustperformtheAnttaskrun.initialize:

C:\workbook\ex08_1>antrun.initialize

Buildfile:build.xml



prepare:



compile:



run.initialize:

[java]addedBillBurke

[java]addedSachaLabourey

[java]addedMarcFleury

[java]addedJaneSwift

[java]addedNomarGarciaparra



Asintheprecedingexercise,allbusinesslogicisimplemented

withinastatelesssessionbean.Ifyouwouldliketoseethe

databaseinitializationcode,takealookat

com.titan.test.Test81Bean'sinitialize()method,which

createsalltheentitybeansforthisexercise.



26.1.5ExamineandRuntheClientApplications

EachexamplemethodofTest81Beanimplementstheexample

codefragmentsshownintheEJBbook.EachTest81Bean

methodisinvokedbyasmall,simpleclientapplication.



26.1.5.1Client_81a

TheClient_81aprogramdemonstratesafewsimplefinder

methodsthatareexposedthroughtheCustomerhome

interface:



publicinterfaceCustomerHomeLocalextendsjavax.ejb.EJBLocalHo

{

...

publicCustomerLocalfindByName(StringlastName,

StringfirstName)

throwsFinderException;



publicCollectionfindByGoodCredit()

throwsFinderException;

...

}



TheCustomerEJB'sdeploymentdescriptordefinesthesefinder

methodsasfollows:





findByName



java.lang.String

java.lang.String







SELECTOBJECT(c)FROMCustomerc

WHEREc.lastName=?1ANDc.firstName=?2









findByGoodCredit









SELECTOBJECT(c)FROMCustomerc

WHEREc.hasGoodCredit=TRUE







TheexamplealsodemonstratesafewejbSelectmethods,

definedintheAddressEJB'sdeploymentdescriptorasfollows:





ejbSelectZipCodes



java.lang.String







SELECTa.zipFROMAddressASa

WHEREa.state=?1











ejbSelectAll







SELECTOBJECT(a)FROMAddressASa









ejbSelectCustomer





com.titan.address.AddressLocal








SELECTOBJECT(C)FROMCustomerASc

WHEREc.homeAddress=?1







BecauseejbSelectmethodsareprivatetotheentitybean

class,theAddresshomeinterfaceneedscustomhomemethods

towrapandinvoketheprivateejbSelectmethods.



publicinterfaceAddressHomeLocalextendsjavax.ejb.EJBLocalHom

{

...

publicCollectionqueryZipCodes(Stringstate)

throwsFinderException;



publicCollectionqueryAll()

throwsFinderException;



publicCustomerLocalqueryCustomer(AddressLocaladdr)

throwsFinderException;



}



ThesecustomhomemethodsneedcorrespondingejbHome

methodsdefinedintheAddressbeanclass.Alltheydois

delegatetotheejbSelectmethodstheywrap.



publicabstractclassAddressBeanimplementsjavax.ejb.EntityBe

{

...

publicabstractCollectionejbSelectZipCodes(Stringstate)

throwsFinderException;



publicabstractCollectionejbSelectAll()

throwsFinderException;



publicabstractCustomerLocalejbSelectCustomer

(AddressLocaladdr)

throwsFinderException;



publicCollectionejbHomeQueryZipCodes(Stringstate)

throwsFinderException



{

returnejbSelectZipCodes(state);

}



publicCollectionejbHomeQueryAll()

throwsFinderException

{

returnejbSelectAll();

}



publicCustomerLocalejbHomeQueryCustomer(AddressLocaladdr)

throwsFinderException

{

returnejbSelectCustomer(addr);

}

...

}



CustomhomemethodsaredescribedbrieflyinChapter5ofthe

EJBbookandinmoredetailinChapter11.Asyoucansee,they

areextremelyusefulinexposingprivateejbSelectmethodsso

thattheycanbeinvokedbytestprogramsorbusinesslogic.All

theworkbookexampleprogramsforChapter8usethecustom

homemethodsforthispurpose.

Client_81ainvokesthesequeriesanddisplaystheiroutput.To

runit,invoketheAnttaskrun.client_81a.Remembertoset

yourJBOSS_HOMEandPATHenvironmentvariables.Theoutput

shouldlooksomethinglikethis:

C:\workbook\ex08_1>antrun.client_81a

Buildfile:build.xml



prepare:



compile:



run.client_81a:

[java]FINDMETHODS

[java]-------------------------------[java]SELECTOBJECT(c)FROMCustomerc

[java]WHEREc.lastName=?1ANDc.firstName=?2

[java]FindBillBurkeusingfindByName



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 26. Exercises for Chapter 8

Tải bản đầy đủ ngay(0 tr)

×