Chapter 1. Welcome to EJB Table of Contents Chapter 1. Welcome to EJB.......................................................................... 1 Section 1.1. OBJECTIVES............................................................................................................................................................... 2 Section 1.2. What is EJB all about?................................................................................................................................................. 3 Section 1.3. What does EJB really give me?................................................................................................................................... 4 Section 1.4. No more vendor lock-in!............................................................................................................................................. 5 Section 1.5. There are no Dumb Questions.................................................................................................................................... 6 Section 1.6. How does it all work?................................................................................................................................................... 7 Section 1.7. Behind the scenes........................................................................................................................................................ 8 Section 1.8. Sharpen your pencil................................................................................................................................................... 10 Section 1.9. Beans come in three flavors....................................................................................................................................... 11 Section 1.10. Session beans can be stateless or stateful................................................................................................................ 12 Section 1.11. There are no Dumb Question................................................................................................................................... 12 Section 1.12. Sharpen your pencil.................................................................................................................................................. 13 Section 1.13. Overheard at! THE TIKIBEAN LOUNGE................................................................................................................ 14 Section 1.14. Example: The Advice Guy bean................................................................................................................................ 15 Section 1.15. Five things you do to build a bean:.......................................................................................................................... 16 Section 1.16. EJB Roles and Responsibilities............................................................................................................................... 26 Section 1.17. Tutorial:.................................................................................................................................................................... 28 Section 1.18. Organize your project directory............................................................................................................................... 29 Section 1.19. Compile the two interfaces and the bean class....................................................................................................... 30 Section 1.20. Start the server......................................................................................................................................................... 31 Section 1.21. You'll see something like this................................................................................................................................... 31 Section 1.22. Start deploytool....................................................................................................................................................... 32 Section 1.23. you'll see something like this................................................................................................................................... 32 Section 1.24. Make a new Application.......................................................................................................................................... 33 Section 1.25. There are no Dumb Questions................................................................................................................................ 33 Section 1.26. Name and save the new application........................................................................................................................ 34 Section 1.27. What you'll see af ter you create and name the application................................................................................... 35 Section 1.28. Now let's make the new enterprise bean (the ejb-jar and the DD)........................................................................ 36 Section 1.29. Now we're in the really cool New Enterprise Bean Wizard.................................................................................... 37 Section 1.30. Create the new ejb-jar............................................................................................................................................. 38 Section 1.31. Add the three class files (including their package directory) to the JAR............................................................... 39 Section 1.32. Confirm that you added ONLY the package directory and the class files.............................................................. 40 Section 1.33. Make it a Stateless Session bean.............................................................................................................................. 41 Section 1.34. Tell it which of the three class files in the JAR is the actual BEAN class............................................................... 42 Section 1.35. Tell it which is the Home interface, and which is the Component interface......................................................... 43 Section 1.36. Verify everything on this screen!............................................................................................................................ 44 Section 1.37. You're done, click Finish.......................................................................................................................................... 45 Section 1.38. Meanwhile back on the main deploytool screen.................................................................................................... 46 Section 1.39. Run your bean through the deploytool verifier...................................................................................................... 47 Section 1.40. Close your eyes and click OK.................................................................................................................................. 48 Section 1.41. Whew! No failed tests.............................................................................................................................................. 49 Section 1.42. Time to Deploy........................................................................................................................................................ 50 Section 1.43. Make it Return a Client Jar...................................................................................................................................... 51 Section 1.44. Give it a name, so clients can look it up.................................................................................................................. 52 Section 1.45. Watch the progress bars go up, then celebrate....................................................................................................... 53 Section 1.46. Now you'll see the AdviceApp inside the server..................................................................................................... 54 Section 1.47. Now all we need is a client....................................................................................................................................... 55 Section 1.48. Organizing your project directory for the client..................................................................................................... 56 Section 1.49. The Client Code (AdviceClient.java)....................................................................................................................... 57 Section 1.50. Run the client!......................................................................................................................................................... 58 Section 1.51. COFFEE CRAM........................................................................................................................................................ 59 Section 1.52. COFFEE CRAM....................................................................................................................................................... 60 Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 1 Intro to EJB 5 4 :HOFRPHWR(-% 4 <RX·UHJRQQDORYH (-%$QGER\ZRQ·W-LPDQG %HWW\EHHQYLRXVZKHQ\RXU HQWHUSULVHEDFNHQGLVELJJHU WKDQWKHLUV\RXPLJKWHYHQ JHWWKDWSURPRWLRQ (QWHUSULVH-DYD%HDQVDUHHDV\:HOODWOHDVWZKHQ\RXFRPSDUH(-%WRZKDW \RX¶GKDYHWRGRWRZULWH\RXURZQVFDODEOHWUDQVDFWLRQDOVHFXUHFRQFXUUHQWHQWHUSULVH VHUYHU,QWKLVFKDSWHUZH¶OOGHYHORSGHSOR\DQGUXQDQ(-%DSSOLFDWLRQEHIRUHGLYLQJ LQWRWKHGHWDLOV%HIRUHZH¶UHGRQHZH¶OOORRNDWWKHXVHEHQH¿WVDQGFKDUDFWHULVWLFVRI (-%DQGZH¶OOORRN EULHÀ\ DWKRZ(-%FRQWDLQHUVZRUN:H¶OOWDNHDKLJKOHYHOORRNDWWKH DUFKLWHFWXUHRI(-%DQGOHDUQDERXWWKHWKUHHEHDQW\SHV7KHPRUH\RXXQGHUVWDQGIURP WKLVFKDSWHUWKHOHVV\RX¶OOKDYHWRPHPRUL]HODWHUVRGRQ¶WVNLSLW ,I\RX¶UHDQ(-%H[SHUW \RXFDQSUREDEO\JHWDZD\ZLWKMXVWDTXLFNVNLP WKLVLVDQHZFKDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 2 H[DPREMHFWLYHV (QWHUSULVH-DYDEHDQV2YHUYLHZ 2IILFLDO :KDWLWUHDOO\PHDQV Copyright Safari Books Online #896963 ,GHQWLI\WKHXVHEHQH¿WVDQG FKDUDFWHULVWLFVRI(QWHUSULVH -DYDEHDQVWHFKQRORJ\IRUYHUVLRQ RIWKH(-%VSHFL¿FDWLRQ <RXQHHGWRNQRZKRZ(-%ZRUNVRYHUDOO ZKDWLW¶VJRRGIRUZKDWLWSURYLGHVDQG ZKDWLWGRHVQ¶WSURYLGH <RXQHHGWRXQGHUVWDQGWKHRYHUDOO DUFKLWHFWXUHRI(-%DQGKRZWKDW DUFKLWHFWXUHVXSSRUWVWKHIHDWXUHVRI(-% )RUH[DPSOH\RXQHHGWRNQRZWKDW (-%VXSSRUWVWUDQVDFWLRQVVHFXULW\DQG FRQFXUUHQF\EXWLWGRHVQRWJXDUDQWHH ORDGEDODQFLQJIDLORYHURUFOXVWHULQJ<RX QHHGWRNQRZWKDW(-%VXSSRUWVWKUHH EHDQW\SHVVHVVLRQHQWLW\DQGPHVVDJH GULYHQDQGWKDWVHVVLRQEHDQVFDQEH VWDWHOHVVRUVWDWHIXO &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 3 LQWURWR(-% ?PI\Q[-2*ITTIJW]\' &RPSRQHQWEDVHGGHYHORSPHQW 7ITH ENTERPRISE JAVABEANS YOU CAN DEVELOP BUILDING BLOCKS%*" COMPONENTSTHAT YOU OR SOMEONE ELSE CAN ASSEMBLE AND REASSEMBLE INTO DIFFERENT APPLICATIONS &OR EXAMPLE YOU MIGHT CREATE A #USTOMER BEAN BEAN IS ANOTHER WORD FOR COMPONENT THAT REPRESENTS A CUSTOMER IN A DATABASE 9OU CAN USE THAT #USTOMER BEAN IN AN ACCOUNTING PROGRAM AN E COMMERCE SHOPPING CART SYSTEM A TECH SUPPORT APPLICATION OR VIRTUALLY ANY OTHER APPLICATION THAT MIGHT NEED TO REPRESENT A CUSTOMER )N FACT WITH SOME BEANS THE BEAN DEVELOPER AND THE APPLICATION ASSEMBLER MIGHT NOT WORK FOR THE SAME COMPANY OR HAVE ANY KNOWLEDGE OF ONE ANOTHER )F YOURE A BEAN DEVELOPER YOU MIGHT BUILD AN /RDER BEAN OR A 0AYROLL BEAN OR A 3HOPPING#ART BEAN THAT DEVELOPERS IN SOME UNRELATED COMPANY CAN BUY AND USE TO CONSTRUCT THEIR OWN CUSTOM APPLICATIONS /NE BEAUTY OF COMPONENT BASED DEVELOPMENT IS THAT YOU TAKE CODE REUSE TO A WHOLE NEW LEVEL )NSTEAD OF REUSING *AVA CLASSES YOU GET TO REUSE A BIGGER CHUNK OF FUNCTIONALITY /FTEN YOU CAN MODIFY THE WAY A BEAN WORKS WITHOUT EVER TOUCHING ITS *AVA CODE 9OULL LEARN IN THIS CHAPTER THAT WHEN YOU DEPLOY A BEAN INTO A SERVER YOU CAN CONlGURE AND CUSTOMIZE THE BEAN DECLARATIVELY THROUGH AN 8-, BASED DEPLOYMENT DESCRIPTORTO CHANGE THE WAY THE BEAN BEHAVES AT RUNTIME With component-based development, you take code reuse to a whole new level. With OO development, you reuse classes, but with components, you reuse a bigger chunk of functionality, and you can customize them without touching code! ts built componens-R-Us n by Bea &XVWRPHU FRPSRQHQW Fred w this onerote 2UGHU FRPSRQHQW &DUW FRPSRQHQW $SSOLFDWLRQ$RQOLQHVKRSSLQJ )UHGDVVHPEOHVDQRQOLQHVKRSSLQJ DSSOLFDWLRQXVLQJWZRFRPSRQHQWVKH ERXJKWIURP%HDQV58VSOXVDWKLUG FRPSRQHQW)UHGGHYHORSHGDWKLV FRPSDQ\ ilt components bu Us R ns ea B by 6XSSRUW FRPSRQHQW &XVWRPHU FRPSRQHQW ,QYHQWRU\ FRPSRQHQW Bill made these two 2UGHU FRPSRQHQW $SSOLFDWLRQ%WHFKQLFDOVXSSRUW %LOODVVHPEOHVDWHFKQLFDOVXSSRUWDSS XVLQJWZRFRPSRQHQWVKHERXJKWIURP %HDQV58VSOXVWZRFRPSRQHQWVKH GHYHORSHGKLPVHOI \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 4 EHQH¿WVRI(-% ?PI\LWM[-2*ZMITTaOQ^MUM' (-%OHWV\RXIRFXVRQWKHEXVLQHVVORJLF IRU\RXUEXVLQHVVDQGOHDYHWKHXQGHUO\LQJ VHUYLFHV WUDQVDFWLRQVQHWZRUNLQJ VHFXULW\HWF WRWKH(-%VHUYHUYHQGRU )MAGINE YOU WORK FOR 'UITAR ,AND A COMPANY THAT SELLS MUSICIANS GEAR ONLINE 9OU HAVE BETTER THINGS TO DO THAN WORK HOURS A WEEK SO WHERE WOULD YOU WANT TO SPEND YOUR TIME 7OULDNT YOU RATHER CONCENTRATE ON HOW 'UITAR ,AND DOES BUSINESS ONLINE AS OPPOSED TO WRITING YOUR OWN SECURE NETWORKED TRANSACTION MANAGEMENT SERVER 7HY NOT WORK ON WHAT YOU KNOW BEST BUSINESS LOGIC FOR YOUR PARTICULAR BUSINESS AND LEAVE THE HEAVY LIFTING IE THE BIG INFRASTRUCTURE SERVICES YOU GET FROM THE SERVER TO SOMEONE ELSE 4HE %*" MODEL IS TO LET EVERYONE DO WHAT THEY DO BESTTHE SERVER VENDORS CONCENTRATE ON THE INFRASTRUCTURE THAT MOST ENTERPRISE APPLICATIONS NEED WHILE THE BUSINESS DEVELOPERS CONCENTRATE ON THEIR OWN BUSINESS LOGIC (-%OHW·V\RXFXVWRPL]HDQG FRQÀJXUHUHXVDEOHFRPSRQHQWVDW GHSOR\WLPHZLWKRXWWRXFKLQJWKH VRXUFHFRGH (-%VHUYHUVJLYH\RXDEXQFK RIVHUYLFHVVRWKDW\RXGRQ·W KDYHWRZULWHWKHP\RXUVHOI 5 7UDQVDFWLRQPDQDJHPHQW 5 6HFXULW\ 5 &RQFXUUHQF\ 5 1HWZRUNLQJ 5 5HVRXUFHPDQDJHPHQW 5 3HUVLVWHQFH 5 0HVVDJLQJ 5 'HSOR\WLPHFXVWRPL]DWLRQ 9OU CAN CHANGE THE WAY A BEAN USES THE UNDERLYING SERVICES SIMPLY BY TWEAKING AN 8-L DOCUMENT AT DEPLOY TIME &OR EXAMPLE YOU CAN COMPLETELY DElNE THE SECURITY ACCESS CONTROL FOR A BEANS METHODS WITHIN 8-, DECLARATIVELY RATHER THAN WITHIN THE BEANS SOURCE CODE PROGRAMMATICALLY !ND YOU CAN CUSTOMIZE THE WAY A BEANS METHODS RUN IN TRANSACTIONS ALL WITHIN THE DEPLOYMENT DESCRIPTOR WITHOUT HAVING TO HARD CODE IN TRANSACTION BOUNDARIES AND BEHAVIORS 4HAT JUST ROCKS rote already iswstuff, y d o b e m So sted all th and te donÕt have to. so you &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 5 LQWURWR(-% you vendor 6WUWZM^MVLWZTWKSQV (QWHUSULVHEHDQVDUHSRUWDEOH³ QRWMXVWWRGLIIHUHQW-90·VEXW WRGLIIHUHQW(-%VHUYHUV /NE OF THE REASONS WE ALL LOVE *AVA IS ITS PORTABILITY ACROSS MULTIPLE PLATFORMS 4HE WHOLE WRITE ONCE RUN ANYWHERE 7/2! THING ,QVWHDGRIOHDUQLQJ DQHZ$3,IRUHDFKDSSVHUYHU ZLWK(-%,RQO\OHDUQ21(DQGP\ FRPSRQHQWVZLOOZRUNRQDQ\(-%VHUYHU 7KRVHYHQGRUVDUHJRQQDKDYHWRVXFNXS WR0(IRUDFKDQJH %*" TAKES PORTABILITY TO A NEW LEVEL INSTEAD OF WRITE ONCE RUN ANYWHERE ITS WRITE ONCE DEPLOY ANYWHERE 7/$! *UST AS 7/2! FREES YOU FROM BEING FORCED TO WORK ON A SINGLE /3 7/$! FREES YOU FROM BEING AT THE MERCY OF YOUR APPLICATION SERVER VENDOR !ND THEN OF COURSE THERES 9/$! BUT WE DIGRESS )N THE OLD DAYS EACH APPLICATION SERVER VENDOR HAD ITS OWN PROPRIETARY !0) 9OU LEARN IT WORK WITH IT AND lNALLY GET YOUR ENTERPRISE APPS UP AND RUNNING !ND THEN GUESS WHAT 9OU NEED A NEW FEATURE !ND THEN GUESS WHAT 9OUR VENDOR SAYS h7ERE CONSIDERING THAT FOR 1 OF NEXT YEARv .OW WHAT ,IKE A DRUG DEALER THEYVE HOOKED YOU AND NOW ITS JUST TOO PAINFUL TO CONSIDER GIVING THEM UP 'IVE THEM UP FOR WHAT !NOTHER VENDOR AND ANOTHER PROPRIETARY !0) AND MORE LOCK IN /NE OF THE CRUCIAL BENElTS OF %*" IS 7/$! !ND NOW THE VENDORS HAVE TO COMPETE NOT JUST TO SELL YOU IN THE lRST PLACE BUT TO KEEP YOU "ECAUSE AS EVERYBODY KNOWS YOU CAN JUST PACK UP YOUR BEANS AND GO ELSEWHERE \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 6 (-%TXHVWLRQV WKHUH DUH QR 'XPE 4XHVWLRQV Q: Q: Q: A: A: A: 7HATS THE DIFFERENCE BETWEEN %*" AND *%% "OTH *%% AND %*" ARE SPECIFICATIONS FOR A SERVER "UT %*" IS ACTUALLY A SUBSET OF *%% )N OTHER WORDS A *%% SERVER MUST INCLUDE AN %*" CONTAINER 7EVE USED THE WORD SERVER ON THESE FIRST PAGES BUT TECHNICALLY THE THING THAT ENTERPRISE JAVABEANS RUN IN IS CALLED AN %*" CONTAINER 3O EVERY *%% SERVER MUST INCLUDE AN %*" CONTAINER ALONG WITH A 7EB CONTAINER THAT CAN RUN SERVLETS AND *30S 4HIS EXAM IS ABOUT THE %*" PORTION OF *%% WHEREAS THE 3#7#$ EXAM 3UN #ERTIFIED 7EB #OMPONENT $EVELOPER IS ABOUT THE 7EB PORTION OF *%% ,ATER IN THIS CHAPTER WELL GET MORE INTO THE DETAILS OF HOW *%% AND %*" FIT TOGETHER &OR THE REST OF THIS BOOK WE USE THE TERMS %*" SERVER CONTAINER AND SERVER INTERCHANGEABLY )F THE DIFFERENCE BETWEEN THE TERMS MATTERS WELL MAKE IT CLEAR #AN ) USE %*" COMPONENTS WITHOUT AN %*" COMPLIANT APP SERVER .OPE %*" COMPONENTS CANT LIVE OUTSIDE OF AN %*" CONTAINER 4HEY DONT HAVE A MAIN METHOD AND EVEN IF YOU ADD ONE TO YOUR BEAN CLASS THE BEAN WOULDNT BE VERY USEFUL ON ITS OWN -OST OF THE METHODS IN AN ENTERPRISE BEAN ARE CALLED BY THE CONTAINER ITSELF AND HAVE NO MEANING OUTSIDE THE SERVER 2EMEMBER THE WHOLE POINT OF AN %*" SERVER IS TO GIVE YOU ALL THE BIG SERVICES SECURITY TRANSACTIONS ETC AND WITHOUT THE SERVER YOUD LOSE EVERYTHING BUT YOUR BASIC BUSINESS LOGIC !ND IF THAT BUSINESS LOGIC RELIES ON THE CONTAINER FOR EXAMPLE CALLING METHODS ON INTERFACES PROVIDED BY THE CONTAINER THEN EVEN THE BUSINESS LOGIC WOULD FAIL Q: #AN ) DESIGN AND WRITE MY CODE IN SUCH A WAY THAT MOST OF THE BUSINESS LOGIC IS IN A PLAIN OLD *AVA CLASS AND JUST HAVE THE BEAN CALL METHODS ON THAT CLASS 4HAT WAY ) COULD STILL REUSE THE BUSINESS LOGIC A: 9ES YOU CAN DO THAT AND IN FACT A LOT OF DESIGNERS WRITE SEPARATE NON BEAN REUSABLE CLASSES AND THEN HAVE THE BEANS INVOKE METHODS ON THOSE CLASSES )F YOUR BEAN CALLS A METHOD ON A NON BEAN *AVA CLASS THAT METHOD IS STILL UNDER THE CONTROL OF THE CONTAINER SO AS FAR AS THE CONTAINER IS CONCERNED THAT NON BEAN METHOD IS JUST PART OF THE BEANS FUNCTIONALITY 7HATS THE DIFFERENCE BETWEEN REGULAR JAVABEANS AND ENTERPRISE JAVABEANS #ONGRATULATIONS 9OURE THE MILLIONTH PERSON TO HAVE ASKED THAT QUESTION 4HE TERM hJAVABEANv MEANS A REUSABLE COMPONENT 2EGULAR OLD NON ENTERPRISE BEANS AND BEANS IS JUST A SHORTER FORM OF JAVABEANS ARE REUSABLE COMPONENTS THAT FOLLOW A NAMING CONVENTION THAT CAN BE USED BY DEVELOPMENT TOOLS "Y FAR THE MOST COMMON TYPE OF JAVABEAN IS ANY '5) COMPONENT LIKE A 3WING BUTTON OR TEXT FIELD .EARLY ALL *AVA )$%S ARE JAVABEAN COMPLIANT SO THAT IF YOURE WORKING IN A VISUAL LAYOUT TOOL YOU CAN CLICK ON A BUTTON AND UP POPS A PROPERTY SHEET WHERE YOU CAN SET THE COLOR SIZE FONT ETC 4HE TOOL KNOWS WHICH PROPERTIES THE BEAN HAS BECAUSE THE BEAN FOLLOWS CONVENTIONS FOR GETTERS AND SETTERS "UT REGULAR JAVABEANS ARENT JUST FOR '5) COMPONENTSOTHER *AVA TECHNOLOGIES INCLUDING *INI AND 3ERVLETS CAN USE JAVABEAN FEATURES %NTERPRISE JAVABEANS ARE ALSO REUSABLE COMPONENTS BUT THATS WHERE THE SIMILARITY ENDS 4HE @BEAN PART OF A REGULAR JAVABEAN IS USED MOSTLY AT DEVELOPMENT TIME AS A WAY TO EASE OR SPEED UP HOOKING ONE BEANS EVENTS TO ANOTHER BEANS METHODS OR SETTING PROPERTY VALUES WHICH OFTEN MEAN THE SAME THING AS INSTANCE VARIABLE VALUES ! REGULAR BEAN RUNS IN A *6- JUST LIKE ANY OTHER NORMAL *AVA CLASS "UT THE hBEANv PART OF AN ENTERPRISE BEAN KICKS IN AT RUNTIME AND AN ENTERPRISE BEAN MUST BE RUN UNDER THE CONTROL OF AN %*" CONTAINER &OR THE REST OF THE BOOK WHEN WE SAY BEAN WE MEAN ENTERPRISE BEAN &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 7 LQWURWR(-% 0W_LWM[Q\ITT_WZS' <RXUEHDQVUXQXQGHUWKHFRQWURO DQGSURWHFWLRQ RIWKH(-%VHUYHU 7KHVHUYHUVWHSVLQWRWKHPLGGOHRIHYHU\ PHWKRGFDOOIURPDFOLHQWWRDEHDQDQGLQVHUWV WKH´VHUYLFHVµOLNHVHFXULW\WUDQVDFWLRQVDQG SHUVLVWHQFH 9OUR BEANS LIVE AND RUN IN THE SERVER AND THE SERVER DOES VIRTUALLY EVERYTHING TO MANAGE TRANSACTIONS SECURITY PERSISTENCE AND EVEN THE LIFE AND DEATH OF YOUR OBJECTS !ND IT DOES ALL THIS BY STEPPING IN EACH TIME A CLIENT MAKES A REQUEST IE CALLS A BUSINESS METHOD ON THE BEAN 4HE SERVER JUMPS IN AND STARTS ASKING QUESTIONS LIKE h$OES THIS CLIENT HAVE SECURITY CLEARANCE TO CALL THIS METHODv OR h$OES THIS BEAN NEED TO RUN AS PART OF A LARGER TRANSACTIONv OR h$OES THIS BEAN NEED TO REFRESH ITSELF WITH DATA FROM THE DATABASE BEFORE RUNNING THAT METHOD FOR THE CLIENTv This client could bevlet, a ANYTHINGÑa serapp, stand-alone Java bean, a another enterprise mobile device... The EJB object intercepts the calls to the bean. A client can NEVER talk directly to the bean. s... HereÕs where it all happen the server steps in and layers in the services. for Most of what you pay happens here. Business logic lives here! 6HUYHU VHUYLFHV (-% REMH FW (Q (-%&RQWDLQHU DQ & OL HQWREMHFW EL]LQWHUIDFH &OLHQW WHUS VHEH UL biz logic separate from data '% $ULGLFXORXVO\KLJKOHYHOYLHZRI(-%DUFKLWHFWXUH \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 8 ZKDWUHDOO\KDSSHQV *MPQVL\PM[KMVM[ 1RWVRIDVWEXGG\ 1RERG\EXW12%2'<WDONVWR WKHEHDQH[FHSWWKHFRQWDLQHU,I\RX ZDQWWKHEHDQ\RXJRWWDJRWKURXJKPH 6KRZPHVRPH,'DQG,·OOFKHFNZLWK WKHFRQWDLQHUDQGLILW·V2.,·OO 8K\HDKSXW WKH%URNHUEHDQRQ WKHSKRQH³,QHHGWR PDNHDWUDGH12: SDVVRQ\RXUUHTXHVW +PPPPOHW·V VHHKHGRHVVHHP WREHRQWKHDSSURYHGOLVW IRUFDOOLQJPDNH7UDGH RQD %URNHUEHDQ (-%2EMHFW &OLHQW &RQWDLQHU &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 9 LQWURWR(-% :KDWGR\RXPHDQ WKHUH·VQREHDQ"-XVW JHWRQHIURPWKHIUHDNLQ·SRRO1R LWGRHVQ·WPDWWHUZKLFKRQH<HV, .12:WKHEHDQVDUHVSRLOHGOLWWOH SULPDGRQQDVEXWWKH\$5(WKHVWDUV RIWKHVKRZULJKWQRZ ,QHHGD%URNHUEHDQ GRZQKHUHQRZ6OLP6OHD]\ QHHGVWRPDNHDWUDGH2KDQG PDNHVXUHWKHEHDQJHWVKLVRZQ QHZWUDQVDFWLRQ &RQWDLQHU +H\ , ZHQWODVW WLPHVRRQHRI\RXFDQ JRQRZ%HVLGHV,·PVWLOO UHFRYHULQJIURPWKDWELJ WUDQVDFWLRQUROOEDFN &RQWDLQHU 7KH\62GRQ·WSD\ XVHQRXJKIRUWKLV :H·UHWKHRQHVZKRGRWKH5($/ EXVLQHVVORJLF%XWDWOHDVWZH QHYHUKDYHWRWDONWRWKHFOLHQWV <HDKKDYLQ·DQ(-%2EMHFW ERG\JXDUGLVSUHWW\ FRRO 2.WKHEHDQLVUHDG\ VRJRDKHDGDQGSDVV WKHFOLHQW·VPHWKRGUHTXHVWWR :K\DUHZHXVLQJWKH5," WKHEHDQ$QGE\WKHZD\\RXFDQ $UHQ·W\RXMXVWPDNLQJPHOHDUQ WHOOOLWWOHPLVV´EHDQTXHHQµVKH VRPHWKLQJWKDW,FDQQHYHUXVH EHWWHUVKDSHXS,LQVWDQWLDWHG LQWKHUHDOZRUOG":K\FDQ·WZH KHUDQG,FDQVHQGKHUWRWKH XVHD5($/DSSVHUYHU" JDUEDJHFROOHFWRU WKH%HDQ3RRO &RQWDLQHU \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 10 (-%DUFKLWHFWXUH 6KDUSHQ \RXU SHQFLO /DEHOWKHWKUHHSDUWVLQWKHGLDJUDP VHUYHU % & OL HQWREMHFW EL]LQWHUIDFH &OLHQW $ & '% 'HVFULEH EULHÀ\ ZKDWHDFKRIWKHWKUHHWKLQJVDUH UHVSRQVLEOHIRURUKRZWKH\EHKDYH $ % & &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 11 LQWURWR(-% EHDQV *MIV[KWUMQV\PZMMNTI^WZ[ (QWLW\ '% 5SE AN ENTITY BEAN TO REPRESENT A THING IN A PERSISTENT STORE 4HAT ALMOST ALWAYS MEANS SOMETHING IN A DATABASE WHERE AN INSTANCE OF AN ENTITY BEAN REPRESENTS A ROW IN A TABLE ALTHOUGH IF THE DATABASE IS NORMALIZED THE BEAN MIGHT BE DRAWING FROM ROWS IN MULTIPLE TABLES ! TYPICAL ENTITY EXAMPLE IS #USTOMER WHERE ONE ENTITY MIGHT REPRESENT "O /LEAN )$ AND ANOTHER ENTITY MIGHT REPRESENT 4RIXIA ,IN )$ 7UL[LD/LQ ,' 3DUW /DYD/DPS %R2OHDQ ,' &XVWRPHU %HDQ ,QYHQWRU\ %HDQ &XVWRPHU %HDQ 0HVVDJHGULYHQ 5SE A MESSAGE DRIVEN BEAN ONLY WHEN YOU NEED A *-3 CONSUMER )N OTHER WORDS A BEAN THAT CAN LISTEN FOR MESSAGES FROM A *-3 MESSAGING SERVICE #LIENTS NEVER CALL MESSAGE DRIVEN BEANS DIRECTLY IN ORDER TO GET A MESSAGE DRIVEN BEAN TO DO SOMETHING A CLIENT MUST SEND A MESSAGE TO A MESSAGING SERVICE 4HAT MEANS A MESSAGE DRIVEN BEAN HAS NO %*"/BJECT BECAUSE THE SERVER GETS THE CLIENT REQUESTS DIRECTLY FROM A MESSAGING SERVICE RATHER THAN AS A CALL FROM THE CLIENT TO THE BEAN ! TYPICAL MESSAGE DRIVEN BEAN MIGHT BE A .EW#USTOMER.OTIlCATION SUBSCRIBER ,·PZDLWLQJIRU DQQRXQFHPHQWVDERXW QHZFXVWRPHUV ,·PZDLWLQJIRUD ELJFDOFXODWLRQMRE WREHSRVWHG 1HZ&XVWRPHU/LVWHQHU %HDQ %LJ&DOF-RE/LVWHQHU %HDQ 6HVVLRQ 5SE A SESSION BEAN FOR EVERYTHING ELSE ,YHULI\ !LMOST ANY KIND OF BACK END SERVICE CAN FUHGLWFDUGV AND OFTEN SHOULD BEGIN WITH A SESSION BEAN 7HERE AN ENTITY BEAN REPRESENTS A THING A SESSION BEAN TYPICALLY REPRESENTS A PROCESS 4O ,·PDQRQOLQH PUT IT ANOTHER WAY WHEN YOU THINK OF ENTITY VKRSSLQJVHVVLRQ &DUG9HUL¿HU BEANS THINK NOUN AND WHEN YOU THINK OF %HDQ SESSION BEANS THINK VERB ! SHOPPING SESSION IS A TYPICAL EXAMPLE OF A SESSION BEAN WHILE A CREDIT CARD PROCESSING SYSTEM MIGHT BE ANOTHER SESSION BEAN &DUW%HDQ ,GRJHQHWLF PDWFKLQJ *HQH%HDQ \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 12 VWDWHIXODQGVWDWHOHVVVHVVLRQEHDQV ;M[[QWVJMIV[KIVJM[\I\MTM[[ WZ[\I\MN]T 7ELL GO OVER ALL THIS IN DETAIL IN THE 3ESSION "EAN CHAPTER &OR NOW YOU NEED TO KNOW THAT SESSION BEANS CAN BE MARKED AT DEPLOYMENT TIME AS EITHER STATELESS OR STATEFUL ! STATEFUL BEAN CAN REMEMBER CONVERSATIONAL STATE BETWEEN METHOD CALLS WHILE A STATELESS BEAN WONT REMEMBER ANYTHING ABOUT THE CLIENT BETWEEN METHOD INVOCATIONS 4HE PHRASE hCONVERSATIONAL STATEv REALLY MEANS hCLIENT SPECIlC STATEv AND A TYPICAL EXAMPLE IS A SHOPPING CART )T WOULDNT BE FUN IF YOU THE SHOPPER GOT A CART PUT SOMETHING IN BUT THEN WHEN YOU GO TO PUT THE SECOND THING IN THE lRST THING VANISHES FROM THE CART .OT TOO USER FRIENDLY 3O A GOOD SHOPPING CART WILL KEEP THE CLIENT SHOPPER STATE IE THE ITEMS IN THE CART FOR AS LONG AS THE SHOPPING SESSION IS ALIVE 7ELL EXPLAIN WHAT WE MEAN BY ALIVE IN THE 3ESSION "EAN CHAPTER 3TATELESS BEANS SIMPLY FORGET ABOUT THE CLIENT ONCE THE METHOD CALL COMPLETES 3O STATELESS BEANS ARE FOR SERVICES THAT DONT REQUIRE A CONTINUED CONVERSATION BETWEEN THE CLIENT AND THE SERVICE 4HAT DOESNT MEAN THE CLIENT WONT KEEP CALLING METHODS ON THE STATELESS BEAN BUT IT DOES MEAN THAT THE CLIENT CANT DEPEND ON THE BEAN REMEMBERING ANYTHING ABOUT THE PREVIOUS METHOD CALLS :DWFKLW WKHUH DUH QR 'XPE 4XHVWLRQV Q: )VE HEARD THAT ONLY STATELESS SESSION BEANS ARE SCALABLE AND THAT NOBODY SHOULD EVER USE STATEFUL SESSION BEANS )S THAT TRUE A: .O NOT COMPLETELY )T IS TRUE THAT STATELESS SESSION BEANS ARE GENERALLY MORE SCALABLE THAN STATEFUL SESSION BEANS BECAUSE OF THE WAY STATELESS BEANS ARE MANAGED BY THE CONTAINER 9OULL SEE THE REASONS FOR THIS IN THE 3ESSION "EAN CHAPTER "UT THAT DOESNT MEAN YOU SHOULD NEVER USE STATEFUL BEANS 9OU SHOULD CONSIDER STATEFUL BEANS WHEN YOU NEED CONVERSATIONAL STATE AND WHEN THE ALTERNATIVES FOR SAVING THAT STATE LIKE USING THE CLIENT TO STORE STATE OR USING A SERVLET TO STORE STATE OR USING A DATABASE TO STORE STATE BETWEEN EACH METHOD CALL FROM THE CLIENT ARE MORE OF A PERFORMANCE HIT THAN THE LESS SCALABLE NATURE OF STATEFUL SESSION BEANS 6WDWHOHVVEHDQV&$1 KDYHVWDWH -XVWQRW FOLHQWVSHFLÀFVWDWH OHVV´PHDQV 6RPHSHRSOHWKLQN³VWDWH FDQKDYH ³QRVWDWH´$VWDWHOHVVEHDQ RWKHUREMHFW LQVWDQFHYDULDEOHVOLNHDQ\ DLQWDLQYDOXHV LWMXVWFDQ¶WXVHWKHPWRP QW VSHFL¿FWRDSDUWLFXODUFOLH &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 13 LQWURWR(-% LQJ ,6VRPHWK Q D H E \ LW W $QHQ RPHWKLQJ V 6 ( 2 ' Q D H $VHVVLRQE 6KDUSHQ \RXU SHQFLO .QRZ\RXUEHDQW\SHV /RRNDWWKHSUREOHPGHVFULSWLRQRQWKHOHIWDQGSXW DFKHFNPDUNIRUWKHEHDQW\SHWKDWZRXOGEHVW¿W WKHSUREOHP7KHUHLVQ¶WRQHSHUIHFWULJKWDQVZHU IRUWKHVH\RXPLJKWGHFLGHWKDWRQHEHDQW\SHZLOO ZRUNLI\RXDSSURDFKLWRQHZD\EXWDQRWKHUEHDQ ZLOOZRUNLI\RXVROYHWKHSUREOHPLQDGLIIHUHQWZD\ (QWLW\ 0HVVDJHGULYHQ 6HVVLRQEHDQ FLUFOHVWDWHOHVVVWDWHIXORUERWK %RRNLQJDWLFNHWIRUDURFNFRQFHUW VWDWHIXOVWDWHOHVV $EDQNDFFRXQW VWDWHIXOVWDWHOHVV 6HDUFKLQJDSURGXFWGDWDEDVH VWDWHIXOVWDWHOHVV 'DWLQJVHUYLFHPDWFKXSV VWDWHIXOVWDWHOHVV 5HFHLYLQJVXEPLWWHGH[SHQVHUHSRUWVDQG VHQGLQJWKHPRXWIRUDSSURYDO VWDWHIXOVWDWHOHVV 2QOLQHH[SHUWGLDJQRVLV²\RXGHVFULEHD V\PSWRPDQGWKHV\VWHPKHOSV\RXGHWHU PLQHWKHFDXVH VWDWHIXOVWDWHOHVV 7KHERRNVLQDOLEUDU\ VWDWHIXOVWDWHOHVV \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 14 7LNLEHDQ/RXQJH RYHUYLHZ EHDQEXOOHWSRLQWV VHVVLRQEHDQDQGEDUWHQGHU :MDD=LHGAFLK (-%LVDFRPSRQHQWEDVHG GHYHORSPHQWPRGHO &RPSRQHQWVDUHUHXVDEOHFKXQNV 6HVVLRQEHDQ,·PVRWLUHGRIGRLQJDOOWKHZRUNDQGJHWWLQJQRQH RIWKHJORU\ %DUWHQGHU:KDWGR\RXPHDQ´QRQHRIWKHJORU\·µ"$UHQ·W\RXWKH RQO\EHDQWKDW·VEHHQUHTXLUHGE\WKHVSHFVLQFHWKHYHU\EHJLQQLQJ" 6LQFH(-%" 6HVVLRQEHDQ)DWORWRIJRRG7+$7GRHVPH$OODQ\RQHZDQWV WRWDONDERXWQRZLVHQWLW\EHDQV(QWLW\EHDQVHQWLW\EHDQVHQWLW\ EHDQV1RWWKDW,GRQ·WOLNHWKHP³VRPHRIP\EHVWIULHQGVDUH HQWLW\EHDQVEXW,ZLVKSHRSOHZRXOGWDONDERXWZKDW,GR %DUWHQGHU1RZWKDW\RXPHQWLRQLWHQWLW\EHDQVDUHPRVWO\ ZKDWIRONVWDONDERXWKHUHDWWKHEDUZKDWZLWKWKHELJ&03 LPSURYHPHQWVLQ(-% 6HVVLRQEHDQ$QGWKDW·VDQRWKHUWKLQJZKDWLVWKH%LJ'HDOZLWK &03",W·VMXVWJRLQJWRDGDWDEDVH6HULRXVO\WHOOPH:+$7LVVR VSHFLDODERXWWKDW"´2RRKKKORRN,WXSGDWHGDUHFRUGµ3OHDVH %DUWHQGHU<HDKEXWWKHSURJUDPPHUVDURXQGKHUHVHHPWROLNH QRWKDYLQJWRGRDOOWKHGDWDEDVHFRGHQRZ$QGWKHUH·VVRPHWKLQJ DERXWSHUVLVWHQWUHODWLRQVKLSV,MXVWFDQ·WTXLWHUHPHPEHU 6HVVLRQEHDQ&05&RQWDLQHUPDQDJHGUHODWLRQVKLSV2.HYHQ ,KDYHWRDGPLWWKDW&05PDNHVWKLQJVDORWHDVLHUIRUGHYHORSHUV %XWWKDW·VQRWZKDWEXJVPH³,.12:HYHU\ERG\OLNHVHQWLW\EHDQV EXWZKDWDERXW0(":KDWDERXWHYHU\WKLQJ,GR"(QWLW\EHDQV UHSUHVHQWWKLQJVLQWKHV\VWHPEXWZLWKRXWPHWKRVHWKLQJVGRQ·W GRPXFK0D\EHDQHQWLW\KDVVRPHJHWWHUVDQGVHWWHUVDQGVRPH TXHULHVVXUHEXWQRWDORWRIEXVLQHVVORJLF7RXVHHQWLW\EHDQVLQDQ DSS\RXSUHWW\PXFK+$9(WRXVHVHVVLRQEHDQVWRGRWKHEXVLQHVV SURFHVVLQJ/LNHDQHQWLW\EHDQPLJKWUHSUHVHQWWKHGULQNV\RXVHOO KHUHDQGWKHLQGLYLGXDOFXVWRPHUVEXWZKDWJRRGDUHGULQNVDQG FXVWRPHUVZLWKRXWDEDUWHQGHU"<RXQHHGVRPHRQHWRDFWXDOO\SXW WKHHQWLWLHV WKHGULQNVDQGWKHFXVWRPHUV WRJHWKHULQDPHDQLQJIXO ZD\$QGWKDW·VZKDWVHVVLRQEHDQVGR:HGRWKHGHDOV:HZRUN ZLWKWKHFOLHQWWRJHWVRPHWKLQJGRQHZKLOHHQWLWLHVMXVWVLWWKHUH ZDLWLQJIRUVHVVLRQEHDQVWRXVHWKHP+H\FDQ,JHWDQRWKHURQHRI WKRVH"$QGGRQ·WHYHQJHWPHVWDUWHGRQPHVVDJHGULYHQEHDQV RIIXQFWLRQDOLW\\RXFDQPRGLI\IRU GLIIHUHQWDSSOLFDWLRQVZLWKRXWWRXFKLQJ WKHMDYDVRXUFHFRGH 2QHEHQHILWRI(-%LV:2'$³:ULWH 2QFH'HSOR\$Q\ZKHUH<RXFDQ GHSOR\\RXU(-%FRPSRQHQWV WRDQ\DSSVHUYHUWKDW·V(-% FRPSOLDQW :2'$PHDQV\RXKDYHWROHDUQ RQO\RQHVWDQGDUG$3,UDWKHUWKDQ SURSULHWDU\YHQGRUVSHFLILF$3,V 7KH(-%DUFKLWHFWXUHXVHVDQ (-%2EMHFWWRLQWHUFHSWFOLHQWFDOOVWRD EHDQ7KLVJLYHVWKHVHUYHUFRQWDLQHU DFKDQFHWRVWHSLQDQGDGGVHUYLFHV (-%VHUYLFHVLQFOXGHWUDQVDFWLRQV VHFXULW\UHVRXUFHPDQDJHPHQW QHWZRUNLQJDQGSHUVLVWHQFH %HDQVFRPHLQWKUHHIODYRUV(QWLW\ 6HVVLRQDQG0HVVDJHGULYHQ (QWLW\EHDQVUHSUHVHQWDXQLTXHO\ LGHQWLILDEOHWKLQJLQDSHUVLVWHQW VWRUHXVXDOO\WKDWPHDQVDURZLQD GDWDEDVHWDEOH 0HVVDJHGULYHQEHDQVDUH-06 PHVVDJLQJVHUYLFHFRQVXPHUV 6HVVLRQEHDQVDUHHYHU\WKLQJHOVH 6HVVLRQEHDQVFDQEHVWDWHIXORU VWDWHOHVV 6WDWHIXOEHDQVFDQUHPHPEHU ´FRQYHUVDWLRQDOVWDWHµZLWKDFOLHQW ZKLOHVWDWHOHVVEHDQVFDQQRW >7REHFRQWLQXHG@ &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 15 LQWURWR(-% 7UHDW\RXUVHOIWR DFROGRQH<RX GHVHUYHLW 7HOO\RXUERVV WKHUHSRUWZLOO KDYHWRZDLW7KHUH·V SRZGHUDW$VSHQ -`IUXTM" 7KDWVKDGHRI <PM)L^QKM/]aJMIV JUHHQLVQ·WUHDOO\ ZRUNLQ·IRU\RX "EFORE WE GET INTO THE GUTS OF %*" LETS LOOK AT HOW TO DEVELOP DEPLOY AND TEST A BEAN FROM START TO lNISH )F YOURE NOT ALREADY FAMILIAR WITH %*" YOU WONT UNDERSTAND EVERYTHING HERE $ONT WORRY ABOUT IT NOW WELL lGURE IT ALL OUT IN LATER CHAPTERS 4HIS IS JUST TO GIVE YOU A FEELING FOR WHAT ITS LIKE TO GET A BEAN UP AND RUNNING /UR lRST BEAN IS FOR THE !DVICE 'UY SERVICEA REMOTE SERVICE THAT GIVES BACK AN ADVICE 3TRING EACH TIME THE CLIENT MAKES A REQUEST 7ELL SPEND THE NEXT SEVERAL PAGES LOOKING AT THE PROCESS AND THEN WELL ACTUALLY MAKE THIS BEAN AS A TUTORIAL 7KH$GYLFH*X\ 2XU¿UVWEHDQLVIRUWKH$GYLFH*X\ VHUYLFH(DFKWLPHWKHFOLHQWPDNHV DUHTXHVWWKH$GYLFH*X\VHUYLFH DQHQWHUSULVHMDYDEHDQ JLYHVEDFN DSLHFHRIVWXQQLQJO\KHOSIXO DQG SUHWHUQDWXUDOO\DSSURSULDWH DGYLFH ,Q+HDG)LUVW-DYDZHGHSOR\HGWKH$GYLFH *X\VHUYLFHXVLQJVWUDLJKW7&3VRFNHWV1RZ IRURQO\ILYHWLPHVWKHDPRXQWRIFRGHDQG HIIRUWZHJHWWRKDYHWKHVDPHVHUYLFHLQ(-% 2IFRXUVHLIRQHIHOWOLNHLWRQHFRXOGDUJXH WKDWWKH$GYLFH*X\GRHVQ·WUHDOO\QHHGDOO WKRVH(-%VHUYLFHVEXWZHGLVDJUHH:H·UH DOUHDG\SODQQLQJWKH,32IRUWKLVEDE\ \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 16 Return to Table of Contents ¿YHVWHSVWREXLOGLQJDEHDQ .Q^M\PQVO[aW]LW\WJ]QTLIJMIV" &RGHWKHEHDQFODVVZLWKDOORIWKH EXVLQHVVPHWKRGV &RGHWZRLQWHUIDFHVIRUWKHEHDQ KRPHDQGFRPSRQHQW &UHDWHDQ;0/GHSOR\PHQWGHVFULSWRU WKDWWHOOVWKHVHUYHUZKDW\RXUEHDQLVDQG KRZLWVKRXOGEHPDQDJHG<RXPXVWQDPHLW HMEMDU[PO 3XWWKHEHDQWKHLQWHUIDFHVDQGWKH GHSOR\PHQWGHVFULSWRULQWRDQHMEMDU¿OH 7KHUHPLJKWEHPRUHWKDQRQHEHDQLQWKH HMEMDUEXWWKHUHZLOODOZD\VEHMXVWRQH GHSOR\PHQWGHVFULSWRU 'HSOR\WKHEHDQLQWRWKHVHUYHUXVLQJWKH WRROVSURYLGHGE\WKHVHUYHUYHQGRU MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ $GYLFH%HDQMDYD MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ 7KHVHDUHWKH VWHSV\RX·OOJRWKURXJK IRUDOPRVWHYHU\EHDQ\RX·OO PDNH&RGLQJLVVLPSOHWKH WULFN\SDUWLVGHSOR\LQJ $GYLFH+RPHMDYD $GYLFHMDYD '2&7<3( HMEMDU 38%,QF HMEMDU[PO -$5 HMEMDU <RXGRQ¶WQHHGWREHDQ;0/H[SHUW ,QIDFW\RXGRQ¶WKDYHWRNQRZDQ\WKLQJDWDOODERXWKRZ;0/ZRUNV<RXGRQHHG WRNQRZDERXWPDQ\RIWKHGHSOR\PHQWGHVFULSWRUWDJVEXW\RXGRQ¶WQHHGWRNQRZ DERXW;0/LQRUGHUWROHDUQWKHWDJV,I\RXWKLQNRIWKHWDJVDVVLPSO\ODEHOVLQDGRFXPHQW ZLWKYHU\VSHFL¿FUHTXLUHPHQWVIRUZKDW\RX¶UHDOORZHGWRW\SHZLWKLQWKRVHODEHOVWKHQDOO\RX QHHGWRNQRZIRUWKHH[DPLVWKHQDPHDQGUHTXLUHPHQWVIRUVRPHRIWKHPRVWLPSRUWDQWODEHOV WDJV :H¶OOORRNDWWKRVHFUXFLDOWDJVLQVHYHUDOFKDSWHUV &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 17 Return to Table of Contents LQWURWR(-% EHDQFODVV LQWHUIDFHV ;0/'' HMEMDU GHSOR\ MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ $GYLFH%HDQMDYD :ULWHWKHEHDQFODVVZLWK WKHDFWXDOEXVLQHVVPHWKRGV WKHFOLHQWFDOOV 4HIS IS WHERE IT ALL HAPPENS 4HE IMPLEMENTATION OF YOUR BUSINESS METHODS DElNED IN THE COMPONENT INTERFACE )N OTHER WORDS YOU WRITE YOUR BUSINESS LOGIC IN THE BEAN CLASS 4HERE ARE THREE BEAN TYPES TO CHOOSE FROM 3ESSION %NTITY AND -ESSAGE DRIVEN AND WELL COVER EACH ONE IN DETAIL IN LATER CHAPTERS OF THE BOOK "EFORE MAKING A BEAN THOUGH YOU MUST DECIDE WHAT TYPE YOU NEED BECAUSE YOUR BEAN CLASS MUST IMPLEMENT ONE OF THREE INTERFACES DEPENDING ON THE TYPE YOU CHOOSE 7EVE CHOSEN A 3ESSION BEAN HERE BECAUSE ITS PERFECT FOR THE !DVICE 'UY APPLICATION !DVICE 'UY GIVES BACK AN ADVICE 3TRING WHEN YOU INVOKE THE SURPRISINGLY NAMED GET!DVICE METHOD 3O OUR BEAN CLASS ON THE NEXT PAGE IMPLEMENTS THE 3ESSION"EAN INTERFACE !ND 3ESSION"EAN ISNT JUST A MARKER INTERFACE IT HAS METHODS YOUR BEAN CLASS MUST IMPLEMENT 4HE METHODS YOU IMPLEMENT FROM THE 3ESSION"EAN INTERFACE ARE KNOWN AS CONTAINER CALLBACKS BECAUSE THE CONTAINER USES THEM TO NOTIFY YOU OF IMPORTANT MILESTONES IN THE BEANS LIFE ness ly one busi we have ofnor the AdviceBean method SXEOLF6WULQJJHW$GYLFH ^ DGYLFHJHQHUDWLQJFRGH ` QRZKRZ <RXPXVWN KHFRGH OW DO H ULW Z WR RXW LWK Z G DQ LW E\K :DWFK DG\ UH -% ( DQ XVLQJ O RR WW HQ SP GHYHOR LWH ZU WR WRNQRZKRZ <RX¶UHH[SHFWHG QFODVV HD HE WK QG FHVD WKHWZRLQWHUID MXVWWKH GULYHQEHDQV XOG RUIRUPHVVDJH KR XV \R QV KDWPHD QW EHDQFODVV 7 UHGHYHORSPH ZD D -% Q( 127XVHD IWKHEHDQRU HR RP VV LOG WRROWKDWEX X¶UH RU\RXXQWLO\R LQWHUIDFHFRGHI KDWFRGHWKH Z WO\ DF H[ Z FHUWDLQ\RXNQR RU\RX WRROLVFUHDWLQJI WR IUXOHV\RXQHHG HR SLO D H¶V HU 7K H[WIHZ HQ WK LQ QG DPD WDLOV NQRZIRUWKHH[ GH U\ JR KH HWLQWRW L]LQJ FKDSWHUVZH¶OOJ RU HP WP RX RUU\DE )RUQRZGRQ¶WZ RGHLQWKLVH[HUFLVH WKHF DQ\WKLQJIURP WROHDUQLW \RX:,//QHHG -XVWNQRZWKDW H RQ HG DOOEHIRUHZH¶U YHU² RRGQHZVKRZH 0/ 7KHUHLVVRPHJ H; WK LOG EX WR WRRO \RX&$1XVHD DP H[ KH U7 FULSWR OWKH GHSOR\PHQWGHV RPHPRUL]HDO XW \R FW SH H[ GRHVQ¶W RXZLOOQHHGWR K\ XJ WKR $O HP ;0/WDJV ZH¶OOFRYHUWK WV HQ HP HO NQRZDIHZ ODWHU $PDUNHULQWHUIDFH DOVRFDOOHGDWDJLQWHUIDFH KDV QRPHWKRGVWRLPSOHPHQWDQGH[LVWVVRWKDW\RXFDQ DQQRXQFHWRWKHZRUOGWKDW´<HV,FDQGRWKLVµ \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 18 WKHEHDQFODVV MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ %HDQFODVV $GYLFH%HDQMDYD 4HE !DVICE"EAN IMPLEMENTS THE 3ESSION"EAN INTERFACE SO IT MUST IMPLEMENT THE METHODS DECLARED IN JAVAXEJB3ESSION"EAN 7ELL GRILL YOU ON EVERYTHING A LITTLE LATER FOR NOW JUST REMEMBER THAT THE BEAN CLASS IS WHERE YOUR ACTUAL BUSINESS LOGIC GOES )N OTHER WORDS THE REASON YOUR BEAN EXISTS IN THE lRST PLACE &OR THE !DVICE 'UY THAT MEANS THE GET!DVICE METHOD SDFNDJHKHDG¿UVW LPSRUWMDYD[HME age you need this pack ree bean type ent one of the esthsageDriven) em pl im T US M u Yo n, Entity, or M interfaces (Sessio SXEOLFFODVV$GYLFH%HDQLPSOHPHQWV6HVVLRQ%HDQ^ SULYDWH6WULQJ>@DGYLFH6WULQJV ^³2QHZRUGLQDSSURSULDWH´³<RXPLJKW ZDQWWRUHWKLQNWKDWKDLUFXW´³<RXUERVVZLOOUHVSHFW\RXLI\RXWHOOKLP ZKDW\RX5($//<WKLQNRIKLP´³9LVXDOL]H\RXUVHOIZLWKEHWWHUFORWKHV´ ³2IFRXUVH\RXGRQ¶WKDYHWRJRWRZRUNWRGD\´³'R\RXUHDOO\WKLQN\RX VKRXOGEHOHDYLQJWKHKRXVHOLNHWKDW"´³5HDGDERRNRQFHD\HDUZKHWKHU \RXQHHGWRRUQRW´` The business method (ge tAdvice()) randomly SXEOLFYRLGHME$FWLYDWH ^ one of these Strings to picks 6\VWHPRXWSULQWOQ ³HMEDFWLYDWH´ return. ` SXEOLFYRLGHME3DVVLYDWH ^ 6\VWHPRXWSULQWOQ ³HMESDVVLYDWH´ ` SXEOLFYRLGHME5HPRYH ^ 6\VWHPRXWSULQWOQ ³HMEUHPRYH´ ` SXEOLFYRLGVHW6HVVLRQ&RQWH[W 6HVVLRQ&RQWH[WFW[ ^ 6\VWHPRXWSULQWOQ ³VHVVLRQFRQWH[W´ ` om the ods are fryou have h t e m r u o These f ean interface, so his simple SessionB hem in here. For t anything to put t donÕt need to do got print bean, we ethods, but weÕve when (or in the m ts so you can see donÕt statemen e called. For now, e for ! if) theyÕrout what these ar worry ab SXEOLF6WULQJJHW$GYLFH ^ 6\VWHPRXWSULQWOQ ³LQJHWDGYLFH´ LQWUDQGRP LQW 0DWKUDQGRP DGYLFH6WULQJVOHQJWK UHWXUQDGYLFH6WULQJV>UDQGRP@ ` SXEOLFYRLGHME&UHDWH ^ 6\VWHPRXWSULQWOQ ³LQHMEFUHDWH´ ` ` Finally! The actual business method from th component interface e the whole point of th. eItÕs bean... the thing the clie nt wants to call. You must have an ejb eat e() method. ItÕs an EJB rule youÕllCrlea later. But it does not comrn about the SessionBean interface e from . &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 19 LQWURWR(-% EHDQ LQWHUIDFHV ;0/'' HMEMDU GHSOR\ LPSRUW MDYD[HME LPSRUWMDYD FHSWLRQ MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ $GYLFHMDYD $GYLFH+RPHMDYD :ULWHWZRLQWHUIDFHVIRUWKHEHDQ 4HESE ARE THE INTERFACES THE CLIENT SEES 7E HAVE AN ENTIRE CHAPTER DEVOTED TO THESE INTERFACES SO YOU DONT HAVE TO UNDERSTAND IT ALL NOW &20321(17LQWHUIDFH 4HIS IS WHERE ALL THE BUSINESS METHODS ARE DECLARED )N OTHER WORDS ITS WHERE YOU PUT THE METHODS THE CLIENT WANTS TO CALL SDFNDJHKHDG¿UVW LPSRUWMDYD[HME LPSRUWMDYDUPL5HPRWH([FHSWLRQ LPSRUW MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ $GYLFHMDYD FRPSRQHQW LQWHUIDFH EXVLQHVV PHWKRGV o you need theseentwts. em at st import t interface, her the EJBOsebjeeclater It must extend eit Õll ct, which we or EJBLocalObje SXEOLFLQWHUIDFH$GYLFHH[WHQGV(-%2EMHFW^ You must declare RemoteException on all methods in SXEOLF6WULQJJHW$GYLFH WKURZV5HPRWH([FHSWLRQ this interface! This is the actu ` al business met whole reas hod. he correspondontothe bean exists). It MU(tST a method in th e bean class. +20(LQWHUIDFH 4HE CLIENT USES THE HOME INTERFACE TO ASK FOR A REFERENCE TO THE COMPONENT INTERFACE 4HE HOME IS THE CLIENTS STARTING POINT FOR GETTING HOLD OF A REFERENCE TO A BEAN OR AT LEAST WHAT THE CLIENT THINKS IS THE BEAN BUT WELL GET TO THAT LATER &OR NOW THINK OF THE HOME AS A KIND OF FACTORY THAT MAKES AND DISTRIBUTES BEAN REFERENCES TO CLIENTS LPSRUW MDYD[HME LPSRUWMDYD FHSWLRQ $GYLFH+RPHMDYD KRPH LQWHUIDFH DIDFWRU\ IRUEHDQ UHIHUHQFHV statements same import as above. EJBHome LPSRUWMDYD[HME tend either the er The home mustEJexBLocalHome, which weÕll see lat, LPSRUWMDYDUPL5HPRWH([FHSWLRQ ns io pt ce ex interface, or O need TW ption This time weption and RemoteExce ce x eE SXEOLFLQWHUIDFH$GYLFH+RPHH[WHQGV(-%+RPH^ Creat SDFNDJHKHDG¿UVW SXEOLF$GYLFHFUHDWH ` WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ the create() meth component interfacode must return your type!! \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 20 WKHEHDQLQWHUIDFHUHODWLRQVKLS :DLWMXVWDPLQXWH KHUHKRZFRPHZH·UH PDNLQJWKHEHDQFODVV EHIRUHWKHLQWHUIDFHV" 7KDWGRHVQ·WVRXQGULJKW $QGWKDW·VQRWHYHQWKHZRUVW WKLQJ$GYLFH%HDQGRHVQ·W LPSOHPHQWWKH$GYLFHLQWHUIDFH"/HWPH JHWWKLVVWUDLJKW\RX0867KDYHWKHVDPH PHWKRGVLQWKHEHDQWKDW\RXKDYHLQWKH FRPSRQHQWLQWHUIDFH\HW\RXGRQ·WPDNHWKH EHDQLPSOHPHQWWKHLQWHUIDFH" (MMM TWO GOOD QUESTIONS )N A NON BEAN *AVA WORLD THE WAY WERE DOING THINGS HERE WOULDNT MAKE MUCH SENSE "UT BEAN WORLD HAS DIFFERENT RULES AND PRACTICES 7E COULD WRITE THE INTERFACES lRST AND SOME DEVELOPERS DO 3OMETIMES THE CHOICE OF WHICH TO DEVELOP lRST DEPENDS ON THE DEVELOPMENT TOOLS YOURE USING TO BUILD YOUR BEANS 3OME TOOLS FOR EXAMPLE EXPECT YOU TO lRST BUILD YOUR BEAN CODING THE ACTUAL BUSINESS LOGIC AND THEN THE TOOL WILL BUILD THE INTERFACES TO MATCH !ND SOME TOOLS DO JUST THE OPPOSITE LOOKING AT THE INTERFACES AND BUILDING A hYOUR CODE GOES HEREv BEAN CLASS WITH ALL OF THE METHODS FROM THE INTERFACE &OR LEARNING %*" WE LIKE TO START WITH THE BEAN FOCUSING ON THE BUSINESS LOGIC BEFORE lGURING OUT THE INTERFACES ,ATER IN THE BOOK WELL DO IT THE OTHER WAY AROUND !S FOR THE BEAN NOT IMPLEMENTING THE COMPONENT INTERFACE YOU COULD DO IT THAT WAY BUT THIS TIME WE STRONGLY URGE YOU NOT TO /N THE NEXT PAGE WELL LOOK AT THIS IN MORE DETAIL &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 21 LQWURWR(-% WKHUH DUH QR 'XPE 4XHVWLRQV Q: 3O WHY DOESNT THE BEAN CLASS !DVICE"EAN IMPLEMENT THE COMPONENT INTERFACE !DVICE IF IT HAS TO IMPLEMENT THE SAME METHODS *AVA CLASSES ARE ALLOWED TO IMPLEMENT MORE THAN ONE INTERFACE SO WHATS THE PROBLEM WITH SAYING Q: FODVV$GYLFH%HDQLPSOHPHQWV $GYLFH6HVVLRQ%HDQ "UT THERES AN EASY SOLUTION TO 4(!4 PROBLEM YOU COULD HAVE THE BEAN EXTEND A CLASS THAT HAS ALL THE IMPLEMENTATIONS THAT YOU NEED TO SATISFY THE COMPILER BUT DONT REALLY NEED TO IMPLEMENT IN YOUR CODE ,IKE THE ADAPTER EVENT LISTENER CLASSES IN !74 7HY NOT DO SOMETHING LIKE THAT HERE AND MAKE A SUPERCLASS FOR YOUR BEAN THAT IMPLEMENTS THE METHODS A: A: ,EGALLY THE BEAN CLASS CAN IMPLEMENT THE COMPONENT INTERFACE BUT THE SPEC DOESNT RECOMMEND IT 2EMEMBER ALTHOUGH TO THE CLIENT IT LOOKS AS THOUGH THE !DVICE"EAN IS THE OBJECT THE CLIENTS INVOKING METHODS ON THE OBJECT THAT IMPLEMENTSIN THE TRUE *AVA SENSETHE COMPONENT INTERFACE THE CLIENT REALLY INVOKES METHODS ON SOMETHING CALLED AN %*"/BJECT THATS IMPLEMENTED BY THE SERVER AT DEPLOY TIME 4HE CLIENT NEVER INTERACTS DIRECTLY WITH THE BEAN .EVER EVER EVER ,ATER IN THE BOOK YOULL SEE THAT IF THE BEAN DOES IMPLEMENT THE COMPONENT INTERFACE YOU COULD SNEAK THINGS PAST THE COMPILER THAT WOULD EXPLODE AT RUNTIME 3O WE STRONGLY URGE YOU NOT TO HAVE YOUR BEAN IMPLEMENT THE COMPONENT INTERFACE "UT THERES ANOTHER ISSUE AS WELLTHE COMPONENT INTERFACE EXTENDS ANOTHER INTERFACE )N OUR EXAMPLE !DVICE EXTENDS %*"/BJECT AND %*"/BJECT IS NOT A MARKER INTERFACE )T HAS METHODS 4HIS MEANS THAT ANY CLASS IMPLEMENTING !DVICE MUST ALSO IMPLEMENT THE METHODS OF %*"/BJECT 9ES YOU COULD DO THAT AND IT WOULD BE LEGAL "UT IT STILL MEANS YOUR BEAN IS CAPABLE OF HAVING METHODS INVOKED THAT THE BEAN SHOULD NEVER KNOW ABOUT 4HE METHODS OF %*"/BJECT ARE METHODS FOR THE CLIENT TO CALL ON THE BEAN BUT ./4 FOR THE BEAN TO ACTUALLY IMPLEMENT 3O ITS NOT THE BEST // PRACTICE !ND THERES STILL ANOTHER REASON WHY ITS NOT GOOD PRACTICE TO HAVE THE BEAN IMPLEMENT THE COMPONENT INTERFACEIF THE INTERFACE IS REMOTE AND %*"/BJECT IS SINCE IT EXTENDS THE JAVARMI2EMOTE INTERFACE THAT WOULD MAKE THE BEAN CLASS A 2EMOTE CLASS AND THAT MUST NEVER BE 4HE BEAN IS PROTECTED BY THE SERVER AND MUST NEVER BE ACCESSED IN ANY OTHER WAY BY ANYTHING BUT THE SERVER )TS THE SERVER THAT MAKES THE %*"/BJECT BY IMPLEMENTING THE !DVICE INTERFACE WHICH )3 REMOTE AND WHICH INTERCEPTS ALL BUSINESS METHOD CALLS TO THE BEAN Q: "UT IF YOU DONT HAVE THE BEAN IMPLEMENT THE INTERFACE IN OTHER WORDS IF !DVICE"EAN DOESNT IMPLEMENT !DVICE DOESNT THIS MEAN THAT THE COMPILER WONT CATCH YOU IF THE BEAN BLOWS IT AND DOESNT MATCH THE METHODS OF THE INTERFACE A: 9ES THATS EXACTLY WHAT IT MEANS !ND YES THAT MAKES MOST *AVA DEVELOPERS A LITTLE QUEASY JUST THINKING ABOUT IT !FTER ALL THATS ONE OF THE BENEFITS OF INTERFACES IN *AVA THAT THE COMPILER GUARANTEES THAT YOU HAVE ALL OF THE INTERFACE METHODS PROPERLY IMPLEMENTED "UT DONT PANIC )N OUR DEVELOPMENT IN THIS BOOK WE DO HAVE TO BE CAREFUL SINCE THE COMPILER ISNT MAKING SURE THAT WEVE IMPLEMENTED THE BUSINESS METHODS FROM THE COMPONENT INTERFACE )N THE REAL WORLD HOWEVER YOULL ALMOST CERTAINLY BE USING AN %*" READY DEVELOPMENT ENVIRONMENT THAT WILL MAKE SURE YOU PROVIDE THE METHODS EITHER BY PUTTING A hYOUR CODE GOES HEREv VERSION OF THE METHOD IN YOUR BEAN CLASS OR BY DOING THE REVERSEFINDING THE BUSINESS METHOD IN THE BEAN CLASS AND PUTTING IT INTO THE COMPONENT INTERFACE !T THE VERY LEAST MOST SERVERS WILL CHECK BEFORE OR AT THE TIME YOU DEPLOY THE BEAN THAT YOUR COMPONENT INTERFACE AND BEAN CLASS HAVE MATCHING METHODS )F THIS STILL BOTHERS YOU THOUGH WE DO HAVE A TECHNIQUE FOR GETTING AROUND IT THAT WELL LOOK AT A LITTLE LATER #HANCES ARE YOU WONT NEED TO USE IT 3O YOUR BEAN WOULD END UP IMPLEMENTING A BUNCH OF METHODS IT SHOULD NEVER HAVE LIKE GET(ANDLE GET%*"(OME \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 22 EHDQ%UDLQ3RZHU YiX`e gfn\i 7KHQDPLQJ FRQYHQWLRQ IRUEHDQVLV 127SDUWRI WKH(-%VSHF $GYLFH$GYLFH+RPH $GYLFH%HDQ HVH <RX¶UHQRWUHTXLUHGWRXVHWK QDPHVIRUHQWHUSULVHEHDQV G ,W¶VDUHDOO\UHDOO\UHDOO\JRR W LGHDKRZHYHU %HVXUHWKD H \RX¶UHQRWIRROHGE\WKHQDP HJDO RIDFODVVRULQWHUIDFH²LW¶VO FH WRKDYHDFRPSRQHQWLQWHUID QDPHG$GYLFH+RPHDQGD FRPSRQHQWLQWHUIDFHQDPHG Q $GYLFH%HDQIRUH[DPSOH2 WKHH[DPDOZD\VJRE\WKH Q FODVVRULQWHUIDFHGHFODUDWLR ODVV KHF RIW DPH KHQ DQW HUWK UDWK %HDQDZDUH GHYHORSPHQW WRROV <RXVDZKRZWKHEHDQGRHVQ¶WLPSOHPHQWWKHFRPSRQHQW LQWHUIDFHHYHQWKRXJKWKHEHDQPXVWKDYHWKHVDPHPHWKRGV %UDLQVWRUPDZD\ WKHUHPD\EHPRUHWKDQRQH LQZKLFK\RX FRXOGKDQGOHWKHVHUHTXLUHPHQWVIRUWKH$GYLFH*X\EHDQ 7KHFRPSRQHQWLQWHUIDFH $GYLFH PXVWH[WHQG(-%2EMHFW 7KHEHDQPXVWLPSOHPHQWWKH6HVVLRQ%HDQLQWHUIDFH7KH EHDQPXVWQRWLPSOHPHQWWKHFRPSRQHQWLQWHUIDFH $GYLFH :HZDQWWKHFRPSLOHUWRYHULI\WKDWWKHEHDQ $GYLFH%HDQ KDVWKHVDPHPHWKRGVDVWKRVHLQWKHFRPSRQHQWLQWHUIDFH +LQW³WKHVDPHPHWKRGVDVWKRVHLQWKHFRPSRQHQWLQWHU IDFH´GRHVQRWPHDQWKHVDPHWKLQJDV³WKHVDPHPHWKRGV DVWKRVHGHFODUHGLQWKHFRPSRQHQWLQWHUIDFH´ +LQWWKLVUHTXLUHVRQO\-DYDNQRZOHGJHQRW(-%NQRZOHGJH 7RGD\PDQ\(-%SURJUDPPHUVXVH(-%VDYY\GHYHORSPHQW WRROV,QRWKHUZRUGVDEHDQFDSDEOH,'(WKDWNQRZVKRZ WKHWKUHHSLHFHV²KRPHLQWHUIDFHFRPSRQHQWLQWHUIDFH DQGEHDQFODVV²DUHUHODWHGWRRQHDQRWKHU0DQ\RI WKHVHWRROVDOVRNQRZKRZWRWDONGLUHFWO\WRRQHRU PRUHDSSVHUYHUVVRWKDW\RXFDQXVHWKHWRROWRERWK GHYHORSDQGGHSOR\\RXUEHDQUDWKHUWKDQVZLWFKLQJIURP DGHYHORSPHQWHQYLURQPHQWWRWKHVHUYHU¶VRZQ DQGRIWHQ OHVVIULHQGO\ GHSOR\PHQWWRROV2QHRIWKHDGYDQWDJHVRIDQ (-%GHYHORSPHQWWRROLVWKDW\RXPLJKWQRWKDYHWRZRUU\DERXW PDWFKLQJXSWKHEXVLQHVVPHWKRGVLQWKHFRPSRQHQWLQWHUIDFH ZLWKWKHDFWXDOEHDQFODVVRUYLFHYHUVD$JRRG(-%DZDUH,'( ZLOOPDNHVXUH\RX¶YHJRWHYHU\WKLQJV\QFHGXS &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 23 LQWURWR(-% EHDQ LQWHUIDFHV ;0/'' HMEMDU GHSOR\ '2&7<3( HMEMDU 38%,QF HMEMDU[PO D Þle required! The DlÓ This name is ed xm Òejb-jar. must be nam tually &UHDWHDQ;0/GHSOR\PHQWGHVFULSWRU (In this book, we wonÕt ac let the ll eÕ w s; ve el rs WKDWWHOOVWKHVHUYHUZKDW\RXUEHDQLVDQGKRZwrite this ou .) s build it for us deployment tool LWVKRXOGEHPDQDJHG 4HE DEPLOYMENT DESCRIPTOR $$ DESCRIBES THE STRUCTURE OF YOUR BEAN INCLUDING HOW THE THREE lLES COMPONENT INTERFACE HOME INTERFACE AND BEAN CLASS ARE RELATED TO ONE ANOTHER 4HE SERVER WONT LOOK AT YOUR NAMING CONVENTION AND lGURE OUT WHICH IS THE HOME WHICH IS THE BEAN ETC 9OU HAVE TO TELL THE SERVER THROUGH THE $$ WHICH CLASS IS WHICH AND HOW THEYRE CONNECTED "UT THE $$ DOES A LOT MORE THAN THAT !ND FOR SOME BEANS THE $$ CAN BE SEVERAL PAGES LONG &OR THIS SIMPLE BEAN THE $$ IS SHORT 2EMEMBER YOU DONT NEED TO MEMORIZE THE SYNTAX OF THE 8-, IN THE $$ ,ATER IN THE BOOK IN SEVERAL DIFFERENT CHAPTERS WELL GO OVER THE ASPECTS OF THE $$ THAT YOU DO NEED TO KNOW "[POYHUVLRQ ´´HQFRGLQJ ´87)´"! '2&7<3(HMEMDU38%/,&µ6XQ0LFURV\VWHPV ,QF'7'(QWHUSULVH-DYD%HDQV(1¶µKWWS MDYDVXQFRPGWGHMEMDUBBGWG¶! HMEMDU! GLVSOD\QDPH!(MEGLVSOD\QDPH! HQWHUSULVHEHDQV! VHVVLRQ! GLVSOD\QDPH!$GYLFH%HDQGLVSOD\QDPH! HMEQDPH!$GYLFH%HDQHMEQDPH! KRPH!KHDG¿UVW$GYLFH+RPHKRPH! UHPRWH!KHDG¿UVW$GYLFHUHPRWH! HMEFODVV!KHDG¿UVW$GYLFH%HDQHMEFODVV! VHVVLRQW\SH!6WDWHOHVVVHVVLRQW\SH! WUDQVDFWLRQW\SH!%HDQWUDQVDFWLRQW\SH! VHFXULW\LGHQWLW\! GHVFULSWLRQ!GHVFULSWLRQ! XVHFDOOHULGHQWLW\!XVHFDOOHULGHQWLW\! VHFXULW\LGHQWLW\! VHVVLRQ! You donÕt have to write the XML by hand if you use a tool that can help you build a deployment descriptor. You can use the J2EE RI bean wizard to do it for you, and the XML it spits out will work in any EJB 2.0 container! For now, just know th every bean in an application at must have an element in the DD that the beanÕs structure and describes type. HQWHUSULVHEHDQV! HMEMDU! \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 24 WKHHMEMDU EHDQ LQWHUIDFHV ;0/'' HMEMDU GHSOR\ -$5 HMEMDU JAR e wonÕt use the (in this book, wthe ejb-bar ourselves; tool to make deploytools do it) weÕll let the 3XWWKHEHDQWKHLQWHUIDFHVDQGWKH GHSOR\PHQWGHVFULSWRULQWRDQHMEMDU¿OH !S A BEAN DEVELOPER OFlCIALLY CALLED A "EAN 0ROVIDER YOULL ALWAYS PUT YOUR BEANS IN A *!2 4HE SPEC SAYS AN EJB JAR IS A *!2 lLE THAT HOLDS THE THINGS THE BEAN DEPENDS ON CLASSES AND INTERFACES ALONG WITH THE DEPLOYMENT DESCRIPTOR 9OU DONT HAVE TO DO THIS BY HAND SINCE WELL USE THE 2) 2ATHER THAN WRITING THE 8-, $$ AND USING THE MDU TOOL TO PACKAGE IT WELL USE THE 2) GHSOR\WRRO WIZARD TO MAKE IT EASIER AND LESS ERROR PRONE )N OTHER WORDS WERE GOING TO COMBINE STEPS AND INTO ONE &OR NOW YOU NEED TO KNOW THAT A BEAN ISNT A BEAN UNTIL YOU MAKE A *!2 lLE WITH THE COMPILED CLASS AND INTERFACES AND THE $$ t descriptor The deploymenmed Òejb-jar.xmlÓ MUST be nabe in a directory and MUST TA-INFÓ named ÒME The ejb-jar Þle must lude all three of the bean .class Þles (thinc plus the bean implementeatitwo interfaces on class) and the deployment descr or. Name the ejb-jar Þle whatever ipt like; thereÕs no required name for the you ejb-jar. P\HMEMDU 0(7$,1) '2&7<3( HMEMDU 38%,QF HMEMDU[PO KHDGILUVW $G L % :DWFKLW $GYLFHFODVV O ory JAR Þle, the direct As with any other g the package must be structure matchine the JAR. In other words, immediately inside have been deÞned in the since these thre e, the Þles must be inside ÒheadÞrstÓ packagdirectory. the ÒheadÞrstÓ $GYLFH+RPHFODVV 7KHH[DPH[SHFWV\RXWRNQRZZKDWLVVXSSRVHGWREHLQWKHHMEMDU¿OHDQGDOVR ZKDWLVQRWVXSSRVHGWREHLQWKHUH7KHFODVVHVDQGLQWHUIDFHVJHQHUDWHGE\WKH FRQWDLQHU \RX¶OOOHDUQZKDWWKRVHDUHDOLWWOHODWHU PXVWQRWEHLQWKHHMEMDU¿OH7KLQN RIWKHHMEMDUDVWKHWKLQJ\RXFUHDWHDVDEHDQGHYHORSHU,W¶V\RXUGHOLYHUDEOH7KH FRQWDLQHUVHUYHUKDVLWVRZQGHOLYHUDEOHVDQGWKRVHGHOLYHUDEOHVGRQ¶WJRLQWRWKHHME MDU,PDJLQHWKDW\RXZRUNIRU%HDQV58VDQG\RXGRQ¶WHYHQKDYHDQ(-%FDSDEOH VHUYHU7KDW¶VWKHZKROHLGHDRIWKHHMEMDULW¶VZKHUHWKHEHDQGHYHORSHUSXWVKLV EXLOGLQJEORFNFRPSRQHQWV LHEHDQV WKDWVRPHRWKHUGHYHORSHUFDQXVHWRDVVHPEOH DQDSSOLFDWLRQ<RXPLJKWXVHDVHUYHUWRROWRKHOSFUHDWHWKH;0/''EXWWKH''LVVWLOO \RXUGHOLYHUDEOHUHJDUGOHVVRIZKR RUZKDW FUHDWHVLW &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 25 LQWURWR(-% EHDQ LQWHUIDFHV ;0/'' HMEMDU GHSOR\ 'HSOR\WKHEHDQLQWRWKHVHUYHUXVLQJWKH WRROVSURYLGHGE\WKHVHUYHUYHQGRU 3OONER OR LATER YOUR BEANS HAVE TO DO SOMETHING 4HEY HAVE TO BE ASSEMBLED INTO AN APPLICATION AND DEPLOYED INTO A SERVER WAITING FOR CLIENTS TO CALL 4HIS IS A HUGE STEP )N FACT WE CHEATED A LITTLE BECAUSE ITS ACTUALLY TWO STEPS !PPLICATION !SSEMBLY AND $EPLOYMENT 5!PPLICATION !SSEMBLY 4HIS MEANS TAKING THE BEAN FROM THE REUSABLE COMPONENT STAGE TO BEING PART OF AN APPLICATION &OR SIMPLE BEANS THAT MIGHT MEAN SIMPLY WRITING A CLIENT THAT CAN ACCESS THE BEAN IE CALL THE BEANS BUSINESS METHODS )N OTHER WORDS A SINGLE BEAN MIGHT BE THE ENTIRE APPLICATION ON THE SERVER SIDE "UT THIS COULD ALSO BE THE STEP WHERE YOU INTEGRATE MULTIPLE BEANS AND OTHER *AVA CLASSES INTO A CUSTOM APPLICATION AND THAT USUALLY MEANS TAKING DIFFERENT BEANS EACH IN ITS OWN EJB JAR WITH ITS OWN $$ AND PUTTING THEM INTO A NEW SINGLE EJB JAR WITH A SINGLE $$ THAT MIGHT DESCRIBE HOW TWO OR MORE BEANS ARE RELATED TO ONE ANOTHER $URING ASSEMBLY YOU MIGHT ALSO ADD NEW INFORMATION TO THE $$ FOR THINGS THE BEAN DEVELOPER DIDNT KNOW ABOUT &OR EXAMPLE THE BEAN DEVELOPER MIGHT WRITE CODE THAT USES A SPECIAL BEAN SPECIlC hPROPERTYv CALLED AN ENVIRONMENT ENTRY WHICH WELL GET INTO IN A LATER CHAPTER FOR THE TAX AMOUNT USED BY THIS APPLICATION "UT THE BEAN DEVELOPER HAS NO IDEA WHAT VALUE TO GIVE THE TAX AMOUNT PROPERTY SO HE LEAVES THE VALUE BLANK IN THE $$ 4HEN THE APPLICATION ASSEMBLER COMES ALONG SEES BY READING THE $$ THAT THE BEAN USES A PROPERTY lGURES OUT WHAT THE VALUE SHOULD BE AND ADDS IT TO THE $$ &OR THE !DVICE BEAN PUTTING THE BEAN IN THE EJB JAR BUILDING THE $$ AND DEPLOYING WILL HAPPEN AS ONE BIG STEP 5$EPLOYMENT 4HIS IS WHERE THE RUBBER MEETS THE ROAD THE BEAN MEETS THE SERVER THE DEVELOPER MEETS THE SYS ADMIN 4HE TWO CRUCIAL PARTS OF DEPLOYMENT ARE NAMING THE BEAN SO THE CLIENT WILL KNOW HOW TO lND IT AND GETTING THE BEAN INTO THE CONTAINERS CONTROL 4HE SPEC DOESNT SAY ANYTHING ABOUT THE WAY IN WHICH YOU DEPLOY YOUR BEANS IT ALL DEPENDS ON THE %*" SERVERCONTAINER THAT YOURE USING \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 26 (-%UROHV -2*:WTM[IVL:M[XWV[QJQTQ\QM[ ,ZRUNIRU%HDQV 58VDQG,GHYHORS UHXVDEOHFRPSRQHQWV WKDWZHVHOOWRRWKHU GHYHORSHUV (-%5ROH%HDQ3URYLGHU Bill 'HOLYHUDEOHHMEMDU¿OHV WKDW LQFOXGHRQHRUPRUHEHDQVDQGDQ;0/ GHSOR\PHQWGHVFULSWRU 3ULPDU\UHVSRQVLELOLW\'HVLJQDQG SURJUDPHQWHUSULVHMDYDEHDQV,QRWKHU ZRUGVZULWHWKHEHDQFRGH &KDUDFWHULVWLFV.QRZVWKHEXVLQHVV ORJLFWKDWVKRXOGEHLQDSDUWLFXODUW\SHRI FRPSRQHQWIRUDSDUWLFXODUGRPDLQ ,·PDGHYHORSHUIRUD ELJRQOLQHERRNVWRUH :HEX\DORWRIEHDQVIURP %HDQV58VEXW,DOVRPDNHP\ RZQEHDQV,PL[WKHPWRJHWKHU LQWRQHZDSSOLFDWLRQVFXVWRPL]HG IRUWKHEXVLQHVVUXOHVRIKRZ ZHVHOOERRNV Annie :DWFKLW 7KHH[DP FRYHUVVXEWOH GLIIHUHQFHV EHWZHHQUROHV Q\ 3D\YHU\FORVHDWWHQWLRQWRD KLV LQW ROHV -%U RI( WLRQ PHQ YHU ERRNHVSHFLDOO\ZKHQZHFR QW PRUHGHWDLOVRIWKHGHSOR\PH WHVWHG GHVFULSWRU&RXQWRQEHLQJ RXQW RQZKRGRHVZKDWDQGF XEWOH RQWKRVHTXHVWLRQVEHLQJV HDQ 7KH$SS$VVHPEOHUDQG% DUHDV 3URYLGHURYHUODSLQVHYHUDO KH QGW HUD SOR\ 'H WKH RHV DVG P $SS$VVHPEOHU)RUWKHH[D KDV \RXQHHGWRNQRZZKLFKUROH UD WKHSULPDU\UHVSRQVLELOLW\IR LQJWR SDUWLFXODUWDVN XVXDOO\KDY HVFULSWRU GRZLWKWKHGHSOR\PHQWG SDJH LQIRUPDWLRQ 7KHVWXIIRQWKLV DQGWKHQH[WDUHMXVWDVWDUW (-%5ROH $SSOLFDWLRQ$VVHPEOHU 'HOLYHUDEOHHMEMDU¿OHV WKDWLQFOXGH RQHRUPRUHEHDQVDQGDQ;0/ GHSOR\PHQWGHVFULSWRUZLWK%HDQ 3URYLGHULQIRDVZHOODVDSSOLFDWLRQ DVVHPEO\LQIR 0D\DOVRFUHDWHFOLHQWV RUGH¿QHLQWHUDFWLRQEHWZHHQRWKHU FRPSRQHQWV VXFKDV-63V 3ULPDU\UHVSRQVLELOLW\&RPELQH RQHRUPRUHHQWHUSULVHEHDQVLQWRD ODUJHUDSSOLFDWLRQ0D\VRPHWLPHVZHDUWKH%HDQ 3URYLGHUKDWPL[LQJQHZDQGH[LVWLQJEHDQV WRJHWKHUWREXLOGDQDSS'H¿QHVWKHVHFXULW\DQG WUDQVDFWLRQEHKDYLRUIRUWKHDSSOLFDWLRQ &KDUDFWHULVWLFV'H¿QLWHO\DGRPDLQH[SHUW 0LJKWQRWGRDVPXFKFRGLQJDVWKH%HDQ3URYLGHU &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 27 LQWURWR(-% (-%5ROH 'HSOR\HU 'HOLYHUDEOH(QWHUSULVHEHDQVWKDWKDYHEHHQ :RZ,WDFWXDOO\GHSOR\HG 8QEHOLHYDEOH,ZRUNIRU WKHVDPHRQOLQHERRNVWRUHDVWKHDSS DVVHPEOHUDQG,WDNHKHUHMEMDUVWXG\ WKHGHSOR\PHQWGHVFULSWRUDQGJHWWKHWKLQJ DFWXDOO\UXQQLQJLQWRWKHVHUYHUDQGZDLWLQJ IRUFOLHQWV,NQRZD/27DERXWWKHZD\RXU V\VWHPVDUHFRQÀJXUHGDQGUXQQLQJKHUH FXVWRPL]HGIRUDVSHFL¿FRSHUDWLRQDOHQYLURQPHQWDQG GHSOR\HGLQWRWKHVHUYHU 3ULPDU\UHVSRQVLELOLW\7DNHWKH$SSOLFDWLRQ $VVHPEOHU¶VGHOLYHUDEOHVWXG\WKHGHSOR\PHQWGHVFULSWRU DQGUHVROYHDQ\H[WHUQDOGHSHQGHQFLHV)RUH[DPSOH LIWKHEHDQUHOLHVRQDSDUWLFXODUUHVRXUFHWKHGHSOR\HU PXVWPDSWKHORJLFDOQDPHIURPWKH%HDQ3URYLGHUWRWKH DFWXDOQDPHRIWKHUHVRXUFHRQWKHVHUYHU5HPHPEHU Dick ZKHQ%LOOZURWHWKHEHDQFRGHKHGLGQ¶WNQRZLWZRXOG HQGXSRQ'LFN¶VVHUYHU%LOOKDGWRPDNHXSDµIDNH¶QDPH IRUWKHGDWDEDVHDQG'LFNKDVWRELQGWKHIDNHQDPHWR VRPHWKLQJUHDO &KDUDFWHULVWLFV$QH[SHUWLQDVSHFL¿FRSHUDWLRQDO GRPDLQ.QRZVWKHVHFXULW\XVHUVDQGUROHVIRUWKLV V\VWHPNQRZVZKDW¶VFRQ¿JXUHGLQWRWKHVHUYHUDQG XQGHUVWDQGVKRZWRLQWHUSUHWWKHGHSOR\PHQWGHVFULSWRU LQIRIURPWKH%HDQ3URYLGHUDQG$SS$VVHPEOHU :HZRUNIRUWKH 6XSHU6HUYHUFRPSDQ\ :H·UHH[SHUWVLQORZOHYHO VHUYLFHVOLNHWUDQVDFWLRQ PDQDJHPHQWSRROLQJDQG VHFXULW\ :HGRWKH ELJVHUYLFHVVR\RXJHW WRIRFXVRQ\RXUEXVLQHVVORJLF :HFRPSHWHZLWK,%0%($ DQG2UDFOHDQGVRPHGD\ZH·OO NLFN/DUU\(OOLVRQ·VEXWW Sue (-%5ROH &RQWDLQHUDQG6HUYHU3URYLGHU 'HOLYHUDEOH(-%FRPSOLDQWVHUYHUGHSOR\PHQW WRROVUXQWLPHHQYLURQPHQWIRUHQWHUSULVHEHDQV 3ULPDU\UHVSRQVLELOLW\,PSOHPHQWLQJWKHVSHF &KDUDFWHULVWLFV([SHUWVLQGLVWULEXWHGREMHFWVDQG WUDQVDFWLRQVDQGRWKHUORZOHYHOV\VWHPVHUYLFHV Carl \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 28 $GYLFH%HDQWXWRULDO <]\WZQIT" /HW·VPDNHGHSOR\DQGWHVWWKH$GYLFH%HDQ 7ELL WRITE THE CODE COMPILE IT START THE SERVER START THE DEPLOYTOOL USE THE DEPLOYTOOL TO MAKE THE $$ AND THE EJB JAR DEPLOY THE BEAN CREATE A CLIENT AND TEST THE BEAN USING THE CLIENT 4HE ONLY THING WE WONT DO IS INSTALL AND CONlGURE THE SERVER 7E ASSUME YOU ALREADY DID THAT )F YOU DONT YET HAVE THE *%% 2) UP AND RUNNING GO TO HTTPJAVASUNCOMJEE AND DOWNLOAD VERSION OF *%% IT INCLUDES SET UP INSTRUCTIONS THEN GO BACK AND DOWNLOAD THE *%% !0) DOCUMENTATION :K\DUHZHXVLQJ WKH5, ":K\FDQ·WZHXVH D5($/DSSVHUYHU" :KLFKVHUYHUZRXOGZHXVH":HXVHWKH 5,IRUOHDUQLQJDQGSUDFWLFLQJEHFDXVH ZHGRQ¶WNQRZZKLFKVHUYHU\RX¶OOQHHG WRXVHDQGWKH5,LVWKHVLPSOHVWRIDOO WKHIUHHO\DYDLODEOHVHUYHUV:HZDQW LWWREHDVHDV\DVSRVVLEOHIRU\RXWR IRFXVRQ(-%WHFKQRORJ\DQGLJQRUHWKH WRROVSHFL¿FWDVNV 2SHQVRXUFHSURGXFWVOLNH-%RVVDUH VWLOOUHDOSURGXFWLRQVHUYHUVVRWKH\WHQG WRKDYHDORWPRUHFRQ¿JXUDWLRQDQG DGPLQLVWUDWLRQWDVNVWRFRSHZLWK8VLQJ WKH5,OHWV\RXVSHQGPRUHWLPHGRLQJ WKHWKLQJV\RX¶OOKDYHWRGRUHJDUGOHVV RIWKHVHUYHUZLWKWKHOHDVWDPRXQWRI WLPHVSHQWOHDUQLQJDVHUYHUVSHFL¿F DSSURDFKWRWKRVHWKLQJV Remember, the exam is for 1.3, NOT 1.4! Whatever you J2EE NOT study for the exam usindo, do 1.4 spec. See the intro for g the details about why the exam more uses 1.3 and not 1.4Ñthe short version we donÕt want to certify folk is: s on something that almost nob ody CertiÞcation is NOT about ÒI is using. know the latest and greatest rele about ÒI know the technologyaseÓ. ItÕs that people are using now. IÕve for at least six months.Ó been using it RWKLQJ 7KHUH¶VQ P D [ RQWKHH ((5, H WK W X DER HUYHU OLFDWLRQV Q\ WKHUDSS 2UDQ\R WRNQRZDERXWD ¶V IRU ¶WKDYH <RXGRQ GLQJ6XQ ROV LQFOX W<RX YHQGRUWR WRUGHSOR\PHQ HQ ELOLWLHV P D S S OR FD YH W H D G K RNQRZZ VHUYHUZLOO W G H H Q GR QW FRPSOLD NQRZ HYHU\(-% XGRQ¶WQHHGWR \R W VRU X E UH WX IHD KDYH RUVSHFL¿F DQ\YHQG QGHWDLOV WLR FRQ¿JXUD &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 29 LQWURWR(-% 7ZOIVQbMaW]ZXZWRMK\LQZMK\WZa !LL THE BEANS IN THIS BOOK ARE ORGANIZED INTO PACKAGES WHICH MEANS YOU MUST BE A LITTLE MORE CAREFUL ABOUT COMPILING AND RUNNING %VERY INSTRUCTION IN THIS CHAPTER ASSUMES YOUVE ORGANIZED YOUR PROJECT EXACTLY THE WAY ITS SHOWN HERE )F YOU DEVIATE FROM THIS STRUCTURE YOURE ON YOUR OWN FOR MAPPING OUR COMMAND LINE AND DEPLOYMENT FORMULAS TO YOUR OWN STRUCTURE 7KHUH·VRQO\21(SURMHFWV GLUHFWRU\IRUWKHZKROH ERRN SDFNDJH SURMHFWRUJDQL]DWLRQ SURMHFWV VKRSSLQJ FODVVHV KHDGILUVW FODVVILOHV (DFKSURMHFWLQWKHERRNKDVLWV RZQGLUHFWRU\7KLVGLUHFWRU\LV IRUWKHDGYLFHSURMHFW DGYLFH VUF KHDGILUVW MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ MDYDILOHV FODVVHV KHDGILUVW FODVVILOHV VUF KHDGILUVW (DFKSURMHFWKDVD VUFGLUHFWRU\ IRUMDYD VRXUFH¿OHV DQGD FODVVHVGLUHFWRU\ IRU FODVVE\WHFRGH¿OHV 7KLVLVZKHUHWKHSDFNDJH GLUHFWRU\VWUXFWXUHEHJLQV 0RVWSURMHFWFODVVHVDUH LQWKH³KHDG¿UVW´SDFNDJH ZKLFKPHDQVWKHFODVVHV PXVWEHLQDGLUHFWRU\ QDPHGKHDG¿UVW MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ MDYDILOHV 7ZOIVQbQVOaW]Z\MZUQVITKWUUIVLTQVM )LOH(GLW:LQGRZ+HOS$FFHVVRUL]H FGaSURMHFWVDGYLFHVUF FRPSLOHIURPWKHVUFGLUHFWRU\ FGaSURMHFWVDGYLFH UXQFOLHQWVIURPWKHVSHFL¿F SURMHFWGLUHFWRU\ \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 30 FRPSLOLQJWKHLQWHUIDFHVDQGEHDQFODVV + WUXQTM\PM\ _WQV\MZNIKM[ IVL\PMJMIVKTI[[ 3O FAR WEVE WRITTEN THE TWO INTERFACES AND THE BEAN CLASS BUT WE STILL HAVE TO COMPILE THEM !FTER THAT WELL MAKE THE EJB JAR WHICH HOLDS CLASS lLES NOT SOURCE lLES 5LJKWQRZWKLVLVKRZ \RXUSURMHFWVGLUHFWRU\ VWUXFWXUHVKRXOGORRN SURMHFWV )LOH(GLW:LQGRZ+HOS:K\$P,+HUH FGaSURMHFWVDGYLFHVUF DGYLFH MDYDFGFODVVHVKHDG¿UVW MDYD 7ERE USING THE G COMPILER mAG SO THE COMMAND LINE ABOVE SAYS h#OMPILE ALL THE JAVA lLES IN THE @HEADlRST DIRECTORY AND THEN PUT THE COMPILED CLASS lLES INTO THE @CLASSES DIRECTORY WHICH YOULL lND BY GOING UP ONE LEVEL FROM THE CURRENT SRC DIRECTORY /H YEAH ALMOST FORGOT BE SURE TO PUT THE CLASSES IN THEIR CORRECT 0!#+!'% DIRECTORY ,OOK FOR THE PACKAGE STRUCTURE INSIDE THE @CLASSES DIRECTORY WHICH MEANS YOU SHOULD SEE A DIRECTORY NAMED @HEADlRST AND 4(!4S WHERE THE CLASS lLES NEED TO GOAND IF YOU DO ./4 lND THE @HEADlRST DIRECTORY THERE THEN MAKE ONE FOR ME 4HANKSv FODVVHV VUF KHDGILUVW KHDGILUVW When we the .class compile, weÕll make in the classÞles land here, directory. es/headÞrst MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ LPSRUW MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ $GYLFH%HDQMDYD $GYLFHMDYD MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ $GYLFH+RPHMDYD a Þles All three .jav src/ he t in e b must tory. ec ir d t headÞrs &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 31 Return to Table of Contents LQWURWR(-% ;\IZ\\PM[MZ ^MZ /PEN UP A NEW TERMINAL FOR THE SERVER 9OULL LEAVE IT RUNNING AND WE WANT TO SEE THE OUTPUT AS IT RUNS SO DONT USE THIS TERMINAL FOR ANYTHING ELSE -AKE THE ADVICE DIRECTORY YOUR WORKING DIRECTORY )LOH(GLW:LQGRZ+HOS%ODK%ODK%ODK FGSURMHFWVDGYLFH MHHYHUERVH AW]¼TT[MM[WUM\PQVOTQSM\PQ[ 4HE VERBOSE mAG WHICH ISNT REQUIRED BUT WE LIKE IT PRINTS OUT A BUNCH OF STUFF IN THE TERMINAL )LOH(GLW:LQGRZ+HOS:KDW7KH" MHHYHUERVH -((VHUYHUOLVWHQSRUW 1DPLQJVHUYLFHVWDUWHG %LQGLQJ'DWD6RXUFHQDPH MGEF'%XUO MGEFFORXGVFDSHUPL&ORXGVFDSH'%FUHDWH WUXH %LQGLQJ'DWD6RXUFHQDPH MGEF,QYHQWRU\'%XUO MGEFFORXGVFDSHUPL &ORXGVFDSH'%FUHDWH WUXH %LQGLQJ'DWD6RXUFHQDPH MGEF'%XUO MGEFFORXGVFDSHUPL&ORXGVFDSH'%FUHDWH WUXH %LQGLQJ'DWD6RXUFHQDPH MGEF(VWRUH'%XUO MGEFFORXGVFDSHUPL &ORXGVFDSH'%FUHDWH WUXH %LQGLQJ'DWD6RXUFHQDPH MGEF&ORXGVFDSHXUO MGEFFORXGVFDSHUPL &ORXGVFDSH'%FUHDWH WUXH %LQGLQJ'DWD6RXUFHQDPH MGEF;$&ORXGVFDSHXUO MGEF;$&ORXGVFDSHBB[D %LQGLQJ'DWD6RXUFHQDPH MGEF;$&ORXGVFDSHBB[DGDWD6RXUFH &20FORXGVFDSHFRUH5HPRWH; D'DWD6RXUFH#F 6WDUWLQJ-06VHUYLFH,QLWLDOL]DWLRQFRPSOHWHZDLWLQJIRUFOLHQWUHTXHVWV%LQGLQJ -06'HVWLQDWLRQMPV7RSLFMDYD[MPV7RSLF!%LQGLQJ-06'HVWLQDWLRQMPV4XHXH MDYD[MPV4XHXH!%LQGLQJ-06&Q[)DFWRU\4XHXH&RQQHFWLRQ)DFWRU\4XHXH1RSURS HUWLHV!%LQGLQJ-06&Q[)DFWRU\MPV7RSLF&RQQHFWLRQ)DFWRU\7RSLF1RSURSHUWLHV !%LQGLQJ-06&Q[)DFWRU\MPV4XHXH&RQQHFWLRQ)DFWRU\4XHXH1RSURSHUWLHV!%LQGLQJ -06&Q[)DFWRU\7RSLF&RQQHFWLRQ)DFWRU\7RSLF1RSURSHUWLHV! 6WDUWLQJZHEVHUYLFHDWSRUW 6WDUWLQJVHFXUHZHEVHUYLFHDWSRUW -((6'. 6WDUWLQJZHEVHUYLFHDWSRUW -((6'. -((VHUYHUVWDUWXSFRPSOHWH \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 32 Return to Table of Contents VWDUWLQJGHSOR\WRRO ;\IZ\LMXTWa\WWT /PEN UP A NEW TERMINAL FOR THE DEPLOYTOOL 4HIS TOOL IS PART OF THE *%% 2) AND IT HAS EVERYTHING YOU NEED TO CREATE THE EJB JAR THE $$ AND TO DO THE lNAL DEPLOYMENT INTO THE 2) SERVER )LOH(GLW:LQGRZ+HOS&KLOO GHSOR\WRRO 6WDUWLQJ'HSOR\PHQWWRROYHUVLRQ 7\SHµGHSOR\WRROKHOS¶IRUFRPPDQGOLQH RSWLRQV aW]¼TT[MM[WUM\PQVOTQSM\PQ[ ! LOVELY SPLASH SCREEN POPS UP AND SITS WHILE THE APPLICATION LOADS )F YOU CLICK THE SPLASH SCREEN IT DISAPPEARS SO DONT PANIC IF IT THEN LOOKS LIKE NOTHINGS HAPPENING 0ATIENCE &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 33 LQWURWR(-% 5ISMIVM_)XXTQKI\QWV 4HE 2) IS A *%% SERVER REMEMBER NOT JUST AN %*" CONTAINER 3O WE HAVE TO DO A SMALL BIT OF *%%ISH STUFF BEFORE WE CAN MAKE THE EJB JAR AND DEPLOY THE APP 4HIS STEP IS WHERE WE CREATE A NEW *%% APPLICATION AND FOR NOW YOU CAN THINK OF THE *%% APPLICATION AS SOMETHING THAT WRAPS THE BEANS AND ADDS A LITTLE MORE INFORMATION FOR THE SERVER 4HE MAIN DIFFERENCE BETWEEN A *%% APPLICATION AND AN %*" APPLICATION IS THAT A *%% APPLICATION CAN INCLUDE WEB COMPONENTS SERVLETS AND *30S AS WELL AS %*" COMPONENTS ALL INTEGRATED AS PART OF A SINGLE APP &KRRVH)LOH 1HZ$SSOLFDWLRQ WKHUH DUH QR 'XPE 4XHVWLRQV Q: $OES THIS MEAN THAT ) -534 HAVE A *%% SERVER IF ) WANT TO USE SERVLETS AND %*"S TOGETHER A: .O 7ITH A *%% SERVER THE WEB COMPONENTS AND %*" COMPONENTS ARE MORE TIGHTLY INTEGRATED WHICH MEANS YOU CAN HAVE ALL OF THE COMPONENTS RESPECT ONE ANOTHERS TRANSAC TIONS AND SECURITY "UT YOURE ALWAYS FREE TO USE A SERVLET AS A CLIENT TO AN ENTERPRISE BEAN EVEN IF THAT BEAN ISNT RUNNING IN THE SAME APPLICATION SERVER OR EVEN THE SAME PHYSICAL MACHINE !NOTHER ADVANTAGE OF A *%% SERVER IS THE EASE WITH WHICH YOU CAN DEPLOY BOTH COMPONENT TYPES AS PART OF ONE ENTERPRISE APPLICATION (AVING SAID ALL THIS CHANCES ARE EXTREMELY HIGH THAT IF YOURE DO ING %*" APPLICATIONS YOU ARE RUNNING THEM IN A *%% SERVER 2EMEMBER THERE ARE VERY FEW STANDALONE %*" CONTAINERS TODAY 6IRTUALLY ALL SIGNIFICANT VENDORS RUN THEIR %*" CONTAINERS WITHIN A *%% SERVER $-((VHUYHUYHQGRUPXVWSDVVD PDVVLYHSLOHRIFRPSDWLELOLW\WHVWV EHIRUHWKHVHUYHUFDQEHFDOOHG ´-((FRPSOLDQWµ$-((VHUYHU IRUH[DPSOHPXVWLQFOXGHDQ(-% FRQWDLQHUDQGWKDWFRQWDLQHUPXVW LPSOHPHQWWKH(-%VSHFLILFDWLRQ \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 34 GHSOR\WRROPDNLQJDQHZDSSOLFDWLRQ 6IUMIVL[I^M\PMVM_IXXTQKI\QWV 4HIS PARTS A LITTLE AWKWARD 9OU CAN USE THE "ROWSE BUTTON TO NAVIGATE THROUGH YOUR OWN DIRECTORY TREE BUT THE EASIEST WAY TO NAME AND SAVE THE APPLICATION IS TO TYPE THE COMPLETE PATH TO THE lLE YOURE ABOUT TO CREATE 4HE THING WERE MAKING IS NOT THE BEAN ITSELFYOU CAN THINK OF IT MORE LIKE A DOCUMENT THAT HOLDS ALL THE INFORMATION ABOUT THE APPLICATION !S A CONVENTION WE SAVE THE APPLICATION IN THE PROJECTS;WHATEVER= DIRECTORYTHE DIRECTORY CORRESPONDING TO THAT PARTICULAR PROJECT &OR THE !DVICE BEAN THAT MEANS THE PROJECTS ADVICE DIRECTORY )F YOU STARTED THE SERVER FROM THE PROJECTSADVICE DIRECTORY IN OTHER WORDS IF ADVICE IS YOUR CURRENT WORKING DIRECTORY THEN YOULL GET THE RIGHT NAME AND LOCATION BY DEFAULT 1DPHWKHDSSOLFDWLRQ$GYLFH$SS ,IQHHGHGLQFOXGHWKHIXOOSDWKWRSURMHFWVDGYLFH$GYLFH$SS &OLFN2. DonÕt w Applicatioorry about the the tool n Display Name Þ in with thwill automatically Þeld; Applicatio e Þle name from t ll it n File Nam e Þeld. he &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 35 Return to Table of Contents LQWURWR(-% ?PI\aW]¼TT[MMIN\MZaW]KZMI\MIVLVIUM \PMIXXTQKI\QWV .OW YOURE BACK AT THE MAIN SCREEN OF THE DEPLOYTOOL 9OU MIGHT HAVE TO CLICK ON THE &ILES OR !PPLICATIONS ICONS TO EXPAND THEM BUT YOULL SEE THAT THE TOOL HAS CREATED AN !PPLICATIONS DIRECTORY WITH SOMETHING CALLED !DVICE!PP INSIDE #LICK ON THE !DVICE!PP ICON AND YOULL SEE INFORMATION ABOUT THE APPLICATION INCLUDING THE NAME LOCATION AND CURRENT CONTENTS !T THIS POINT THERES NOTHING BUT A -%4! ).& DIRECTORY THAT HOLDS MORE INFO ABOUT THE APP WHICH WE WONT EVER NEED TO LOOK AT &OLFNRQWKH$GYLFH$SSLFRQ \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 36 Return to Table of Contents GHSOR\WRROPDNLQJDQHZHQWHUSULVHEHDQ 6W_TM\¼[UISM\PMVM_MV\MZXZQ[M JMIV\PMMRJRIZIVL\PM,, 4HIS IS WHAT WERE REALLY AFTERTHE ACTUAL BEAN 4HE PREVIOUS STEPS MAKING THE *%% APPLICATION WERE TO SATISFY THE *%% 2) BECAUSE WE HAVE TO DEPLOY THE BEAN WITHIN A *%% APP &KRRVH)LOH1HZ(QWHUSULVH%HDQ &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 37 Return to Table of Contents LQWURWR(-% 6W__M¼ZMQV\PMZMITTaKWWT 6M_-V\MZXZQ[M*MIV?QbIZL 4HIS PART OF THE DEPLOYTOOL IS WHERE ALMOST EVERYTHING HAPPENS 4HE KEY THINGS WELL DO ARE 5 #REATE THE EJB JAR 5 0UT THE BEAN CLASS AND THE TWO INTERFACES INTO THE EJB JAR 5 #REATE THE DEPLOYMENT DESCRIPTOR THAT DESCRIBES THE BEAN &OLFN1H[W! \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 38 Return to Table of Contents GHSOR\WRROFUHDWHDQHZHMEMDU +ZMI\M\PMVM_MRJRIZ &OR NOW JUST ACCEPT THE DEFAULTS 4HE RADIO BUTTON ON THE TOP LEFT OF THE SCREEN SHOWS THAT YOURE MAKING A NEW *!2 WITHIN THE !DVICE!PP APPLICATION .OTICE THAT THE !DVICE!PP IS PART OF A DROP DOWN LISTIF THERE WERE OTHER EJB JARS ALREADY IN THE APPLICATION WE COULD HAVE CHOSEN TO PUT THE NEW BEAN IN A PRE EXISTING *!2 4HE TOOL PICKS AN ESPECIALLY HELPFUL DISPLAY NAME h%JBv AND YOULL SEE THIS BACK ON THE MAIN DEPLOYTOOL SCREEN WHEN WERE DONE 4HAT NAME ISNT USED ANYWHERE IN YOUR REAL APPLICATION SO ITS NO BIG DEAL BUT IF YOU HAVE MORE THAN ONE *!2 IN AN APPLICATION YOU MIGHT WANT TO GIVE IT A MORE DESCRIPTIVE NAME #ART *!2 !CCOUNT *!2 ETC &OLFN(GLW &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 39 Return to Table of Contents LQWURWR(-% )LL\PM\PZMMKTI[[NQTM[QVKT]LQVO\PMQZ XIKSIOMLQZMK\WZa\W\PM2): 4HIS IS THE MOST IMPORTANT PART OF THE WHOLE PROCESS )N OTHER WORDS DONT SCREW IT UP 4HE KEY IS TO GET THE CORRECT CLASSES INTO THE *!2 IN THEIR PACKAGE DIRECTORY STRUCTURE AND ONLY THEIR PACKAGE DIRECTORY STRUCTURE )N OTHER WORDS IF YOU PUT THE THREE CLASS lLES INTO THE *!2 WITHOUT THE HEADlRST DIRECTORY YOUR BEAN WONT DEPLOY /R IF YOU INCLUDE THE CLASSES DIRECTORY AS WELL AS THE HEADlRST DIRECTORY YOUR BEANS WONT DEPLOY 2EMEMBER THE EJB JAR IS STILL A *!2 lLE SO THE USUAL *!2 RULES ABOUT PACKAGE STRUCTURES APPLY HERE 1DYLJDWHWRWKH$GYLFHGLUHFWRU\ ([SDQGWKHFODVVHVGLUHFWRU\WRVHHWKHKHDGÀUVWGLUHFWRU\ 6HOHFWWKHKHDGÀUVWGLUHFWRU\ &OLFN$GG Use the navigation controls here to get to your Advice directory where you can see your, classes and src direct Then select the headÞorirstes. directory. You donÕt to expand it... we didhave it so that you could see whatÕs in there. \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 40 Return to Table of Contents GHSOR\WRROFRQ¿UP-$5FRQWHQWV +WVNQZU\PI\aW]ILLML764A\PM XIKSIOMLQZMK\WZaIVL\PMKTI[[NQTM[ 9OU GOTTA GET THIS PART RIGHT ,OOK AT THE BOTTOM WINDOW THAT SAYS h#ONTENTS OF %JBv AND VERIFY THAT THE ONLY THING IN THE *!2 BESIDES THE -%4! ).& DIRECTORY IS THE HEADlRST DIRECTORY INCLUDING THE CONTENTS OF THE DIRECTORY 4HE CLASSIC MISTAKE WE SEE ALL THE TIME NOT THAT YOUD EVER DO THAT IS TO ADD ONLY THE CLASS lLES BUT NOT THE PACKAGE DIRECTORY "E SURE THAT YOU HAVE THE HEADlRST DIRECTORY AND NOT THE CLASSES DIRECTORY WITH THE THREE CLASS lLES !NOTHER COMMON MISTAKE IS TO ADD THE SOURCE lLES INSTEAD OF THE CLASS lLES 3O DONT FEEL BAD IF IT HAPPENS TO YOU 9HULI\WKDW\RXKDYHWKHULJKWFODVVHV DQGSDFNDJHGLUHFWRU\ ,I\RXGRQ¶WVHOHFWWKHPIURPWKHERWWRPZLQGRZFOLFN5HPRYHDQGVWDUWRYHU &OLFN2.ZKHQ\RX¶UHGRQHWKHQFOLFN1(;7 &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 41 Return to Table of Contents LQWURWR(-% 5ISMQ\I;\I\MTM[[;M[[QWVJMIV .OW WERE AT THE PLACE WHERE WE GIVE THE TOOL THE BEANS @STRUCTURAL INFORMATION 7HAT KIND OF BEAN IT IS WHICH CLASS lLE IS THE HOME INTERFACE AND SO ON 2EMEMBER THE TOOL USES THIS TO CREATE THE DEPLOYMENT DESCRIPTOR !ND THE %*" CONTAINER USES THE DEPLOYMENT DESCRIPTOR TO lGURE OUT HOW TO DEPLOY AND MANAGE THE BEAN 4HE !DVICE BEAN IS SIMPLECLIENT CALLS A METHOD ON THE REMOTE OBJECT THE REMOTE OBJECT RETURNS A VALUE THEN THE REMOTE OBJECT FORGETS THE WHOLE THING EVER HAPPENED %ND OF STORY 4HAT SCENARIO IS JUST PERFECT FOR A STATELESS SESSION BEAN SOLUTION )F THE !DVICE BEAN NEEDED TO REMEMBER THE ADVICE IT GAVE TO THE CLIENT AND USE IT IN SOME WAY IN FUTURE INVOCATIONS FROM THAT SAME CLIENT THEN WED MAKE IT STATEFUL "UT WE DONT SO WE WONT !ND AS YOULL LEARN LATER IT WOULDNT MAKE SENSE TO HAVE THE !DVICE BEAN BE AN ENTITY OR MESSAGE DRIVEN BEAN "UT ITS TOO LATE TO MAKE IT ANYTHING BUT A SESSION BEAN ANYWAYYOUR BEAN CLASS IMPLEMENTS THE 3ESSION"EAN INTERFACE 3O YOURE ALREADY COMMITTED TO A BEAN TYPE "UT WHETHER THE SESSION BEAN IS STATELESS OR STATEFUL CAN BE A LITTLE MORE SUBTLE &OR NOW JUST MAKE IT STATELESS 6HOHFWWKH6HVVLRQUDGLREXWWRQ 6HOHFWWKH6WDWHOHVVUDGLREXWWRQ DonÕt click the Next button!! We have way more to do on this screen . \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 42 Return to Table of Contents GHSOR\WRROUHODWLQJWKHEHDQ¶VFODVV¿OHV <MTTQ\_PQKPWN\PM\PZMMKTI[[NQTM[QV\PM2): Q[\PMIK\]IT*-)6KTI[[ 9OU HAVE THREE CLASS lLES IN THE *!2 THE HOME INTERFACE !DVICE(OME THE COMPONENT INTERFACE !DVICE AND THE BEAN !DVICE"EAN "UT THE %*" CONTAINER NEEDS TO KNOW WHICH IS WHICH 2EMEMBER THE NAMING CONVENTION DOESNT MEAN ANYTHING TO ANYONE BUT YOU 4HE CONTAINER WONT LOOK AT THE THREE CLASSES IN THE *!2 AND RECOGNIZE THAT !DVICE(OME MUST BE THE HOME INTERFACE AND SO ON .O THE NAMING CONVENTION IS FOR YOU AND ANYONE ELSE USING YOUR BEAN COMPONENTS 3O NOW YOU HAVE TO TELL THE TOOL WHICH OF THE lLES IS THE BEAN CLASS WELL DO THE TWO INTERFACES ON THE NEXT PAGE 4HE TOOL THEN WILL PUT THIS INFO INTO THE DEPLOYMENT DESCRIPTOR &OLFNWKH(QWHUSULVH%HDQ&ODVVGURSGRZQPHQX 6HOHFWKHDGÀUVW$GYLFH%HDQ 7KHUHDUHVRPHYHQGRUWRROV WKDWFDQXVH\RXUQDPLQJ FRQYHQWLRQDPRQJRWKHU WKLQJVWRILJXUHRXWZKLFKLV WKHKRPHZKLFKLVWKHEHDQ HWF%XWWKH5,GRHVQ·WGRWKLV DQGGHYHORSPHQWWRROVXSSRUW LVQRWSDUWRIWKH(-%VSHF 7KHRQO\WRROVUHTXLUHGE\ WKHVSHFDUHIRUWKH'HSOR\HU QRWWKH%HDQ3URYLGHURU$SS $VVHPEOHU &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 43 LQWURWR(-% <MTTQ\_PQKPQ[\PM0WUMQV\MZNIKMIVL_PQKP Q[\PM+WUXWVMV\QV\MZNIKM .OW YOU HAVE TO DO THE SAME THING YOU DID FOR THE BEAN CLASS BUT WITH THE TWO INTERFACES 9OULL NOTICE THAT THERE ARE TWO DIFFERENT PLACES WHERE YOU CAN SELECT INTERFACES,OCAL AND 2EMOTE 4HIS BEAN IS REMOTE WHICH MEANS ITS A *AVA 2-) REMOTE SERVICE BUT WELL GET INTO THAT IN THE NEXT CHAPTER SO WERE USING ONLY THE BOTTOM SECTION FOR 2EMOTE INTERFACES *UST LEAVE THE ,OCAL INTERFACES SECTION ALONE 4HE STRANGE PART IS THAT THIS SCREEN USES THE TERM 2EMOTE )NTERFACE RATHER THAN 2EMOTE #OMPONENT )NTERFACE 4HATS JUST A BAD INTERFACE DESIGN IN THE DEPLOYTOOL )TS ACTUALLY AN ARTIFACT OF THE PREVIOUS VERSION OF %*" WHEN THERE WERE ONLY REMOTE INTERFACES ,OCAL INTERFACES ARE NEW TO %*" BUT WE HAVE TO LIVE WITH IT &OLFNWKH5HPRWH+RPH,QWHUIDFHGURSGRZQPHQX 6HOHFWKHDGÀUVW$GYLFH+RPH &OLFNWKH5HPRWH,QWHUIDFHGURSGRZQPHQX 6HOHFWKHDGÀUVW$GYLFH \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 44 GHSOR\WRROYHULI\VWUXFWXUDOLQIRUPDWLRQ >MZQNaM^MZa\PQVOWV\PQ[[KZMMV #HOOSING THE BEAN CLASS THE HOME INTERFACE AND THE COMPONENT INTERFACE IS A PERMANENT DECISION /NCE YOURE DONE WITH THE REST OF THE BEAN WIZARD AND THE DEPLOYMENT DESCRIPTOR IS CREATED YOURE STUCK WITH IT )F YOU ACCIDENTALLY MIX UP THE HOME AND COMPONENT INTERFACES YOUR BEAN WONT WORK )N THE 2) IT WONT EVEN DEPLOY SOME SERVERS LET YOU DEPLOY STRUCTURALLY BAD BEANS WHICH MEANS THEY BLOW UP AT RUNTIME BUT THE 2) WONT EVEN LET YOU IN THE SERVER DOOR %HVXUH\RXKDYHWKHIROORZLQJVHWWLQJV (QWHUSULVH%HDQ&ODVVKHDGÀUVW$GYLFH%HDQ 5HPRWH+RPH,QWHUIDFHKHDGÀUVW$GYLFH+RPH 5HPRWH,QWHUIDFHKHDGÀUVW$GYLFH &OLFN1(;7 Be sure your screen looks just like this before you hit the Next > button ! &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 45 Return to Table of Contents LQWURWR(-% AW]¼ZMLWVMKTQKS.QVQ[P "ECAUSE THIS BEAN IS SO SIMPLE AND WE DONT CARE ABOUT TRANSACTIONS SECURITY ENVIRONMENT ENTRIES AND DATABASE ACCESS WEVE DONE EVERYTHING WE NEED TO MAKE THE DEPLOYMENT DESCRIPTOR AND PUT IT IN A *!2 WITH THE CLASS lLES 3O YOU CAN IGNORE THE 4RANSACTIONS -ANAGEMENT SCREEN ALTHOUGH YOULL BECOME INTIMATE WITH IT LATER IN THE BOOK &OLFN)LQLVK \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 46 Return to Table of Contents GHSOR\WRROPDLQVFUHHQDIWHUPDNLQJDEHDQ 5MIV_PQTMJIKSWV\PMUIQVLMXTWa\WWT[KZMMV 4HINGS HAVE CHANGED 4HE !DVICE!PP ICON EXPANDS TO SHOW YOUR EJB JAR NAMED h%JBv AND THE EJB JAR EXPANDS TO SHOW THE CUTE BEAN ICON NAMED h!DVICE"EANv )F YOU SELECT THE !DVICE"EAN YOULL SEE A BUNCH OF TABBED PANELS THATLL SHOW YOU THE CHOICES YOU MADE IN THE BEAN WIZARD 3OME OF THE THINGS YOU CHOSE CAN BE CHANGED BUT SOME CANT )N THE 'ENERAL PANEL FOR EXAMPLE YOU CANT CHANGE THE CLASS lLE DESIGNATIONS FOR THE HOME COMPONENT AND BEAN )N FACT YOU CAN SEE THAT THE DROP DOWN LISTS FOR THESE ARE GRAYED OUT HERE "UT GEE YOU CAN STILL CHANGE IT FROM 3TATELESS TO 3TATEFUL $GPLUH\RXUZRUN &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 47 Return to Table of Contents LQWURWR(-% :]VaW]ZJMIV\PZW]OP\PMLMXTWa\WWT^MZQNQMZ 9OU ALREADY KNOW YOUR BEAN CLASSES COMPILE BUT JUST BECAUSE IT COMPILES DOESNT MEAN IT FOLLOWS BEAN LAW 4HE VERIlER TAKES YOUR JAR AND RUNS IT THROUGH A BUNCH OF TESTS TO SEE IF IT MEETS THE MINIMUM REQUIREMENTS FOR DEPLOYMENT !S YOU LEARN MORE ABOUT THE %*" SPEC YOULL SEE THAT THE VERIlER IS TESTING YOUR BEANS CODE AND THE DEPLOYMENT DESCRIPTOR TO SEE IF IT COMPLIES WITH THE SPEC &OR EXAMPLE A STATELESS SESSION BEANS HOME INTERFACE MUST HAVE A NO ARG CREATE METHOD DECLARED AND NOTHING ELSE !ND THE BEAN CLASS MUST HAVE METHODS THAT MATCH THOSE DECLARED IN THE COMPONENT INTERFACE !ND IF THE COMPONENT INTERFACE IS REMOTE IT MUST DECLARE THAT EACH METHOD THROWS 2EMOTE%XCEPTION $ONT WORRY ABOUT REMEMBERING THESE EXAMPLES ITS JUST TO GIVE YOU AN IDEA OF THE KINDS OF THINGS THE VERIlER DOES &OLFNRQWKH(MELFRQ WKHOLWWOHMDU WRKLJKOLJKWLW &KRRVH7RROV 9HULÀHU &URVV\RXU¿QJHUV \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 48 Return to Table of Contents GHSOR\WRROUXQQLQJWKHYHUL¿HU +TW[MaW]ZMaM[IVLKTQKS73 4HE VERIlER SCREEN SHOWS YOU THE NAME OF THE *!2 AS EJB JAR ICJAR IN A TMP DIRECTORY BUT THATS JUST HOW THE 2) SAVES YOUR EJB JAR UNTIL YOURE READY TO DEPLOY 4HE TMP lLE AND THE *!2 WILL GO AWAY WHEN YOU DEPLOY OR DELETE THE BEAN FROM YOUR APPLICATION !T LEAST ITS SUPPOSED TO 3OMETIMES YOU MIGHT HAVE TO lND THAT DIRECTORY AND DELETE IT YOURSELF 2EPEAT AFTER ME 4HE DEPLOYTOOL IS FREE 4HE DEPLOYTOOL IS NOT A PRODUCTION TOOL ) DID NOT PAY ANY MONEY FOR THIS TOOL ) WILL LEARN TO APPRECIATE ITS STRENGTHS AND LOOK PAST THE WAYS IT IS A PAIN IN THE A &KRRVHWKH)DLOXUHV2QO\UDGLREXWWRQ &OLFN2. &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 49 Return to Table of Contents LQWURWR(-% ?PM_ 6WNIQTML\M[\[ )F EVERYTHING VERIlES YOU SEE A NICE LITTLE MESSAGE IN THE $ETAILS BOX AT THE BOTTOM OF THE VERIlER WINDOW )F THERE ARE FAILURES YOULL SEE THEM IN THE 2ESULTS BOX 9OU CAN CLICK ON A @FAILURE MESSAGE TO GET MORE DETAILS ABOUT WHAT WENT WRONG $ONT PANIC IF YOU SEE A MILLION FAILURES USUALLY YOU CAN lX ONE THING AND THEY ALL GO AWAY 5NLIKE COMPILER ERROR MESSAGES WHICH ARE SOMETIMES ABOUT AS HELPFUL AS 6#2 INSTRUCTIONS THE VERIlER FAILURE MESSAGES ARE PRETTY EXPLICIT 9OU CAN USUALLY lGURE OUT EXACTLY WHERE YOU WENT WRONG 3OMETIMES YOU CAN lX THE PROBLEM BACK IN THE MAIN DEPLOYTOOL WINDOW BY CLICKING ON ONE OF THE TABBED PANELS AND CHANGING SOMETHING &OR EXAMPLE IF YOU FORGOT TO SPECIFY A TRANSACTION ATTRIBUTE FOR AN ENTITY BEAN METHOD YOU CAN GO TO THE 4RANSACTIONS TABBED PANEL IN THE DEPLOYTOOL WINDOW AND SET THE ATTRIBUTE WITHOUT STARTING OVER IN THE BEAN WIZARD "UT IF YOU HAVE PROBLEMS WITH YOUR ACTUAL CLASS lLES YOULL HAVE TO MODIFY THEM RECOMPILE THEM AND THEN UPDATE YOUR EJB JAR /R IF THE PROBLEM IS THAT YOU MADE A SETTING IN THE BEAN WIZARD THAT CANT BE CHANGED LIKE SELECTING THE BEAN CLASS WHEN THE TOOL ASKS FOR THE HOME INTERFACE YOULL HAVE TO DELETE THE EJB JAR AND START OVER WITH THE BEAN WIZARD %HKDSS\DERXWWKHZRQGHUIXOPHVVDJHDWWKHERWWRP &OLFN&ORVH Click Close to put away the window. If you click OK, youÕll just run the veriÞer again. 4O UPDATE YOUR EJB JAR lLE CHOOSE THE @4OOLS THEN @5PDATE &ILES MENU OPTIONS \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 50 Return to Table of Contents GHSOR\WRROGRLQJWKHGHSOR\ <QUM\W,MXTWa /NCE YOUVE VERIlED YOURE READY TO DEPLOY 4HERES NOT MUCH THAT CAN GO WRONG UNLESS SOMETHING BAD HAPPENS IN THE SERVER !T THIS POINT ALMOST ANYTHING THAT GOES WRONG CAN BE lXED BY SHUTTING DOWN THE SERVER AND DEPLOYTOOL AND STARTING THEM BACK UP AGAIN $RASTIC YES BUT IT USUALLY WORKS 3OMETIMES YOU DO HAVE TO TAKE EVEN HARSHER ACTION BY RETURNING THE SERVER BACK TO ITS FRESHLY INSTALLED STATE BUT FORTUNATELY THERES A COMMAND THAT CAN DO THAT 7HEN ALL ELSE FAILS BRING UP A NEW TERMINAL AND TYPE CLEANUP 4HIS SCRIPT SHUTS DOWN THE *%% SERVER BUT IT ALSO CLEANS OUT ALL THE LOGS DIRECTORIES AND lLES THAT HAVE BEEN MADE ON THAT MACHINE !FTER THAT YOU CAN TRY STARTING THE SERVER THEN THE DEPLOYTOOL AND DEPLOYING AGAIN !ND IF YOU EVER OPEN THE DEPLOYTOOL AND YOU DONT SEE YOUR APPLICATION DONT PANIC *UST GO TO THE &ILE MENU AND CHOOSE /PEN JUST LIKE YOU WOULD TO OPEN A DOCUMENT IN ANY OTHER APPLICATION 4HINK OF THE *%% APP LIKE A DOCUMENT AS FAR AS THE DEPLOYTOOL IS CONCERNED &KRRVH7RROV 'HSOR\ &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 51 Return to Table of Contents LQWURWR(-% 5ISMQ\:M\]ZVI+TQMV\2IZ /NCE IT STARTS THE SERVER IS GOING TO DO A TON OF THINGS TO GET YOUR BEAN READY /NE OF THEM IS TO GENERATE THE CLASS lLES THAT IMPLEMENT YOUR TWO INTERFACES THE HOME AND THE COMPONENT INTERFACES !ND SINCE THEYRE REMOTE INTERFACES THE SERVER WILL ALSO CREATE THE REMOTE STUB CLASSES FOR THOSE INTERFACES ,OTS MORE ON THESE LATER 7ELL THE CLIENT NEEDS THE TWO INTERFACES AND THE TWO STUBS 9OU COULD HAVE GIVEN THE CLIENT THE INTERFACES SINCE YOU CREATED THOSE "UT ONLY THE SERVER CAN MAKE THE STUB CLASSES AND THE CLIENT WILL NEVER WORK WITHOUT THEM 3O YOUR CLIENT APPLICATION MIGHT BE SITTING THERE ALL NICE AND COMPILED AND JUST WAITING FOR YOU TO GIVE IT THE STUB CLASSES SO THAT IT CAN ACTUALLY RUN &ORTUNATELY YOU CAN ASK THE 2) DEPLOYTOOL TO GIVE YOU A CLIENT JAR THAT HAS EVERYTHING THE CLIENT NEEDS AND A LOT MORE IT TURNS OUT BUT SINCE THIS ISNT A REAL PRODUCTION ENVIRONMENT WERE JUST GOING TO LET THAT GO 6IRTUALLY ALL %*" APPLICATION SERVERS MUST CREATE THE STUB CLASSES SO YOULL HAVE TO lND OUT WHERE YOUR SERVER PUTS THEM SO YOU CAN GIVE THEM TO THE CLIENT 6HOHFWWKH5HWXUQ&OLHQW-DUFKHFNER[ SXWLWLQWKHSURMHFWVDGYLFHGLUHFWRU\ &OLFN1H[W! \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 52 GHSOR\WRROJLYLQJWKHEHDQD-1',QDPH /Q^MQ\IVIUM[WKTQMV\[KIVTWWSQ\]X 7ERE NOW DANGEROUSLY CLOSE TO DEPLOYMENT AND THE LAST STEP IS TO GIVE THE BEAN A *.$) NAME 4HATS THE NAME CLIENTS USE TO GET A REFERENCE TO THE BEAN 7ELL TO WHAT THE CLIENT THINKS IS THE BEAN BUT WELL SAVE THE GORY DETAILS FOR THE NEXT CHAPTER 4HE BEANS *.$) NAME IS SIMPLY THE LOGICAL NAME YOU CHOOSE OR IN THE REAL WORLD WHOEVER DEPLOYS THE BEAN )T DOESNT HAVE TO MATCH ANYTHING FROM THE BEAN ITSELF 7E COULD FOR EXAMPLE NAME THIS BEAN (OMER WHICH WOULD IN FACT MAKE IT EVEN MORE FUN AND CHALLENGING FOR THE CLIENTS THAN IT ALREADY WOULD BE WITH A MEANINGFUL NAME 7\SHLQWKH-1',QDPH$GYLVRU &OLFN)LQLVK 7DNHDGHHSEUHDWKDQGKROGLWXQWLOWKHGHSOR\SURFHVVFRPSOHWHV $GYLVRU &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 53 LQWURWR(-% ?I\KP\PMXZWOZM[[JIZ[OW]X\PMVKMTMJZI\M 7AIT FOR IT WAIT FOR IT WAIT FOR IT 7HEN THE BEANS DEPLOYED SUCCESSFULLY YOULL SEE THE LINE h$EPLOYMENT OF !DVICE!PP IS COMPLETEv IN THE WINDOW !T THAT POINT CLIENTS CAN ACCESS THE BEAN 9OU DID IT &OLFN2. ItÕs a miracle! It worked! \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Page 54 Return to Table of Contents GHSOR\WRROPDLQVFUHHQDIWHUGHSOR\LQJ 6W_aW]¼TT[MM\PM)L^QKM)XXQV[QLM\PM[MZ ^MZ 5NDER THE 3ERVERS ICON YOULL SEE THE LOCALHOST ICON REPRESENTING THE *%% SERVER YOU STARTED BEFORE YOU LAUNCHED THE DEPLOYTOOL !ND UNDER THE LOCALHOST ICON YOU CAN NOW SEE THAT YOUR !DVICE!PP IS DEPLOYED 9OULL ALSO SEE THE 5NDEPLOY BUTTON THAT YOU CAN USE TO WELL UNDEPLOY ([SDQGWKH6HUYHUV!ORFDOKRVWLFRQVWRVHHWKH$GYLFH$SS &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 55 LQWURWR(-% 6W_ITT_MVMMLQ[IKTQMV\ 7E HAVE A FRESHLY DEPLOYED BEAN IN A *%% SERVER BUT WE CANT TEST IT UNTIL WE HAVE A CLIENT 4HE CLIENT HAS TO DO lVE THINGS *HWDUHIHUHQFHWRD-1',,QLWLDO&RQWH[W ZH¶OOOHDUQDERXWWKDWLQWKH&OLHQW9LHZFKDSWHU 8VHWKH,QLWLDO&RQWH[WWRGRDORRNXSRQWKH KRPHLQWHUIDFHRIWKHEHDQ WKDWZHQDPHG ³$GYLVRU´ZKHQZHGHSOR\HG :H·UHXVLQJDVWDQGDORQH-DYD SURJUDPDVWKHFOLHQW ,QWKHUHDOZRUOG\RXUFOLHQWV ZLOOOLNHO\EHVHUYOHWVRURWKHU EHDQV 1DUURZDQGFDVWWKHWKLQJZHJHWEDFN IURPWKHORRNXS 7KDWWKLQJLVVRPHWKLQJ WKDWLPSOHPHQWVWKH$GYLFH+RPHLQWHUIDFH :H¶OOOHDUQDERXWQDUURZLQJLQWKH&OLHQW9LHZ FKDSWHU 7KHÀYHWKLQJVWKHFOLHQWPXVW GRDUHWKHVDPHUHJDUGOHVVRI WKHW\SHRIFOLHQW &DOOFUHDWHRQWKHKRPHLQWHUIDFHWRJHW EDFNDUHIHUHQFHWRWKHFRPSRQHQWLQWHUIDFH 6RXVLQJDVWDQGDORQH-DYD SURJUDPWHDFKHV\RXWRGRWKH VDPHWKLQJV\RX·OOQHHGWRGR ZLWKDVHUYOHWFOLHQW &DOOJHW$GYLFH WKHEXVLQHVVPHWKRG WKHUHDVRQZH¶UHKHUHUHPHPEHU" RQWKH FRPSRQHQWLQWHUIDFHDQGSULQWWKHUHVXOW ,I\RXUFOLHQWLVDQRWKHU HQWHUSULVHEHDQWKHFRGHZLOO EHVOLJKWO\GLIIHUHQWEXW\RX VWLOOKDYHWRGRWKHÀYHVWHSV 7KHUH¶VQRWKLQJRQWKHH[DPDERXWVHUYOHWVRU-63V 7KHH[DPH[SHFWV\RXWRNQRZKRZWKHFOLHQWJHWVDQGXVHVD EHDQ WKHVWHSVDERYH EXWWKHW\SHRIFOLHQWGRHVQ¶WPDWWHU7KHFOLHQW FRGHIRUJHWWLQJDUHIHUHQFHWRDEHDQDQGXOWLPDWHO\FDOOLQJPHWKRGVRQWKH EHDQLVYLUWXDOO\WKHVDPHZKHWKHUWKHFOLHQWLVDVHUYOHWRUVWDQGDORQH-DYD DSS$QGDOOWKHH[DPFDUHVDERXWLVWKHSDUWRIWKHFRGHZKHUHWKHFOLHQWLV WU\LQJWRJHWDQGXVHDEHDQ 7KHRQO\NQRZOHGJHRIVHUYOHWVDQG-63V\RXQHHGIRUWKHH[DPLVWRNQRZ WKDWWKH(-%VSHFGRHV127JXDUDQWHHVXSSRUWIRUWKHP6HUYOHWVDQG-63V DUHJXDUDQWHHGE\WKH-((VSHFEXWQRWE\WKH(-%VSHF \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 56 RUJDQL]LQJWKHFRPSOHWHSURMHFW 7ZOIVQbQVOaW]ZXZWRMK\LQZMK\WZaNWZ\PMKTQMV\ 7KLVLVZKDW\RXUSURMHFWVDGYLFH GLUHFWRU\VKRXOGORRNOLNHDIWHUZH ZULWHDQGFRPSLOHWKHFOLHQWFRGH SURMHFWV This is the client JA the server gave us R when we deployed. MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ $GYLFH&OLHQWMDYD $GYLFH$SS&OLHQWMDU DGYLFH VUF KHDGILUVW KHDGILUVW $GYLFH%HDQFODVV $GYLFH&OLHQWFODVV FODVVHV MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ LPSRUW MDYD[HME LPSRUWMDYD FHSWLRQ $GYLFHFODVV $GYLFH+RPHFODVV so we ter client,in a es t a st ju This is the client class p it wonÕt putWeÕre just gonna keend class) package. put it all (source a easy and vice directory. in the ad $GYLFH%HDQMDYD $GYLFHMDYD MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ $GYLFH+RPHMDYD &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 57 LQWURWR(-% 7KH&OLHQW&RGH $GYLFH&OLHQWMDYD LPSRUWMDYD[QDPLQJ LPSRUWMDYDUPL LPSRUWMDYD[UPL LPSRUWKHDG¿UVW LPSRUWMDYD[HME A bunch of imports SXEOLFFODVV$GYLFH&OLHQW^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ QHZ$GYLFH&OLHQW JR ` InitialContext is our entry point ming service, into the JNDI na SXEOLFYRLGJR ^ we do the lookup. ere wh DI WU\^ e bean using the JN &RQWH[WLF QHZ,QLWLDO&RQWH[W . Lookup the Aditvicdu ent ym plo de g rin 2EMHFWR LFORRNXS ³$GYLVRU´ name we gave $GYLFH+RPHKRPH $GYLFH+RPH 3RUWDEOH5HPRWH2EMHFWQDUURZ R$GYLFH+RPHFODVV $GYLFHDGYLVRU KRPHFUHDWH Just go with it for now 6\VWHPRXWSULQWOQ DGYLVRUJHW$GYLFH make perfect sense a litt. ItÕll all `FDWFK ([FHSWLRQH[ ^ le later. H[SULQW6WDFN7UDFH ` Finally we get to the act ` The method declared in ual business method! ` the component (Advice) and implement erface ed in the bean claint ss. &RPSLOHWKHFOLHQW )LOH(GLW:LQGRZ+HOS$OO7KLV)RU-XVW7KDW FGaSURMHFWVDGYLFH The client JAR Þle The client class MDYDFFODVVSDWK^&/$663$7+`$GYLFH$SS&OLHQWMDU$GYLFH&OLHQWMDYD 4HE CLIENT NEEDS ACCESS TO THE TWO INTERFACES !DVICE !DVICE(OME AND THE TWO STUB CLASSES THAT IMPLEMENT THOSE INTERFACES 4HEYRE BOTH IN THE CLIENT *!2 THE SERVER MADE BUT WE CANT COMPILE THE CLIENT WITHOUT THEM 4HE CLEANEST WAY IS TO ADD THEM TO THE CLASSPATH AT COMPILE TIME USING THE FODVVSDWK COMPILER mAG \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 58 UXQQLQJWKHFOLHQW 5XQWKHFOLHQW )LOH(GLW:LQGRZ+HOS$OO7KLV)RU-XVW7KDW FGaSURMHFWVDGYLFH MDYDFS^&/$663$7+`$GYLFH$SS&OLHQWMDU$GYLFH&OLHQW !T RUNTIME THE CLIENT STILL NEEDS ACCESS TO THE TWO INTERFACES !DVICE !DVICE(OME AND THE TWO STUB CLASSES THAT IMPLEMENT THOSE INTERFACES 4HEYRE BOTH IN THE CLIENT *!2 THE SERVER MADE WE HAVE TO ADD THEM TO THE CLASSPATH 4HE BEST WAY IS TO USE THE FS COMPILER mAG .OTE &OR NOW THE CLIENT MUST BE ON THE SAME PHYSICAL MACHINE AS THE SERVER ,ATER WELL SEE HOW TO CHANGE THIS AND RUN THE CLIENT AGAINST A *%% SERVER ON A DIFFERENT MACHINE <RX·UHNLGGLQJPH ULJKW"$OOWKLVZRUNMXVWWR FDOORQHOLWWOHPHWKRGWKDWUHWXUQV D6WULQJ",KDYHWRGRDOOWKLV MXVWWRGR+HOOR:RUOGLQ(-%" ,WKLQN,FDQDQVZHUWKDW³\HDKWKLV LVPDMRURYHUNLOOIRUD+HOOR:RUOGLVK DSSOLNHWKH$GYLFH%HDQ%XWLQWKHFRQWH[WRI DUHDOHQWHUSULVHDSSZLWKD]LOOLRQFXVWRPHUVDGD\ KLWWLQJ\RXUVHUYHU DQGDQLQVDQHO\VKRUWGHDGOLQH WKLVH[WUDZRUNDQGUXQWLPHRYHUKHDGLVSUHWW\WULYLDO (VSHFLDOO\ZKHQ\RXFRQVLGHUHYHU\WKLQJ\RX*(76R, WKLQN\RXQHHGWRORRNDWWKLVIURPDPRUHUHDO ZRUOGSHUVSHFWLYH 2.\HVLI\RX·UHJRQQDEHDOOSLFN\DERXWLW ,·P QRWUHDOEXWWKDWVKRXOGQ·W DIIHFWP\FUHGLELOLW\ &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 59 LQWURWR(-% 0RFN([DP 1 7HICH ARE FEATURES EVERY %*" CONTAINER MUST IMPLEMENT OR SUPPORT #HOOSE ALL THAT APPLY R! R" R# R$ R% 2 3 ! '5) BEAN DEPLOYMENT UTILITY 3YNCHRONOUS INVOCATION FOR ALL BEAN TYPES 4RANSACTION SUPPORT FOR ALL BEAN TYPES 2EMOTE CLIENT VIEWS FOR ALL BEAN TYPES *.$) NAMESPACE 7HICH ARE GUARANTEED CAPABILITIES OF %*" #HOOSE ALL THAT APPLY R! R" R# R$ R% ,OCAL HOME INTERFACES FOR MESSAGE DRIVEN BEANS $IRTY DETECTION MECHANISMS TO REDUCE MEMORY FOOTPRINTS 2UN AS SECURITY IDENTITY FUNCTIONALITY 4HE *$"# EXTENSION 3ESSION BEAN FAILOVER 7HICH ARE FEATURES IN %*" #HOOSE ALL THAT APPLY R! R" R# R$ R% 0ORTABLE lNDER QUERY SYNTAX #ONTAINER MANAGED PERSISTENCE ,OCAL INTERFACES FOR SESSION BEANS 8-, BASED DEPLOYMENT DESCRIPTORS 3YNCHRONOUS MESSAGE DRIVEN BEANS \RXDUHKHUH Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 1. Welcome to EJB Return to Table of Contents Page 60 PRFNH[DPDQVZHUV 0RFN([DP$QVZHUV 1 7HICH ARE FEATURES EVERY %*" CONTAINER MUST IMPLEMENT OR SUPPORT #HOOSE ALL THAT APPLY R! R" R# R$ R% 2 3 (spec: 30) ve to be GUI ! '5) BEAN DEPLOYMENT UTILITY - it doesnÕt ha 3YNCHRONOUS INVOCATION FOR ALL BEAN TYPES - MDBs cannot be synchronous 4RANSACTION SUPPORT FOR ALL BEAN TYPES 2EMOTE CLIENT VIEWS FOR ALL BEAN TYPES *.$) NAMESPACE - MDBs do no t have client vi ews 7HICH ARE GUARANTEED CAPABILITIES OF %*" #HOOSE ALL THAT APPLY R! R" R# R$ R% ve clients - MDBs donÕt ha $IRTY DETECTION MECHANISMS TO REDUCE MEMORY FOOTPRINTS - nice, but not guaranteed ,OCAL HOME INTERFACES FOR MESSAGE DRIVEN BEANS 2UN AS SECURITY IDENTITY FUNCTIONALITY 4HE *$"# EXTENSION 3ESSION BEAN FAILOVER - maybe available, but not guaran teed 7HICH ARE FEATURES IN %*" #HOOSE ALL THAT APPLY R! R" R# R$ R% (spec: 25-26) 0ORTABLE lNDER QUERY SYNTAX #ONTAINER MANAGED PERSISTENCE ,OCAL INTERFACES FOR SESSION BEANS 8-, BASED DEPLOYMENT DESCRIPTORS 3YNCHRONOUS MESSAGE DRIVEN BEANS - MDBs are always asynchronous &KDSWHU Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Table of Contents Chapter 2. EJB Architecture........................................................................ 1 Section 2.1. objectives Background................................................................................................................................................. 2 Section 2.2. You remember this picture......................................................................................................................................... 3 Section 2.3. Making a remote method call..................................................................................................................................... 4 Section 2.4. There's a "helper" on the server as well...................................................................................................................... 5 Section 2.5. There are no dumb questions..................................................................................................................................... 6 Section 2.6. What about and return values arguments?................................................................................................................ 7 Section 2.7. What really gets passed when you pass an object to a remote method?.................................................................... 9 Section 2.8. There are no Dumb Questions.................................................................................................................................. 12 Section 2.9. Passing a Remote object through a remote method call.......................................................................................... 14 Section 2.10. Brain power.............................................................................................................................................................. 15 Section 2.11. There are no Dumb Question................................................................................................................................... 17 Section 2.12. What must the Remote object and the stub have in common?.............................................................................. 19 Section 2.13. The client calls business methods on the stub through the Remote business interface....................................... 20 Section 2.14. Sharpen your pencil................................................................................................................................................. 21 Section 2.15. How EJB uses RMI.................................................................................................................................................. 22 Section 2.16. The Remote object is not the bean, it's the bean's bodyguard—the EJBObject .................................................... 23 Section 2.17. The Component interface........................................................................................................................................ 24 Section 2.18. There are no Dumb Questions................................................................................................................................ 26 Section 2.19. Who writes the class that really DOES implement the component interface? In other words, who makes the EJBObject class?........................................................................................................................................................................... 28 Section 2.20. Who creates what?.................................................................................................................................................. 29 Section 2.21. THE TIKIBEAN LOUNGE...................................................................................................................................... 30 Section 2.22. There are no Dumb Questions................................................................................................................................ 31 Section 2.23. Sharpen your pencil................................................................................................................................................ 37 Section 2.24. Architectural over view: Session beans.................................................................................................................. 39 Section 2.25. Architectural overview: Entity beans..................................................................................................................... 40 Section 2.26. Architectural overview : Creating a Stateful Session bean..................................................................................... 41 Section 2.27. Architectural overview: Creating a Stateless Session bean.................................................................................... 42 Section 2.28. Who creates the stateless session bean, and when?.............................................................................................. 43 Section 2.29. Stateless session beans are more scalable.............................................................................................................. 44 Section 2.30. Brain power............................................................................................................................................................. 45 Section 2.31. there are no Dumb Questions................................................................................................................................. 45 Section 2.32. Sharpen your pencil................................................................................................................................................ 45 Section 2.33. There are no Dumb Questions................................................................................................................................ 46 Section 2.34. Architectural over view: Message-driven beans..................................................................................................... 47 Section 2.35. Exercise: What goes where?................................................................................................................................... 48 Section 2.36. Exercise: Organize your beans................................................................................................................................ 49 Section 2.37. Exercise Solutions: What goes where?................................................................................................................... 50 Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 1 architectural overview 5 4 (-%$UFKLWHFWXUH 4 ,WDOOORRNVVR LPSUHVVLYHIURPWKLV YLHZ7RREDGZHFDQ·W VWD\DWWKLVOHYHO (-%LVDERXWLQIUDVWUXFWXUH<RXUFRPSRQHQWVDUHWKHEXLOGLQJEORFNV:LWK (-%\RXFDQEXLOGELJDSSOLFDWLRQV7KHNLQGRIDSSOLFDWLRQVWKDWFRXOGUXQHYHU\WKLQJ IURPWKH9LFWRULD¶V6HFUHWEDFNHQGWRGRFXPHQWKDQGOLQJV\VWHPVDW&(51%XWDQ DUFKLWHFWXUHZLWKWKLVPXFKÀH[LELOLW\SRZHUDQGVFDODELOLW\LVQ¶WVLPSOH,WDOOEHJLQVZLWK DGLVWULEXWHGSURJUDPPLQJPRGHOZKHUHFOLHQWVVHUYHUVDQGHYHQGLIIHUHQWSLHFHVRIWKH VDPHDSSOLFDWLRQDUHUXQQLQJZKRNQRZVZKHUHRQWKHQHWZRUN%XWKRZGRHVWKHFOLHQW ¿QGDEHDQ"+RZGRHVWKHFOLHQWFDOOPHWKRGVLW":K\DUHWKHUHGLIIHUHQWEHDQW\SHV":LOO %HQPDUU\-/R" WKLVLVDQHZFKDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 2 QRREMHFWLYHV %DFNJURXQG :H¶UHLQWKLVFKDSWHUIRUEDFNJURXQGQRWEHFDXVHRIDQH[DP REMHFWLYH$OWKRXJK\RXFRXOGVD\WKDWHYHU\REMHFWLYHLQWKH H[DPGHSHQGVRQ\RXUXQGHUVWDQGLQJZKDW¶VLQWKLVFKDSWHU Copyright Safari Books Online #896963 %XWGRQ¶WZRUU\ZH¶OOKDYHSOHQW\RIREMHFWLYHVEHJLQQLQJ ZLWK&KDSWHU%\&KDSWHU\RXZLOOORRNEDFNORQJLQJO\RQ WKLVFKDSWHUDQGUHPHPEHUZKDWLWZDVOLNHQRWWRKDYHDQ\ REMHFWLYHV<RX¶OOPLVVWKLVFKDSWHUZKHQLW¶VJRQHVRVDYRUWKH PRPHQWV\RXKDYHZLWKLW &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 3 DUFKLWHFWXUDORYHUYLHZ AW]ZMUMUJMZ\PQ[XQK\]ZM "UT IT WAS TOO HIGH LEVEL TO GET US ANYWHERE 4HINK ABOUT HOW MUCH IS MISSING FROM THIS PICTURE ,IKE HOW DOES THE CLIENT GET A REFERENCE TO SOMETHING RUNNING ON A DIFFERENT MACHINE (OW DOES THE CLIENT ACTUALLY COMMUNICATE WITH THE BEAN (OW IS IT THAT THE SERVER CAN STEP INTO THE MIDDLE OF A CLIENT TO BEAN METHOD CALL "ENEATH %*" THERES *AVAS DISTRIBUTED TECHNOLOGY FOR 2EMOTE -ETHOD )NVOCATION 2-) !LTHOUGH %*" HIDES SOME OF THE COMPLEXITIES OF 2-) FROM THE BEAN DEVELOPER ITS STILL THERE AND UNLESS YOU TRULY UNDERSTAND IT SOME PIECES WILL NEVER MAKE SENSE 3O WE START OUR DESCENT FROM A HIGH LEVEL VIEW TO THE BLOOD AND GUTS OF %*" WITH A LESSON ON 2-) )F YOURE ONE OF THE FORTUNATE WHOVE ALREADY WORKED A LOT WITH 2-) YOU CAN SKIP THIS AND GO STRAIGHT TO THE PART WHERE WE TALK ABOUT THE WAYS IN WHICH %*" USES 2-) "UT YOU SHOULD STILL AT LEAST SKIM IT EVEN IF YOURE AN EXPERIENCED %*" DEVELOPER IF FOR NO OTHER REASON THAN TO GET COMFORTABLE WITH THE TERMINOLOGY AND PICTURES WELL USE THROUGHOUT THE REST OF THE BOOK /+ BACK TO WHERE WE STARTEDWHATS MISSING FROM THIS PICTURE 3TART BY LOOKING AT THE PLACES WHERE A MIRACLE OCCURS reÕs too soon to tell if the re the but e, her e acl a mir . is.. probably miracle occurs here miracle occurs here VHUYHU VHUYLFHV W (-% REMHF (Q DQ & OL HQWREMHFW EL]LQWHUIDFH &OLHQW WHUS VHEH UL '% $ULGLFXORXVO\KLJKOHYHOYLHZRI(-%DUFKLWHFWXUH \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 4 UHPRWHPHWKRGV 5ISQVOIZMUW\MUM\PWLKITT 7HEN YOU WRITE A CLIENT TO ACCESS A BEAN THE CLIENT IS EITHER LOCAL OR REMOTE ! LOCAL CLIENT MEANS THE CLIENT IS RUNNING IN THE SAME *6- AS THE BEAN )N OTHER WORDS BOTH THE BEAN AND THE CLIENT LIVE IN THE SAME HEAP 7ELL TALK ABOUT THAT MUCH MORE IN THE #LIENT 6IEW CHAPTER BUT FOR NOW REMEMBER THAT LOCAL MEANS SAME HEAP *6- #HANCES ARE YOULL USE LOCAL CLIENTS ONLY WITH ENTITY BEANS AND ONLY UNDER VERY SPECIAL CIRCUMSTANCES 9OULL USE A REMOTE CLIENT WHEN YOU WANT A BEAN TO BE USED BY THE OUTSIDE WORLD -OST ENTERPRISE APPLICATIONS HAVE A REMOTE CLIENT EVEN IF SOME OF THE BEANS USED IN THE APPLICATION TALK TO ONE ANOTHER AS LOCAL CLIENTS 7ELL EXPLORE EVERY GORY DETAIL ABOUT THIS BEFORE THE BOOK IS OVER 3O HOW DOES AN OBJECT IN ONE HEAP*6- DIRECTLY CALL A METHOD ON A REFERENCE TO AN OBJECT RUNNING IN ANOTHER HEAP*6- 4ECHNICALLY ITS NOT POSSIBLE *AVA REFERENCES HOLD BITS THAT DONT MEAN ANYTHING OUTSIDE THE CURRENTLY RUNNING *6- )F YOURE AN OBJECT AND YOU HAVE A REFERENCE TO ANOTHER OBJECT THAT OBJECT MUST BE IN THE SAME HEAP WITH YOU *AVA 2-) 2EMOTE -ETHOD )NVOCATION SOLVES THIS PROBLEM BY GIVING THE CLIENT A PROXY CALLED A STUB OBJECT THAT ACTS AS THE GO BETWEEN FOR THE CLIENT AND 2EMOTE OBJECT 4HE CLIENT CALLS A METHOD ON THE STUB AND THE STUB TAKES CARE OF THE LOW LEVEL COMMUNICATION SOCKETS AND STREAMS WITH THE 2EMOTE OBJECT :LWK50,\RXUFOLHQWREMHFW JHWVWRDFWOLNHLW·VPDNLQJD UHPRWHPHWKRGFDOO%XWZKDW LW·VUHDOO\GRLQJLVFDOOLQJD PHWKRGRQD´SUR[\µREMHFW UXQQLQJLQWKHVDPHKHDS ZLWKWKHFOLHQW7KHSUR[\LV FDOOHGD´VWXEµDQGLWKDQGOHV DOOWKHORZOHYHOQHWZRUNLQJ VRFNHWVDQGVWUHDPV mote object, ends to be the Re Helper ÒstubÓ prpretoxy... something that knows . but itÕs just a icate with the Remote object how to commun JHW$GYLFH &OLHQWKHDS $ G Y LF H JHW FOL HQWREMHFW The clientÕs goal is to cal object (the thing th l a method on the Remote Remote object is in aatdifdoes the real work). But since the method on a local Òst ferent heap, the client calls a over the network) ubÓ object (which in turn sends info 5H PRWH REMHF W UHP E· RWH¶VWX 6HUYHUKHDS The Remote Object is the object with the method that actually does the real work for whatever that method is supposed to do (checkCredit(), calculatePi(), etc.). &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 5 DUFKLWHFWXUDORYHUYLHZ <PMZM¼[I¹PMTXMZºWV\PM[MZ ^MZI[_MTT 4HE 2EMOTE OBJECT HAS THE METHOD THE CLIENT WANTS TO CALL "UT WHEN THE STUB MAKES A NETWORK CONNECTION TO THE SERVER SOMETHING ON THE SERVER HAS TO TAKE THE INFORMATION IN THE INCOMING STREAM AND TURN IT INTO A METHOD CALL ON THE 2EMOTE OBJECT 9OU COULD PUT THE NETWORKING CODE INTO YOUR 2EMOTE OBJECT BUT THAT DEFEATS THE WHOLE POINT OF 2-)TO MAKE IT AS EASY FOR YOUR CLIENT TO CALL A METHOD ON AN OBJECT ACROSS THE NETWORK AS IT IS TO CALL A METHOD ON AN OBJECT IN THE SAME HEAP 4HE GOAL OF 2-) IS TO PROMOTE NETWORK TRANSPARENCY )N OTHER WORDS THE FACT THAT THE OBJECTS ARE IN DIFFERENT MACHINES SHOULD BE NEARLY TRANSPARENT TO THE DEVELOPER 7HICH MEANS TO YOU LESS CODESIMPLER CODE 3O WITH THAT AS THE GOAL 2-) TAKES CARE OF THE SERVER SIDE OF THE METHOD CALL AS WELL 4HE THING ON THE SERVER SIDE THAT ACCEPTS THE SOCKET CONNECTION IS CALLED A SKELETON )TS THE COUNTERPART TO THE CLIENT STUB )N THE EARLY VERSIONS OF 2-) FOR EVERY STUB THERE WAS A MATCHING SKELETON OBJECT 4ODAY THOUGH THATS NOT ALWAYS TRUE 4HE FUNCTIONALITY OF THE SKELETON HAS TO HAPPEN SOMEHOW ON THE SERVER SIDE BUT AN ACTUAL SKELETON OBJECT IS OPTIONAL 7E WONT GO INTO ANY OF THE DETAILS BECAUSE IT DOESNT MAKE ANY DIFFERENCE TO US WITH %*" (OW THE CONTAINER CHOOSES TO IMPLEMENT ITS SKELETON BEHAVIOR IS UP TO THE VENDOR !LL WE CARE ABOUT IS THAT SOMETHING IS ON THE SERVER THAT THE STUB KNOWS HOW TO TALK TO AND THAT SOMETHING KNOWS HOW TO INTERPRET THE MESSAGE FROM THE STUB AND INVOKE A METHOD ON THE 2EMOTE OBJECT Socket connection pts the incomingthe stub is saying, ce ac on et el sk he T gures out what d calls from the stub, Þod to call on which object) an th me . (i.e. which e Remote object the method on th &OLHQWKHDS JHW$GYLFH JHW$GYLFH 6HUYHUKHDS $ G Y LF H JHW FOL HQWREMHFW a method call to To the client, itÕs juste same heap. th another object in ¶VNHOHWRQ· 5H PRWH REMHF W UHP E· RWH¶VWX To the Remote object, itÕs just a method call from another object in the same heap. \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 6 (-%DUFKLWHFWXUH WKHUH DUH QR 'XPE 4XHVWLRQV Q: Q: A: A: (OW CAN YOU HAVE hNETWORK TRANSPARENCYv 7HAT HAPPENS IF THE NETWORK OR THE SERVER IS DOWN WHEN THE CLIENT CALLS THE REMOTE METHOD )T SEEMS LIKE THERES A ,/4 MORE THAT CAN GO WRONG THAN IF THE CLIENT OBJECT IS JUST MAKING A PLAIN OLD METHOD CALL TO ANOTHER OBJECT IN THE HEAP 9ES YES 9OU OBVIOUSLY UNDERSTAND THAT hNETWORK TRANSPARENCYv IS NOT ONLY A MYTH ITS A BAD IDEA /F COURSE THE REMOTE METHOD CALL CAN FAIL IN WAYS A LOCAL METHOD CALL WOULD NOT AND THE CLIENT NEEDS TO BE PREPARED FOR THAT 4HATS WHY IN *AVA 2-) ALL REMOTE METHODS MUST DECLARE A JAVARMI2EMOTE%XCEPTION WHICH IS A CHECKED EXCEPTION 4HAT MEANS THE CLIENT HAS TO HANDLE OR DECLARE THE EXCEPTION )N OTHER WORDS THE CLIENT CANT REALLY PRETEND THE METHOD CALL ISNT REMOTE "UT WAIT THERES MORETHE CLIENT HAS TO DO SOMETHING SPECIAL TO EVEN GET THE REFERENCE TO THE 2EMOTE OBJECT IN THE FIRST PLACE !ND WHAT EXACTLY IS THAT REFERENCE )TS REALLY A REFERENCE TO THE 2EMOTE OBJECTS PROXYTHE STUB 3O NO 2-) DOES NOT GIVE YOU TRUE NETWORK TRANSPARENCY 4HE DESIGNERS OF 2-) WANT THE CLIENT TO ACKNOWLEDGE THAT THINGS CAN GO HORRIBLY WRONG WITH A REMOTE METHOD INVOCATION 3TILL WHEN YOU LOOK AT EVERYTHING THAT NEEDS TO HAPPEN TO MAKE A REMOTE METHOD CALL NETWORKING 3OCKET CONNECTION STREAMS PACKAGING UP ARGUMENTS ETC THE CLIENT HAS TO DO ONLY A COUPLE OF THINGS USE A SPECIAL LOOKUP PROCESS TO GET THE REFERENCE TO THE REMOTE OBJECT AND WRAP REMOTE METHOD CALLS IN A TRYCATCH 4HATS PRETTY TRIVIAL WHEN YOU CONSIDER WHAT IT WOULD TAKE IF THE CLIENT HAD TO MANAGE THE WHOLE PROCESS !M ) RESPONSIBLE FOR BUILDING THE STUB AND THE SKELETON (OW DOES THE STUB KNOW WHAT METHODS MY 2EMOTE OBJECT HAS &OR THAT MATTER HOW DOES THE CLIENT KNOW WHAT METHODS MY 2EMOTE OBJECT HAS .O YOU DONT NEED TO MAKE THE STUBS AND SKELETONS 7ITH PLAIN OLD 2-) YOU USE THE 2-) COMPILER RMIC TO GENERATE THEM "UT FOR THE OTHER TWO QUESTIONS WELL LET YOU THINK ABOUT IT FOR A MINUTE BEFORE WE LOOK AT THE DETAILS YiX`e gfn\i ,Q-DYDZKDW¶VWKHEHVWZD\WRWHOOWKHFOLHQW ZKDWPHWKRGVVKHFDQFDOO",QRWKHUZRUGV KRZGR\RXH[SRVH\RXUSXEOLFPHWKRGVWR RWKHUV" 7KLQNDERXWWKHUHODWLRQVKLSEHWZHHQWKH VWXEDQGWKHDFWXDO5HPRWHREMHFW:KDW PXVWWKH\ERWKKDYHLQFRPPRQ" :H·OOVHHWKHDQVZHUVHYHUDOSDJHVIURPQRZ !ND THERES EVEN A WAY TO MAKE IT EASIER FOR THE CLIENT USING AN %*" DESIGN PATTERN WELL SEE IN THE LAST CHAPTER &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 7 DUFKLWHFWXUDORYHUYLHZ ?PI\IJW]\IZO]UMV\[IVLZM\]ZV^IT]M[' 2EMOTE METHOD CALLS ARE JUST LIKE LOCAL METHOD CALLS EXCEPT FOR THE 2EMOTE%XCEPTIONS !ND WHAT GOOD WOULD A METHOD CALL BE IF YOU COULDNT PASS ARGUMENTS OR GET A RETURN VALUE 9OU MIGHT AS WELL BE DOING 20# THE WAY YOUR PARENTS DID 4HIS BRINGS US TO ONE OF THE KEY JOBS FOR THE STUB AND THE SKELETON OR WHATEVER IS DOING THE SKELETONISH THINGS PACKING AND UNPACKING VALUES SHIPPED OVER THE WIRE 2EMEMBER THE CLIENT IS REALLY CALLING A METHOD ON THE STUB AND THE STUB IS LOCAL TO THE CLIENT IE IN THE SAME HEAP 3O FROM THE CLIENTS PERSPECTIVE THERES NOTHING SPECIAL ABOUT SENDING ARGUMENTS WITH THE METHOD )TS THE STUB THAT DOES ALL THE DIRTY WORK 4HE STUB HAS TO PACKAGE UP THE ARGUMENTS THROUGH A PROCESS KNOWN AS MARSHALLING AND SEND THEM IN THE OUTPUT STREAM THROUGH THE 3OCKET CONNECTION WITH THE SERVER 4HE SKELETON THING ON THE SERVER HAS TO PROCESS THE STREAM FROM THE STUB UNPACK THE ARGUMENTS lGURE OUT WHAT TO DO WITH EVERYTHING FOR INSTANCE WHICH METHOD TO CALL ON WHICH OBJECT AND THEN INVOKE THE METHOD A LOCAL CALL ON THE 2EMOTE OBJECT WITH THE ARGUMENTS 4HEN IT ALL HAPPENS IN REVERSE 4HE SKELETON PACKAGES UP THE RETURN VALUES AND SHIPS THEM TO THE STUB WHO UNPACKS THEM AND GIVES THEM TO THE CLIENT AS PLAIN OLD GARDEN VARIETY RETURN VALUES "UT IN ORDER TO SEND ARGUMENTS AND RETURN VALUES THEY MUST BE PRIMITIVES 3ERIALIZABLE OBJECTS AN ARRAY OR COLLECTION OF PRIMITIVES OR 3ERIALIZABLE OBJECTS OR A 2EMOTE OBJECT 1DPH JHW &OLHQW 7KHVWXEDQGVNHOHWRQ DUHLQLWIRUWKHZKROH URXQGWULS7KH\·UHERWK UHVSRQVLEOHIRUSDFNLQJ DQGXQSDFNLQJWKHYDOXHV VKLSSHGRYHUWKHZLUH %XWLWZRQ·WZRUNLIWKH DUJXPHQWVDQGUHWXUQ YDOXHVDUHQ·WVKLSSDEOH 6KLSSDEOHYDOXHVPXVWEH RQHRIWKHVH 5 3ULPLWLYHV 5 6HULDOL]DEOHREMHFWV 5 $QDUUD\RUFROOHFWLRQ RISULPLWLYHVRU 6HULDOL]DEOHREMHFWV 5 $5HPRWHREMHFW JHW1DPH JHW1DPH 6HUYHU ´-RQµ ´-RQµ VWXE ´-RQµ VNHOHWRQ 5HPRWHREMHFW FOLHQW e argument and The stub packages upththod call information, me e th h sends it wit value for the client. then unpacks the return 53&VWDQGVIRU5HPRWH3URFHGXUH&DOO%RULQJ The skeleton (or whatever is doing the skeleton behavior on the server) unpacks the method arguments for the Remote object method call, then packs and ships the return value. \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 8 SDVVLQJREMHFWVUHPRWHO\ :DLW DPLQXWH-DYD SDVVHVREMHFWVE\SDVVLQJ DFRS\RIWKHREMHFWUHIHUHQFH QRWWKHREMHFWLWVHOI6RKRZFDQ 7+$7HYHUZRUNZLWKDUHPRWH PHWKRGFDOO"7KDWUHIHUHQFH ZRXOGQ·WPHDQDQ\WKLQJRQ WKHRWKHUKHDS ,QRUGLQDU\ORFDOPHWKRGFDOOV-DYD SDVVHVDQREMHFWUHIHUHQFHE\YDOXH ,QRWKHUZRUGVE\FRS\LQJWKHELWVLQ WKHUHIHUHQFHYDULDEOH 7KHREMHFWLWVHOILVQHYHUSDVVHG WeÕll think of remote controla local Java reference as a Something we cato an object on the heap. call methods) on n use to push buttons (i.e. the object. 'RJ REMHFW ^a\g 'RJ YRLGJR ^ 'RJ¿GR QHZ'RJ ng here?? What are we really passi WKLVWUDLQ3HW ¿GR ` A copy of the reference (the remote control), not the Dog object 'RJ REMHFW YRLGWUDLQ3HW 'RJDUJ ^` :HNQRZ\RXNQRZDOOWKLV REYLRXVO\EXWMXVWVR¶ZH·UHDOO RQWKHVDPHSDJH· ZKLFKLV DQRGGWKLQJWRVD\VLQFHZH FOHDUO\$5(DOORQWKHVDPH SDJH EXW\RXJHWWKHLGHD Yj_ ter and the Now the ÒargÓ paramentical copies. Both ide are le iab var ÒÞdoÓ g object. reference the same Do ^a\g 'RJ 'RJ &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 9 DUFKLWHFWXUDORYHUYLHZ ?PI\ZMITTaOM\[XI[[ML _PMVaW] XI[[IVWJRMK\\WIZMUW\MUM\PWL' 'RJ REMHFW ^a\g 'RJ &OLHQW ,PDJLQHWKLV&/,(17FRGH WU\^ 'RJ¿GR QHZ'RJ NOW what are we really passing here?? UHPRWH6WXEWUDLQ3HW ¿GR `FDWFK 5HPRWH([FHSWLRQH[ ^ H[SULQW6WDFN7UDFH ` 'RJ REMHFW ue! NOT the reference val ItÕs a serialized copy t.of the actual Dog objec $QGWKLV5(027(PHWKRG ,I\RXUUHPRWHPHWKRG KDVDQDUJXPHQW WKDW·VDQREMHFWW\SH WKHDUJXPHQWLV SDVVHGDVDIXOOFRS\ RIWKHREMHFWLWVHOI )RUUHPRWHFDOOV -DYDSDVVHVREMHFWV E\REMHFWFRS\QRW UHIHUHQFHFRS\ $VHULDOL]HGFRS\RI WKHREMHFWLVVKLSSHG WRWKH5HPRWHREMHFW YRLGWUDLQ3HW 'RJDUJ ^` 6HULDOL]DEOH y The server Dog is a cop of the client Dog. 'RJ REMHFW ^a\g 'RJ 'RJ REMHFW 'RJ Yj_ &OLHQW 'RJ 6HUYHU 'RJLPSOHPHQWV 6HULDOL]DEOH \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 10 UHPRWHPHWKRGDUJXPHQWV *HWWLQJWKHREMHFWDUJXPHQWIURP WKHFOLHQWWRWKHVHUYHU &OLHQWLQYRNHVWUDLQ3HW P\'RJ RQWKH VWXESDVVLQJDFRS\RIWKHUHIHUHQFHWR WKH'RJREMHFW 'RJ 3H WUDLQ W P\'RJ FOLHQW Client passes a co of a reference to py the Dog object on the heap. ThereÕs only one Dog object. VNHO 5HPRWH REMHFW VNHO 5HPRWH REMHFW VWXE 7KHVWXEPDNHVDVHULDOL]HGFRS\RI WKHREMHFWDQGVHQGVWKDWFRS\RYHU WKHZLUHWRWKHVNHOHWRQ a serialized The stub sendDs og. copy of the 'RJ 'RJ FOLHQW VWXE &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 11 DUFKLWHFWXUDORYHUYLHZ 8QSDFNLQJ GHVHULDOL]LQJ WKH REMHFWRQWKHVHUYHU 7KHVNHOHWRQGHVHULDOL]HVWKHSDVVHG DUJXPHQWFUHDWLQJDQHZ'RJREMHFWLQWKH 5HPRWHREMHFW·VKHDS 'RJ 5HPRWH REMHFW VNHO FOLHQW VWXE 'RJ The new D is just lik one on the og client side. e the 7KHVNHOHWRQLQYRNHVWKHPHWKRGRQ WKH5HPRWHREMHFWSDVVLQJDSODLQROG -DYDUHIHUHQFHWRWKHQHZ'RJREMHFW 'RJ VNHO FOLHQW VWXE WUD LQ3HW QHZ'RJ 5HPRWH REMHFW 'RJ te object gets By the time the Remon, the Dog is the method invocatioon the heap. just another object \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 12 UHPRWHPHWKRGDUJXPHQWV WKHUH DUH QR 'XPE 4XHVWLRQV Q: Q: A: A: )M SITTING HERE WITH A (ASH-AP FULL OF 3ERIALIZABLE #USTOMER OBJECTS WITH 3TRING KEYS $O ) HAVE TO WORRY ABOUT WHETHER THE (ASH-AP ITSELF IS 3ERIALIZABLE /+ THIS IS KIND OF A TRICKY ONE !LL OF THE #OLLECTION IMPLEMENTATIONS IN THE *3% !0) ARE 3ERIALIZABLE 3O YOU DONT HAVE TO WORRY ABOUT A (ASHMAPAS LONG AS WHAT YOU PUT IN THE (ASHMAP IS 3ERIALIZABLE YOURE FINE "UT THERE IS ONE PLACE WHERE THINGS CAN FAIL #HANCES ARE YOULL NEVER SEE THIS BUT ITS STILL WORTH MENTIONING 9OU PROBABLY ALREADY KNOW THAT THE -AP CLASSES LIKE (ASH-AP AND (ASHTABLE HAVE A VALUES METHOD THAT RETURNS A COLLECTION OF JUST THE VALUES WITHOUT THE KEYS )N OTHER WORDS IF YOU CALLED IT ON YOUR (ASH-AP YOUD GET A #OLLECTION OF #USTOMER OBJECTS "UT WHAT TYPE OF COLLECTION !ND THERES THE PROBLEM 9OU DONT KNOW !LL YOU KNOW IS THAT ITS SOMETHING THAT IMPLEMENTS THE #OLLECTION INTERFACE "UT THAT ISNT ENOUGH TO TELL YOU WHETHER THE #OLLECTION RETURNED BY VALUES IS 3ERIALIZABLE )N OTHER WORDS YOU MIGHT GET BACK A #OLLECTION THATEVEN WHEN FILLED WITH 3ERIALIZABLE OBJECTSIS NOT ITSELF 3ERIALIZABLE 4HE BOTTOM LINE DONT RELY ON THE #OLLECTION RETURNED FROM A MAPS VALUES METHOD TO BE 3ERIALIZABLE 0UT YOUR VALUES INTO SOMETHING YOU CAN TRUST TO BE 3ERIALIZABLE LIKE !RRAY,IST BEFORE TRYING TO SHIP THEM AS PART OF A REMOTE METHOD CALL )F SOMETHING YOURE PASSING TO A REMOTE METHOD ISNT 3ERIALIZABLE IS THIS A COMPILE TIME OR RUNTIME FAILURE 2UNTIME 5SUALLY ANYWAY 2EMEMBER THAT THE DECLARED ARGUMENT OR RETURN TYPE IS NOT NECESSARILY THE SAME AS THE ACTUAL RUNTIME TYPE 4HE ONLY CASE WHERE IT CAN FAIL AT COMPILE TIME IS IF THE REMOTE METHOD ACTUALLY USES 3ERIALIZABLE AS THE DECLARED TYPE OF THE ARGUMENT OR RETURN VALUE SXEOLFYRLGWDNH,W 6HULDOL]DEOHV )N THAT CIRCUMSTANCE THE COMPILER CAN USE NORMAL *AVA TYPE CHECKING TO SEE IF THE DECLARED TYPE OF THE THING BEING PASSED IMPLEMENTS 3ERIALIZABLE -OST OF THE TIME THE DECLARED ARGUMENT OR RETURN TYPE IS SOMETHING OTHER THAN 3ERIALIZABLE LIKE $OG !RRAY,IST 3TRING ETC SO *AVA WONT KNOW WHETHER THE RUNTIME OBJECT IS 3ERIALIZABLE UNTIL IT ACTUALLY TRIES TO DO THE SERIALIZATION AND THEN IT THROWS AN EXCEPTION !ND WITH COLLECTIONS AND ARRAYS IF ANY OF THE OBJECTS INSIDE ARENT 3ERIALIZABLE THE WHOLE SERIALIZATION FAILS Q: $OES MY CLASS HAVE TO EXPLICITLY IMPLEMENT 3ERIALIZABLE OR CAN ) INHERIT 3ERIALIZABLENESS FROM MY SUPERCLASS A: 2EMEMBER *AVAS )3 ! RULE IF YOUR PARENT SUPERCLASS IS SOMETHING THEN SO ARE YOU )F $OG EXTENDS !NIMAL AND !NIMAL IMPLEMENTS 3ERIALIZABLE THEN $OG IS 3ERIALIZABLE WHETHER THE $OG CLASS EXPLICITLY DECLARES IT OR NOT (OWEVER IT IS CONSIDERED GOOD PRACTICE TO EXPLICITLY DECLARE YOUR CLASS AS 3ERIALIZABLE EVEN IF YOUR SUPERCLASS DOES JUST SO THAT OTHERS LOOKING AT YOUR CLASS !0) DONT HAVE TO HUNT THROUGH YOUR CLASS INHERITANCE TREE TO SEE IF SOMEBODY UP THERE IS 3ERIALIZABLE &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Page 13 Return to Table of Contents DUFKLWHFWXUDORYHUYLHZ 5HPHPEHUDUJXPHQWVDQG UHWXUQYDOXHVIRUDUHPRWH PHWKRGPXVWEHRQHRIWKHVH 5 3ULPLWLYHV 5 6HULDOL]DEOHREMHFWV 5 $QDUUD\RUFROOHFWLRQRISULPLWLYHV RU6HULDOL]DEOHREMHFWV ,GRQ·WJHWZK\LW·VOHJDO WRSDVVD5HPRWHREMHFWLQD UHPRWHPHWKRG,VQ·WWKHZKROH SRLQWRID5HPRWHREMHFWWRVWD\ UHPRWH":K\ZRXOG\RXVHQGD 5HPRWHREMHFWWRWKHFOLHQW"" 7KDWPDNHVQRVHQVH 5 $5HPRWHREMHFW ItÕll make sense in a minute. But before you turn the page, think about the implications of passing a Remote object through a remote method call... \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 14 UHPRWHPHWKRGDUJXPHQWV 8I[[QVOI:MUW\MWJRMK\\PZW]OPI ZMUW\MUM\PWLKITT )T DOES NOT MAKE SENSE TO SEND A 2EMOTE OBJECT IN A REMOTE METHOD CALL !FTER ALL THE WHOLE POINT OF A 2EMOTE OBJECT IS TO STAY REMOTE 4O BE ACCESSED BY CLIENTS WHO LIVE SOMEWHERE ELSE )N ANOTHER HEAP "UT WHAT IF YOU WANT TO PASS A REMOTE CLIENT A REFERENCE TO ANOTHER 2EMOTE OBJECT 7HAT IF RATHER THAN HANDING YOUR CLIENT A FULL BLOWN COPY OF A #USTOMER YOU SEND HIM A STUB TO A 2EMOTE #USTOMER 4HINK ABOUT IT "EFORE YOU READ THE NEXT PAGE :KHQ\RXSDVVD5HPRWH REMHFWWRRUIURPDUHPRWH PHWKRG-DYDDFWXDOO\VHQGV WKH5HPRWHREMHFW·VVWXE ,QRWKHUZRUGVDWUXQWLPH WKH5HPRWHREMHFWVWD\VULJKW ZKHUHLWLVDQGLWVVWXELV VHQWRYHUWKHZLUHLQVWHDG YiX`e gfn\i :KDWDUHWKHLPSOLFDWLRQVRISDVVLQJDVWXEWRD 5HPRWH&XVWRPHUREMHFWDVRSSRVHGWRSDVVLQJD QRQUHPRWH&XVWRPHUREMHFW" A remote re remote obje ference is a stub to a a remote re ct. If the client has reference toference, he has a local talk to the a stub, and the stub can Remote obje ct. :KDWDUHWKHEHQH¿WVRISDVVLQJDVWXELQVWHDGRI WKHUHDO&XVWRPHUREMHFW" FOLHQW VHUYHU )RR VWXE :KDWDUHWKHGUDZEDFNV" ^gg^ )RR nce 1RWHZKHWKHUWRSDVVVHULDOL]HGREMHFWVRUVWXEV WRUHPRWHREMHFWVLVDFUXFLDOGHVLJQGHFLVLRQ:H¶OO H[SORUHWKLVZKHQZHORRNDWSHUIRUPDQFHDQG SDWWHUQV stub to a Remote object 5HPRWH )RR REMHFW local refere to a stub &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 15 VWDWHIXODQGVWDWHOHVVVHVVLRQEHDQV ;M[[QWVJMIV[KIVJM[\I\MTM[[ WZ[\I\MN]T 7ELL GO OVER ALL THIS IN DETAIL IN THE 3ESSION "EAN CHAPTER &OR NOW YOU NEED TO KNOW THAT SESSION BEANS CAN BE MARKED AT DEPLOYMENT TIME AS EITHER STATELESS OR STATEFUL ! STATEFUL BEAN CAN REMEMBER CONVERSATIONAL STATE BETWEEN METHOD CALLS WHILE A STATELESS BEAN WONT REMEMBER ANYTHING ABOUT THE CLIENT BETWEEN METHOD INVOCATIONS 4HE PHRASE hCONVERSATIONAL STATEv REALLY MEANS hCLIENT SPECIlC STATEv AND A TYPICAL EXAMPLE IS A SHOPPING CART )T WOULDNT BE FUN IF YOU THE SHOPPER GOT A CART PUT SOMETHING IN BUT THEN WHEN YOU GO TO PUT THE SECOND THING IN THE lRST THING VANISHES FROM THE CART .OT TOO USER FRIENDLY 3O A GOOD SHOPPING CART WILL KEEP THE CLIENT SHOPPER STATE IE THE ITEMS IN THE CART FOR AS LONG AS THE SHOPPING SESSION IS ALIVE 7ELL EXPLAIN WHAT WE MEAN BY ALIVE IN THE 3ESSION "EAN CHAPTER 3TATELESS BEANS SIMPLY FORGET ABOUT THE CLIENT ONCE THE METHOD CALL COMPLETES 3O STATELESS BEANS ARE FOR SERVICES THAT DONT REQUIRE A CONTINUED CONVERSATION BETWEEN THE CLIENT AND THE SERVICE 4HAT DOESNT MEAN THE CLIENT WONT KEEP CALLING METHODS ON THE STATELESS BEAN BUT IT DOES MEAN THAT THE CLIENT CANT DEPEND ON THE BEAN REMEMBERING ANYTHING ABOUT THE PREVIOUS METHOD CALLS :DWFKLW WKHUH DUH QR 'XPE 4XHVWLRQV Q: )VE HEARD THAT ONLY STATELESS SESSION BEANS ARE SCALABLE AND THAT NOBODY SHOULD EVER USE STATEFUL SESSION BEANS )S THAT TRUE A: .O NOT COMPLETELY )T IS TRUE THAT STATELESS SESSION BEANS ARE GENERALLY MORE SCALABLE THAN STATEFUL SESSION BEANS BECAUSE OF THE WAY STATELESS BEANS ARE MANAGED BY THE CONTAINER 9OULL SEE THE REASONS FOR THIS IN THE 3ESSION "EAN CHAPTER "UT THAT DOESNT MEAN YOU SHOULD NEVER USE STATEFUL BEANS 9OU SHOULD CONSIDER STATEFUL BEANS WHEN YOU NEED CONVERSATIONAL STATE AND WHEN THE ALTERNATIVES FOR SAVING THAT STATE LIKE USING THE CLIENT TO STORE STATE OR USING A SERVLET TO STORE STATE OR USING A DATABASE TO STORE STATE BETWEEN EACH METHOD CALL FROM THE CLIENT ARE MORE OF A PERFORMANCE HIT THAN THE LESS SCALABLE NATURE OF STATEFUL SESSION BEANS 6WDWHOHVVEHDQV&$1 KDYHVWDWH -XVWQRW FOLHQWVSHFLÀFVWDWH OHVV´PHDQV 6RPHSHRSOHWKLQN³VWDWH FDQKDYH ³QRVWDWH´$VWDWHOHVVEHDQ RWKHUREMHFW LQVWDQFHYDULDEOHVOLNHDQ\ DLQWDLQYDOXHV LWMXVWFDQ¶WXVHWKHPWRP QW VSHFL¿FWRDSDUWLFXODUFOLH &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 16 DUFKLWHFWXUDORYHUYLHZ :KHQWKHUHWXUQYDOXHLVD 5HPRWHREMHFW 7KHFOLHQWLQYRNHVJHW&XVWRPHU RQ5HPRWH REMHFW$ XVLQJVWXE$ JHW&XVWRPHU JHW&XVWRPHU JHW&XVWRPHU FOLHQW 5HPRWH REMHFW$ VNHO VWXE$ 5HPRWH REMHFW% &XVWRPHU 5HPRWHREMHFW$UHWXUQVDUHIHUHQFHWRD &XVWRPHUREMHFW 5HPRWHREMHFW% 7KHVNHOHWRQ VXEVWLWXWHV DQGVHULDOL]HV WKH5HPRWHREMHFW·V VWXEDQGVHQGVLWEDFNWRWKHFOLHQW turns ct (A) simply re The Remote obncjee to the Customer a local refere e object B), but itÕs object (Remot ub that gets sent back. actually the st 7KH&XVWRPHUVWXE % LVGHVHULDOL]HGRQWKH FOLHQWDQGWKHFOLHQWJHWVDORFDOUHIHUHQFHWRWKH QHZVWXEREMHFW 5HPRWH REMHFW$ VNHO FOLHQW VWXE$ ORFDOUHIHUHQFH WRVWXE% VWXE% k a local Now the client gets bacme r stub sto Cu the to nce refere object (B). 5HPRWH 5HPRWH REMHFW% VWXE% Serialized stub B (the Customerto Remote object object). &XVWRPHU \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 17 50,TXHVWLRQV WKHUH DUH QR 'XPE 4XHVWLRQV :MDD=LHGAFLK Q: 7HAT HAPPENS IF THE CLIENT OBJECT AND THE 2EMOTE OBJECT ARE RUNNING IN DIFFERENT *6-S BUT ON THE SAME PHYSICAL MACHINE )N OTHER WORDS THEYRE BOTH RUNNING IN *AVA PROGRAMS ON THE SAME SERVER (-%XVHV-DYD50, 5HPRWH0HWKRG,QYRFDWLRQ VR WKDW\RXUEHDQVFDQEHDFFHVVHGE\UHPRWHFOLHQWV $UHPRWHFOLHQWLQWKLVFRQWH[WLVDQREMHFWUXQQLQJLQ DGLIIHUHQW-90ZKLFKDOVRPHDQVDGLIIHUHQWKHDS A: $OESNT MAKE ANY DIFFERENCE !LL THAT MATTERS IS WHETHER THE TWO OBJECTS LIVE IN DIFFERENT HEAPS AND *6-S DO NOT SHARE HEAPS WITH ONE ANOTHER THANK YOU VERY MUCH NO MATTER HOW INTIMATE THEY ARE COHABITING THE SAME SERVER )N FACT YOU CAN AND WITH %*" OFTEN MUST USE 2-) EVEN WHEN THE OBJECTS ARE IN THE SAME HEAP Q: 7HY IN THE WORLD WOULD YOU EVER WANT TO DO USE 2-) IF YOU DONT NEED TO )SNT THERE ENOUGH OVERHEAD WITH REMOTE CALLS AS IT IS A: $5HPRWHREMHFWVWD\VLQLWVRZQKHDSZKLOHFOLHQWV LQYRNHPHWKRGVRQWKH5HPRWHREMHFW·VSUR[\FDOOHG DVWXE 7KHVWXEREMHFWKDQGOHVDOOWKHORZOHYHOQHWZRUNLQJ GHWDLOVLQFRPPXQLFDWLQJZLWKWKH5HPRWHREMHFW :KHQWKHFOLHQWZDQWVWRFDOODPHWKRGRQD5HPRWH REMHFWWKHFOLHQWFDOOVWKHVDPHPHWKRGRQWKHVWXE 7KHVWXEOLYHVLQWKHVDPHKHDSDVWKHFOLHQW 7RWKHFOLHQWDUHPRWHPHWKRGFDOOLVLGHQWLFDOWRD ORFDOPHWKRGFDOOH[FHSWDUHPRWHPHWKRGFDQWKURZ D5HPRWH([FHSWLRQ DFKHFNHGH[FHSWLRQ 7ELL GO INTO THIS IN MORE DETAIL LATER BUT THE MAIN REASON IS BECAUSE IF YOU DONT USE 2-) FOR METHOD CALLS YOURE LOCKING DOWN YOUR DESIGN IN SUCH A WAY THAT YOU CANT DISTRIBUTE YOUR OBJECTS IN DIFFERENT PLACES IN YOUR NETWORK OR EVEN ON THE SAME SERVER )N OTHER WORDS WITHOUT 2-) YOU MUST HAVE BOTH OBJECTS IN THE SAME HEAP 7KHVWXESDFNDJHVXSWKHPHWKRGDUJXPHQWVDQG &OR A DISTRIBUTED PROGRAMMING MODEL THATS A PRETTY PERMANENT DECISION WITH NO CHANCE TO CHANGE YOUR MIND LATER WITHOUT REWRITING CODE /N THE OTHER HAND IF YOU DO USE 2-) YOU CAN DECIDE LATER TO SPLIT YOUR PROGRAM UP INTO DIFFERENT NODES ON YOUR SYSTEM WITH LITTLE OR USUALLY NO CODE CHANGES $UJXPHQWVDQGUHWXUQYDOXHVPXVWEHRQHRIWKH 3O THE TRADEOFF IS FLEXIBILITY FOR PERFORMANCE BUT FOR MOST DISTRIBUTED ENTERPRISE APPS THE OVERHEAD OF A REMOTE CALL IS NOT YOUR BIGGEST PROBLEM )TS USUALLY YOUR BANDWIDTH ANDOR CONCURRENCY THAT HURTS THE MOST /VERALL YOU PROBABLY HAVE BIGGER PERFORMANCE FISH TO FRY IN AREAS OTHER THAN WHETHER YOUR CALLS ARE REMOTE OR LOCAL "UT THE STORY ISNT ALWAYS THAT SIMPLE SO WELL EXPLORE THIS AGAIN LATER IN THE BOOK VHQGVLQIRUPDWLRQDERXWWKHFDOOWRDVNHOHWRQRQ WKHVHUYHU7KHVNHOHWRQREMHFWLWVHOILVRSWLRQDOEXW WKHVNHOHWRQ·VZRUNPXVWEHGRQHE\VRPHWKLQJRQ WKHVHUYHU:HGRQ·WKDYHWRFDUHZKR³RUZKDW³LV DFWXDOO\GRLQJWKHVNHOHWRQ·VZRUN IROORZLQJDSULPLWLYHD6HULDOL]DEOHREMHFWDQDUUD\ RUFROOHFWLRQRISULPLWLYHVRU6HULDOL]DEOHREMHFWVRUD 5HPRWHREMHFW,IWKHYDOXHLVQ·WRQHRIWKHVH\RX·OO JHWDUXQWLPHH[FHSWLRQ ,IDQREMHFWLVSDVVHGDVDQDUJXPHQWRUUHWXUQ YDOXHWKHREMHFWLVVHQWDVDVHULDOL]HGFRS\WKHQ GHVHULDOL]HGRQWKH5HPRWHREMHFW·VORFDOKHDS ,ID5HPRWHREMHFWLVSDVVHGDVDQDUJXPHQWRUUHWXUQ YDOXHWKHREMHFW·VVWXELVVHQWLQVWHDG &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 18 DUFKLWHFWXUDORYHUYLHZ 2KP\,DOPRVWIRUJRWWKH PRVWLPSRUWDQWWKLQJDERXWUHPRWH PHWKRGFDOOV:KHQ\RXSDVVDVHULDOL]HG REMHFWDVDQDUJXPHQWRUUHWXUQYDOXH\RX EHWWHUPDNHVXUHWKHFODVVÀOHIRUWKHW\SH \RX·UHSDVVLQJLVDYDLODEOHRQWKHRWKHU VLGH,IWKHFODVVLVQ·WWKHUHWKHREMHFW ZLOOQHYHUGHVHULDOL]HDQG \RX·UHVFUHZHG 7KDWLQFOXGHVWKHVWXE FODVVHV,IWKHFOLHQW GRHVQ·WKDYHWKHFODVVIRU WKHVWXEREMHFWLW·V KRSHOHVV \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 19 5HPRWHLQWHUIDFH ?PI\U][\\PM:MUW\MWJRMK\ IVL\PM[\]JPI^MQVKWUUWV' (OW DOES THE CLIENT KNOW WHICH METHODS TO CALL (OW DOES THE STUB KNOW WHICH METHODS THE 2EMOTE OBJECT HAS 2EMEMBER IF THE STUB IS PRETENDING TO BE THE 2EMOTE OBJECT THE STUB MUST HAVE THE SAME METHODS AS THE 2EMOTE OBJECT /F COURSE YOU KNOW THE ANSWER TO THIS !N INTERFACE 4HE WAY ALL METHODS IN A DISTRIBUTED ENVIRONMENT SHOULD BE EXPOSED TO A CLIENT 7E CALL THIS THE BUSINESS INTERFACE BECAUSE IT HAS THE BUSINESS METHODS THE CLIENT WANTS TO CALL 4ECHNICALLY THE BUSINESS INTERFACE FOR A 2EMOTE OBJECT MUST BE SURPRISINGLY A 2EMOTE INTERFACE ,WDOOEHJLQVZLWKD5HPRWH LQWHUIDFH%RWKWKH5HPRWH REMHFWDQGWKHVWXELPSOHPHQW WKHVDPHLQWHUIDFHWKHRQH ZLWKWKHPHWKRGVWKHFOLHQW ZDQWVWRFDOO $5HPRWHLQWHUIDFHPXVW H[WHQGMDYDUPL5HPRWHDQG HYHU\PHWKRGPXVWGHFODUHD 5HPRWH([FHSWLRQ 4O BE 2EMOTE AN INTERFACE MUST FOLLOW THREE RULES 5 LWPXVWH[WHQGMDYDUPL5HPRWH 5 HDFKPHWKRGPXVWGHFODUHD MDYDUPL5HPRWH([FHSWLRQ 5 DUJXPHQWVDQGUHWXUQW\SHVPXVWEH VKLSSDEOH 6HULDOL]DEOHSULPLWLYHHWF Remote RemoteException and mi package a.r jav in interface are i.Remote T extend java.rm US M e ac rf te in . A Remote have any methods) SXEOLFLQWHUIDFH'LFH5ROOHUH[WHQGV5HPRWH^ (which doesnÕt All of your methods must declare SXEOLFLQWUROO'LFH WKURZV5HPRWH([FHSWLRQ a RemoteException. ` LPSRUWMDYDUPL &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 20 DUFKLWHFWXUDORYHUYLHZ <PMKTQMV\KITT[J][QVM[[UM\PWL[ WV\PM[\]J\PZW]OP\PM:MUW\M J][QVM[[QV\MZNIKM LQWHUIDFH!! 5HPRWH 2EMEMBER AS FAR AS THE CLIENTS CONCERNED HES CALLING METHODS ON 4HE 2EAL 4HING 4HE ACTUAL 2EMOTE OBJECT 4HE THING THAT HAS THE METHODS HE WANTS TO CALL 4HE ONLY THING REMINDING THE CLIENT THAT HE ISNT CALLING METHODS DIRECTLY ON THE 2EMOTE OBJECT IS THE 2EMOTE%XCEPTIONS HE HAS TO DEAL WITH LQWHUIDFH!! 'LFH5ROOHU UROO'LFH the stub class UROO'LFH 'LFH5ROOHU,PSO UROO'LFH te the Remcolass object Remote ollerImpl (therolling eR ic D e th h Bot e actual dice object with y)thand the DiceRoller stub rface. it functional e Remote DiceRoller inte th t en em impl &OLHQWKHDS XE 'L FH5 ROOHUVW mote business The client uses the Re ods on the stub. th me l cal to interface te object both mo Re e The stub and th te interface. implement the Remo O 'LFH5ROOHU 6HUYHUKHDS 'LFH5ROOHU FOLHQW 'LFH5ROOHU6WXE the Remote erface (business) int 'L PS FH5 ROOHU, From this to show a skpoelint forward, weÕre not about is that eton object. All we ca going re SOMETHING is handling the skeleton func on the server tionality. \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 21 (-%DUFKLWHFWXUH 6KDUSHQ \RXU SHQFLO This sharpen gets you on the one most common mistake EJB developers make. So donÕt skip it! %DVHGRQWKLVVFHQDULRGUDZWKHFODVVHVEHORZLQWRWKH DSSURSULDWHVORWIRUZKHWKHUWKH\PXVWEHRQWKHFOLHQWVHUYHURU ERWK \RXFDQUHXVHDFODVV 7KHSLFWXUHLVVLPSOL¿HGVR\RX DUHQ¶WVHHLQJDOORIWKHSOD\HUVLQYROYHG 'RJ 'RJ 5HPRWH 'RJ7UDLQHU 3H WUDLQ W P\'RJ FOLHQW VWXE 'RJ7UDLQHU,PSO 'RJ7UDLQHU LQWHUIDFH 'RJ7UDLQHU6WXE FOLHQWFODVV 'RJ &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 22 DUFKLWHFWXUDORYHUYLHZ 0W_-2*][M[:51 )N %*" A CLIENTS ENTRY POINT INTO THE ENTERPRISE APPLICATION IS NEARLY ALWAYS THROUGH A REFERENCE STUB TO A 2EMOTE OBJECT 9ES IT IS POSSIBLE AND SOMETIMES NECESSARY TO USE A LOCAL CLIENT IE A CLIENT IN THE SAME HEAP AS THE BEAN AND WHICH DOESNT USE 2-) TO INVOKE BUSINESS METHODS BUT THIS IS FOR ONLY A FEW VERY SPECIAL CASES 3ODLQ50, 3O 2-) LIES AT THE HEART OF MOST CLIENT TO BEAN COMMUNICATION "UT AS YOU SAW A HINT OF IN THE lRST CHAPTER THE %*" ARCHITECTURE IS A LITTLE MORE COMPLEX THAN A SIMPLE CLIENT TO STUB TO 2EMOTE OBJECT SCENARIO )N %*" THE BEANTHE THING ON WHICH THE CLIENT WANTS TO CALL BUSINESS METHODSIS NOT 2EMOTE Business logic lives here! VHUYHU VWXE EL]LQWHUIDFH FOLHQW EL]LQWHUIDFH &OLHQW 5HPRWH REMHFW '% KHDS Business logic lives here! 50,LQ(-% VHUYHU (-% W 2EM H F (Q KHDS DQ 5HPRWH REMHFW VHUYLFHV VWXE EL]LQWHUIDFH FOLHQW EL]LQWHUIDFH &OLHQW WHUS VHEH UL '% \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 23 (-%REMHFW <PM:MUW\MWJRMK\Q[VW\\PMJMIVQ\¼[ \PMJMIV¼[JWLaO]IZL ¸ \PM-2*7JRMK\ )N %*" REMEMBER THE 2EMOTE OBJECT %*"/BJECT IS THE BEANS BODYGUARD 4HE BEAN SITS BACK PROTECTED FROM ALL CLIENT INVOCATIONS WHILE THE %*"/BJECT IMPLEMENTS THE 2EMOTE INTERFACE AND TAKES THE REMOTE CALLS /NCE THE CALL GETS TO THE %*"/BJECT THE SERVER JUMPS IN WITH ALL THE SERVICES LIKE SECURITY IS THIS CLIENT AUTHORIZED TO CALL THIS METHOD TRANSACTIONS IS THIS CALL PART OF AN EXISTING TRANSACTION OR SHOULD WE START ANOTHER TRANSACTION AND PERSISTENCE DOES THE BEAN NEED TO LOAD INFO FROM THE DATABASE BEFORE RUNNING THE CLIENTS METHOD 4HE %*"/BJECT IMPLEMENTS THE 2EMOTE BUSINESS INTERFACE SO THE REMOTE CALLS FROM THE CLIENT COME TO THE %*"/BJECT "UT ITS STILL THE BEAN THAT HAS THE REAL BUSINESS LOGIC EVEN THOUGH THE BEAN DOESNT IMPLEMENT THE 2EMOTE INTERFACE IN THE TECHNICAL *AVA WAY 1RERG\WDONVWRWKH EHDQZLWKRXWJRLQJ WKURXJKPHÀUVW The bean doesnÕt the Remote (busineactually implement it DOES have the ss) interface! (But real business logic.) (-%VHUYHU (-% W 2EM H F KHDS (Q DQ 5HPRWH REMHFW VHUYLFHV VWXE EL]LQWHUIDFH FOLHQW 7KHEHDQFODVVGRHV127 LPSOHPHQWWKHEXVLQHVV LQWHUIDFH LQWKHIRUPDO -DYDZD\ EXWWKHEHDQ KDVWKHUHDOEXVLQHVVORJLF IXQFWLRQDOLW\ emote ub and the Rme interface, Both the stem sa e th ent object impl nÕt have the REAL but they doc implementations. business logi EL]LQWHUIDFH &OLHQW %RWKWKH5HPRWHREMHFW DQGWKHVWXELPSOHPHQWWKH VDPHLQWHUIDFH³WKHEXVLQHVV LQWHUIDFH FDOOHGD&RPSRQHQW LQWHUIDFH ³EXWZLWKRXWWKH UHDOEXVLQHVVORJLFEHKDYLRU WHUS VHEH UL '% &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 24 DUFKLWHFWXUDORYHUYLHZ <PM+WUXWVMV\QV\MZNIKM )N %*" THE BUSINESS INTERFACE IS CALLED THE COMPONENT INTERFACE 4HIS IS WHERE YOU EXPOSE YOUR BUSINESS METHODS TO THE CLIENT 4HE MAIN DIFFERENCE BETWEEN AN 2-) INTERFACE AND A REMOTE COMPONENT INTERFACE IS THAT WITH %*" YOU EXTEND JAVAXEJB%*"/BJECT INSTEAD OF JAVARMI2EMOTE LQWHUIDFH!! 5HPRWH ces All Remote interfa must extend e. java.rmi.Remot LQWHUIDFH!! (-%2EMHFW In RMI, java.rmi.Ryoemu extend EJB, your inote directly. In terface exte javax.ejb.E nd in turn exteJBObject, which s nds Remote. .H\SRLQWV $Q\LQWHUIDFHZLWKMDYDUPL5HPRWHLQLWVLQKHULWDQFH WUHHLVD5HPRWHLQWHUIDFH VHYHUDOPHWKRGV 7KH(-%2EMHFWLQWHUIDFHH[WHQGV5HPRWHVR (-%2EMHFWLVD5HPRWHLQWHUIDFH LQWHUIDFH!! %RRN&DUW <RXUUHPRWHFRPSRQHQWLQWHUIDFHPXVWH[WHQGWKH (-%2EMHFWLQWHUIDFH <RXFDQKDYHDORFDOFRPSRQHQWLQWHUIDFHDQGWKH UXOHVDUHGLIIHUHQWEXWZH¶OOORRNDWWKDWLQWKHFKDSWHU RQ&OLHQW9LHZ <RXH[SRVH\RXUEXVLQHVVPHWKRGVWRWKHFOLHQW WKURXJKWKHFRPSRQHQWLQWHUIDFH 7KH(-%2EMHFWLQWHUIDFHDGGVDGGLWLRQDOPHWKRGVIRU WKHFOLHQWWRXVH :H¶OOVHHWKRVHODWHU DGG%RRN UHPRYH%RRN VKRZ%RRNV,Q&DUW GR&KHFNRXW interface Your component.ejb.E JBObject. extends javax you put your This is where ods. This is business meth uses! what the client :KRHYHULPSOHPHQWVWKH%RRN&DUW LQWHUIDFHPXVWLPSOHPHQWDOOWKHPHWKRGV IURPERWK%RRN&DUWDQG(-%2EMHFW7KH (-%2EMHFWLQWHUIDFHDGGVWKHPHWKRGV WKDWDOO(-%FOLHQWVPLJKWQHHG \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 25 WKHEHDQFODVV +RZWKHEHDQFODVVÀWVLQ -6($3, LQWHUIDFH!! 5HPRWH LQWHUIDFH!! (QWHUSULVH%HDQ -(($3, QRPHWKRGV QRPHWKRGV -(($3, LQWHUIDFH!! (-%2EMHFW VHYHUDOPHWKRGV <28ZULWHWKLV LQWHUIDFH WKHUHPRWH FRPSRQHQW LQWHUIDFH LQWHUIDFH!! %RRN&DUW DGG%RRN UHPRYH%RRN VKRZ%RRNV,Q&DUW GR&KHFNRXW LQWHUIDFH!! 6HVVLRQ%HDQ -(($3, VHYHUDOPHWKRGV %RRN&DUW%HDQ <28ZULWHWKLV FODVV DGG%RRN UHPRYH%RRN WKHEHDQFODVV VKRZ%RRNV,Q&DUW GR&KHFNRXW RWKHUPHWKRGV <RX5($//< KDYHWRNQRZ WKHLQWHUIDFHV QRZ 7KHH[DPH[SHFWV\RXWRN OLYHV H[DFWO\ZKHUHHDFKPHWKRG WKRGV PH KLFK ZZ NQR GWR QHH <RX DUH KLFK GZ WDQ EMHF -%2 DUHLQ( H LQ6HVVLRQ%HDQIRUH[DPSO DFW $QG\RXKDYHWRNQRZWKHH[ GV VLJQDWXUHIRUWKRVHPHWKR HVLQ :H¶OOJHWLQWRWKHVHLQWHUIDF DQG WKHQH[WFRXSOHRIFKDSWHUV ZKHQZHGRSD\DWWHQWLRQ ookCCaarrttBBeeaann does not implement the ookCart interfa ce! ut it sure looks lik e it does...) The bean developer must make sure that h the the bean class has methods that matcexact methods in the component interface,the ly as if the bean class DID implement component interface. &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 26 DUFKLWHFWXUDORYHUYLHZ WKHUH DUH QR 'XPE 4XHVWLRQV Q: ) JUST WANT TO BE SURE )M CLEAR ABOUT THIS INTERFACES CAN %84%.$ OTHER INTERFACES A: 9ES INTERFACES HAVE THEIR OWN INHERITANCE TREE )N FACT WITH INTERFACES YOU CAN DO SOMETHING YOU CANT EVER DO WITH A CLASSAN INTERFACE CAN EXTEND MORE THAN ONE INTERFACE LQWHUIDFH&DUWH[WHQGV(-%2EMHFW&DUW%XVLQHVV Q: "UT WHAT DOES THAT REALLY MEAN WHEN AN INTERFACE EXTENDS ANOTHER INTERFACE %XTENDING MEANS INHERITANCE BUT WHAT IS THE INTERFACE INHERITING A: 7HEN ONE INTERFACE EXTENDS ANOTHER IT INHERITS EVERYTHING FROM THAT INTERFACE 7HOEVER IMPLEMENTS AN INTERFACE MUST IMPLEMENT NOT JUST THE METHODS FROM THAT INTERFACE BUT ALSO THE METHODS FROM EVERY INTERFACE THAT INTERFACE EXTENDS ALL THE WAY UP THE INTERFACE INHERITANCE TREE 3O IN THIS EXAMPLE WHOEVER IMPLEMENTS "OOK#ART MUST ALSO IMPLEMENT THE METHODS OF %*"/BJECT :KHQ\RXLPSOHPHQW DQLQWHUIDFH\RXPXVW LPSOHPHQWDOOWKH PHWKRGVWKDWLQWHUIDFH LQKHULWVIURPLWVVXSHU LQWHUIDFHV 6RZKRHYHULPSOHPHQWV %RRN&DUWPXVWLPSOHPHQW WKHPHWKRGVIURPERWK %RRN&DUWDQG(-%2EMHFW Q: 7HY DOESNT THE BEAN IMPLEMENT THE 2EMOTEBUSINESS INTERFACE )SNT THE WHOLE POINT OF AN INTERFACE IMPLEMENTATION TO USE THE COMPILER TO KEEP YOU HONEST AND TO SUPPORT *AVA TYPE CHECKING A: 9OU ASKED THIS QUESTION BEFORE "UT HEY WE ALL FORGET THINGS SO WELL REMIND YOU AGAIN 4HE BEAN DOESNT IMPLEMENT THE 2EMOTE INTERFACE BECAUSE THE BEAN IS NEVER SUPPOSED TO BE A 2EMOTE OBJECT IN THE *AVA 2-) SENSE )N OTHER WORDS YOU NEVER EVER WANT ANYONE TO HAVE A STUB TO THE ACTUAL BEAN )F YOU WERE TO SOMEHOW SNEAK A REMOTE REFERENCE IE A STUB OUT TO THE WORLD YOUD BE DEFEATING THE WHOLE PURPOSE OF %*" )F YOU LET A CLIENT TALK DIRECTLY TO THE BEAN THEN THE SERVER WOULDNT BE ABLE TO APPLY ITS SERVICES AND IF YOU DONT NEED THE SERVICES YOU SEE WHERE WERE GOING HERE 4ECHNICALLY IT IS LEGAL TO HAVE THE BEAN IMPLEMENT THE 2EMOTE INTERFACE BUT ITS A REALLY BAD IDEA SINCE YOU COULD MAKE MISTAKES THAT WOULDNT BE CAUGHT AT COMPILE TIME BUT WHICH WOULD BLOW UP LATER "UT YOU DONT NEED TO DO IT SINCE VIRTUALLY ALL BEAN DEVELOPMENT TOOLS INCLUDING NEARLY EVERY BEAN AWARE )$% UNDERSTAND THE RELATIONSHIP BETWEEN THE BEAN THE %*"/BJECT AND THE 2EMOTE INTERFACE AND THEY GUARANTEE THAT THE COMPONENT INTERFACE AND THE BEAN CLASS HAVE MATCHING METHODS Q: "UT WHAT IF ) JUST lND THIS TOO DISTURBING 4HIS WHOLE IDEA VIOLATES MY *AVA SENSIBILITIES THE VERY PRINCIPLES UPON WHICH ) CODE 3URELY THERE MUST BE SOMETHING ) CAN DO A: 9OU COULD TRUST US ON THE WHOLE hYOURE ALMOST CERTAINLY USING TOOLS SO IT REALLY WONT MATTER 2EALLYv THING BUT NO SO IF YOU INSIST YES THERE IS SOMETHING YOU CAN DO THATLL PROBABLY HELP YOU FEEL BETTER )TS ON THE NEXT PAGE BUT YOU CAN PROBABLY FIGURE IT OUT YOURSELF ANYWAY .ONETHELESS WERE STICKING BY OUR STORY THAT MOST DEVELOPERS WONT NEED TO DO THIS \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 27 LQWHUIDFHLPSOHPHQWDWLRQ $GHVLJQIRUWKRVHZKRIHHO VTXHDPLVKWKDWWKHEHDQ GRHVQ·WLPSOHPHQWWKH EXVLQHVVPHWKRGLQWHUIDFH LQWHUIDFH!! 5HPRWH Create an int the business meerthfaodce that has not extend EJBObjs, but does ect (so itÕs not a remote int erface). LQWHUIDFH!! (QWHUSULVH%HDQ QRPHWKRGV QRPHWKRGV LQWHUIDFH!! %RRN&DUW%XVLQHVV LQWHUIDFH!! (-%2EMHFW VHYHUDOPHWKRGV LQWHUIDFH!! %RRN&DUW QRPHWKRGV DGG%RRN UHPRYH%RRN VKRZ%RRNV,Q&DUW GR&KHFNRXW LQWHUIDFH!! 6HVVLRQ%HDQ VHYHUDOPHWKRGV %RRN&DUW%HDQ DGG%RRN UHPRYH%RRN VKRZ%RRNV,Q&DUW GR&KHFNRXW RWKHUPHWKRGV rface ponent inBtOebject m o c e h t , w No th the EJ extends boAND the business ce a interf interface. method Make the bean class lem ent both the SessionBeanimp interface AND the business metho d interface. Now, the compile r will make sure you have the right methods in the bean class. &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 28 DUFKLWHFWXUDORYHUYLHZ 6R,ZULWHP\EHDQDQG ,SXWLQPDWFKLQJPHWKRGV IURPWKHFRPSRQHQWLQWHUIDFH ZLWKRXWRIÀFLDOO\LPSOHPHQWLQJ WKHFRPSRQHQWLQWHUIDFH%XWLI, GRQ·WLPSOHPHQWWKHFRPSRQHQW LQWHUIDFHZKRGRHV" ?PW_ZQ\M[\PMKTI[[\PI\ZMITTa,7-; QUXTMUMV\\PMKWUXWVMV\QV\MZNIKM' 1VW\PMZ_WZL[_PWUISM[\PM -2*7JRMK\KTI[[' 4HE CONTAINER 9OU DECLARE THE METHODS BUT THE CONTAINER IMPLEMENTS YOUR COMPONENT INTERFACE 2EMEMBER YOUR COMPONENT INTERFACE IS THE ONE THAT EXTENDS %*"/BJECT SO THE CONTAINER HAS TO IMPLEMENT NOT JUST YOUR BUSINESS METHODS BUT ALSO THE METHODS OF %*"/BJECT WHICH WE HAVENT YET LOOKED AT Q: "UT HOW DOES THE CONTAINER KNOW WHAT TO PUT IN THOSE METHODS )M THE ONE WHO DECLARED THOSE METHODS A: 2EMEMBER THE CONTAINER ISNT IMPLEMENTING THE REAL BUSINESS LOGIC 4HE TRUE FUNCTIONALITY FOR THOSE BUSINESS METHODS LIVES IN YOUR BEAN CLASSTHE CLASS THAT YOU IMPLEMENT 4HE CLASS IMPLEMENTING THE COMPONENT INTERFACE IS GOING TO BE THE %*"/BJECT 4HE BODYGUARD 4HE 2EMOTE OBJECT !ND REMEMBER THAT THE %*"/BJECT IS ONLY PRETENDING TO BE THE BEAN )T CAN RESPOND TO THE REMOTE METHOD CALLS COMING FROM THE CLIENT VIA THE STUB BUT THE %*"/BJECTS ONLY JOB IS TO CAPTURE THE INCOMING CLIENT CALLS TO THE BEAN !FTER THAT ITS UP TO THE CONTAINER SERVER TO TAKE OVER 7E DONT REALLY KNOW HOW THE %*"/BJECT IS IMPLEMENTEDITS COMPLETELY UP TO THE VENDOR "UT WE DONT REALLY CARE !LL YOU NEED TO KNOW IS THAT AN %*" CONTAINER IS REQUIRED BY THE SPEC TO GENERATE THE CODE FOR THE %*"/BJECT AND ITS CORRESPONDING STUB \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 29 ZKRFUHDWHVZKLFKFODVV ?PWKZMI\M[_PI\' &OR A BEAN WITH A REMOTE CLIENT VIEW IN OTHER WORDS A BEAN THAT CAN BE ACCESSED BY REMOTE CLIENTS YOU KNOW THAT YOU HAVE TO WRITE THE #OMPONENT INTERFACE AND THE "EAN CLASS "UT SOMEBODY HAS TO WRITE THE CLASS THAT IMPLEMENTS YOUR #OMPONENT INTERFACE TO MAKE THE 2EMOTE %*"/BJECT AND SOMEBODY HAS TO MAKE THE STUB THAT GOES WITH THAT %*"/BJECT 4HAT SOMEBODY IS THE #ONTAINER !ND THOUGH WE HAVENT YET TALKED ABOUT THE (OME WEVE LISTED THE RELEVANT PIECES HERE FOR COMPLETENESS <RX WKH&RQWDLQHU 7KH&RPSRQHQWLQWHUIDFH H[WHQGVMDYD[HME(-%2EMHFW 7KH(-%2EMHFWFODVV LPSOHPHQWV\RXU&RPSRQHQWLQWHUIDFH 7KH%HDQFODVV LPSOHPHQWVMDYD[HME6HVVLRQ%HDQ RUMDYD[HME(QWLW\%HDQ 7KH(-%2EMHFWVWXEFODVV LPSOHPHQWV\RXU&RPSRQHQWLQWHUIDFH DQGNQRZVKRZWRWDONWRWKH(-%2EMHFW 7KH+RPHLQWHUIDFH H[WHQGVMDYD[HME(-%+RPHZH¶OO WDONDERXWWKLVRQWKHQH[WSDJH 7KH+RPHFODVV LPSOHPHQWV\RXU+RPHLQWHUIDFH 7KH+RPHVWXEFODVV LPSOHPHQWV\RXU+RPHLQWHUIDFH DQGNQRZVKRZWRWDONWRWKH+RPH &RQWDLQHU <RX W (-% 2EMHF &RQWDLQHU 5HPRWH REMHFW (Q DQ VWXE VHUYHU VHUYLFHV &OLHQW FRPSRQHQWLQWHUIDFH We havenÕt lked about so we donÕt ta show it on thisthpie Home yet, cture. WHUS VHEH UL <RX '% &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 30 DUFKLWHFWXUDORYHUYLHZ (-%2EMHFW+H\%HDQLHGRQ·W\RXHYHUJHWWLUHGRI DOZD\VKDYLQJPHLQWKHPLGGOHRIHYHU\WKLQJ"'RQ·W \RXHYHUMXVWZDQWWRKDYHDGLUHFWFRQYHUVDWLRQZLWK VRPHRQH" %HDQ1R,·PWRRLPSRUWDQW,·PWRRYDOXDEOH$QG ,·PVXUHDVKHOOQRWJRQQDVWDUWVFUHHQLQJP\RZQFDOOV 7KDW·VZKDW\RXSHRSOHDUHIRU (-%2EMHFW<RXSHRSOH" %HDQ<HDK\RXSHRSOHZKRZRUNIRUWKHFRQWDLQHU <RXWKH+RPHWKHVWXEVDOORI\RX0\MRELVWRKDQGOH WKHFRPSOH[EXVLQHVVORJLF7KHFULWLFDOIXQFWLRQVWKDW PHDQWKHGLIIHUHQFHEHWZHHQVXFFHVVDQGIDLOXUHLQDQ HQWHUSULVHHQYLURQPHQW (-%2EMHFW *HH]VRXQGVOLNHDPDUNHWLQJVSHHFK 2.VR\RXKDYHVRPHLPSRUWDQWPHWKRGVEXW,VWLOO GRQ·WVHHZK\\RXQHHGWRKDYHPHLQHYHU\FDOO (-%2EMHFW,JXHVVWKDWPDNHVVHQVH<RXSXWWKH VHFXULW\FKHFNVLQWKH;0/GHSOR\PHQWGHVFULSWRUDQG WKHQZKHQ,JHWWKHFDOOWKHVHUYHUFDQFKHFNWRVHH LIWKHFOLHQWKDVWKHULJKWDXWKRUL]DWLRQ%XWZKDWLI VHFXULW\LVQ·WHYHQDQLVVXHIRU\RX":KDWLIZKRHYHU GHSOR\V\RXGRHVQ·WFDUHZKRFDOOVWKHPHWKRGV" %HDQ1RWWRREULJKWDUH\RX"%XWDWOHDVW\RXFDQOLIW KHDY\REMHFWV7+,1.DERXWDOOWKHRWKHUWKLQJVWKDW PDWWHUOLNHWUDQVDFWLRQVDQGSHUVLVWHQFH (-%2EMHFW2K,IRUJRWDERXWWUDQVDFWLRQV2.WKDW PDNHVVHQVHWRR7KHVHUYHUKDVWRÀJXUHRXWLIWKHUH·V DWUDQVDFWLRQFRQWH[WEHIRUHLWFDOOV\RXUPHWKRGVR WKDW\RXUPHWKRGFDQUXQLQDWUDQVDFWLRQ(LWKHU\RXU RZQRUWKHFDOOHU·V %HDQ'XK (-%2EMHFW%XWZKHUHGRHVSHUVLVWHQFHFRPHLQ" %HDQ/RRNP\ZRUNLVWRRLPSRUWDQWWREH LQWHUUXSWHGE\FOLHQWVZKRKDYHQREXVLQHVVFDOOLQJPH LQWKHÀUVWSODFH'R\RXKRQHVWO\WKLQNWKDW,DPJRLQJ WRFKHFNVHFXULW\FOHDUDQFHVIRUHYHU\FDOOHU"/LNH, GRQ·WKDYHEHWWHUWKLQJVWRGR" %HDQ:HOOWKLQNDERXWHQWLW\EHDQVIRUDPLQXWH,I ,·PDQHQWLW\EHDQWKDWPHDQV,·PUHSUHVHQWLQJVRPH HQWLW\LQWKHXQGHUO\LQJSHUVLVWHQWVWRUHDQG³ (-%2EMHFW2.VRLW·VUHDOO\MXVWDVHFXULW\WKLQJEXW ,GRQ·WVHHZK\\RXFDQ·WMXVWKDYHWKHFRGHWRFKHFN WKHVHFXULW\DFFHVVRIWKHFDOOHU7KDWZRXOGVDYHDORW RIRYHUKHDG QDPHO\0( %HDQ8KQHZVÁDVKWKHZRUGSKUDVH´SHUVLVWHQW VWRUHµLVQRWDV\QRQ\PIRU´GDWDEDVHµ$GDWDEDVHLV MXVWRQHH[DPSOHRIDSHUVLVWHQWVWRUH%XWLILWPDNHV \RXIHHOEHWWHUWRWKLQNDERXWLWWKDWZD\JRDKHDGIRU QRZ%XWDV,ZDVVD\LQJLI,UHSUHVHQWDQHQWLW\VD\ DFXVWRPHUQDPHG7RP'XIIWKHQZKDWKDSSHQVLIWKH FOLHQWFDOOVP\JHW$GGUHVV PHWKRG"7KHVHUYHUFDQ·W MXVWKDQGPHWKHFDOO %HDQ)LUVWRIDOOVHFXULW\LVMXVW21(RIWKHUHDVRQV \RXKDYHWRWDNHP\FDOOV,·OOJHWWRWKRVHRWKHU UHDVRQVLQDPLQXWH%XWDVIRUSXWWLQJLQFRGHWRGRP\ RZQFKHFNVDFWXDOO\,&$1GRWKDWLIWKHSURJUDPPHU ZDQWVPHWR%XWLW·VXVXDOO\QRWWKHZD\WRKDQGOH VHFXULW\ (-%2EMHFW:KDW·VZURQJZLWK\RXKDQGOLQJWKH VHFXULW\FKHFNVLQ\RXURZQFRGH" %HDQ<RXUHDOO\GRQ·WNQRZGR\RX">UROOVH\HV@)LUVW LIWKHVHFXULW\FKHFNVDUHFRGHGLQWRPHWKHQ,·PQRW DVUHXVDEOH7KHZKROHSRLQWRI(-%LVWRFRQÀJXUHDQG FXVWRPL]HEHDQVDWUXQWLPHZLWKRXWUHZULWLQJWKHFRGH ,I,·YHJRWVHFXULW\SURJUDPPLQJLQP\-DYDFRGHWKHQ LWFDQ·WEHFKDQJHGZLWKRXWWRXFKLQJWKHVRXUFH$QG ZKRZDQWVWKDW" (-%2EMHFW³ZDLW³E\SHUVLVWHQWVWRUHGRQ·W\RXMXVW PHDQ'$7$%$6(":K\GRQ·W\RXMXVWVD\GDWDEDVH" (-%2EMHFW%HFDXVH %HDQ%HFDXVH,KDYHWRORDGLQ7RP'XII·V LQIRUPDWLRQIURPWKHGDWDEDVHÀUVW (-%2EMHFW%HFDXVH %HDQ%HFDXVH,·GUHWXUQDQDGGUHVVWKDWPLJKWQRW HYHQEHYDOLG8QOHVV,·PVWLOOLQDSUHYLRXVWUDQVDFWLRQ ZLWKWKLVFOLHQWWKHQWKHVHUYHUKDVWRWHOOPHWR ORDGP\VHOIZLWK7RP'XII·VGDWDEDVHLQIR%()25( WKHVHUYHUWHOOVPHWRUXQWKHJHW$GGUHVV PHWKRG 2WKHUZLVHZKRNQRZVZKDW,·GUHWXUQ"2.ZHOOWKDW·V ODVWFDOOVRZH·OOKDYHWRFRQWLQXHWKLVVRPHRWKHUWLPH \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 31 (-%DUFKLWHFWXUH there are no Dumb Questions Q: (OW AND WHEN DOES THE CONTAINER CREATE THE %*"/BJECT AND THE (OME AND THE STUBS A: 7HEN YOU DEPLOY A BEAN THE CONTAINER LOOKS AT THE $$ AND TAKES IT FROM THERE 2EMEMBER THE $$ GIVES THE FULLY QUALIFIED NAME OF YOUR 2EMOTE #OMPONENT %*"/BJECT INTERFACE AND YOUR 2EMOTE (OME INTERFACE 3O ONCE THE CONTAINER GETS THOSE INTERFACES IT GENERATES CODE FOR THE TWO CLASSES IMPLEMENTING THOSE INTERFACES !ND BECAUSE THEYRE 2EMOTE THE CONTAINER ALSO CREATES THE CLIENT STUBS THAT KNOW HOW TO COMMUNICATE BACK TO THE 2EMOTE OBJECTS Q: !RE THESE ALWAYS PLAIN OLD 2-) STUBS ) SEE THAT WHEN WE DEPLOYED USING THE 2) IT PRINTED OUT A MESSAGE THAT IT WAS RUNNING RMIC A: 4HE CONTAINER CAN DO WHAT EVER IT WANTS TO CREATE THE STUBS THE REQUIREMENT IS THAT THE STUBS BE 2-) ))/0 COMPLIANT )N FACT A SERVER CAN USE SOMETHING CALLED DYNAMIC PROXIES TO IMPLEMENT THE STUB FUNCTIONAL ITY BUT WE DONT CARE 7HEN WE SAY hSTUBv WE MEAN SOMETHING WITH STUB BEHAVIOR 7HETHER ITS AN 2-) STUB OR SOMETHING ELSE IS AN IMPLEMENTATION DETAIL FOR THE SERVER 7ELL LOOK AT THAT IN MORE DETAIL IN THE NEXT CHAPTER 4HE BOTTOM LINE IS THAT YOU REALLY DONT KNOW WHAT THE STUB CLASS CODE LOOKS LIKE &OR THAT MATTER YOU REALLY DONT KNOW HOW THE %*"/BJECT AND THE (OME ARE IMPLEMENTED 9OURE NOT SUPPOSED TO KNOW 9OU MIGHT HAVE AN %*" CONTAINER THAT LETS YOU VIEW POSSIBLY EVEN HACK THE GENERATED SOURCE CODE BUT DONT COUNT ON IT !ND IF WE WERE YOU WE JUST WOULDNT GO THERE EVEN IF WE COULD Q: 3O THESE CLASSES ARE UP TO THE VENDORS IMPLEMENTATION A: 9ES 4HE VENDOR HAS ALL SORTS OF CHOICES FOR IMPLEMENTING THESE CLASSES AND MIGHT USE THE STUBS AND OR (OME AND %*"/BJECTS TO GET SOME PERFORMANCE ADVANTAGES "UT AGAIN THATS NOT UP FOR YOU TO MESS WITH OR EVEN KNOW ABOUT 4HE ONLY REQUIREMENT IN THE SPEC THAT YOURE GUARANTEED AND REQUIRED TO ADHERE TO IS THAT 2EMOTE OBJECTS MUST FOLLOW THE RULES FOR 2-) ))/0 WHICH MEANS *AVAS 2EMOTE -ETHOD )NVOCATION USING THE ))/0 #/2"! STANDARD WIRE PROTOCOL Q: 9OU BROUGHT IT UP HOW IS 2-) ))/0 DIFFERENT FROM REGULAR 2-) A: 0LAIN OLD 2-) USES *2-0 AS ITS WIRE PROTOCOL "UT ))/0 LETS 2EMOTE OBJECTS INTEROPERATE THROUGH #/2"! WE WONT BE SAYING MUCH AT ALL ABOUT #/2"! IN THIS BOOK ITS DEFINITELY OUT OF SCOPE FOR THE EXAM AND THE BOOK EXCEPT IN A FEW TINY CASES WELL SEE SCATTERED THROUGHOUT THE CHAPTERS 4HAT GIVES YOUR OBJECTS A CHANCE TO BE ACCESSED FOR EXAMPLE BY EVEN NON *AVA CLIENTS /NE THING ))/0 SPECIFIES IS THE WAY INFORMATION FOR TRANSACTIONS AND SECURITY CAN BE PROPAGATED ALONG WITH THE METHOD CALL AND YOUR CONTAINER MIGHT BE TAKING ADVANTAGE OF THAT &OR THE MOST PART YOULL BARELY NOTICE THE DIFFERENCE BETWEEN PLAIN OLD 2-) AND 2-) ))/0 "UT THERE ARE A COUPLE OF PLACES WHERE ITS DIFFERENT AND ONE OF THESE DIFFERENCES IS DEFINITELY ON THE EXAMTHE NEED TO NARROW A STUB 7ELL COVER NARROWING IN DETAIL IN THE NEXT CHAPTER ON #LIENT 6IEW &OR NOW JUST KNOW THAT ITS SOMETHING A CLIENT MUST DO WITH AN %*" STUB THAT THEY DONT HAVE TO DO WITH A PLAIN *AVA STUB BECAUSE THE %*" SPEC TELLS YOU TO ASSUME THAT THE STUB IS USING ))/0 AND THUS MIGHT BE A DIFFERENT KIND OF STUB Q: 7HEN ARE WE GOING TO TALK ABOUT THE (OME A: Q: .EXT PAGE 7HY DID YOU TAKE SO LONG )SNT THE (OME IMPORTANT A: !S CRUCIAL AS THE (OME IS ITS USUALLY JUST THE WAY YOU GET A REFERENCE TO SOMETHING THAT IMPLEMENTS THE #OMPONENT INTERFACE )N OTHER WORDS YOU USE THE (OME TO GET AN %*"/BJECT STUB FOR 2EMOTE CLIENTS WHICH IS ALL WEVE TALKED ABOUT SO FAR &OR %NTITY BEANS THE (OME CAN HAVE A MORE IMPORTANT ROLE AND WELL SEE THAT BUT EVEN WITH %NTITY BEANS THE (OMES PRIMARY USE IS STILL TO GET %*"/BJECT STUBS !FTER THAT MOST OF THE COMMUNICATION BETWEEN THE CLIENT AND THE BEAN COMES THROUGH THE %*"/BJECT AND NOT THE (OME -OST OF THE TIME IN FACT CLIENTS USE THE (OME JUST TO GET THE %*"/BJECT REFERENCE AND THEN THE (OME REFERENCE IS TOSSED OUT NOT NEEDED &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Page 32 Return to Table of Contents DUFKLWHFWXUDORYHUYLHZ <PMJMIV0WUM %VERY 3ESSION AND %NTITY BEAN HAS A (OME -ESSAGE DRIVEN BEANS DONT HAVE HOMES BECAUSE MESSAGE DRIVEN BEANS DONT HAVE A CLIENT VIEW IN OTHER WORDS CLIENTS CANT GET A REFERENCE TO A -ESSAGE DRIVEN BEAN 4HE (OME HAS ONE MAIN JOB TO HAND OUT REFERENCES TO A BEANS #OMPONENT INTERFACE &OR A 3ESSION BEAN THATS JUST ABOUT ALL YOULL DO WITH THE BEANS (OME &OR %NTITY BEANS THOUGH THE (OME PLAYS A MUCH BIGGER ROLE %ACH DEPLOYED BEAN HAS ITS OWN (OME AND THAT (OME IS RESPONSIBLE FOR ALL BEAN INSTANCES OF THAT TYPE &OR EXAMPLE IF YOU DEPLOY A 3HOPPING#ART 3ESSION BEAN THE CONTAINER WILL CREATE ONE 3HOPPING#ART BEAN (OME 4HAT 3HOPPING#ART (OME TAKES CARE OF ALL THE INSTANCES OF 3HOPPING#ART BEANS )N OTHER WORDS IF CLIENTS EACH WANT THEIR OWN 3HOPPING#ART BEAN REFERENCE WHICH REMEMBER MEANS A REFERENCE TO THE 3HOPPING#ART BEANS #OMPONENT INTERFACE THE ONE AND ONLY 3HOPPING#ART (OME WILL HAND OUT ALL REFERENCES )F YOU DEPLOY THREE BEANS AS PART OF AN APPLICATION SAY A 3HOPPING#ART #USTOMER AND 0RODUCT THERE WILL BE THREE (OMES IN THE SERVER REPRESENTING EACH OF THOSE DEPLOYED BEANS )T MAKES NO DIFFERENCE HOW MANY %*"/BJECTS AND STUBS THE (OME OBJECTS HAND OUT THERE WILL STILL BE ONLY THREE (OMES 3O DOES THAT MEAN THAT FOR EACH (OME THERE IS ONLY A SINGLE INSTANCE OF THE CLASS THAT IMPLEMENTS THE (OME INTERFACE FOR THAT BEAN TYPE .OT NECESSARILY BUT THATS EXACTLY HOW WERE SUPPOSED TO THINK ABOUT IT 7ELL ACTUALLY REFER TO THE (OME AS THE (OME OBJECT AND WELL ASSUME THAT THERES ALWAYS JUST ONE PER DEPLOYED BEAN TYPE 4HE SPEC GUARANTEES THAT YOU CAN THINK ABOUT IT THAT WAY REGARDLESS OF WHAT YOUR VENDOR ACTUALLY DOES WITH ITS IMPLEMENTATION FOR THE (OME (DFKGHSOR\HG6HVVLRQDQG (QWLW\EHDQKDVD+RPH)RU H[DPSOHWKH$GYLFH%HDQKDV DQ$GYLFH%HDQ+RPH1R PDWWHUKRZPDQ\FOLHQWVJHW DQ$GYLFH%HDQWKHUH·VRQO\ RQH$GYLFH%HDQ+RPH 7KH+RPH·VMRELVWRKDQG RXWUHIHUHQFHVWRWKDWEHDQ·V &RPSRQHQWLQWHUIDFH 7HFKQLFDOO\WKHUH¶VDOLWWOHPRUHWRWKHVWRU\ EHFDXVHDEHDQPLJKWKDYHERWKDORFDO+RPH DQGD5HPRWH+RPHEXWWKDW¶VUHDOO\XQOLNHO\ (YHQWKHQWKHUHZRXOGEHRQO\RQHRIHDFK +RPHW\SH 5HPRWHRUORFDO QRPDWWHUKRZ PDQ\EHDQVRIWKDWW\SHDUHDOLYH \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 33 WKHKRPH *HWWLQJDQGXVLQJD+RPHIRUWKH$GYLFH%HDQ 7KLVVFHQDULRDVVXPHV$GYLFH%HDQLVDVWDWHIXO6HVVLRQEHDQ 7KH$GYLFH%HDQLVGHSOR\HGDQGWKHVHUYHU VHUYLFHV LQVWDQWLDWHVWKH$GYLFH%HDQ+RPHREMHFW DQGUHJLVWHUVLWZLWK-1', +RPHLQWHUIDFH FOLHQW +RPH REMHFW -1',1DPLQJ 6HUYLFH ³$GYLVRU´ VWXE 7KHFOLHQWGRHVD-1',ORRNXSRQWKH+RPH VHUYLFHV XVLQJWKHUHJLVWHUHGQDPH´$GYLVRUµ +RPHLQWHUIDFH FOLHQW ORRNX S ´$G YLVRU µ +RPH REMHFW -1',1DPLQJ 6HUYLFH ³$GYLVRU´ VWXE &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 34 DUFKLWHFWXUDORYHUYLHZ VHUYLFHV -1',VHQGVEDFNDVWXEWRWKH5HPRWH+RPHREMHFW +RPHLQWHUIDFH FOLHQW +RPH REMHFW -1',1DPLQJ 6HUYLFH VWXE ³$GYLVRU´ VWXE 7KHFOLHQWDVNVWKH+RPHIRUDUHIHUHQFHWRWKH &RPSRQHQWLQWHUIDFHE\FDOOLQJFUHDWH FUH DWH VWXE +RPHLQWHUIDFH FOLHQW +RPHLQWHUIDFH VHUYLFHV ,QRWKHUZRUGVWKHFOLHQWZDQWVWR´FUHDWHµDEHDQDQG JHWWKHVWXEEDFNWRWKHEHDQ·V(-%2EMHFW +RPH REMHFW \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 35 WKHKRPH 7KH+RPHREMHFWPDNHVWKH(-%2EMHFW DQGVHQGVEDFNWKHVWXE +RPHLQWHUIDFH FOLHQW +RPHLQWHUIDFH VHUYLFHV 1RZWKH´VHUYLFHVµNLFNLQDQGWKHEHDQLVFUHDWHG VWXE FUH DWH EHDQ +RPH REMHFW 7KH(-%2EMHFWLVPDGH WKHERG\JXDUGIRUWKLVQHZO\ VWXE VWXE VHUYLFHV (-% REMHFW EHDQ +RPHLQWHUIDFH FOLHQW +RPHLQWHUIDFH &RPSRQHQWLQWHUIDFH FUHDWHGEHDQ DQGLWVVWXELVUHWXUQHGWRWKHFOLHQW +RPH REMHFW &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 36 DUFKLWHFWXUDORYHUYLHZ 1RZWKHFOLHQWFDQ ÀQDOO\ GRZKDWKH5($//<ZDQWV VWXE VHUYLFHV &RPSRQHQWLQWHUIDFH VWXE (-% REMHFW EHDQ +RPHLQWHUIDFH FOLHQW +RPHLQWHUIDFH &RPSRQHQWLQWHUIDFH WRGR³FDOODEXVLQHVVPHWKRGRQWKHEHDQ :KLFKRI FRXUVHKDVWRJRWKURXJKWKH&RPSRQHQWLQWHUIDFH +RPH REMHFW 7KHFOLHQWFDQJHWULGRIKLV+RPHVWXELIKHGRHVQ·W +RPHLQWHUIDFH FOLHQW VWXE VHUYLFHV &RPSRQHQWLQWHUIDFH VWXE (-% REMHFW EHDQ +RPHLQWHUIDFH &RPSRQHQWLQWHUIDFH ZDQWDFFHVVWRPRUHEHDQVRIWKLVW\SH $GYLFH%HDQ EXWKHFDQVWLOONHHSFDOOLQJPHWKRGVRQWKH &RPSRQHQWLQWHUIDFH +RPH REMHFW \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 37 (-%DUFKLWHFWXUH 6KDUSHQ \RXU SHQFLO (-%/LIHF\FOH &OLHQWKDVRQO\D +RPHVWXEEXWZDQWV WRLQYRNHDEXVLQHVV PHWKRGRQDEHDQ ,QWKHVFHQDULREHORZDVVXPHWKHFOLHQWKDVSUHYLRXVO\GRQHD-1',ORRNXSDQG JRWWHQEDFNDVWXEWRWKH5HPRWH+RPHREMHFW (YHU\WKLQJLQWKHSLFWXUHLVZKDWKDSSHQV$)7(5WKHFOLHQWKDVWKH+RPHVWXEDQG QRZZDQWVWRJHWDUHIHUHQFHWRDQ(-%2EMHFWDQGXOWLPDWHO\FDOODEXVLQHVVPHWKRG RQWKHEHDQ 1XPEHUWKHDUURZV XVLQJWKHER[HVRYHUWKHDUURZV LQWKHRUGHULQZKLFKWKH\ RFFXU7KHVHDUURZVDUHQ¶WQHFHVVDULO\GLUHFWPHWKRGFDOOV DOWKRXJKWKH\PLJKWEH EXWUDWKHUDUURZVSRLQWLQJWRWKHQH[W7+,1*WKDWKDSSHQV7HOODVWRU\IRUZKDWKDS SHQVDWHDFKDUURZ7KHUHPLJKWEHPRUHWKDQRQHULJKWDQVZHUGHSHQGLQJRQKRZ \RXWHOOWKHVWRU\6RPHDUURZVDUHPLVVLQJ\RXFDQDGGWKHPLI\RXZDQWRUMXVW DVVXPHVRPHWKLQJVDUHKDSSHQLQJWKDW\RXGRQ¶WKDYHDUURZVIRU VHUYLFHV 5HOD[DQGWDNH\RXUWLPH ,I\RXJHWVWXFNÀLSEDFNWKURXJKWKHSUHYLRXVSDJHVDQGVWXG\WKHGLDJUDPV (-% REMHFW VWXE EHDQ FOLHQW VWXE +RPH REMHFW 2 The stub tells the Home that the client wants to ÒcreateÓ a bean. &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 38 DUFKLWHFWXUDORYHUYLHZ :MDD=LHGAFLK %HDQVWKDWDUHH[SRVHGWRUHPRWHFOLHQWVKDYHWZR5HPRWH LQWHUIDFHVRQHIRUWKH(-%+RPHDQGRQHIRUWKH(-%2EMHFW 5HPRWHLQWHUIDFHPXVWH[WHQG GLUHFWO\RULQGLUHFWO\ YDUPL5HPRWHDQGDOOPHWKRGVPXVWGHFODUHD YDUPL5HPRWH([FHSWLRQ E TS ARE BLU Y 9/5 RED VIOLE WRITTEN B RE 2OSES ARE A S CE A RF TE IN TE O 4HE 2EM IG PROJECT QUITE A B RED AND RE A S /BJECT SE *" O % 2 THE ILL MAKE W R E RV 4HE SE L FLOAT ETALS WIL RED THE P R 2EMOTE VE E 2OSES ARE N IS ASS ITSELF CL N A E B 4HE Y IT ORK 9OU TR R lNEST W STICK A LOT MORE U O T O N 9EAH /+ THESE THINGS WILL ER URSELF 2EMEMB UP WITH THEM YO ME IF YOU CO (-%WKHLQWHUIDFHWKDWH[WHQGV(-%2EMHFWLVFDOOHGWKH HPRWH&RPSRQHQWLQWHUIDFH,WLVZKHUHWKHEXVLQHVVPHWKRGV HGHFODUHG KHFOLHQWQHYHUFDOOVPHWKRGVRQWKHEHDQLWVHOIEHFDXVHWKH HDQLV127D5HPRWHREMHFW KHFRQWDLQHULPSOHPHQWVWKH5HPRWH&RPSRQHQWLQWHUIDFHE\ XLOGLQJDFODVVWKDWLPSOHPHQWVLW7KLVFODVVLVXVHGWRPDNHWKH -%2EMHFWIRUWKHEHDQ 7KHEHDQ·VERG\JXDUG KHFRQWDLQHUDOVRFUHDWHVWKHVWXEWRWKH(-%2EMHFW RXFUHDWHWKH5HPRWH&RPSRQHQWLQWHUIDFHE\ZULWLQJDQ WHUIDFHWKDWH[WHQGVMDYD[HME(-%2EMHFW DQLQWHUIDFHWKDW [WHQGVMDYDUPL5HPRWH <RXDOVRFUHDWHWKHEHDQFODVVZKHUHWKHDFWXDOEXVLQHVV PHWKRGVDUHLPSOHPHQWHG GHVSLWHWKHIDFWWKDWWKHEHDQFODVV WHFKQLFDOO\GRHVQ·WLPSOHPHQWWKH5HPRWH&RPSRQHQWLQWHUIDFH 7KH+RPHLVWKHIDFWRU\IRUWKHEHDQ,WVPDLQMRELVWRKDQGWKH FOLHQWDUHIHUHQFHWRWKHEHDQ%XWUHPHPEHUWKHFOLHQWFDQQHYHU WUXO\JHWDUHIHUHQFHWRWKHEHDQ³WKHEHVWWKHFOLHQWFDQGRLVWR JHWDUHIHUHQFHWRWKHEHDQ·V(-%2EMHFW <RXFUHDWHWKH+RPHLQWHUIDFHE\ZULWLQJDQLQWHUIDFHWKDW H[WHQGVMDYD[HME(-%+RPH DQLQWHUIDFHWKDWH[WHQGV MDYDUPL5HPRWH 7KHFRQWDLQHULVUHVSRQVLEOHIRULPSOHPHQWLQJWKH+RPHLQWHUIDFH E\EXLOGLQJDFODVVWKDWLPSOHPHQWVLWDQGWKHFRQWDLQHUDOVR JHQHUDWHVWKHVWXEIRUWKH+RPH 7KHUHLVRQO\RQH+RPHSHUGHSOR\HGEHDQ)RUH[DPSOHD 6KRSSLQJ&DUWEHDQZRXOGKDYHDVLQJOH6KRSSLQJ&DUW+RPH UHJDUGOHVVRIKRZPDQ\6KRSSLQJ&DUWEHDQVKDYHEHHQFUHDWHG \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 39 VHVVLRQEHDQRYHUYLHZ )ZKPQ\MK\]ZITW^MZ ^QM_";M[[QWVJMIV[ &OLHQWVVKDUHWKH+RPHEXWQHYHUWKHEHDQ (DFKFOLHQWJHWVKLVRZQ(-%2EMHFWUHIHUHQFHDQGKLVRZQEHDQ7KHFOLHQWQHYHU VKDUHVDEHDQZLWKDQRWKHUFOLHQWDOWKRXJKWKHPHDQLQJRI³VKDUHV´GHSHQGV RQZKHWKHUWKHEHDQLVVWDWHIXORUVWDWHOHVV :H¶OOVHHWKDWLQWKHQH[WFKDSWHU +RZHYHUWKHUH¶VRQO\RQH+RPHREMHFWIRUWKLVSDUWLFXODUEHDQW\SH VD\ $GYLFH%HDQ VRERWKFOLHQWVKDYHDVWXEWRWKHRQHDQGRQO\$GYLFH+RPH%RWK FOLHQWVDVNWKHVDPH$GYLFH+RPHIRUDUHIHUHQFHWRDQ$GYLFHEHDQ 2IFRXUVH WKHFOLHQWQHYHUJHWVWKHUHIHUHQFHWRWKHEHDQLQVWDQFHEXWLQVWHDGJHWVDUHIHUHQFH WRWKHEHDQ¶V(-%2EMHFW$QGVLQFH(-%2EMHFWLV5HPRWHWKHFOLHQWVJHWVDVWXE 6HUYHU VWXE FOLHQW VHUYLFHV &OLHQW2QH (-% REMHFW EHDQ VWXE +RPH REMHFW 7KHUH·VRQO\RQH+RPH REMHFWEXWHDFKFOLHQW JHWVKLVRZQGHGLFDWHG EHDQDQG(-%2EMHFW VHUYLFHV EHDQ &OLHQW7ZR (-% REMHFW VWXE FOLHQW VWXE A session bean client is guaranteed to be the only client executing a method on that bean. While the client is in the middle of a business method, that bean belongs to that client. &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 40 DUFKLWHFWXUDORYHUYLHZ )ZKPQ\MK\]ZITW^MZ ^QM_"-V\Q\aJMIV[ &OLHQWVVKDUHWKH+RPHDQGPD\VKDUHWKHEHDQ (DFKFOLHQWKDVKLVRZQUHIHUHQFHWRWKHRQHDQGRQO\+RPHIRUWKLVEHDQ VD\ &XVWRPHU%HDQ %XWLIWZRFOLHQWVDUHWU\LQJWRDFFHVVWKHVDPH&XVWRPHU )UHG6PLWK WKHQERWKFOLHQWVKDYHDUHIHUHQFHWRWKHVDPH(-%2EMHFW7KH(-%2EMHFWIRU ,QRWKHUZRUGVWKH(-%2EMHFWLVWKHERG\JXDUGIRUDSDUWLFXODU&XVWRPHU OLNH)UHG6PLWK ,IDOOWKHFOLHQWVDUHWU\LQJWRDFFHVV)UHG6PLWKWKH\ZLOOHDFKKDYHWKHLURZQVWXE RIFRXUVHEXWDOOVWXEVZLOOFRPPXQLFDWHZLWKWKHVDPH5HPRWH(-%2EMHFW$QGWKHUHZLOO EHRQO\RQHEHDQUHSUHVHQWLQJ)UHG6PLWK,IDFOLHQWZDQWVWRDFFHVVWZRGLIIHUHQW FXVWRPHUVWKRXJKWKHFOLHQWZLOOKDYHWZRVWXEVDQGWKRVHVWXEVZLOOEHIRUWZRGLIIHUHQW (-%2EMHFWVRQHIRUHDFKFXVWRPHU$QGWKDWDOVRPHDQVWZRGLIIHUHQWEHDQV 6HUYHU &OLHQW2QH VWXE FOLHQW )UHG6PLWK )UHG6PLWK·V ERG\JXDUG EHDQ VWXE &XVWRPHU WDEOH (-% REMHFW '% VHUYLFHV +RPH REMHFW &OLHQW7ZR EHDQ VWXE (-% REMHFW 6N\OHU6DQ 6N\OHU6DQ·V ERG\JXDUG FOLHQW VWXE VWXE \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 41 VHVVLRQEHDQRYHUYLHZ )ZKPQ\MK\]ZITW^MZ ^QM_" +ZMI\QVOI;\I\MN]T;M[[QWVJMIV (-% REMHFW VWXE VHUYLFHV $IWHUJHWWLQJD+RPHVWXEWKHFOLHQWFDOOV³FUHDWH´RQWKH+RPH 7KH+RPHFUHDWHVWKHEHDQDQGWKH(-%2EMHFWIRUWKHEHDQDQG KDQGVEDFNWKH(-%2EMHFWVWXE EHDQ FOLHQW +RPH REMHFW VWXE The client calls create() on the Home stub (create() is a method in the Home interface). The stub sends the create() call to the Remote Home object. The Home object steps in and adds its services. The EJBObject is created/instantiated for the bean. The bean itself is instantiated. The EJBObject stub is returned to the client, so the client can call business methods on the Component interface. &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 42 DUFKLWHFWXUDORYHUYLHZ )ZKPQ\MK\]ZITW^MZ ^QM_" +ZMI\QVOI;\I\MTM[[;M[[QWVJMIV (-% REMHFW VWXE VHUYLFHV $IWHUJHWWLQJD+RPHVWXEWKHFOLHQWFDOOV³FUHDWH´RQWKH+RPH7KH +RPHJLYHVWKHFOLHQWDVWXEWRDQH[LVWLQJ(-%2EMHFWEXWGRHVQRW DVVRFLDWHDEHDQZLWKWKLV(-%2EMHFW,QVWHDGWKHEHDQVWD\VLQDSRRO XQWLOWKHFOLHQWXVHVWKH(-%2EMHFWVWXEWRFDOODEXVLQHVVPHWKRG EHDQ FOLHQW +RPH REMHFW VWXE EHDQSRRO The client calls create() on the Home stub (create() is a method in the Home interface). The stub sends the create() call to the Remote Home object. The Home container steps in and adds its services. An EJBObject is created for this client. The bean stays in the bean pool ! It comes out only to service an actual business method, if the client invokes one on the EJBObject stub. The EJBObject stub is returned to the client, so the client can call business methods on the Component interface. \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 43 VHVVLRQEHDQRYHUYLHZ 2.VRWKHEHDQFRPHVRXW RIWKHSRRORQO\ZKHQWKHFOLHQW FDOOVDEXVLQHVVPHWKRGEXWWKHQ KRZGLGWKHEHDQJHWLQWKHSRROLQ WKHÀUVWSODFH",ILWZDVQ·WFUHDWHG ZKHQWKHFOLHQWFDOOHGFUHDWH WKHQZKDWGLGFDXVHWKHEHDQWR EHFUHDWHG" ?PWKZMI\M[\PM[\I\MTM[[[M[[QWV JMIVIVL_PMV' &IRST WE HAVE TO DElNE WHAT CREATE MEANS &OR A SESSION BEAN IT MEANS THE BEAN INSTANCE IS PHYSICALLY INSTANTIATED AND INITIALIZED AS A BEAN &OR AN ENTITY BEAN ITS COMPLETELY DIFFERENT SO THIS CONVERSATION APPLIES JUST TO SESSION BEANS ,ATER WELL GET INTO WHAT IT MEANS TO CREATE AN ENTITY BEAN &OR STATEFUL SESSION BEANS THE CREATE IS TRIGGERED BY THE CLIENT 4HE CLIENT CALLS CREATE ON A (OME STUB AND EVERYTHING HAPPENS AT THAT POINTAN %*"/BJECT IS INSTANTIATED FOR THIS NEW ABOUT TO BE CREATED BEAN AND THEN THE BEAN ITSELF IS CREATED AND LINKED TO THE %*"/BJECT THE BEANS BODYGUARD "UT FOR STATELESS SESSION BEANS THE CLIENT CREATE AND THE ACTUAL CREATION OF THE BEAN ARE DECOUPLED )N OTHER WORDS JUST BECAUSE THE CLIENT CALLS CREATE ON A (OME STUB DOESNT MEAN A BEAN WILL BE CREATED AT THAT POINT 3TATELESS SESSION BEANS ARENT CREATED UNTIL THE CONTAINER DECIDES IT NEEDS ONE AND THATS REALLY UP TO THE CONTAINER )T MIGHT FOR EXAMPLE MAKE A BUNCH OF BEAN INSTANCES IE CREATE SOME BEANS AND PLOP THEM IN A POOL BEFORE EVEN A SINGLE CLIENT HAS ASKED FOR ONE BY CALLING CREATE ON A (OME STUB /R THE CONTAINER MIGHT MAKE JUST IN TIME BEANS AND WAIT UNTIL THE CLIENT INVOKES A BUSINESS METHOD BEFORE GOING TO THE TROUBLE OF PHYSICALLY CREATING THE BEAN &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 44 DUFKLWHFWXUDORYHUYLHZ ;\I\MTM[[[M[[QWVJMIV[IZMUWZM[KITIJTM &OLHQWVGRQ·WVKDUH(-%2EMHFWVEXWWKHVDPH EHDQFDQVHUYLFHPXOWLSOH(-%2EMHFWV-XVWQRWDW WKHVDPHWLPH$VLQJOHEHDQFDQKDQGOHPXOWLSOH FOLHQWVDVORQJDVRQO\RQHFOLHQWDWDWLPHLVLQWKH PLGGOHRIDEXVLQHVVPHWKRGLQYRFDWLRQ &OLHQW2QH FOLHQW 6HUYHU VWXE VWXE (-% REMHFW +RPH REMHFW EHDQ &OLHQW7ZR (-% REMHFW EHDQSRRO VWXE FOLHQW VWXE The bean comes out of the pool ONLY when a client invokes a business method on the EJBObject stub. So, a single bean can jump out to handle one clientÕs call, then jump back in the pool, then jump out to handle a different clientÕs call, and then jump back in the pool... \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 45 VHVVLRQEHDQRYHUYLHZ :K\GRHVWKH³SRRO´DUFKLWHFWXUHZRUNWRPDNH VWDWHOHVVVHVVLRQEHDQVPRUHVFDODEOHEXW QRWVWDWHIXOVHVVLRQEHDQV":K\FDQ¶WVWDWHIXO VHVVLRQEHDQVXVHWKHEHDQSRRO" there are no 6KDUSHQ \RXU SHQFLO 7) $VWDWHIXOVHVVLRQEHDQFDQEHVKDUHG EHWZHHQPXOWLSOHFOLHQWV 7) $QHQWLW\EHDQFDQEHVKDUHGEHWZHHQ PXOWLSOHFOLHQWVDVORQJDVWKHHQWLW\ EHLQJVKDUHGLVWKHVDPH 7) 6WDWHOHVVVHVVLRQEHDQVDUHFUHDWHG ZKHQWKHFOLHQWLQYRNHVFUHDWHRQWKH +RPH 7) 6WDWHIXOVHVVLRQEHDQVDUHFUHDWHG ZKHQWKHFOLHQWLQYRNHVFUHDWHRQWKH +RPH Dumb Questions Q: (ERES SOMETHING THAT IS 2%!,,9 STARTING TO ANNOY ME WHY DO YOU HAVE A #OMPONENT INTERFACE AND A (OME INTERFACE WHEN THE 2EMOTE OBJECTS ARE CALLED THE (OME AND THE %*"/BJECT 7HY ISNT IT JUST THE %*"/BJECT INTERFACE AND THE (OME/BJECT INTERFACE /R THE (OME AND THE %*" INTERFACES 7HY THE INCONSISTENCY 7) 7KHUHPXVWEHRQHVWDWHOHVVVHVVLRQ EHDQSHUFOLHQWIRUDVORQJDVWKHFOLHQW KROGVDUHIHUHQFHWRDQ(-%2EMHFW 7) 7KHUHPXVWEHRQHVWDWHOHVVVHVVLRQ EHDQSHUFOLHQWIRUDVORQJDVWKHFOLHQW LVLQWKHPLGGOHRIDEXVLQHVVPHWKRG LQYRFDWLRQRQWKHEHDQ A: !CTUALLY THAT REALLY PISSES US OFF TOO "UT YOULL GET USED TO IT "E THANKFUL IT WAS EVEN WORSE IF YOU LEARNED %*" PRIOR TO VERSION WHEN IT WAS CALLED THE (OME AND THE 2EMOTE .OW THAT WAS A REAL PROBLEM BECAUSE FIRST OF ALL BOTH THE (OME AND THE 2EMOTE WERE 2EMOTE IN THE JAVARMI SENSE 3ECOND AS OF %*" YOU CAN HAVE A (OME AND A 2EMOTE THAT ARENT ACTUALLY 2EMOTE 3O THEY HAD TO CHANGE THE NAME TO #OMPONENT INTERFACE INSTEAD OF CALLING IT 4(% 2EMOTE INTERFACE SINCE IT MIGHT IN FACT NOT BE 2EMOTE )F YOU JUST REMEMBER THAT THE #OMPONENT INTERFACE IS WHERE THE BUSINESS METHODS ARE AND THE (OME IS WHERE THE UM (OME METHODS ARE YOU SHOULD BE FINE *UST REMEMBER #OMPONENT BUSINESS %*"/BJECT OR %*",OCAL/BJECT BUT WE WONT GO THERE UNTIL THE NEXT CHAPTER 7) (DFKHQWLW\EHDQPXVWKDYHLWV (-%2EMHFW $QVZHUV)7)7)77 YiX`e gfn\i &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 46 DUFKLWHFWXUDORYHUYLHZ there are no Dumb Questions Q: (OW DOES THIS WORK )S THERE ONE BEAN POOL FOR ALL BEANS OR ONE BEAN POOL FOR A PARTICULAR TYPE OF BEAN A: )N REALITY WE DONT KNOW WHAT THE CONTAINER IMPLEMENTATION IS BUT CONCEPTUALLY THERES ONE POOL FOR EVERY BEAN TYPE 3O IF YOU DEPLOY AN !DVICE"EAN AND A 7EATHER"EAN AND BOTH ARE STATELESS SESSION BEANS THEYLL EACH GET THEIR OWN POOL Q: $OES EACH STATELESS SESSION BEAN HAVE ITS OWN %*"/BJECT A: 3ORT OF ! STATELESS SESSION BEAN DOESNT NEED A BODYGUARD UNTIL HES ACTUALLY INVOLVED IN A METHOD INVOCATION 3O THE CLIENT GETS A REFERENCE TO AN %*"/BJECT BUT THE BEAN ISNT ASSOCIATED WITH THAT %*"/BJECT UNTIL THE CLIENT CALLS A BUSINESS METHOD !T THAT POINT THE BEAN SLIDES OUT OF THE POOL TO SERVICE THE METHOD 3O THE %*"/BJECT THE CLIENT HAS IS FOR A KIND OF BEAN !DVICE"EAN 7EATHER"EAN ETC BUT NOT A SPECIFIC INSTANCE OF A BEAN Q: 7HY DONT STATEFUL SESSION BEANS HAVE A POOL A: (AVE YOU ALREADY THOUGHT ABOUT THIS IN THE "RAIN 0OWER ON THE PREVIOUS PAGE "ECAUSE IF YOU HAVENT DONT READ ANY FURTHER UNTIL YOUVE COME UP WITH YOUR OWN IDEAS )F YOU MADE IT THIS FAR WE ASSUME THAT YOU ALREADY KNOW THE ANSWER AND WERE JUST CONFIRMING IT ! STATEFUL BEAN REMEMBER HOLDS CLIENT CONVERSATIONAL STATE 4HAT MEANS THE BEAN HAS TO SAVE CLIENT SPECIFIC STATE IN OTHER WORDS IT HAS TO REMEMBER THINGS ABOUT THIS CLIENT ACROSS MULTIPLE METHOD INVOCATIONS FROM THE CLIENT 4HINK OF A SHOPPING CART AGAINA STATEFUL BEAN NEEDS TO REMEMBER WHATS IN THE CLIENTS CART EACH TIME THE CLIENT CALLS ADD)TEM4O#ART ! STATELESS BEAN ON THE OTHER HAND DOESNT HAVE TO REMEMBER ANYTHING ON THE CLIENTS BEHALF SO TO A CLIENT ONE STATELESS BEAN OF A PARTICULAR TYPE IS AS GOOD AS ANY OTHER BEAN OF THAT TYPE )F THE !DVICE"EAN SIMPLY RETURNS A PIECE OF ADVICE NOT CONNECTED IN ANY WAY TO ANYTHING IT TOLD THE CLIENT BEFORE OR ANYTHING THE CLIENT TOLD IT THEN THERES NO NEED FOR THE !DVICE"EAN TO BE STATEFUL )N THAT CASE EACH TIME A CLIENT CALLS GET!DVICE ON THE #OMPONENT INTERFACE IE THE %*"/BJECT ANY !DVICE"EAN IS JUST AS CAPABLE OF RUNNING THE METHOD AS ANY OTHER /N THE OTHER HAND IF THE !DVICE"EAN WERE MODIFIED TO RETURN SAY RANDOM BUT NON REPEATING ADVICE THEN THE !DVICE"EAN WOULD HAVE TO BE STATEFUL SO THAT IT COULD KEEP TRACK OF PREVIOUS ADVICE AND NEVER REPEAT IT Q: (OW LONG DOES A STATEFUL BEAN KEEP CLIENT SPECIlC STATE A: /NLY FOR THE LIFE OF THE SESSION ! SESSION CONTINUES UNTIL THE CLIENT TELLS THE BEAN HES DONE BY CALLING REMOVE ON THE BEANS #OMPONENT INTERFACE OR THE SERVER CRASHES OR THE BEAN TIMES OUT WELL COVER THAT IN THE 3ESSION LIFECYCLE CHAPTER Q: 3O STATEFUL SESSION BEANS ARENT SCALABLE A: .O THEY ARE *UST NOT AS SCALABLE AS STATELESS BEANS Q: (OW CAN A STATEFUL BEAN BE SCALABLE IF YOU ALWAYS NEED ONE BEAN FOR EVERY CLIENT A: 9OU DO NEED A SEPARATE BEAN ALLOCATED FOR EACH CLIENT BUT NOT EVERY BEAN HAS TO BE ACTIVELY CONSUMING RESOURCES )F THE STATEFUL SESSION BEAN CLIENT IS TAKING A LONG TIME BETWEEN METHOD CALLS THE STATEFUL BEAN CAN BE TEMPORARILY TAKEN DOWN AND PUT IN A STATE CALLED PASSIVATION 4HIS STATE PRESERVES THE CLIENT SPECIFIC STATE OF COURSE BUT REDUCES THE NUMBER OF BEANS CURRENTLY ALIVE IN THE SERVER 4HE BEAN COMES OUT OF PASSIVATION AND BACK INTO ACTIVE DUTY ACTIVATION WHEN THE CLIENT CALLS A BUSINESS METHOD \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 47 PHVVDJHGULYHQEHDQRYHUYLHZ )ZKPQ\MK\]ZITW^MZ ^QM_" 5M[[IOMLZQ^MVJMIV[ 0HVVDJHGULYHQEHDQVGRQ¶WKDYHDFOLHQWYLHZ7KDWPHDQVWKH\GRQ¶W KDYHLQWHUIDFHV 5HPRWHRUORFDO WKDWH[SRVHPHWKRGVWRWKHFOLHQW,Q RWKHUZRUGVPHVVDJHGULYHQEHDQVGRQ¶WKDYHD+RPHRU(-%2EMHFW 7KH\GRQ¶WKDYHD+RPHLQWHUIDFHRUD&RPSRQHQWLQWHUIDFH -DYD0HVVDJLQJ6HUYLFH -06 VHUYLFHV M E FOLHQW EHDQ O EHDQ EHDQSRRO D The client sends a message to a JMS messaging service. The messaging service delivers the message to the container. The container gets a message-driven bean out of the pool. The container delivers the message to the bean (by calling the beanÕs onMessage() MessageListener interface method). &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 48 DUFKLWHFWXUDORYHUYLHZ ?PI\OWM[_PMZM' 3ODFHWKHREMHFWVDQGFODVVHVLQWKHDSSURSULDWHVSRWRQHLWKHU WKHFOLHQWWKHVHUYHURULQERWKSODFHV \HV\RXFDQUHXVHDQ REMHFW 1RWHQRWDOOWKHSLHFHVDUHKHUHVRZKHQ\RX¶UHGRQH LI\RXFDQWKLQNRIRWKHUWKLQJVWKDWVKRXOGJRLQWRWKHSLFWXUH FODVVHVRUREMHFWV GUDZWKHPLQ ([HUFLVH VHUYHU FOLHQW (-% REMHFW (-%2EMHFWLQWHUIDFH EHDQ FOLHQWFODVV VWXE '% EHDQFODVV VWXE KRPHVWXEFODVV KRPHFODVV +RPH REMHFW \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 49 EHDQWDEOH 7ZOIVQbMaW]ZJMIV[ ([HUFLVH )LQLVKWKHWDEOHE\SXWWLQJLQDFKHFNPDUN HYHQEHWWHULI\RX DGGQRWHV LQWKHER[HVFRUUHVSRQGLQJWRWKHODEHOVWKDWDSSO\ WRWKDWEHDQW\SH:H¶YHGRQHRQHRIWKHER[HVIRU\RX,I\RX JHWVWXFNJREDFNWKURXJKWKHSUHYLRXVWZRFKDSWHUV<RXPLJKW KDYHWRPDNH\RXUEHVWJXHVVRQDIHZWKLQJV7KDW¶V2.² \RX¶OOKDYHLWDOOZRUNHGRXWZD\EHIRUHWKHHQGRIWKHERRN:H EHOLHYHLQ\RX<RXFDQGRLW>FXHWKHPHVRQJIURP³5RFN\´@ 6WDWHIXO6HVVLRQ 6WDWHIXO6HVVLRQ %HDQV %HDQV 8VHVDSRRO (QWLW\%HDQV 0HVVDJHGULYHQ %HDQV Yes. Since they donÕt keep any client-speciÞc data, you donÕt need one per each client. 0XOWLSOHFOLHQWVFDQKDYHDUHIHUHQFH WRWKHVDPHEHDQ *XDUDQWHHGWRVXUYLYHDFRQWDLQHU FUDVK +DVDFOLHQWYLHZ $OORZVDV\QFKURQRXVFRPPXQLFDWLRQ 5HSUHVHQWVDSURFHVV 5HSUHVHQWVD´WKLQJµLQDQXQGHUO\ LQJSHUVLVWHQWVWRUH OLNHDGDWDEDVH &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 50 H[HUFLVHVROXWLRQ DUFKLWHFWXUDORYHUYLHZ ([HUFLVH 6ROXWLRQV ?PI\OWM[_PMZM' VWXE (-%2EMHFWLQWHUIDFH (-% REMHFW EHDQFODVV (-%2EMHFWLQWHUIDFH KRPHVWXEFODVV VWXE FOLHQWFODVV EHDQ +RPH REMHFW KRPHFODVV FOLHQW KRPHVWXEFODVV VHUYHU '% 127(\RXZRQ¶W¿QGD¿QLVKHGVROXWLRQIRUWKH2UJDQL]H <RXU%HDQVWDEOH:HZDQW<28WR¿OOWKLVRXW,W¶V\HW DQRWKHUVSHFLDO/HDUQLQJ2SSRUWXQLW\IRUZKLFK\RX¶OO DOZD\VUHPHPEHUXVZLWKIRQGQHVV \RXDUHKHUH Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 2. EJB Architecture Return to Table of Contents Page 51 (-%DUFKLWHFWXUH 2KPDQ7KLVFKDSWHUZDV TXLWHDQRUGHDO&DQZHSOHDVH MXVWVNLSWKHH[DPTXHVWLRQVIRUWKLV FKDSWHU",SURPLVH,·OOGRWKHPLQDOO WKHRWKHUFKDSWHUV <RX¶UHLQOXFN7KLVFKDSWHULV DOOEDFNJURXQGNQRZOHGJHVR WKHUHDUHQ¶WDQ\REMHFWLYHVRU H[DPTXHVWLRQV $SSUHFLDWHWKHPRPHQW² &KDSWHULVMXVWDSDJHWXUQ DZD\ &KDSWHU Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Table of Contents Chapter 3. Exposing Yourself...................................................................... 1 Section 3.1. Objectives..................................................................................................................................................................... 2 Section 3.2. What the client really wants........................................................................................................................................ 3 Section 3.3. It all starts with the home interface............................................................................................................................ 4 Section 3.4. There are no Dumb Questions.................................................................................................................................... 4 Section 3.5. How a client uses a session bean: create, use, and remove........................................................................................ 5 Section 3.6. But first, we have to get a home interface reference.................................................................................................. 6 Section 3.7. What's JNDI?.............................................................................................................................................................. 6 Section 3.8. Getting the home interface stub................................................................................................................................. 7 Section 3.9. There are no Dumb Questions.................................................................................................................................... 8 Section 3.10. Let's take another look at the complete client code.................................................................................................. 9 Section 3.11. Sharpen your pencil................................................................................................................................................... 9 Section 3.12. Just when you thought a simple cast would be enough.......................................................................................... 10 Section 3.13. But NO. You have to narrow the object as well!...................................................................................................... 10 Section 3.14. OK, I'll bite. Why can't you just do a plain old cast ?.............................................................................................. 10 Section 3.15. PortableRemoteObject.narrow().............................................................................................................................. 11 Section 3.16. Think of narrowing as "Exotic Casting".................................................................................................................. 12 Section 3.17. Now that we (finally) have the home stub, let's use it to get what we REALLY want............................................ 13 Section 3.18. There are no Dumb Questions................................................................................................................................. 14 Section 3.19. Writing the Remote home interface for a session bean.......................................................................................... 15 Section 3.20. Remote home interface examples for session beans.............................................................................................. 16 Section 3.21. If you're a client, and you want to........................................................................................................................... 19 Section 3.22. Sharpen your pencil................................................................................................................................................ 22 Section 3.23. But enough about the home... let's talk about the EJB object. The component interface. The thing you REALLY want............................................................................................................................................................................................... 23 Section 3.24. Imagine what else you might want to do with your EJB object reference............................................................. 25 Section 3.25. Online shopping should not be rushed.................................................................................................................. 28 Section 3.26. Thankfully, we've got handles................................................................................................................................. 29 Section 3.27. isIdentical ?: how to find out if two stubs refer to the same bean.......................................................................... 31 Section 3.28. There are no Dumb Questions................................................................................................................................ 33 Section 3.29. A bean's client interfaces can be local .................................................................................................................... 35 Section 3.30. BRAIN POWER....................................................................................................................................................... 35 Section 3.31. Sharpen your pencil................................................................................................................................................. 37 Section 3.32. Which methods make sense for the local client interfaces?.................................................................................. 38 Section 3.33. When you think handle , think Remote.................................................................................................................. 39 Section 3.34. Who needs EJBMetaData when you've got reflection?.......................................................................................... 39 Section 3.35. Do you need isIdentical() when there's equals() ?................................................................................................. 40 Section 3.36. Why so many remove methods?............................................................................................................................. 41 Section 3.37. How can you use a remove that takes a handle when you don't have a handle?................................................... 43 Section 3.38. There are no Dumb Questions................................................................................................................................ 43 Section 3.39. Comparing Remote vs. Local interfaces................................................................................................................. 44 Section 3.40. Sharpen your pencil................................................................................................................................................ 45 Section 3.41. Writing the local client interfaces........................................................................................................................... 46 Section 3.42. You can have both a Remote and local client view for a bean, but you probabl won't.......................................... 47 Section 3.43. Sharpen your pencil................................................................................................................................................ 48 Section 3.44. Exceptions in client interfaces: what the client might get..................................................................................... 49 Section 3.45. Local client code...................................................................................................................................................... 50 Section 3.46. BRAIN POWER: What has to change inside the bean class?................................................................................. 51 Section 3.47. Arguments to Remote vs. local methods................................................................................................................ 53 Section 3.48. There are no Dumb Questions................................................................................................................................ 54 Section 3.49. Exercise: BE the Container..................................................................................................................................... 55 Section 3.50. Exercise Solutions: BE the Container..................................................................................................................... 57 Section 3.51. COFFEE CRAM....................................................................................................................................................... 58 Section 3.52. COFFEE CRAM....................................................................................................................................................... 60 Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 1 the client view 5 4 ([SRVLQJ<RXUVHOI 4 <RXFDQ·WNHHS\RXUEHDQSULYDWH&OLHQWVQHHGWRVHHZKDW\RX¶YHJRW ([FHSWIRUPHVVDJHGULYHQEHDQVZKLFKGRQ¶WKDYHDFOLHQWYLHZ 7KH$GYLFH%HDQ H[SRVHGWKHJHW$GYLFH PHWKRGLQLWV&RPSRQHQWLQWHUIDFH²WKHSODFHZKHUH\RXGHFODUH EXVLQHVVPHWKRGV%XWWKDW¶VQRWDOOWKHFOLHQWVHHV5HPHPEHUWKH$GYLFHLQWHUIDFH H[WHQGHG(-%2EMHFWDQLQWHUIDFHZLWKPHWKRGVRILWVRZQ0HWKRGVWKHFOLHQWFDQVHH 0HWKRGVWKHFOLHQWFDQFDOO$QGLWZRUNVWKHVDPHZD\ZLWKWKH+RPHLQWHUIDFH,QWKLV FKDSWHUZH¶OOOHDUQZKDW\RXUHDOO\H[SRVHWRWKHFOLHQWDQGKRZWKHFOLHQWZRUNVLQFOXGLQJ ERWK5HPRWHDQGORFDOLQWHUIDFHV WKLVLVDQHZFKDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 2 H[DPREMHFWLYHV 7KH&OLHQW9LHZ 2IILFLDO ,GHQWLI\FRUUHFWDQGLQFRUUHFW :KDWLWUHDOO\PHDQV <RXKDYHWRNQRZHYHU\WKLQJDERXWWKHKRPHLQWHUIDFH 7KLVSDUWLFXODUREMHFWLYHGRHVQ¶WLQFOXGHWKHVSHFLDO FKDUDFWHULVWLFVRIDQHQWLW\EHDQKRPHEXWPRVWRIWKH GHWDLOVDERXWWKHFOLHQW¶VYLHZRIDEHDQ¶VKRPHDUHVWLOO FRYHUHGLQWKLVREMHFWLYH DQGWKLVFKDSWHU )RUH[DPSOH\RXKDYHWRNQRZH[DFWO\ZKLFKPHWKRGV DUHLQMDYD[HME(-%+RPH WKH5HPRWHKRPHLQWHUIDFH DQGZKLFKPHWKRGVDUHLQMDYD[HME(-%/RFDO+RPH WKHORFDOKRPHLQWHUIDFH $QGLW¶VQRWHQRXJKWRNQRZ ZKDWWKHPHWKRGVDUH²\RXDOVRKDYHWRNQRZWKH FLUFXPVWDQFHVXQGHUZKLFKWKH\FDQEHFDOOHG<RXKDYH WRNQRZIRULQVWDQFHWKDWD5HPRWHVHVVLRQEHDQFOLHQW FDQUHPRYHDEHDQXVLQJWKHEHDQ¶VKRPHEXWDORFDO FOLHQWFDQQRW$QG\RXKDYHWRNQRZWKDWDORFDOKRPH KDVIHZHUPHWKRGVWKDQD5HPRWHKRPHDQGZKDWWKDW PHDQVIRUWKHFOLHQW )LQDOO\IRU2EMHFWLYH\RXKDYHWRNQRZWKHLQVDQG RXWVRIKRZDFOLHQWGRHVD-1',ORRNXSRQDEHDQ¶V KRPHLQWHUIDFH7KDWLQFOXGHVWKHV\QWD[RIWKHFOLHQW¶V ORRNXSFRGHWKHUXOHVIRUSHUIRUPLQJWKHORRNXSDQG KRZWRXVHWKHKRPHLQWHUIDFHWRJHWDUHIHUHQFHWR DEHDQ¶VFRPSRQHQWLQWHUIDFH<RXKDYHWRNQRZIRU H[DPSOHWKHUXOHVIRUQDUURZLQJDKRPHVWXEDQGJLYHQ DFRGHVQLSSHW\RXPXVWEHDEOHWRUHFRJQL]HZKHWKHU WKHFOLHQWLVORFDORU5HPRWH Copyright Safari Books Online #896963 VWDWHPHQWVRUH[DPSOHVDERXWWKH FOLHQWYLHZRIDVHVVLRQEHDQ¶VORFDO DQGUHPRWHKRPHLQWHUIDFHVLQFOXGLQJ WKHFRGHXVHGE\WKHFOLHQWWRORFDWHD VHVVLRQEHDQ¶VKRPHLQWHUIDFH ,GHQWLI\FRUUHFWDQGLQFRUUHFW VWDWHPHQWVRUH[DPSOHVDERXWWKH FOLHQWYLHZRIDVHVVLRQEHDQ¶VORFDO DQGUHPRWHFRPSRQHQWLQWHUIDFHV 7KLVREMHFWLYHLVMXVWOLNHH[FHSWLW¶VDERXWWKH FRPSRQHQWLQWHUIDFH%XWDJDLQ\RXPXVWNQRZDOOWKH PHWKRGVRIERWKMDYD[HME(-%2EMHFWDQG MDYD[HME(-%/RFDO2EMHFWDQGKRZWKH\¶UHXVHGE\WKH FOLHQWDQG\RXPXVWEHDEOHWRUHFRJQL]HWKHGLIIHUHQFH EHWZHHQD5HPRWHDQGORFDOFOLHQWMXVWE\ORRNLQJDW FRGH &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 3 WKHFOLHQWYLHZ ?PI\\PMKTQMV\ZMITTa_IV\[ 4HE CLIENT HAS A GOAL ! VISION ! QUEST 3HE WANTS TO CALL A BUSINESS METHOD ON THE BEAN 3OMETHING EXPOSED IN THE COMPONENT INTERFACE .EVER FORGET THAT ULTIMATE GOAL IT IS EASY TO GET BOGGED DOWN IN ALL THE DETAILS "UT IF YOU KEEP FOCUSED ON THE CLIENTS DRIVING NEED YOULL HAVE A MUCH EASIER TIME REMEMBERING THINGS LIKE SAY THE RETURN TYPE OF A SESSION BEANS HOME CREATE METHOD :KDW,UHDOO\ ZDQWLVWRFDOOD EXVLQHVVPHWKRGRQ DEHDQ %XW,FDQ·W JHWDUHIHUHQFH WRWKHEHDQ,KDYHWR JRWKURXJKWKHEHDQ·V FRPSRQHQWLQWHUIDFH WKH(-%REMHFW %XWKRZGR,JHWD UHIHUHQFHWRWKH(-%REMHFW" ,KDYHWRJRWKURXJKWKHEHDQ·V KRPH<HDKWKDW·VULJKW,QHHG WRVWDUWE\JHWWLQJWKHVWXEWR WKHEHDQ·V+RPHREMHFW \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Page 4 Return to Table of Contents WKH+RPHLQWHUIDFH 1\ITT[\IZ\[_Q\P\PMPWUMQV\MZNIKM 4HE CLIENT WANTS THE BEAN 7ELL TOO BAD 4HE CLIENT WILL NEVER GET THE BEAN BECAUSE NOBODY TALKS TO THE BEAN EXCEPT THE CONTAINER 4HE BEST THE CLIENT CAN HOPE FOR IS A REFERENCE TO THE BEANS BODYGUARDTHE COMPONENT INTERFACE !ND THE CLIENT GETS A REFERENCE TO THE BEANS COMPONENT INTERFACE BY CALLING A METHOD ON THE BEANS HOME INTERFACE WKHUH DUH QR 'XPE 4XHVWLRQV Q: (OW COME YOU SAID h!ND THE CLIENT GETS A REFERENCE TO THE BEANS COMPONENT INTERFACEv 9OU CANT HAVE A REFERENCE TO AN INTERFACE IN *AVAYOU CAN REFERENCE AN OBJECT BUT YOU CANT REFERENCE AN INTERFACE 4HE REFERENCE VARIABLE CAN BE DECLARED AS AN INTERFACE TYPE BUT THATS NOT THE SAME THING A: 7ELL ACTUALLY IT IS THE SAME THING )N THIS BOOK AND IN THE SPEC AND IN THE EXAM EVERYWHERE YOU SEE THE PHRASE hREFERENCE TO AN INTERFACEv DO A MENTAL SEARCH AND REPLACE TO MAKE IT hREFERENCE TO AN OBJECT THAT IMPLEMENTS THE INTERFACEv )T CAN FEEL A LITTLE STRANGE IF YOU HAVENT READ DOCUMENTS THAT USE THIS CONVENTION BUT YOU BETTER GET USED TO IT "UT DONT WORRY BY #HAPTER YOULL WONDER HOW ANYONE EVER SAID IT DIFFERENTLY Q: 9OU SAID THE CLIENTS ULTIMATE GOAL IS TO CALL METHODS ON THE BEAN /+ THE COMPONENT INTERFACE BUT YOU KNOW WHAT ) MEAN "UT WITH ENTITY BEANS YOU CAN HAVE BUSINESS METHODS IN THE HOME RIGHT 3O WITH ENTITY BEANS ISNT IT TRUE THAT SOMETIMES THE CLIENTS GOAL IS *534 TO USE THE HOME :KDWWKHFOLHQW5($//< ZDQWVLVDUHIHUHQFHWRWKH EHDQ%XWWKHEHVWWKHFOLHQW FDQGRLVJHWDUHIHUHQFH WRWKHEHDQ·VFRPSRQHQW LQWHUIDFHWKH(-%REMHFW %XWLIVKHZDQWVDQ(-% REMHFWUHIHUHQFHWKHFOLHQW KDVWRJHWDUHIHUHQFHWRWKH EHDQ·VKRPHLQWHUIDFH 6RWKDW·VZKHUHLWEHJLQV WKHFOLHQWGRHVDORRNXSRQ WKHEHDQ·VKRPH A: 9ES YOURE RIGHT 4HEYRE CALLED hHOME BUSINESS METHODSv AS OPPOSED TO PLAIN OLD hHOME METHODSv OR PLAIN OLD hBUSINESS METHODSv "UT THEYRE A SPECIAL CASE WELL LOOK AT LATER IN THE BOOK 4HERE ARE OTHER REASONS TOO FOR WHY YOU MIGHT NEED ONLY THE HOME OF AN ENTITY BEAN &OR EXAMPLE IF YOU WANT TO CREATE A BUNCH OF NEW CUSTOMERS IN A DATABASE BUT YOU DONT WANT TO DO ANY OTHER OPERATIONS ON REFERENCES TO THOSE ENTITY CUSTOMERS :HXVHWKHZRUG³(-%REMHFW´WRPHDQWKH EHDQ¶VFRPSRQHQWLQWHUIDFH WKHERG\JXDUG LHWKHWKLQJUHFHLYLQJPHWKRGFDOOVPHDQW IRUWKHEHDQUHJDUGOHVVRIZKHWKHUWKHFOLHQW LVORFDORUUHPRWH &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 5 WKHFOLHQWYLHZ 0W_IKTQMV\][M[I[M[[QWVJMIV" KZMI\M][MIVLZMUW^M LQWHUIDFH!! 5HPRWH Note: over the next sev eral pages, weÕre lookin REMOTE interfaceg satforonly the a bean. Later weÕll see ÒlocalÓ versions). the special-case &UHDWH &OLHQWDVNVWKHKRPHLQWHUIDFHIRUDUHIHUHQFHWR WKHEHDQ¶VFRPSRQHQWLQWHUIDFH LQWHUIDFH!! (-%+RPH PHWKRGV :KLFKPHDQVWKHFOLHQWFDOOVDFUHDWH PHWKRG RQWKHKRPHVWXE LQWHUIDFH!! $GYLFH+RPH FUHDWH 8VH oper must The bean devel method () te ea cr put a in the beanÕs home LQWHUIDFH!! 5HPRWH &OLHQWFDOOVEXVLQHVVPHWKRGVGHFODUHGLQWKH FRPSRQHQWLQWHUIDFH :KLFKPHDQVWKHFOLHQWFDOOVPHWKRGVRQWKH(-% REMHFWVWXE LQWHUIDFH!! (-%2EMHFW UHPRYH PRUHPHWKRGV JBObject The javax.ejb.E ve() interface hascliaenretmocan method the call. 5HPRYH &OLHQWWHOOVWKHEHDQWKDWKH¶VGRQHXVLQJLW :KLFKPHDQVWKHFOLHQWFDOOVUHPRYH RQWKH EHDQ¶V(-%REMHFWVWXE LQWHUIDFH!! $GYLFH JHW$GYLFH In the compo nent interfac the develop e, business meter puts in the component inhods, but since the the EJB terface EXTE client seesObjthect interface, thNeDS BOTH interf e methods from aces. \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 6 XVLQJ-1',WRJHWWKHKRPH *]\NQZ[\_MPI^M\WOM\IPWUMQV\MZNIKMZMNMZMVKM 1VW\PMZ_WZL[_MPI^M\WOM\\PM[\]J\W\PMPWUM WJRMK\\PM\PQVO_M][M\WKITTKZMI\M[W\PI\_MKIVOM\ _PI\_MZMITTa_IV\¸\PM-2*WJRMK\[\]J 7HEN YOU THE CLIENT WANT A REFERENCE TO A HOME INTERFACE YOU GO THROUGH *.$) 4HE PROCESS IS PRETTY STRAIGHTFORWARD YOU GIVE *.$) A LOGICAL NAME THE NAME THE DEPLOYER TOLD THE SERVER TO USE AND YOU GET BACK SOMETHING THAT IMPLEMENTS THE HOME INTERFACE &RQWH[WLF QHZ,QLWLDO&RQWH[W 2EMHFWR LFORRNXS ³$GYLVRU´ DIHZPRUHVWHSV ed ext object assign The InitialContere to the JNDI nce ref a is Ó Òic to lookup service. Give it a name (whateve used to regist er that beranthwie bean deployer and get back an th the server) object. s into a JNDI organizes thingeeÓ. Òvirtual directory tr e tree is eitherd a Each level of thdir ectory (calle another virtual an ob ject. ÒcontextÓ) or... ?PI\¼[26,1' *.$) STANDS FOR *AVA .AMING AND $IRECTORY )NTERFACE AND ITS AN !0) FOR ACCESSING NAMING AND DIRECTORY SERVICES !LTHOUGH *.$) IS QUITE POWERFUL THERE ARE ONLY A FEW PIECES OF IT YOU NEED TO KNOW FOR %*"HOW CLIENTS lND IT HOW CLIENTS USE IT HOW BEANS USE IT AND HOW TO PUT THINGS INTO IT 4HE *.$) !0) CAN WORK WITH MANY DIFFERENT SERVICES AS LONG AS THAT REM SERVICE HAS A *.$) DRIVER CALLED A 3ERVICE 0ROVIDER )TS A LOT LIKE *$"# WHERE YOU THE DEVELOPER USE THE *$"# !0) TO SEND 31, STATEMENTS TO A VARIETY OF DIFFERENT DATABASES 4HE *.$) DRIVER TRANSLATES THE METHOD CALLS YOU MAKE ON THE *.$) !0) INTO SOMETHING THE UNDERLYING NAMINGDIRECTORY SERVICE UNDERSTANDS IRR a JNDI Ôc EDU ED] REM ontextÕ just a plain old object REM a JNDI context can hold objects as well as other contexts. EDW WD] REM REM ]RR &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 7 WKHFOLHQWYLHZ $-1',´YLUWXDO GLUHFWRU\WUHHµ *HWWLQJWKHKRPHLQWHUIDFHVWXE IRR LetÕs say that ÒbarÓ is where thise InitialContext for your beans. *HWDQ,QLWLDO&RQWH[W &RQWH[WLF QHZ,QLWLDO&RQWH[W 7KH,QLWLDO&RQWH[W DVXEW\SHRI&RQWH[W LV\RXUHQWU\ SRLQWLQWRWKH-1',WUHH,W¶VVLPSO\WKH¿UVWFRQWH[WIURP ZKLFK\RXVWDUWQDYLJDWLQJ.LQGRIOLNH\RXUFXUUHQW ZRUNLQJGLUHFWRU\ WKHRQH\RXFGWR 7KH,QLWLDO&RQWH[WFRQVWUXFWRU¿JXUHVRXWZKHUH\RX VKRXOGVWDUW :H¶OOWDONDERXWWKDWLQDPLQXWH %RWK &RQWH[WDQG,QLWLDO&RQWH[WDUHSDUWRIWKHMDYD[QDPLQJ SDFNDJHSDUWRI-((EXWDOVRDGGHGWR-6(ZLWK YHUVLRQ EDU ED] REM REM REM $GYLVRU &XVWRPHU 7D[5DWH t you start wit, hifis Whichever contex t. For example en ex nt lCo your Initia was ÒtazÓ, th om t ex nt lCo the Initia could lookup fr t u yo ing th the only ÒItemDBÓ objec e th be here would EDW WD] REM REM ]RR 6KRSSHU ,WHP'% /RRNXSWKHEHDQ·VKRPHXVLQJWKH ,QLWLDO&RQWH[W 2EMHFWR LFORRNXS ³$GYLVRU´ 7KHORRNXSPHWKRGWDNHVD6WULQJWKDWPXVWPDWFKWKHQDPH DVVLJQHGWRWKLVEHDQ¶V-1',GHSOR\PHQW,IWKHGHSOR\HU DVVLJQHGDQDGGLWLRQDOFRQWH[WWRWKHEHDQE\QDPLQJLW DWGHSOR\WLPH ³EDW$GYLVRU´WKHQWKHORRNXSFRGHZRXOG FKDQJHWR LFORRNXS ³EDW$GYLVRU´ $VVLJQWKHUHVXOWRIWKHORRNXSWRWKH +RPHLQWHUIDFHUHIHUHQFH $GYLFH+RPHKRPH $GYLFH+RPH R 7KHUHWXUQW\SHRIWKHFRQWH[WORRNXSPHWKRGLV2EMHFW VR\RXKDYHWRFDVWLWEDFNWRWKHEHDQ¶VKRPHLQWHUIDFH W\SHEHIRUH\RXFDQFDOO$GYLFH+RPHPHWKRGV isnÕt quite Warning: This code OKS like it LO it ugh ho alt ... right whatÕs out d Þn Õll We . be should . ges pa w fe a t jus in g wron \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 8 WKH+RPHLQWHUIDFH WKHUH DUH QR 'XPE 4XHVWLRQV Q: (OW DO ) KNOW WHAT THE DEVELOPER NAMED THE BEAN A: !CTUALLY ITS NOT UP TO THE BEAN DEVELOPER THE %*" ROLE KNOWN AS "EAN 0ROVIDERTHE ONE WHO ACTUALLY WROTE THE BEAN CODE TO GIVE THE BEAN ITS *.$) NAME 2EMEMBER THE "EAN 0ROVIDER MIGHT HAVE WRITTEN THAT BEAN AS A REUSABLE COMPONENT FOR "EANS @2 5S AND THUS MIGHT HAVE NO IDEA WHERE AND HOW THE BEAN WILL BE USED )TS THE DEPLOYERTHE PERSON WHO ACTUALLY GETS THE BEAN RUNNING IN THE SERVER AS PART OF SOME APPLICATION WHO REGISTERS THE BEAN UNDER A LOGICAL NAME "UT THE BOTTOM LINE IS THAT THERES NO STANDARD OR AUTOMATIC MECHANISM FOR LEARNING THE NAMES OF REGISTERED BEANS !S A CLIENT SOMEBODY SOMEHOW HAS TO TELL YOU THAT SAY THE BEAN WAS REGISTERED AS h!DVISORv .OTICE THAT h!DVISORv WHILE DESCRIBING THE SERVICE IS NOT A 3TRING THAT CORRESPONDS DIRECTLY TO THE NAMES OF ANY OF THE OTHER PIECES OF THE BEAN 2EMEMBER THE COMPONENT INTERFACE WAS !DVICE THE HOME INTERFACE WAS !DVICE(OME AND THE BEAN ITSELF IS !DVICE"EAN 4HE NAME h!DVISORv WAS JUST SOMETHING THE DEPLOYER THOUGHT HAD A NICE RING TO IT /F COURSE IN YOUR COMPANY YOU MIGHT AND PROBABLY WILL HAVE STRICT NAMING GUIDELINES TO FOLLOW FOR HOW BEANS ARE REGISTERED WITH *.$) AT DEPLOYMENT TIME 5NLESS ITS YOUR OWN COMPANY IN WHICH CASE YOU CAN DO WHATEVER YOU DARN WELL PLEASE INCLUDING NAMING EACH BEAN AFTER YOUR FAVORITE ROCK STAR OR -ATRIX CHARACTER Q: ) JUST THOUGHT OF AN EVEN ")''%2 PROBLEM HOW THE HECK DO ) KNOW WHERE TO lND THE SERVER !ND HOW DO ) SPECIFY IT ) DIDNT SEE ANY CODE FOR AN )0 ADDRESS OR 4#0 PORT NUMBER A: 'OOD CATCH 9EAH THATS ALL A BIT OF A MYSTERY ISNT IT 7E HAVE THREE ANSWERS FOR NOW )RUWKHH[DP\RXGRQ¶WQHHG WRNQRZPXFKDERXW-1', 7ERE CHEATING A LITTLE BECAUSE THE CODE WERE USING WORKS ONLY BECAUSE WERE USING THE 2EFERENCE )MPLEMENTATION AND EVEN THEN ONLY BECAUSE WERE RUNNING THE SERVER ON THE SAME PHYSICAL MACHINE AS THE CLIENT 3O WERE TAKING ADVANTAGE OF DEFAULT SETTINGS THAT ARE IN PLACE AUTOMATICALLY BECAUSE WERE RUNNING THE 2EFERENCE )MPLEMENTATION )RUWKHFOLHQWUHODWHGREMHFWLYHV WKHRQHVIURPWKLVFKDSWHU DOO\RXQHHGWRNQRZLVWKH IXQGDPHQWDOSURFHVVIRUGRLQJD -1',ORRNXSWKDW\RXQHHGWR VWDUWZLWKDQ,QLWLDO&RQWH[WDQG WKHQFDOOORRNXS ZKLFKUHWXUQV VRPHWKLQJRIW\SH2EMHFW 7E LIED A LITTLE IN POINT NUMBER ABOVE BECAUSE THIS CODE COULD BE CORRECT IF IT WERE INSIDE A BEAN 7ELL GET TO THAT IN THE "EAN %NVIRONMENT CHAPTER <RXGR127QHHGWRNQRZKRZ WKHFOLHQWRUVHUYHU¿QGVDQG JHWVDUHIHUHQFHWRWKHFRUUHFW ,QLWLDO&RQWH[WRQO\WKDWDQ ,QLWLDO&RQWH[WLVQHHGHG )N REALITY A CLIENT DOES NEED TO KNOW HOW TO FIND THE *.$) SERVICE WHERE A BEANS HOME IS REGISTERED 4HERE ARE SEVERAL WAYS YOU CAN DO THISYOU COULD PASS INFORMATION TO THE )NITIAL#ONTEXT CONSTRUCTOR A 0ROPERTIES OBJECT THAT CONTAINS EVERYTHING THE )NITIAL#ONTEXT NEEDS TO FIND THE SERVER AND THE STARTING CONTEXT /R THERE ARE SEVERAL PLACES WHERE *.$) PROPERTIES CAN BE PLACED ON THE CLIENTS MACHINE )N EITHER CASE THE CLIENT -534 BE GIVEN SOMETHINGEITHER INFO FOR THE )NITIAL#ONTEXT CONSTRUCTOR OR A PROPERTIES FILE )TS DIFFERENT FOR EACH VENDORS SERVER TOO SO YOU HAVE TO CHECK YOUR DOCUMENTATION IN ORDER TO KNOW WHAT THE CLIENT NEEDS ,QWKH%HDQ(QYLURQPHQW FKDSWHUZH¶OODGGDWLQ\ELWPRUH -1',LQIRIRUKRZWKHEHDQLWVHOI XVHV-1',WRORRNXSWKLQJVWKDW KDYHEHHQVSHFL¿FDOO\SODFHG WKHUHIRUWKHEHDQ %XWWKDW¶VDERXWLWIRU\RXU-1', NQRZOHGJH<RXGRQ¶WKDYHWR NQRZDQ\GHWDLOVDERXWWKHUHVW RIWKH-1',$3,RWKHUWKDQWKH &RQWH[WORRNXS PHWKRG &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 9 WKHFOLHQWYLHZ 4M\¼[\ISMIVW\PMZTWWSI\\PMKWUXTM\MKTQMV\KWLM LPSRUWMDYD[QDPLQJ LPSRUWMDYDUPL LPSRUWMDYD[UPL LPSRUWKHDG¿UVW LPSRUWMDYD[HME ts, weÕll look A bunch of impor ually at the at each one individ bottom of the page SXEOLFFODVV$GYLFH&OLHQW^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ QHZ$GYLFH&OLHQW JR ` InitialContext o the is our entry pointdoint the lookup we ere wh e, vic JNDI naming serdvisorÓ SXEOLFYRLGJR ^ on the name ÒA WU\^ What is THIS??? y &RQWH[WLF QHZ,QLWLDO&RQWH[W not just a plain old casWh 2EMHFWR LFORRNXS ³$GYLVRU´ t? $GYLFH+RPHKRPH $GYLFH+RPH 3RUWDEOH5HPRWH2EMHFWQDUURZ R$GYLFH+RPHFODVV Call create on the hom $GYLFHDGYLVRU KRPHFUHDWH e to get us wh REALLY want Ñ the 6\VWHPRXWSULQWOQ DGYLVRUJHW$GYLFH `FDWFK 5HPRWH([FHSWLRQUH[ ^ UH[SULQW6WDFN7UDFH `FDWFK &UHDWH([FHSWLRQFH[ ^ FH[SULQW6WDFN7UDFH `FDWFK ([FHSWLRQH[ ^ H[SULQW6WDFN7UDFH ` ` ` at we component interface . The point of everything! To call a business method on the bean (via the EJBObject stub) to handle (or s Not a good wahay nd le) exceptionme T NO , rather to show so nt wa we t bu here... ptions... ce ex d ke ec ch of the 6KDUSHQ \RXU SHQFLO -ATCH THE CLASS NAME WITH THE PACKAGE ITS FROM9OU CAN USE THE SAME PACKAGE NAME MORE THAN ONCE )F YOURE NOT SURE MAKE YOUR BEST GUESS 0ACKAGE .AME #LASS .AME MDYD[QDPLQJ ,QLWLDO&RQWH[W MDYDUPL $GYLFH+RPH MDYD[UPL 3RUWDEOH5HPRWH2EMHFW KHDG¿UVW 5HPRWH([FHSWLRQ MDYD[HME $GYLFH &UHDWH([FHSWLRQ \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 10 FDVWLQJDQGQDUURZLQJ 2][\_PMVaW]\PW]OP\I[QUXTMKI[\_W]TLJMMVW]OP 4HE RETURN VALUE OF THE #ONTEXTLOOKUP METHOD IS TYPE /BJECT 3O WERE THINKING A SIMPLE CAST SHOULD BE ENOUGH TO FORCE THE OBJECT REFERENCED BY O BACK TO THE !DVICE(OME IMPLEMENTATION THAT WE KNOW IT REALLY IS &RQWH[WLF QHZ,QLWLDO&RQWH[W 2EMHFWR LFORRNXS ³$GYLVRU´ $GYLFH+RPHKRPH $GYLFH+RPH R , but isnÕt. This LOOKS right home interface, With a Remoteeno . casting is not ugh *]\67AW]PI^M\WVIZZW_\PMWJRMK\I[_MTT .ARROWING FORCES THE OBJECT RETURNED FROM THE *.$) LOOKUP TO BE ABSOLUTELY POSITIVELY SOMETHING THAT IMPLEMENTS THE HOME INTERFACE )N OTHER WORDS SOMETHING YOU CAN CAST TO !DVICE(OME &RQWH[WLF QHZ,QLWLDO&RQWH[W 2EMHFWR LFORRNXS ³$GYLVRU´ $GYLFH+RPHKRPH $GYLFH+RPH 3RUWDEOH5HPRWH2EMHFWQDUURZ R$GYLFH+RPHFODVV 731¼TTJQ\M?PaKIV¼\aW]R][\LWIXTIQVWTLKI[\ ' !CCORDING TO THE SPEC YOUTHE CLIENTMUST ASSUME THAT THE SERVER IS USING 2-) ))/0 RATHER THAN REGULAR OLD 2-) .ORMAL 2-) USES *2-0 AS THE WIRE PROTOCOL WHICH ASSUMES THAT WERE ALWAYS TALKING *AVA ALL THE WAY DOWN )F THIS WERE PLAIN 2-) YOUD ALWAYS KNOW THAT WHAT YOU GET OUT OF THE LOOKUP IS POLYMORPHICALLY SOMETHING THAT )3 ! HOME INTERFACE )N OTHER WORDS AN OBJECT WHOSE CLASS TYPE IMPLEMENTS THE HOME INTERFACE FOR THAT BEAN !ND FOR THAT SCENARIO A NORMAL *AVA LANGUAGE CAST WOULD LET YOU ASSIGN THE OBJECT BACK TO THE HOME INTERFACE TYPE SO THAT YOU CAN CALL THE HOME METHODS /THERWISE REMEMBER YOUD BE STUCK CALLING ONLY METHODS OF TYPE /BJECT EQUALS HASH#ODE TO3TRING ETC WHEN WHAT YOU REALLY WANT TO CALL IS CREATE "UT WHEN THE WIRE PROTOCOL IS ))/0 THE RULES CHANGE A LITTLE 4HE NARROW OPERATION GIVES YOU SOMETHING THAT IS CASTABLE &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 11 WKHFOLHQWYLHZ 8WZ\IJTM:MUW\M7JRMK\VIZZW_ 4HE JAVAXRMI0ORTABLE2EMOTE/BJECTS NARROW METHOD RUNS CODE WRITTEN BY THE SERVER VENDOR "UT ALL WE CARE ABOUT IS THAT IT TAKES THE OBJECT WE GOT FROM *.$) AND GIVES US BACK SOMETHING THAT REALLY DOES IMPLEMENT THE HOME INTERFACE )N OTHER WORDS IT GIVES US BACK SOMETHING WE CAN THEN CAST TO THE HOME INTERFACE TYPE AND CALL CREATE 3RUWDEOH5HPRWH2EMHFWQDUURZ R$GYLFH+RPHFODVV the object you got from JNDI the interfac pe you want it to bee. ty It must be a Remote inte rface! I,,23 WKHGHWDLOVR HHGWRNQRZ <RXGRQ¶WQ 3 NQRZKRZ,,2 GRQ¶WQHHGWR QRQ-DYD RS HO )RU(-%\RX HY G WR V\RXLQWHQG RW ZRUNVXQOHV Q¶WJRWKHUH1 WV%XWZHZR HLWKHU DP H[ H &25%$FOLHQ WK RNDQGQRWLQ KHUHLQWKHER IVFRSH UHZD\RXWR D LOV WD GH H 7K HF ZWKDWWKHVS QHHGWRNQR XPHWKDWWKH %XW\RX'2 VV D WR V HU 3URYLG Q UHTXLUHV%HDQ WKDW\RXUEHD LQJ,,23DQG FRQWDLQHULVXV FRPSOLDQW PXVWEH,,23 3FRPSOLDQW" WDNHWREH,,2 HG :KDWGRHVLW VZKHQQHHG XE VW H RW P 5H H 3 2 ,, Q\ WL 1DUURZLQJWK HU IRWK HDFRXSOHR $QGWKHUHDU DSWHU RNDWLQWKLVFK OR ¶OO ZH V XH LVV 2KDYHWREH ' RX \ DW WK HLV ¶W EXW\RXGRQ 7KHERWWRPOLQ HG XV J LQ 23LVEH DW:H¶OO WK DZDUHWKDW,, DQ WK H RU PXFKP QHHGWRNQRZ \RXQHHGWRNQRZDERXW LQJ FRYHUHYHU\WK RQH HFKDSWHU¶VG ,,23EHIRUHWK 7KHKRPHVWXEUHWXUQHGIURP D-1',ORRNXSPLJKWQRW LPSOHPHQWWKHKRPHLQWHUIDFH <RXPLJKWJHWEDFNDQ,,23 VWXEWKDWLVQ·WFDVWDEOHWRWKH KRPHLQWHUIDFHRI\RXUEHDQ $QGWKDWPHDQV\RXFRXOGQ·W FDOOFUHDWH 7RJHWDVWXEWKDW·VFDVWDEOH WRWKHKRPHLQWHUIDFH\RX KDYHWRILUVWQDUURZ WKH REMHFW\RXJHWIURPWKH-1', ORRNXSRQWKHEHDQKRPH %XWRQO\ZKHQWKHKRPH LQWHUIDFHLV5HPRWH \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 12 3RUWDEOH5HPRWH2EMHFWQDUURZ :LWKD5HPRWH KRPHVWXEIURP -1',DQRUGLQDU\FDVW LVQ·WJRRGHQRXJK<RXQHHG VRPHWKLQJPRUHH[RWLF \RXQHHGWRQDUURZLW 7KLQNRIQDUURZLQJDV ´([RWLF&DVWLQJµ .ARROWING IS NOT THE SAME AS CASTING BUT YOU CAN THINK OF IT AS A FORM OF hEXOTIC CASTINGv #ASTING IS ABOUT POLYMORPHISM 7ITH A CAST THE OBJECT DOESNT CHANGE BUT THE WAY YOU REFER TO THAT OBJECT DOES 7ITH NARROWING YOU MIGHT ACTUALLY GET A DIFFERENT OBJECT &DVW $QLPDODQL QHZ'RJ 'RJ¿GR 'RJ DQL u see Casting lets yo multiple ONE object infere e ways. The reines nc at type determ canwh call, methods you ct itself but the obje itÕs a Dog. always knows 'RJ REMHFW Yfa ^a\g $QLPDO 'RJ 1DUURZ QDUURZ R$GYLFH+RPHFODVV ,,23 VWXE g 2EMHFW `ge] $GYLFH+RPH KRPH VWXE The narrow method might return a completely different object (or it might not). But regardless, youÕll get a stub that really DOES implem the interface, so youent can then cast it. &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 13 WKHFOLHQWYLHZ 1RZWKDWZH ÀQDOO\ KDYHWKHKRPHVWXE OHW·VXVHLWWRJHWZKDWZH5($//<ZDQW &DOOFUHDWH RQWKHKRPHLQWHUIDFHWR JHWWKH(-%REMHFWVWXE $GYLFHDGYLVRU KRPHFUHDWH 7KHFUHDWHPHWKRGUHWXUQVDUHIHUHQFHWRWKH FRPSRQHQWLQWHUIDFH$GYLFH,QRWKHUZRUGVLWUHWXUQV DVWXEWRWKH(-%REMHFW ZKLFKLPSOHPHQWV$GYLFHWKH 5HPRWHFRPSRQHQWLQWHUIDFHIRUWKLVEHDQ <RXGRQ¶W QHHGWRFDVWDQGQDUURZWKH(-%REMHFWVWXE :DLWDPLQXWHKRZ FRPHZHGLGQ·WKDYHWR FDVWDQGQDUURZWKH(-% REMHFWVWXEEXWZHKDGWR IRUWKHKRPHVWXE" &DOODEXVLQHVVPHWKRGRQWKH FRPSRQHQWLQWHUIDFH (-%REMHFWVWXE 6\VWHPRXWSULQWOQ DGYLVRUJHW$GYLFH 1RWKLQJVSHFLDOKHUH,W¶VMXVWDSODLQROGPHWKRGFDOORQWKH UHIHUHQFHWRWKH$GYLFHLQWHUIDFH :HOOQRWTXLWH5HPHPEHUHYHU\5HPRWHPHWKRGFDOO GHFODUHVD5HPRWH([FHSWLRQ$QGWKDW¶VDFKHFNHG H[FHSWLRQVR\RX0867KDQGOHRUGHFODUHLW WU\^ 6\VWHPRXWSULQWOQ DGYLVRUJHW$GYLFH `FDWFK 5HPRWH([FHSWLRQUH[ ^ UH[SULQW6WDFN7UDFH ` &D 9, 1D FR \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Page 14 Return to Table of Contents GRQ¶WQDUURZWKH(-%2EMHFW WKHUH DUH QR 'XPE 4XHVWLRQV Q: /+ ) KNOW ) KNOW ) DONT NEED TO LEARN THE DETAILS OF ))/0 BUT ) STILL WANT TO UNDERSTAND 7(9 THEY USE IT A: /+ A LITTLE MORE ))/0 THE WIRE PROTOCOL FOR #/2"! CAN REPRESENT MORE INFORMATION THAN PLAIN 2-) &OR EXAMPLE ))/0 CAN PROPAGATE BOTH TRANSACTION AND SECURITY INFORMATION IMPORTANT THINGS THAT YOU CANT SEND WITH A NON ))/0 REMOTE METHOD CALL 3O ))/0 LETS A CONTAINER AT LEAST HAVE THE POTENTIAL FOR INTEROPERATING WITH OTHER SERVERS INCLUDING POSSIBLY ONE THAT ISNT *AVA BASED 2EMEMBER #/2"! IS A STANDARD THAT AMONG OTHER THINGS CAN GIVE TWO OBJECTS WRITTEN IN TWO DIFFERENT LANGUAGES A CHANCE TO INVOKE EACH OTHERS METHODS 4HIS DOES NOT MEAN THAT YOUR SERVER IS NECESSARILY USING ))/0 4HE SPEC SAYS THAT 9/5THE DEVELOPERHAVE TO ASSUME THE SERVER IS USING ))/0 WHICH MEANS YOU HAVE TO BE SURE YOUR BEANS ARE ))/0 COMPLIANT WELL TALK ABOUT ))/0 COMPLIANCE A LITTLE LATER IN THIS CHAPTER Q: )F MY SERVER DOESNT USE ))/0 DO ) STILL HAVE TO DO THE WHOLE NARROWING THING A: 9ES AND NO 9OUR CODE MIGHT WORK JUST FINE WITH NOTHING MORE THAN A CAST "UTAND THIS IS A REALLY HUGE BUTYOUR CLIENT CODE WONT BE VENDOR INDEPENDENT )N OTHER WORDS YOU WONT HAVE A PORTABLE APP IF YOU DONT USE NARROW BECAUSE REDEPLOYING THE BEAN ON A SERVER THAT DOES USE ))/0 WILL BREAK THE CLIENTS Q: )S THERE ANY DOWNSIDE TO USING NARROW %SPE CIALLY IF THE SERVER IS NOT USING ))/0 7KHGHFODUHGUHWXUQW\SHRI FUHDWH LVWKHFRPSRQHQW LQWHUIDFHQRW2EMHFW 6RWKH(-%REMHFWFRPHVEDFN IURPFUHDWH DOUHDG\NQRZLQJ ZKDWLWLV DQLPSOHPHQWDWLRQ RI\RXUFRPSRQHQWLQWHUIDFH SXEOLF$GYLFHFUHDWH e home The return type ofmeththod is e() at cre ace interf interface ALWAYS the component b doesnÕt need So the EJB object stu w.. rro na a cast or a A: .O DOWNSIDE WELL WHATEVER OVERHEAD THERE IS WOULDNT BE WORTH THE PORTABILITY TRADEOFF )F YOUR SERVER ISNT USING ))/0 NARROW IS MOST LIKELY A NO OP IE DO NOTHING METHOD 4HE SPEC SAYS TO ALWAYS NARROW AND IT WONT HURT YOU IF IT ISNT NEEDED &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 15 WKHFOLHQWYLHZ ?ZQ\QVO\PM:MUW\MPWUMQV\MZNIKMNWZI[M[[QWVJMIV .OW THAT YOUVE SEEN THE LOOKUP AND CREATE PROCESS FROM THE CLIENTS POINT OF VIEW WELL SEE WHAT YOU HAVE TO DO TO WRITE A HOME INTERFACE FOR YOUR BEAN &OR SESSION BEANS THE PROCESS IS VERY EASY )N FACT FOR STATELESS SESSION BEANS ITS LUDICROUSLY EASYYOU JUST DECLARE A SINGLE NO ARG CREATE METHOD SDFNDJHKHDG¿UVW LPSRUWMDYD[HME LPSRUWMDYDUPL5HPRWH([FHSWLRQ SXEOLFLQWHUIDFH$GYLFH+RPHH[WHQGV(-%+RPH^ SXEOLF$GYLFHFUHDWH WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ ` 5XOHVIRUWKHKRPHLQWHUIDFH ,PSRUWMDYD[HME DQGMDYDUPL5HPRWH([FHSWLRQ ([WHQG(-%+RPH 'HFODUHDFUHDWH PHWKRGWKDWUHWXUQVWKHFRPSRQHQWLQWHUIDFHDQG GHFODUHVD&UHDWH([FHSWLRQDQG5HPRWH([FHSWLRQ 4 )RUVWDWHOHVVVHVVLRQEHDQVWKHUHFDQEHRQO\RQHFUHDWH DQGLWPXVW127 KDYHDUJXPHQWV 4 6WDWHIXOVHVVLRQEHDQVFDQKDYHPXOWLSOHRYHUORDGHGFUHDWH PHWKRGVDQG GR127QHHGWRKDYHDQRDUJFUHDWH 4 $OOFUHDWH PHWKRGVPXVWGHFODUHD&UHDWH([FHSWLRQDQG5HPRWH([FHSWLRQ EXWWKH\FDQDOVRGHFODUHRWKHUDSSOLFDWLRQ FKHFNHG H[FHSWLRQV 4 7KHQDPHRIFUHDWHPHWKRGVLQVWDWHIXOEHDQVPXVWEHJLQZLWK³FUHDWH´ FUHDWH$FFRXQW FUHDWH%LJ'RJ FUHDWH)DVKLRQ$GYLVRU HWF 4 )RUVWDWHIXOVHVVLRQEHDQVDUJXPHQWVPXVWEH50,,,23FRPSDWLEOH \RX NQRZ6HULDOL]DEOHSULPWLYH5HPRWHRUDUUD\VRUFROOHFWLRQVRIDQ\RIWKRVH \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 16 WKH+RPHLQWHUIDFH :MUW\MPWUMQV\MZNIKMM`IUXTM[NWZ[M[[QWVJMIV[ 4HE EXAMPLES ON THIS PAGE ARE ALL LEGAL EXAMPLES OF 2EMOTE HOME INTERFACES 9OULL SEE SOME THAT COULD BE BOTH STATELESS AND STATEFUL AND SOME THAT COULD BE ONLY STATEFUL BECAUSE THEY HAVE A CREATE METHOD WITH ARGUMENTS 7EVE DROPPED THE PACKAGE AND IMPORT STATEMENTS TO PUT MORE ON THE PAGE SXEOLFLQWHUIDFH&DUW+RPHH[WHQGV(-%+RPH^ SXEOLF&DUWFUHDWH 6WULQJVWRUH,' WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ SXEOLF&DUWFUHDWH WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ ` SXEOLFLQWHUIDFH0DWFKHU+RPHH[WHQGV(-%+RPH^ SXEOLF0DWFKHUFUHDWH 6WULQJFXVWRPHU,' WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ SXEOLF0DWFKHUFUHDWH1HZ&XVWRPHU 6WULQJQDPH6WULQJORJLQ WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ ` SXEOLFLQWHUIDFH7LFNHWV+RPHH[WHQGV(-%+RPH^ SXEOLF7LFNHWVFUHDWH WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ ` SXEOLFLQWHUIDFH&OXE+RPHH[WHQGV(-%+RPH^ SXEOLF&OXEFUHDWH([LVWLQJ 6WULQJFOXE,' WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ SXEOLF&OXEFUHDWH1HZ&OXE 6WULQJFOXE1DPH WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ ` 7KHUH¶VRQO\RQHLQWHUIDFHKHUHWKDWFRXOGEHDVWDWHOHVVVHVVLRQEHDQ¶V KRPH²QXPEHU1RWLFHWRRWKDWQXPEHUKDVWZRFUHDWHPHWKRGVWKDW ERWKKDYHWKHVDPHDUJXPHQW²D6WULQJ²EXWWKHPHWKRGVDUHQDPHG GLIIHUHQWO\WRUHÀHFWZKDWWKDWSDUWLFXODUFUHDWHPHWKRGLVIRU &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 17 WKHFOLHQWYLHZ ,ZRQGHUZKDWHOVH ,FDQGRZLWKWKHKRPH LQWHUIDFH,NQRZWKHUH·V PRUHWKDQMXVWWKHFUHDWH PHWKRGZKDWPHWKRGVDUH LQ(-%+RPH" LQWHUIDFH!! 5HPRWH LQWHUIDFH!! (-%+RPH ZKDWPHWKRGV DUHLQKHUH" FOLHQWFDQFDOO WKHVHPHWKRGV LQWHUIDFH!! $GYLFH+RPH FUHDWH The client sees EJBHome! Al everything in are exposed tol of these methods the AdviceHome the client through interface. Remember, any class that implements AdviceHome must from implement ALL the methods e both AdviceHome AND EJBHom ). (Remote doesnÕt have any methods \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 18 WKH(-%+RPHLQWHUIDFH :KDWWKH&/,(17VHHV :KDW<28ZULWH LQWHUIDFH!! $GYLFH+RPH LQWHUIDFH!! $GYLFH+RPH FUHDWH FUHDWH JHW(-%0HWD'DWD JHW+RPH+DQGOH UHPRYH +DQGOHK UHPRYH 2EMHFWNH\ 5HPHPEHUWKHFRQWDLQHU LPSOHPHQWV\RXUKRPH LQWHUIDFHDQGPDWFKLQJVWXE FUHDWH FUHDWH 7KHFRQWDLQHUPXVW LPSOHPHQW(9(5<7+,1*IURP $GYLFH+RPHDQG(-%+RPH JHW(-%0HWD'DWD JHW+RPH+DQGOH UHPRYH +DQGOHK UHPRYH 2EMHFWNH\ JHW(-%0HWD'DWD JHW+RPH+DQGOH UHPRYH +DQGOHK UHPRYH 2EMHFWNH\ $GYLFH+RPH,PSO the class of the acttual Remote home objec $GYLFH+RPH,PSOBVWXE the class home objecof the t stub 1RWHWKHVHDUHQ¶WQHFHVVDULO\WKHUHDOQDPHV² WKHVHUYHUJHQHUDWHVWKHVHFODVVHVDQGLWFDQ QDPHWKHPZKDWHYHULWZDQWVWR &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Page 19 Return to Table of Contents WKHFOLHQWYLHZ ,I\RX·UHDFOLHQWDQG\RXZDQWWR JHWUHÁHFWLRQOLNHLQIRUPDWLRQDERXWWKHEHDQ 8QOHVV\RX¶UHDWRROYHQGRU\RX¶OOSUREDEO\QHYHUQHHGWRFDOOWKLV PHWKRG,WUHWXUQVWKH(-%0HWD'DWDLQWHUIDFH²VRPHWKLQJ\RXFDQ XVHWRJHWPRUHVSHFL¿FFODVVLQIRUPDWLRQDERXWWKHEHDQ,I\RX¶YH JRW\RXUVHOIDQ(-%0HWD'DWDUHIHUHQFH E\FDOOLQJJHW(-%0HWD'DWD \RXFDQFDOOJHW+RPH,QWHUIDFH&ODVV JHW3ULPDU\.H\&ODVV LV6HVVLRQ DQGPRUH VHULDOL]HWKHKRPHVRWKDW\RXFDQXVHWKHKRPH DJDLQODWHUZLWKRXWKDYLQJWRJRWKURXJK-1', ,PDJLQH\RX¶UHDFOLHQWDQG\RX¶YHEHHQZRUNLQJZLWKDKRPH² PDNLQJDEXQFKRIEHDQVFDOOLQJKRPHPHWKRGVRQHQWLW\EHDQV ZKDWHYHU$QGQRZ\RXKDYHWRUHERRW\RXUPDFKLQH2UPRYHWR DQRWKHUPDFKLQH%XW\RXZDQWWRFRQWLQXHZRUNLQJZLWKWKLVKRPH :KDWGR\RXGR" &DOOWKLVPHWKRG LQWHUIDFH!! (-%+RPH (-%0HWD'DWDJHW(-%0HWD'DWD +RPH+DQGOHJHW+RPH+DQGOH YRLGUHPRYH +DQGOHK YRLGUHPRYH 2EMHFWNH\ LQWHUIDFH!! (-%+RPH (-%0HWD'DWDJHW(-%0HWD'DWD +RPH+DQGOHJHW+RPH+DQGOH YRLGUHPRYH +DQGOHK YRLGUHPRYH 2EMHFWNH\ <RXFRXOGJREDFNWKURXJK-1',DQGGRWKHZKROHORRNXSWKLQJDJDLQ %XWLI\RXDVNWKHKRPHIRUDKDQGOH\RX¶OOJHWEDFND6HULDOL]DEOH WKLQJ\RXFDQVDYHDQGXVHODWHUWRJHWWKHKRPHVWXEEDFNZLWKRXW JRLQJWKURXJK-1', PRUHRQKDQGOHVDOLWWOHODWHU WHOOWKHKRPH\RX·UHGRQHZLWKDVHVVLRQEHDQ :KHQ\RX¶UHGRQHZLWKDVHVVLRQEHDQ\RXFDQWHOOWKHKRPHE\FDOOLQJ UHPRYH DQGSDVVLQJWKH(-%REMHFW¶VKDQGOH<HVWKDW¶VULJKWWKH(-% REMHFW-XVWDVWKHKRPHREMHFWFDQJLYH\RXDKDQGOH VRWKDW\RXFDQ JHWWKHKRPHVWXEEDFNODWHUZLWKRXWJRLQJWKURXJK-1', WKH(-%REMHFW FDQJLYH\RXDKDQGOHWRLWVHOI :H¶OOVHHPRUHRIWKDWODWHULQWKLVFKDSWHU <RXFDQXVHWKLVYHUVLRQRIUHPRYH IRUHQWLW\EHDQVDVZHOOEXWZLWK HQWLW\EHDQVLW¶VXVXDOO\HDVLHUWRFDOOWKHRWKHUUHPRYH WHOOWKHKRPHWRUHPRYHDQHQWLW\EHDQ 1RWLFHZHGLGQ¶WVD\³7HOOWKHKRPH\RX¶UHGRQHZLWKDQHQWLW\EHDQ´7KDW¶V EHFDXVHFDOOLQJUHPRYHRQDQHQWLW\EHDQLVGUDVWLFDOO\GLIIHUHQWIURPFDOO LQJUHPRYHRQDVHVVLRQEHDQ:H¶OOJHWLQWRWKHGHWDLOVLQWKHHQWLW\EHDQ FKDSWHUVEXWWKHVKRUWYHUVLRQLVZKHQ\RXUHPRYHDQHQWLW\EHDQ\RX¶UH QRWMXVWWHOOLQJWKHFRQWDLQHUWKDW\RX¶UHGRQHZLWKWKHEHDQ\RX¶UHWHOOLQJLW WKDWHYHU\RQHLVGRQHZLWKWKHEHDQ)RUHYHU%HFDXVHFDOOLQJUHPRYH RQ DQHQWLW\EHDQPHDQV³'HOHWHWKLVHQWLW\IURPWKHSHUVLVWHQWVWRUH´ :KLFK XVXDOO\PHDQV³'HOHWHWKLVURZIURPWKHGDWDEDVH´ 7KLVYHUVLRQRIUHPRYHWDNHVDSULPDU\NH\ZKLFKVHVVLRQEHDQVGRQ¶WKDYH VRXQOLNHWKHRWKHUUHPRYH WKLVYHUVLRQFDQEHXVHGIRUHQWLW\EHDQVRQO\ LQWHUIDFH!! (-%+RPH (-%0HWD'DWDJHW(-%0HWD'DWD +RPH+DQGOHJHW+RPH+DQGOH YRLGUHPRYH +DQGOHK YRLGUHPRYH 2EMHFWNH\ LQWHUIDFH!! (-%+RPH (-%0HWD'DWDJHW(-%0HWD'DWD +RPH+DQGOHJHW+RPH+DQGOH YRLGUHPRYH +DQGOHK YRLGUHPRYH 2EMHFWNH\ \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 20 WKH(-%+RPHLQWHUIDFH +H\GLG,MXVW VHHVRPHWKLQJDERXW DSULPDU\NH\",QWKHKRPH LQWHUIDFHIRUDVHVVLRQ EHDQ"7KDWFDQ·WEH ULJKW ,NQRZLVQ·WWKDWRGG" %XWORRNWKHUH·VRQO\RQH KRPHLQWHUIDFH(-%+RPH UHJDUGOHVVRIWKHEHDQW\SH7KHUH·V QRVHSDUDWH(-%6HVVLRQ+RPH RU(-%(QWLW\+RPHLW·VMXVW (-%+RPHIRUHYHU\WKLQJ Q: $OES THIS MEAN THE CLIENT HAS TO KNOW THAT SHES USING A SESSION BEAN AND NOT AN ENTITY BEAN )SNT THAT SOMETHING THE CLIENT SHOULDNT HAVE TO KNOW A: 9ES THE CLIENT DOES HAVE TO KNOW THAT WHEN SHES GOT THE HOME INTERFACE FOR A SESSION BEAN HE CANT CALL THE REMOVE/BJECT PRIMARY+EY METHOD )F SHE DOES SHELL GET AN EXCEPTION JAVAXEJB2EMOVE%XCEPTION )T DOES FEEL LIKE MORE OF AN IMPLEMENTATION DETAIL THAN THE CLIENT SHOULD HAVE TO KNOW IE THAT ITS A SESSION VS ENTITY BEAN BUT IN REALITY YOU CANT EXPECT TO WRITE AN %*" CLIENT WITHOUT KNOWING WHETHER YOURE COMMUNICATING WITH A SESSION OR ENTITY BEAN &OR ONE THING THE WAY THE CLIENT INTERACTS WITH AN ENTITY BEAN HOME IS COMPLETELY DIFFERENT FROM THE WAY A CLIENT USES A SESSION BEAN HOME 9OULL SEE DRAMATIC DIFFERENCES WHEN WE GET TO THE ENTITY BEAN CHAPTERS &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 21 WKHFOLHQWYLHZ :MDD=LHGAFLK 7KHPHWKRGVRIWKHEHDQDUHH[SRVHGWRWKHFOLHQW WKURXJKWKHFRPSRQHQWLQWHUIDFH 7KHFOLHQWFDQ·WGLUHFWO\JHWDUHIHUHQFHWRWKH EHDQWKHFOLHQWPXVWJRWKURXJKWKHEHDQ·V(-% REMHFWZKLFKLPSOHPHQWVWKHFRPSRQHQWLQWHU IDFH 7KHFOLHQWJHWVDUHIHUHQFHWRWKHEHDQ·V(-% REMHFWIURPWKHEHDQ·VKRPH E CLIENT STUB ON TH S A HOME ANT FI E D 4HERE WA QUITE DE THE 64HAT MA D DE WA S N E E D ! NARROW BE HEEDE TO ST CA LIANT &OR THE "! COMP 2 / # S A W IT SE U "ECA 2OSES ARE RED BLUE IS THE SKY YOU CANT GET A BEAN FROM *.$ ) )TS ONLY THE HOME A CLIENT WILL SPY WHEN HE DOES A LOOKUP ON *.$ ) 7RJHWWKHEHDQ·VKRPHWKHFOLHQWGRHVDORRNXS RQ-1',XVLQJWKHORJLFDOQDPHXQGHUZKLFKWKH EHDQZDVGHSOR\HG 7RGRD-1',ORRNXSWKHFOLHQWPXVWILUVWJHWDQ ,QLWLDO&RQWH[WZKLFKLVWKHHQWU\SRLQWLQWRWKH VHUYHU·V-1',´YLUWXDOGLUHFWRU\WUHHµ )RUD5HPRWHKRPHLQWHUIDFHWKHVWXEUHWXUQHG IURP-1',PXVWEHERWKFDVWDQGQDUURZHG 1DUURZLQJLVWKH´H[RWLFFDVWLQJµQHHGHGIRUVWXE REMHFWVWKDWFRPHIURPDPHWKRGWKDWGRHVQRW UHWXUQWKHVWXE·VFOLHQWLQWHUIDFH6LQFHWKH-1', ORRNXSUHWXUQVW\SH2EMHFWWKHREMHFWUHWXUQHG IURPWKHORRNXSPXVWEHQDUURZHGWRWKHEHDQ·V KRPHLQWHUIDFHDQGWKHQFDVWWRWKHEHDQ·VKRPH LQWHUIDFH 1DUURZLQJLVUHTXLUHGIRU,,23VWXEV ,,23LV WKHZLUHSURWRFROIRU&25%$ EHFDXVHZKDW·V UHWXUQHGIURPWKHORRNXSPLJKWQRWEHFDSDEOHRI LPSOHPHQWLQJPXOWLSOHLQWHUIDFHVDQGWKXVZRXOG NQRZRQO\DERXWWKHPHWKRGVLQW\SH2EMHFW 1DUURZLQJUHWXUQVDQREMHFWWKDWLPSOHPHQWVWKH KRPHLQWHUIDFH 7KHKRPHLQWHUIDFHH[WHQGV(-%+RPHZKLFKKDV IRXUDGGLWLRQDOPHWKRGVWKHFOLHQWFDQVHH JHW(-%0HWD'DWDJHW+RPH+DQGOH UHPRYH +DQGOHK UHPRYH 2EMHFWSULPDU\.H\ 7KHUHPRYH 2EMHFWSULPDU\.H\ PXVWQRWEH FDOOHGRQDVHVVLRQEHDQ \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 22 WKH+RPHLQWHUIDFH 6KDUSHQ \RXU SHQFLO %DVHGRQWKHUXOHVIRUVHVVLRQEHDQKRPHLQWHUIDFHVZKLFK VWDWHPHQWVDUHWUXHDERXWWKLVLQWHUIDFH LPSRUWMDYD[HME(-%+RPH LPSRUWMDYDUPL5HPRWH([FHSWLRQ SXEOLFLQWHUIDFH&DUW+RPHH[WHQGV(-%+RPH^ SXEOLF&DUWFUHDWH WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ ` R&DUW+RPHPXVWQRWEHWKHKRPHRIDVWDWHIXOVHVVLRQEHDQ R7KHLQWHUIDFHLVPLVVLQJDQLPSRUWVWDWHPHQW R7KHFUHDWHPHWKRGLVPLVVLQJDQH[FHSWLRQ R&DUWPXVWEHWKHFODVVW\SHRIWKHEHDQ R&DUWPXVWEHWKHLQWHUIDFHWKDWH[WHQGV(-%2EMHFW R7KHREMHFWUHWXUQHGIURPFUHDWH PXVWEHQDUURZHG R7KHREMHFWUHWXUQHGIURPFUHDWH GRHVQRWQHHGDFDVW <RX0867EH DEOHWRORRNDW FRGHDQGLQIHU DORWDERXWLW RRNDW 7KHH[DPH[SHFWV\RXWRO HDQG FOLHQWLQWHUIDFHRUEHDQFRG JV\RX PDNHLQIHUHQFHVDERXWWKLQ ZDOORI GRQ¶WVHH<RX0867NQR SRQHQW FRP DQG PH UKR VIR WKHUXOH UH LQWHUIDFHV$QGWKHUH¶VPR &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 23 WKHFOLHQWYLHZ *]\MVW]OPIJW]\\PMPWUMTM\¼[\ITSIJW]\ \PM-2*WJRMK\<PMKWUXWVMV\QV\MZNIKM <PM\PQVOaW]:-)44A_IV\ 2EMEMBER ALL THAT )NITIAL#ONTEXT *.$) LOOKUP CAST NARROW CREATE BUSINESS WAS JUST TO GET WHAT YOU REALLY WANTED ALL ALONGSOMETHING WITH THE BUSINESS METHODS 3OMETHING YOU CAN USE TO GET THE BEAN TO DO WHATEVER IT IS THAT BEAN WAS CREATED FOR .UMBER CRUNCHING ONLINE SHOPPING ADVICE SDFNDJHKHDG¿UVW LPSRUWMDYD[HME LPSRUWMDYDUPL5HPRWH([FHSWLRQ SXEOLFLQWHUIDFH$GYLFHH[WHQGV(-%2EMHFW^ SXEOLF6WULQJJHW$GYLFH WKURZV5HPRWH([FHSWLRQ ` 5XOHVIRUWKHFRPSRQHQWLQWHUIDFH ,PSRUWMDYD[HME DQGMDYDUPL5HPRWH([FHSWLRQ ([WHQG(-%2EMHFW 'HFODUHRQHRUPRUHEXVLQHVVPHWKRGVWKDWWKURZD5HPRWH([FHSWLRQ 4 $UJXPHQWVDQGUHWXUQW\SHVPXVWEH50,,,23FRPSDWLEOH 6HULDOL]DEOHSULPL WLYH5HPRWHRUDUUD\VRUFROOHFWLRQVRIDQ\RIWKRVH 4 <RXFDQKDYHRYHUORDGHGPHWKRGV 4 (DFKPHWKRGPXVWGHFODUHD5HPRWH([FHSWLRQ 4 <RXFDQGHFODUH\RXURZQDSSOLFDWLRQH[FHSWLRQVEXWWKH\PXVW127EHUXQ WLPHH[FHSWLRQV LQRWKHUZRUGVWKH\PXVWEHFRPSLOHUFKHFNHGH[FHSWLRQV² VXEFODVVHVRI([FHSWLRQEXWQRWVXEFODVVHVRI5XQWLPH([FHSWLRQ \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 24 WKH&RPSRQHQWLQWHUIDFH ,ZRQGHUZKDWHOVH, FDQGRZLWKWKHFRPSRQHQW LQWHUIDFH,NQRZWKHUH·V PRUHWKDQMXVWWKHEXVLQHVV PHWKRGVZKDWPHWKRGVDUH LQ(-%2EMHFW" LQWHUIDFH!! 5HPRWH LQWHUIDFH!! (-%2EMHFW ZKDWPHWKRGV DUHLQKHUH" FOLHQWFDQFDOO WKHVHPHWKRGV LQWHUIDFH!! $GYLFH JHW$GYLFH Just as with interface, thethcle EJBHome in EJBObject! ient sees everything All of these methods are expo through the bean sed to the client interface (Advice Õs component ). Any class that implements the Advice must implement ALL methods from both Advice AND EJBObject. &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Page 25 Return to Table of Contents WKHFOLHQWYLHZ 1UIOQVM_PI\MT[MaW]UQOP\_IV\\WLW _Q\PaW]Z-2*WJRMK\ZMNMZMVKM 9OURE A CLIENT 9OU HAVE A REFERENCE TO THE !DVICE"EANS COMPONENT INTERFACE 9OU KNOW YOU CAN CALL GET!DVICE "UT NOW THAT YOUVE GONE TO ALL THE TROUBLE OF GETTING THE STUB ARE THERE OTHER THINGS YOU MIGHT WANT TO DO ,FDQWKLQNRIVRPHWKLQJV OLNHZKDWLI,KDYHWKHEHDQEXW, ORVWWKHUHIHUHQFHWRWKHKRPHDQGQRZ ,ZDQWWRPDNHPRUHEHDQVRIWKDWW\SH" 6XUHO\WKHEHDQNQRZVLWVRZQKRPHULJKW" ,FDQ·WEHOLHYHWKH\ZRXOGKDYHEHHQ VWXSLGHQRXJKWRPDNH\RXJREDFN WKURXJK-1', \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 26 WKH(-%2EMHFWLQWHUIDFH :KDW<28ZULWH :KDWWKH&/,(17VHHV LQWHUIDFH!! $GYLFH LQWHUIDFH!! $GYLFH JHW$GYLFH JHW3ULPDU\.H\ JHW(-%+RPH JHW+DQGOH UHPRYH LV,GHQWLFDO JHW$GYLFH (-%2EMHFW WKH EHDQ·VERG\JXDUG IRUEXVLQHVV PHWKRGV 5HPHPEHUWKHFRQWDLQHU LPSOHPHQWV\RXUFRPSRQHQW LQWHUIDFHDQGPDWFKLQJVWXE 7KHFRQWDLQHUPXVWLPSOHPHQW (9(5<7+,1*IURP$GYLFHDQG (-%2EMHFW <RXEHWWHU NQRZWKHVH RWKHUPHWKRGV LQVLGHDQGRXW $GYLFH,PSO JHW$GYLFH JHW3ULPDU\.H\ JHW(-%+RPH JHW+DQGOH UHPRYH LV,GHQWLFDO the class of the acttual Remote EJB objec $GYLFH,PSOBVWXE JHW$GYLFH JHW3ULPDU\.H\ JHW(-%+RPH JHW+DQGOH UHPRYH LV,GHQWLFDO the class EJB objecof the t stub QRZDOO 7KHH[DPH[SHFWV\RXWRN MHFW ¿YHRIWKHPHWKRGVLQ(-%2E H H\¶U DWWK ZWK NQR HWR KDY <RX VDQG DYDLODEOHWR5HPRWHFOLHQW H[DFWO\KRZWKH\¶UHXVHG &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 27 WKHFOLHQWYLHZ ,I\RX·UHDFOLHQWDQG\RXZDQWWR JHWWKHSULPDU\NH\RIDQHQWLW\EHDQ :HZRQ¶WJRLQWRWKLVQRZVLQFHZHKDYHPLOOLRQSDJHVRQHQWLW\EHDQV FRPLQJXS-XVWNQRZWKDWWKLVGRHVQRWDSSO\WRVHVVLRQEHDQVZKLFKGRQ¶W KDYHDXQLTXHLGHQWLW\H[SRVHGWRWKHFOLHQW%XWLIWKHFOLHQWVRPHKRZJHWV DUHIHUHQFHWRDQHQWLW\EHDQ PD\EHE\VHDUFKLQJRQDFXVWRPHU¶VQDPH DQGZDQWVWKHDFWXDOSULPDU\NH\WKLVLVWKHPHWKRGWRFDOO7U\LWRQD VHVVLRQEHDQDQG\RX¶OOJHWDELJIDW5HPRWH([FHSWLRQ RU(-%([FHSWLRQLI WKHFOLHQWLVORFDO JHWWKHEHDQ·VKRPH ,PDJLQH\RX¶YHJRWDEHDQEXW\RXGRQ¶WKDYHWKHEHDQ¶VKRPH$QGQRZ \RXZDQWWRPDNHPRUHEHDQVRIWKDWW\SH:KDWGR\RXGR"<RXFRXOGGRD -1',ORRNXSDQGJHWWKHKRPHLQWKHXVXDOZD\%XWZKDWLI\RXGRQ¶WKDYH HQRXJKLQIRUPDWLRQWRGRWKH-1',ORRNXS"<RXFDQDVNWKHEHDQWRJLYH \RXDUHIHUHQFHWRLWVKRPH(YHQLI\RXDUHFDSDEOHRIGRLQJD-1',ORRNXS RQWKHKRPHFDOOLQJJHW(-%+RPH RQWKHEHDQLVPRUHHI¿FLHQW VDYHDUHIHUHQFHWRWKH(-%2EMHFW <RX¶UHVKRSSLQJRQOLQHFDUHIXOO\SXWWLQJLWHPVLQ\RXUFDUWDIWHUKRXUVRI SDLQVWDNLQJUHVHDUFKDQGGHFLVLRQPDNLQJRQZKHWKHU\RXUJLUOIULHQGZLOO SUHIHU\RXLQFRUQÀRZHUEOXHRUWKH0DUWKD6WHZDUWVHDIRDPJUHHQ%XW EHIRUH\RXFDQ¿QLVK\RXKDYHWRVZLWFKWRDQRWKHUPDFKLQH1RSUREOHP <RXFDQDVNWKHEHDQIRUDKDQGOHWRWKH(-%REMHFW<RXFDQXVHWKH KDQGOHWRJHWEDFNWR\RXURULJLQDO(-%REMHFWDQGNHHSVKRSSLQJ WHOOWKHEHDQ\RX·UHGRQHZLWKLW :KHQ\RX¶UH¿QLVKHGZLWKWKHEHDQLW¶VJRRGPDQQHUVWRWHOOLW\RX¶UHGRQH VRWKHFRQWDLQHUFDQIUHHXSDQ\UHVRXUFHVLWPLJKWEHNHHSLQJRQ\RXU EHKDOI'$1*(5:H¶UHWDONLQJRQO\DERXWVHVVLRQEHDQVKHUH$OWKRXJK \RXFDQFDOOUHPRYH RQDQHQWLW\EHDQUHPHPEHULWKDVDYHU\GLIIHUHQW PHDQLQJ ZH¶OOVHHWKDWLQWKHHQWLW\EHDQFKDSWHUV FDOOWKLVPHWKRG LQWHUIDFH!! (-%2EMHFW 2EMHFWJHW3ULPDU\.H\ (-%+RPHJHW(-%+RPH +DQGOHJHW+DQGOH YRLGUHPRYH ERROHDQLV,GHQWLFDO 2EMHFWR LQWHUIDFH!! (-%2EMHFW 2EMHFWJHW3ULPDU\.H\ (-%+RPHJHW(-%+RPH +DQGOHJHW+DQGOH YRLGUHPRYH ERROHDQLV,GHQWLFDO 2EMHFWR LQWHUIDFH!! (-%2EMHFW 2EMHFWJHW3ULPDU\.H\ (-%+RPHJHW(-%+RPH +DQGOHJHW+DQGOH YRLGUHPRYH ERROHDQLV,GHQWLFDO 2EMHFWR LQWHUIDFH!! (-%2EMHFW 2EMHFWJHW3ULPDU\.H\ (-%+RPHJHW(-%+RPH +DQGOHJHW+DQGOH YRLGUHPRYH ERROHDQLV,GHQWLFDO 2EMHFWR FRPSDUHWZR(-%REMHFWUHIHUHQFHVWRVHHLIWKH\ UHIHUHQFHWKHVDPHEHDQ <RX¶YHJRWWZRUHIHUHQFHVWRVHVVLRQEHDQ(-%REMHFWV1RZ\RXZDQWWR NQRZLIWKH\¶UHUHDOO\UHIHUHQFHVWRWKHVDPHEHDQ7KHLV,GHQWLFDO PHWKRG WDNHVDQ(-%REMHFWUHIHUHQFHDQGFRPSDUHVLWWRWKH(-%REMHFWRQZKLFK \RXLQYRNHGLV,GHQWLFDO DQGUHWXUQVWUXHRUIDOVH LQWHUIDFH!! (-%2EMHFW 2EMHFWJHW3ULPDU\.H\ (-%+RPHJHW(-%+RPH +DQGOHJHW+DQGOH YRLGUHPRYH ERROHDQLV,GHQWLFDO 2EMHFWR \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Page 28 Return to Table of Contents WKHJHW+DQGOH PHWKRG 9OURE SHOPPING )TS TOUGH BECAUSE YOU CANT DECIDE WHETHER YOURE A SPRING OR A SUMMER 9OU DONT WANT TO BE RUSHED BUT YOUVE ALREADY GOT A BUNCH OF STUFF IN YOUR CART WHEN YOU REALIZE YOURE lVE MINUTES LATE FOR WORK 7VTQVM[PWXXQVO[PW]TLVW\JMZ][PML 9OUD LOVE TO CONTINUE WITH YOUR SHOPPING ONCE YOU GET TO WORK 7HAT DO YOU DO )F IT WERE !MAZON YOUR SHOPPING CART WOULD STILL BE THERE WHEN YOU LOG IN FROM THE 7EB "UT THIS IS A PROPRIETARY 3WING BASED SHOPPING CLIENT APP YOURE USING (OW CAN YOU GET YOUR %*" OBJECT STUB FROM YOUR HOME MACHINE TO YOUR WORK MACHINE 9OU COULD TRY SERIALIZING THE STUB 9EAH THAT MIGHT WORK 4HEN AGAIN IT MIGHT NOT 4HE STUB HAS A LIVE NETWORK CONNECTION AND THERES CERTAINLY NO GUARANTEE YOU CAN GET THAT SAME CONNECTION TO THE SAME %*" OBJECT AGAIN !ND SINCE THAT %*" OBJECT IS THE COMPONENT INTERFACE FOR YOUR OWN PERSONAL TEMPORARY SHOPPING CART BEAN YOU NEED A WAY TO GET BACK TO YOUR EXACT SAME %*" OBJECT AGAIN FROM WORK +PPPP\QH[WGRRU QHLJKERUVD\VFRZER\ERRWV DUHVH[\%XWWKHVDQGDOVZLOOPDNH PHORRNPRUHVHQVLWLYH7KHQDJDLQ JLUOVOLNHWKDWUXJJHGORRNVRPD\EH ,VKRXOGJRZLWKWKHKLNLQJERRWV 6KRSSLQJLVVRWLPHFRQVXPLQJ LQWHUIDFH!! (-%2EMHFW 2EMHFWJHW3ULPDU\.H\ (-%+RPHJHW(-%+RPH +DQGOHJHW+DQGOH YRLGUHPRYH ERROHDQLV,GHQWLFDO 2EMHFWR &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 29 WKHFOLHQWYLHZ <PIVSN]TTa_M¼^MOW\PIVLTM[ handle handle handle handle LQWHUIDFH!! +DQGOH JHW(-%2EMHFW no handle ! HANDLE CAN RESCUE YOUR SHOPPING EXPERIENCE !SK THE BEAN VIA THE %*"/BJECT INTERFACE FOR A HANDLE +DQGOHP\+DQGOH P\&DUWJHW+DQGOH SERIALIZE IT EMAIL IT TO YOURSELF THEN DESERIALIZE ON YOUR WORK MACHINE AND YOURE BACK IN BUSINESS 4HE HANDLE IS A 3ERIALIZABLE THING THAT KNOWS HOW TO GET BACK TO THE STUB )T HAS A SINGLE METHOD SXEOLF(-%2EMHFWJHW(-%2EMHFW 3O WHEN YOU CALL IT YOU HAVE TO CAST AND NARROW THE STUB THAT COMES BACK 2EMEMBER YOU ALWAYS HAVE TO CAST AND NARROW A STUB UNLESS THE METHOD THAT RETURNS IT HAS THE ACTUAL 2EMOTE INTERFACE AS ITS DECLARED RETURN TYPE 3INCE THE HANDLES METHOD HAS NO FRICKIN CLUE WHAT YOUR COMPONENT INTERFACE IS SAY 3HOPPING#ART YOURE FACED WITH THE SAME SCENARIO YOU HAD WITH THE HOME STUB YOU GOT FROM THE *.$) LOOKUP METHOD #AST AND NARROW #AST AND NARROW #AST AND NARROW $KDQGOHLVD6HULDOL]DEOH REMHFWWKDWNQRZVKRZWRJHW EDFNWRWKHRULJLQDO5HPRWH (-%REMHFW,W·VQRWDVWXE EXWLWFDQ*(7WKHVWXE ,WKDVMXVWRQHPHWKRG JHW(-%2EMHFW WKDWUHWXUQV W\SH2EMHFW 6R\RXKDYHWRQDUURZDQG FDVWWKHVWXE\RXJHWEDFN )N YOUR CLIENT CODE YOULL HAVE SOMETHING LIKE \RXUFRGHWRJHWWKHVHULDOL]HGKDQGOH\RXVDYHGHDUOLHU +DQGOHK WKLVUHVWRUH7KH+DQGOH QRZXVHLWWRJHWWKH(-%2EMHFWVWXE 2EMHFWR KJHW(-%2EMHFW 6KRSSLQJFDUW 6KRSSLQJ 3RUWDEOH5HPRWH2EMHFWQDUURZ R6KRSSLQJFODVV \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 30 (-%+DQGOHV :DLWDPLQXWHLVQ·WDKDQGOHDELJ VHFXULW\SUREOHP"<RXDOUHDG\VDLG,FRXOG VHULDOL]HDKDQGOHDQGSXWLWRQDQRWKHUPDFKLQHVR ZKDW·VWRVWRSPHIURPJLYLQJWKHKDQGOHWRVRPHRQH HOVH"6RPHRQHZKRGLGQ·WKDYHDFFHVVWRWKHVWXELQWKH ÀUVWSODFH"$QGDQRWKHUWKLQJEXJVPHDERXWKDQGOHV ,KRSH\RX·UHQRWWHOOLQJPHWKHVHUYHUKDVWRNHHS VKRSSLQJFDUWVDURXQGIRUHYHUMXVWLQFDVHD FOLHQWFRPHVEDFNXVLQJDKDQGOH *RRGE\HVFDODELOLW\ 'RQ·WZRUU\<RXFDQ·WXVHDKDQGOHDVD ZD\WRYLRODWH\RXUEHDQ·VVHFXULW\ 9OUR SECURITY IS ON A METHOD BY METHOD BASIS SO EVEN IF YOU GIVE A HANDLE TO SOMEONE ELSE IF THAT CLIENT DOESNT HAVE AUTHORIZATION TO CALL METHODS ON THE BEAN THE STUB THEY GET BACK FROM THE HANDLE WILL BE USELESS -XVWEHFDXVH\RXVWLOOKDYHDKDQGOHGRHVQ·W PHDQWKHVHUYHUVWLOOKDV\RXUEHDQ )F YOURE SHOPPING AND YOU GET A HANDLE AND THEN THE SERVER DETECTS THAT YOU HAVENT BEEN DOING ANYTHING WITH YOUR CART FOR A WHILE THE SERVER CAN TEMPORARILY SAVE YOUR BEAN KNOWN AS PASSIVATION TO CONSERVE RESOURCES BUT KEEP YOUR CART AROUND JUST IN CASE YOU COME BACK "UT IF YOU STILL DONT COME BACK WITHIN SOME TIME PERIOD THE SERVER WILL DESTROY YOUR CART WITH NO HOPE OF RESURRECTING IT 4HAT BEAN IS HISTORY )N THAT CASE YOUR CART WONT BE THERE WHEN YOU CALL GET%*"/BJECT ON THE HANDLE AND YOULL GET A 2EMOTE%XCEPTION &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 31 WKHFOLHQWYLHZ Q[1LMV\QKIT' PW_\WNQVLW]\QN\ _W[\]J[ ZMNMZ\W\PM[IUMJMIV LQWHUIDFH!! (-%2EMHFW These twins are identical if theyÕre stateLES can each do the S, since they clients wonÕt same thing and But if theyÕreknstow the difference. they are alway ateFUL, then twins cannot bes distinct. Stateful they can hold identical, because ormation spec to their own uninf ique client. iÞc 2EMHFWJHW3ULPDU\.H\ (-%+RPHJHW(-%+RPH +DQGOHJHW+DQGOH YRLGUHPRYH ERROHDQLV,GHQWLFDO 2EMHFWR )F YOUVE GOT TWO STUBS AND YOU WANT TO KNOW IF THEY REFER TO THE SAME BEAN YOU CALL IS)DENTICAL ON ONE REFERENCE PASSING IN THE REFERENCE YOU WANT TO COMPARE IT AGAINST *UST LIKE THE WAY YOU USE THE EQUALS METHOD 4HE TRICK IS STATELESS SESSION BEANS STATEFUL SESSION BEANS AND ENTITY BEANS EACH HAVE DIFFERENT RULES FOR WHAT CAUSES IS)DENTICAL TO RETURN TRUE 6WDWHOHVVVHVVLRQEHDQV 4RUE IF BOTH REFERENCES CAME FROM THE SAME HOME EVEN IF THE STUBS ARE REFERRING TO TWO DIFFERENT 2EMOTE %*" OBJECTS 4O THE SERVER ONE STATELESS BEAN IS AS GOOD AS ANY OTHER BEAN FROM THE SAME HOME BECAUSE THE CLIENT WOULD NEVER BE ABLE TO TELL THE DIFFERENCE SINCE THE BEAN CANT HOLD ANY CLIENT SPECIlC STATE 6WDWHIXOVHVVLRQEHDQV &ALSE NO MATTER WHAT FOR ANY TWO UNIQUE STUBS EVEN IF FROM THE SAME HOME !FTER ALL MY SHOPPING CART ISNT THE SAME AS YOURS (QWLW\EHDQV 4RUE IF THE STUBS REFER TO TWO ENTITIES WITH THE SAME PRIMARY KEY \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 32 WKHLV,GHQWLFDO PHWKRG 6WDWHOHVV EHDQV These beans ar all identical e EHDQ EHDQ EHDQ LV,GHQWLFDO DOZD\VUHWXUQVWUXH HYHQIRUGLIIHUHQWEHDQV 7KHLV,GHQWLFDO PHWKRGLV OLNHFDOOLQJD5HPRWHHTXDOV PHWKRGH[FHSW\RX·UHQRW DVNLQJLIWZRREMHFWVRQ\RXU KHDSDUHPHDQLQJIXOO\HTXLYDOHQW \RX·UHDVNLQJLIWZR5HPRWH REMHFWVDUHPHDQLQJIXOO\ HTXLYDOHQWRQWKHVHUYHU 6WDWHIXO EHDQV (QWLW\ EHDQV These beans are NEVER identical. Your cart is different from mine. EHDQ EHDQ EHDQ LV,GHQWLFDO DOZD\VUHWXUQVIDOVH IRUGLIIHUHQWEHDQVHYHQLIWKH EHDQVDUHIURPWKHVDPHKRPH These two are identical EHDQ EHDQ EHDQ LV,GHQWLFDO UHWXUQVWUXHIRUEHDQV WKDWUHIHUHQFHWKHVDPHHQWLW\ LQ RWKHUZRUGVWKHVDPHSULPDU\NH\ :HXVHWKHWHUP´EHDQµKHUHDOLWWOHORRVHO\EHFDXVH FRQFHSWXDOO\WKHVHUYHUXVHVRQO\RQHEHDQWR UHSUHVHQWDSDUWLFXODUHQWLW\6RWKHUHZRXOGEHRQO\ RQHEHDQZLWKDSULPDU\NH\RIEXWFOLHQWVPD\ KDYHPXOWLSOH(-%REMHFWUHIHUHQFHVWRLW &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 33 WKHFOLHQWYLHZ WKHUH DUH QR 'XPE 4XHVWLRQV Q: 7HY CANT YOU JUST USE THE EQUALS METHOD INSTEAD OF IS)DENTICAL )SNT THAT WHAT EQUALS IS FOR A: 2EMEMBER WERE TALKING ABOUT 2EMOTE OBJECTS 4HE EQUALS METHOD COMPARES TWO OBJECTS ON THE SAME (%!0 WHERE IS)DENTICAL COMPARES TWO 2EMOTE OBJECTS ON THE 3%26%2 Q: ) STILL DONT SEE WHY THEY COULDNT HAVE JUST IMPLEMENTED THE EQUALS METHOD ON THE STUB TO DO THE SAME THING A: 4HE EQUALS METHOD IS NOT A REMOTE METHOD FOR ONE THING 9OU CAN ALWAYS CALL EQUALS ON A STUB BECAUSE YOU CAN CALL IT ON ANY OBJECT ON YOUR HEAP "UT ITS NOT PART OF THE REMOTE INTERFACE SO IT CANT BE A REMOTE METHOD FOR EXAMPLE IT DOESNT DECLARE A 2EMOTE%XCEPTION ETC !ND REMEMBER THE EQUALS METHOD IS USED TO SEE IF TWO OBJECTS ON THE HEAP ARE MEANINGFULLY EQUIVALENT 4HE VENDOR CAN IMPLEMENT THE EQUALS METHOD ON THE STUB ANY WAY IT LIKES BUT THAT STILL DOESNT TELL YOU ANYTHING ABOUT WHATS GOING ON BACK AT THE SERVER END *UST BECAUSE TWO STUBS DONT PASS THE EQUALS TEST DOESNT MEAN THE SERVER DOESNT CONSIDER THE TWO %*" OBJECTS TO BE IDENTICAL OR REFERENCING IDENTICAL BEANS 9OUR SERVER MAY BE USING 2-) STUBS FOR EXAMPLE THAT HAVE NO LOGIC FOR HOW THEIR COMPARISONS RELATE TO MEANINGFUL COMPARISONS OF TWO %*" OBJECTS ON THE SERVER 2-) STUBS KNOW ABOUT 2EMOTE OBJECTS BUT THEY DONT KNOW WHAT THOSE 2EMOTE OBJECTS REPRESENT Q: (OW COME THERES A METHOD IN THE %*"/BJECT INTERFACE FOR GETTING THE BEANS HOME )F YOU DONT (!6% THE HOME THEN HOW DID YOU GET THE BEAN IN THE lRST PLACE A: 4HERE ARE OTHER WAYS TO GET A REFERENCE TO AN %*" OBJECT )TS TRUE THAT YOU CANT USE *.$) TO LOOK UP THE %*" OBJECT ONLY THE HOME IS REGISTERED "54 THERES NOTHING TO STOP YOU FROM PASSING AN %*" OBJECT REFERENCE AS AN ARGUMENT OR RETURN VALUE 9OU MIGHT HAVE A BUSINESS METHOD IN ONE BEAN WHOSE SOLE JOB IS TO HAND YOU BACK A REFERENCE TO AN %*" OBJECT FOR A DIFFERENT BEAN .OW SUPPOSE YOU HAVE THIS %*" OBJECT REFERENCE TO A BEAN WHOSE HOME YOU NEVER HAD AND NOW YOU WANT TO MAKE MORE OF THOSE BEANS FOR YOURSELF 9OU CAN DO THAT BY ASKING FOR THE BEANS HOME USING GET%*"(OME !ND EVEN IF YOU DO HAVE ENOUGH INFORMATION TO DO A LOOKUP IN *.$) FOR THAT BEANS HOME *.$) LOOKUPS ARE EXPENSIVE 9OULL SAVE SOME OVERHEAD IF YOU JUST GET THE HOME REFERENCE FROM THE BEAN DIRECTLY Q: 7HY CANT YOU SERIALIZE THE STUB 7HY DO WE NEED HANDLES A: 7E DIDNT SAY THE STUB WASNT 3ERIALIZABLE "UT EVEN IF YOU CAN SERIALIZE IT THAT DOESNT MEAN ITS GOT ENOUGH INFORMATION TO GET YOU BACK TO THE SAME OR MEANINGFULLY IDENTICAL %*" OBJECT 7HEN THE STUB COMES OVER FROM THE SERVER ITS ALREADY KNOWLEDGEABLE ABOUT HOW TO CONTACT A PARTICULAR 2EMOTE OBJECT 7HEN THAT STUB IS RECREATED THAT EXACT 2EMOTE OBJECT MIGHT NOT EVEN EXIST ANY LONGER Q: A: 4HEN HOW WOULD THE HANDLE BE ANY BETTER 4HE HANDLE HAS THE @SMARTS TO COMMUNICATE WITH THE SERVER AND GET BACK SOMETHING THAT IS JUST THE SAME AS THE %*" OBJECT YOU HAD BEFORE )N OTHER WORDS IT MIGHT NOT BE THE SAME %*" OBJECT BUT THE CLIENT WILL NEVER BE ABLE TO TELL THE DIFFERENCE \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 34 WKH5HPRWHLQWHUIDFHV :MDD=LHGAFLK <RXH[SRVH\RXUEHDQ·VEXVLQHVVPHWKRGVLQWKH FRPSRQHQWLQWHUIDFH 5HPRWHFRPSRQHQWLQWHUIDFHVPXVWH[WHQG MDYD[HME(-%2EMHFW URSTATELESS HOME )N YO MORE E AND NO WRITE ON L SEE IL W T N IE BUT THE CL R IONAL FOU AN ADDIT S= ;METHOD ALIVE N THEY ARE RED WHE RE A TS FIVE S E G SE 2O E CLIENT /BJECT TH &ROM %*" S= ;METHOD ,Q(-%WKHUH ·V FDQDQGWKHU H·V FDQ 2QWKHH[DPVR PHIRONVDUHFRQ IXVHGE\ WKHZRUG³FDQ´D VLQ³&DQDVHVV LRQEHDQ FOLHQWFDOOJHW3ULP DU\.H\ RQWKH( -% REMHFWVWXE"´ :DWFKLW 'HSHQGVRQKRZ \RXGH¿QH³FDQ ´$QG IRUWKHH[DPWK HDQVZHUZRXOG EH12 ,QRWKHUZRUGVMX VWEHFDXVHVRP HWKLQJ LVOHJDOO\H[SRV HGWRWKHFDOOHU ZKLFK PHDQVWKHFDOOH UFDQ&203,/( ZLWKWKH PHWKRGFDOOLIWK HVSHFVD\V\RX FDQ¶W WKHQ\RXFDQ¶W6 RMXVWEHFDXVH\R XFDQ FRPSLOH GRHVQ¶W PHDQ\RXFDQ DFFRUG LQJWRWKHVSHF 5HPHPEHUWKH UH¶VFRP SLOHUODZDQGWK HQWKHUH¶V(-% VSHFODZ 2QWKHH[DPZ H¶UHORRNLQJIRU (-%ODZ,I WKHTXHVWLRQ'2( 6LQYROYHFRPSLO DWLRQ \RX¶OONQRZIURP WKHZRUGLQJ 7KHFOLHQWJHWVDUHIHUHQFHWRWKHEHDQ·V(-%2EMHFW E\FDOOLQJDPHWKRGRQWKHEHDQ·VKRPHLQWHUIDFH 5HIHUHQFHVWRERWKVWDWHOHVVDQGVWDWHIXOVHVVLRQ EHDQVDUHUHWULHYHGIURPWKHKRPH·VFUHDWH PHWKRGV )URPWKH(-%2EMHFWLQWHUIDFHWKHFOLHQWVHHVILYH DGGLWLRQDOPHWKRGVJHW(-%+RPH JHW+DQGOH UHPRYH LV,GHQWLFDO DQGJHW3ULPDU\.H\ 2QO\HQWLW\EHDQFOLHQWVDUHDOORZHGWRFDOO JHW3ULPDU\.H\ RQWKHEHDQ·VFRPSRQHQW LQWHUIDFH6HVVLRQEHDQFOLHQWVZLOOJHWD 5HPRWH([FHSWLRQ 7KHJHW(-%+RPH PHWKRGUHWXUQVDUHIHUHQFHWR WKHEHDQ·VKRPHLQWHUIDFHVRWKDWWKHFOLHQWGRHVQ·W KDYHWRJRWKURXJKD-1',ORRNXSLIWKH\ZDQWWR PDNHPRUHEHDQVRIWKDWW\SH 7KHJHW+DQGOH PHWKRGUHWXUQVD6HULDOL]DEOH REMHFWWKDWFDQEHXVHGODWHUWRUHHVWDEOLVKFRQWDFW ZLWKWKHVHUYHUDQGJHWEDFNWKHVWXEWRWKH FRPSRQHQWLQWHUIDFHWKDWWKHFOLHQWXVHGWRJHWWKH KDQGOH 7KHKDQGOHKDVRQHPHWKRGJHW(-%2EMHFW WKDW UHWXUQVWKH5HPRWHVWXEDVW\SH(-%2EMHFW7KDW PHDQVWKHVWXEPXVWEHFDVWDQGQDUURZHGMXVWDV \RXPXVWGRZLWKWKHKRPHVWXEWKDW\RXJHWIURPD -1',ORRNXS 7KHLV,GHQWLFDO PHWKRGLVNLQGRIOLNHGRLQJDQ HTXDOV PHWKRGRQWKHVHUYHU,WUHWXUQVWUXHIRU WZRGLIIHUHQWVWDWHOHVVEHDQVIURPWKHVDPHKRPH IDOVHIRUWZRGLIIHUHQWVWDWHIXOEHDQVIURPWKHVDPH KRPHDQGWUXHIRUUHIHUHQFHVWRHQWLWLHVZLWKWKH VDPHSULPDU\NH\ &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 35 WKHFOLHQWYLHZ )JMIV¼[KTQMV\QV\MZNIKM[KIVJMTWKIT 7EVE LOOKED AT ONLY THE 2EMOTE CLIENT INTERFACES FOR A BEAN SO FAR BUT AS OF %*" SESSION AND ENTITY BEANS CAN EXPOSE A LOCAL CLIENT VIEW )N OTHER WORDS CLIENT INTERFACES THAT DO NOT EXTEND JAVARMI2EMOTE 7HAT DOES THIS MEAN 4HAT THE HOME OBJECT AND %*" OBJECT ARE NOT 2EMOTE OBJECTS 4HEYRE RUNNING IN THE SAME *6- AS THE CLIENT AND THE BEAN )N THE ENTITY BEAN #-2 CHAPTER WELL LOOK AT WHY LOCAL INTERFACES WERE ADDED TO THE SPEC &OR NOW THINK OF THEM AS A VERY SPECIAL CASE 5HPRWHFOLHQWYLHZ LQWHUIDFH!! 5HPRWH LQWHUIDFH!! 5HPRWH LQWHUIDFH!! (-%+RPH PHWKRGV LQWHUIDFH!! (-%2EMHFW PHWKRGV LQWHUIDFH!! $GYLFH+RPH LQWHUIDFH!! $GYLFH JHW$GYLFH FUHDWH /RFDOFOLHQWYLHZ LQWHUIDFH!! (-%/RFDO+RPH PHWKRGV LQWHUIDFH!! (-%/RFDO2EMHFW PHWKRGV LQWHUIDFH!! $GYLFH+RPH/RFDO LQWHUIDFH!! $GYLFH/RFDO JHW$GYLFH FUHDWH The local interfaces do NOT extend java.rmi.Remote is (and our naming convention not required... but you neeishd it SOMETHING to distingu ces) from your Remote interfa YiX`e gfn\i 7KH5HPRWHLQWHUIDFHV(-%+RPHDQG(-%2EMHFWKDYHPRUHPHWKRGV WKDQWKHORFDOLQWHUIDFHV(-%/RFDO+RPHDQG(-%/RFDO2EMHFW)OLS EDFNWKURXJKWKLVFKDSWHUDQGORRNDWWKHPHWKRGVIRU(-%+RPH DQG(-%2EMHFWDQGWU\WRZRUNRXWZKLFKPHWKRGVLQWKRVH5HPRWH LQWHUIDFHVPLJKWEHLQDSSURSULDWHRUQRWQHHGHGLQWKHORFDOLQWHUIDFHV 7KLQNDERXWWKHLPSOLFDWLRQVRIKDYLQJWKHLQWHUIDFHVORFDOWRWKHFOLHQW \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 36 5HPRWHYVORFDOFOLHQWYLHZ FOLHQWKHDS VWXE VHUYLFHV &RPSRQHQWLQWHUIDFH (-% REMHFW EHDQ HV VHUYLF VWXE +RPHLQWHUIDFH FOLHQW +RPHLQWHUIDFH &RPSRQHQWLQWHUIDFH 5(027(FOLHQWYLHZ +RPH REMHFW VHUYHUKHDS (-% REMHFW EHDQ +RPH REMHFW HV VHUYLF FOLHQW VHUYLFHV No stubs!! +RPHLQWHUIDFH The client still canÕt get to the bean directly, because the server still needs a place to intercept the call to the bean (so the server can add services). But this time, the client has a local reference to the home and component interface objects. &RPSRQHQWLQWHUIDFH /2&$/FOLHQWYLHZ VHUYHUKHDS &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 37 WKHFOLHQWYLHZ 6KDUSHQ \RXU SHQFLO *LYHQWKDWWKHFOLHQWKDVDSODLQROGHYHU\GD\-DYD UHIHUHQFHWRWKHKRPHDQGFRPSRQHQWLQWHUIDFHV LHWKHKRPHREMHFWDQGWKH(-%REMHFW ZKLFK RIWKH5HPRWHLQWHUIDFHPHWKRGVGR\RXWKLQNDUH DSSURSULDWHIRUWKHORFDOFOLHQWYLHZ" ,QRWKHUZRUGVZKLFKPHWKRGVRI(-%+RPHDUHQRWLQ (-%/RFDO+RPHDQGZKLFKPHWKRGVRI(-%2EMHFWDUHQRWLQ (-%/RFDO2EMHFW":K\" 2IWKHIRXUPHWKRGVLQ (-%+RPH(-%/RFDO+RPH KDVRQO\RQH:KLFKRQH" LQWHUIDFH!! LQWHUIDFH!! (-%+RPH (-%/RFDO+RPH (-%0HWD'DWDJHW(-%0HWD'DWD +RPH+DQGOHJHW+RPH+DQGOH 1 BBBBBBBBBBBBBBBBBBBBBBBBB YRLGUHPRYH +DQGOHK YRLGUHPRYH 2EMHFWNH\ 2IWKH¿YHPHWKRGVLQ(-%2EMHFW (-%/RFDO2EMHFWKDVRQO\IRXU DQG RQHRIWKHIRXULVVOLJKWO\GLIIHUHQW :KLFKIRXU" LQWHUIDFH!! LQWHUIDFH!! (-%2EMHFW (-%/RFDO2EMHFW 2EMHFWJHW3ULPDU\.H\ (-%+RPHJHW(-%+RPH +DQGOHJHW+DQGOH YRLGUHPRYH ERROHDQLV,GHQWLFDO (-%2EMHFWR 1 2 3 4 BBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBB \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Page 38 Return to Table of Contents ORFDOLQWHUIDFHV ?PQKPUM\PWL[UISM[MV[MNWZ\PMTWKITKTQMV\QV\MZNIKM[' 9OU ALREADY KNOW THAT THE LOCAL CLIENT INTERFACES ARE MISSING SOME OF THE METHODS FROM THEIR 2EMOTE COUNTERPARTS ,ETS lGURE OUT WHICH ONES ARE MISSING AND WHY ,W_MVMMLPIVLTM[_Q\PTWKITQV\MZNIKM[' 2EMEMBER WHY HANDLES EXIST IN %*"TO GIVE YOU A 3ERIALIZABLE OBJECT THAT YOU CAN USE TO RE ESTABLISH A STUB TO THE %*" OBJECT YOUD BEEN WORKING WITH 4HE HANDLE IS JUST AN ABSTRACTION OF A REMOTE CONNECTION 3O DOES THIS MAKE SENSE ON A LOCAL CLIENT ,W_MVMML-2*5M\I,I\I_Q\PTWKITQV\MZNIKM[' 2EMEMBER WHAT %*"-ETA$ATA IS USED FORTO GET REmECTION LIKE INFO ABOUT A BEAN )F YOU CALL GET%*"-ETA$ATA ON A BEANS 2EMOTE HOME YOU GET BACK AN OBJECT THAT IMPLEMENTS %*"-ETA$ATA 4HAT INTERFACE %*"-ETA$ATA HAS METHODS THAT LET YOU INTERROGATE THE BEAN AND LEARN MORE ABOUT THE CLASSES THAT MAKE UP THE COMPONENT 7OULD A LOCAL CLIENT EVER NEED %*"-ETA$ATA ,W_MVMMLQ[1LMV\QKIT_Q\PTWKITQV\MZNIKM[' 2EMEMBER WHY IS)DENTICAL EXISTSTO LET YOU COMPARE TWO HOME OR COMPONENT INTERFACE REFERENCES TO SEE IF THEY REFER TO hMEANINGFULLY EQUIVALENTv BEANS ON THE SERVER 7OULD YOU NEED TO USE IS)DENTICAL ON A LOCAL CLIENT "IG (INT THE SERVER IS FREE TO IMPLEMENT EQUALS ANY WAY IT CHOOSES ,W_MVMMLXZQUIZaSMaQVNWZUI\QWV_Q\PTWKITQV\MZNIKM[' 2EMEMBER WHY PRIMARY KEYS EXISTTO UNIQUELY IDENTIFY ENTITY BEANS 7OULD YOU EVER NEED TO IDENTIFY AN ENTITY BEAN ON A LOCAL CLIENT ,W_MVMMLZMUW^MUM\PWL[_Q\PTWKITQV\MZNIKM[' 2EMEMBER WHAT REMOVE IS USED FORTO TELL THE CONTAINER THAT YOURE DONE WITH A BEAN FOR 3ESSION BEANS OR TO TELL THE CONTAINER TO PERMANENTLY DELETE THE ENTITY FOR ENTITY BEANS 7OULD A LOCAL CLIENT NEED TO CALL REMOVE ON A BEAN &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 39 WKHFOLHQWYLHZ ?PMVaW]\PQVSPIVLTM\PQVS:MUW\M 4WKITKTQMV\[LWV¼\VMMLPIVLTM[ ,OCAL CLIENTS HAVE NO USE FOR A HANDLE BECAUSE HANDLES ARE STRICTLY FOR GETTING A SAVABLE 3ERIALIZABLE OBJECT THAT KNOWS HOW TO REESTABLISH COMMUNICATION WITH THE 2EMOTE OBJECT A local client Java referencehatos a plain old the home or component thereÕs no needinterface, so for handles. no stubs, no handles VHUYLFHV /2&$/FOLHQWYLHZ (-% REMHFW EHDQ +RPH REMHFW HV VHUYLF FOLHQW ?PWVMML[-2*5M\I,I\I_PMV aW]¼^MOW\ZMNTMK\QWV' 4WKITKTQMV\[LWV¼\VMML-2*5M\I,I\I 7ITH THE *AVA REmECTION !0) YOU CAN INTERROGATE AN OBJECT TO GET ALL SORTS OF INFORMATION ABOUT ITS CLASS 7ITH 2EMOTE OBJECTS YOU DONT HAVE THAT OPTION BECAUSE YOU CANT GET A REFERENCE TO THE CLASS OF THE 2EMOTE OBJECT 4HE ONLY THING YOU CAN INTERROGATE ON A 2EMOTE CLIENT ARE THE STUB OBJECTS BUT THEY CANT TELL YOU ANYTHING ABOUT THE REAL %*" OBJECT OR (OME OBJECT 3O WHILE A 2EMOTE HOME CLIENT HAS TO USE %*"-ETA$ATA THE INTERFACE RETURNED FROM THE %*"(OME GET%*"-ETA$ATA METHOD TO GET INFO A LOCAL CLIENT WILL SIMPLY USE THE *AVA REmECTION METHODS GET#LASS ETC \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 40 ORFDOLQWHUIDFHV ,WaW]VMMLQ[1LMV\QKIT_PMV\PMZM¼[MY]IT[ ' 4WKITKTQMV\[[\QTTVMMLQ[1LMV\QKIT 2EMEMBER FOR A 2EMOTE CLIENT THE ONLY LOCAL COMPARISON YOU DO IS ON TWO STUB OBJECTS USING EQUALS 4HIS DOESNT WORK WHEN YOU WANT TO COMPARE SOMETHING BACK ON THE SERVER IN THIS CASE THE TWO %*" OBJECT REFERENCES 4HATS WHAT IS)DENTICAL IS FOR "UT LOCAL CLIENTS HAVE THE REAL THING 4HEY HAVE THE REAL REFERENCE TO THE %*" OBJECT SO THEY CAN USE EQUALS TO SEE IF TWO %*" OBJECT LOCAL REFERENCES ARE MEANINGFULLY EQUIVALENT "UT THATS STILL NOT WHAT YOU WANT 4HERE IS NO GUARANTEE IN THE SPEC FOR THE RESULTS YOULL GET WITH EQUALS 3O WHILE IT SEEMS LIKE YOU COULD JUST USE EQUALS RATHER THAN IS)DENTICAL WITH A LOCAL CLIENT THE SPEC DOES NOT GUARANTEE THAT THE RESULTS WILL BE THE SAME "OTTOM LINE IF YOU WANT TO KNOW IF TWO %*" OBJECT REFERENCES ARE REFERENCING THE SAME SESSION OBJECT YOU HAVE TO USE IS)DENTICAL EVEN WHEN THE %*" OBJECT IS LOCAL 5(027(FOLHQWYLHZ FOLHQW (-% REMHFW VWXE VHUYLFHV LV,GHQWLFDO LV,GHQWLFDO EHDQ A Remote client has to make a Re method call, isIde mote meaningful comp ntical(), to get ar iso n inf o about an EJB object. wonÕt tell youCaanlling equals() on a stub ything useful. /2&$/FOLHQWYLHZ HTXDOV FOLHQW LV,GHQWLFDO (-% REMHFW VHUYLFHV cal() on () and isIdenti Calling equals object reference are e a local EJBteed to give you the sam not guaran still need isIdentical() result. You e if two EJB object to determinare to the same Session references object. EHDQ &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 41 WKHFOLHQWYLHZ ?Pa[WUIVaZMUW^MUM\PWL[' .WZ:MUW\MKTQMV\[\ _WQV\PMPWUMXT][WVMQV\PM KWUXWVMV\QV\MZNIKM 2EMEMBER 2EMOTE CLIENTS HAVE THREE REMOVE METHODS AVAILABLE TWO IN THE HOME AND ONE IN THE COMPONENT INTERFACE 4HE REMOVE THAT COMES FROM %*"/BJECT IS SIMPLE IF YOU CALL IT YOURE SAYING YOU WANT TO REMOVE THAT VERY BEAN )N OTHER WORDS THE BEAN WHOSE %*"/BJECT YOU USED TO CALL REMOVE !ND FOR SESSION BEANS REMEMBER CALLING REMOVE SIMPLY TELLS THE CONTAINER THAT YOURE DONE WITH THE BEAN )TS GOOD MANNERS AND IT IMPROVES SCALABILITY SINCE THE SERVER CAN STOP KEEPING CLIENT SPECIlC RESOURCES ON YOUR BEHALF RATHER THAN WAITING SAY FOR YOUR SHOPPING SESSION TO TIME OUT FROM INACTIVITY "UT THINGS ARENT SO SIMPLE WHEN YOU CALL REMOVE ON A HOME &OR ONE THING YOU ACTUALLY CANT REMOVE A HOME 4HE SERVER KEEPS THE BEAN HOME ALIVE WHETHER YOURE AROUND OR NOT SO THERES NO SIGNIlCANT CLIENT SPECIlC RESOURCES 4HERES NO NEED TO TELL THE SERVER YOURE DONE WITH THE HOME BECAUSE THE SERVER WOULD SIMPLY SAY h3O WHATv 4HEN WHAT DOES IT MEAN TO CALL REMOVE ON A HOME )T MEANS YOURE TELLING THE HOME TO REMOVE ONE OF THE BEANS THAT CAME FROM THAT HOME !ND THAT MEANS YOU HAVE TO IDENTIFY WHICH BEAN YOURE TALKING ABOUT 5(027(FOLHQWYLHZ LQWHUIDFH!! (-%+RPH (-%0HWD'DWDJHW(-%0HWD'DWD +RPH+DQGOHJHW+RPH+DQGOH YRLGUHPRYH +DQGOHK YRLGUHPRYH 2EMHFWNH\ LQWHUIDFH!! (-%2EMHFW 2EMHFWJHW3ULPDU\.H\ (-%+RPHJHW(-%+RPH +DQGOHJHW+DQGOH YRLGUHPRYH ERROHDQLV,GHQWLFDO 2EMHFWR ainer ves tell the cont These two remoeciÞc bean from that to remove a sp have to say WHICH home, but you her the handle (session bean using eit s), or primary key or entity beanonly). (entity beans This version tells the container to remove the be EJBObject you an on whose invoked remove() . \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 42 UHPRYHPHWKRGV :K\WKHUH·VQRWDQRDUJUHPRYH PHWKRGLQWKHKRPH +H\KRPLH,·G OLNHWRUHPRYHRQHRIWKH EHDQV\RXMXVWJDYHPH 7KDQNV 5RJHUWKDW 0U&OLHQW,·OOWHOOWKH FRQWDLQHUULJKWQRZ :KLFKEHDQ"""<RX·YHJLYHQRXW OLNHDPLOOLRQEHDQVVRIDUDQG,KDYH QRLGHDZKLFKRQH\RXZDQWWRQXNH &RPHRQZRUNZLWKPHKHUH+RPLH, QHHGVRPHWKLQJWKDWXQLTXHO\LGHQWLÀHV WKHEHDQ2WKHUZLVH,·OOMXVWSLFNRQHDW UDQGRPDQGNLOOLW$QGZHUHDOO\GRQ·W ZDQWWKDWQRZGRZH" &RQWDLQHUSOHDVH UHPRYHDEHDQRQ EHKDOIRIWKLVFOLHQW &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Page 43 Return to Table of Contents WKHFOLHQWYLHZ 0W_KIVaW]][MIZMUW^M\PI\\ISM[I PIVLTM_PMVaW]LWV¼\PI^MIPIVLTM' 4WKITKTQMV\[LWV¼\PI^MPIVLTM[[WTWKITPWUM[ LWV¼\PI^MIZMUW^M\PI\\ISM[IPIVLTM <RXGRQ·WFDOOUHPRYHRQD KRPHWRUHPRYHWKHKRPH )F LOCAL HOME INTERFACES DONT HAVE HANDLES THEN THERES NO WAY YOU COULD HAVE A REMOVE METHOD THAT TAKES A HANDLE "ECAUSE IN ORDER TO PASS A HANDLE TO THE HOME THAT UNIQUELY IDENTIlES THE BEAN YOURE TRYING TO REMOVE YOUD HAVE TO lRST GET THE BEANS HANDLE !ND SINCE LOCALLY EXPOSED BEANS DONT HAVE HANDLES YOU SEE THE PROBLEM <RXFDOOUHPRYHRQDKRPH WRWHOOWKHKRPHWRUHPRYH DEHDQ WKHUH DUH QR 'XPE 4XHVWLRQV Q: 7HAT IF YOU HAVE A LOCAL HOME BUT YOU HAVE A 2EMOTE %*" OBJECT REFERENCE #ANT YOU PASS THE 2EMOTE BEANS HANDLE TO THE LOCAL HOME A: ./ "ECAUSE YOU CANT MIX LOCAL AND 2EMOTE INTERFACES TOGETHER /NLY A LOCAL BEAN COMES FROM A LOCAL HOME AND VICE VERSA 3O IT WILL .%6%2 BE POSSIBLE TO HAVE A 2EMOTE BEANS HANDLE TO GIVE TO THAT SAME 2EMOTE BEANS HOME UNLESS THAT HOME IS ALSO 2EMOTE 7E DIDNT SAY THAT VERY WELL DID WE /+ HOW ABOUT THISA 2EMOTE HOME WILL HAND OUT ONLY 2EMOTE REFERENCES TO THE COMPONENT %*"/BJECT INTERFACE FOR THAT BEAN TYPE AND A LOCAL HOME WILL ONLY RETURN LOCAL REFERENCES TO THE COMPONENT %*",OCAL/BJECT INTERFACE Q: A: 4ELL ME AGAIN WHY YOU CANT REMOVE THE HOME 4HERES NEVER A REASON TO REMOVE THE HOME IN OTHER WORDS TO TELL THE CONTAINER YOURE DONE WITH IT BECAUSE THE CONTAINER MUST KEEP THE HOME AROUND WITH OR WITHOUT YOUR INTEREST 3O IF YOU WERE ABLE TO SAY REMOVE TO THE HOME THE CONTAINER WOULD SAY h'EEDONT FLATTER YOURSELF BUDDY 7HAT ) DO HERE ON THE SERVER IS NOT !"/54 YOU ) COULD CARE LESS WHEN YOURE DONE WITH YOUR HOME REFERENCEv $EHDQIURPWKDWKRPHW\SH 7KDWPHDQV\RXPXVW XQLTXHO\LGHQWLI\WKHEHDQ \RXZDQWUHPRYHGZKHQ \RXFDOODKRPHUHPRYH PHWKRG)RUHQWLW\EHDQV XVHWKHSULPDU\NH\RUD KDQGOHDQGIRUVHVVLRQ EHDQVXVHDKDQGOH %XWKDQGOHVRQO\ZRUNIRU 5HPRWHKRPHFOLHQWV \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 44 FRPSDULQJORFDOYV5HPRWH +WUXIZQVO:MUW\M^[4WKITQV\MZNIKM[ 4HE %*"/BJECT AND %*"(OME INTERFACES HAVE MORE METHODS THAN THE %*",OCAL/BJECT AND %*",OCAL(OME INTERFACES BECAUSE THERE ARE METHODS THAT DONT MAKE SENSE IN A LOCAL CONTEXT 5(027(FOLHQWYLHZ /2&$/FOLHQWYLHZ LQWHUIDFH!! LQWHUIDFH!! (-%+RPH (-%/RFDO+RPH (-%0HWD'DWDJHW(-%0HWD'DWD (-%0HWD'DWDJHW(-%0HWD'DWD +RPH+DQGOHJHW+RPH+DQGOH +RPH+DQGOHJHW+RPH+DQGOH YRLGUHPRYH +DQGOHK YRLGUHPRYH +DQGOHK YRLGUHPRYH 2EMHFWNH\ YRLGUHPRYH 2EMHFWNH\ LQWHUIDFH!! (-%2EMHFW LQWHUIDFH!! (-%/RFDO2EMHFW 2EMHFWJHW3ULPDU\.H\ 2EMHFWJHW3ULPDU\.H\ (-%+RPHJHW(-%+RPH (-%/RFDO+RPHJHW(-%/RFDO+RPH +DQGOHJHW+DQGOH +DQGOHJHW+DQGOH YRLGUHPRYH YRLGUHPRYH ERROHDQLV,GHQWLFDO (-%2EMHFWR ERROHDQLV,GHQWLFDO 2EMHFWR Local clients can use reßection they donÕt need EJBMetaData., so No need for a ha ndle with a local clientÑthereÕs no stub! locally-accessed be ans donÕt have handles, so yo remove one. u canÕt use a handle to changes from Remote to local clients No handles in local &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 45 WKHFOLHQWYLHZ 6KDUSHQ \RXU SHQFLO %DVHGRQZKDW\RXQRZNQRZDERXWWKHGLIIHUHQFHEHWZHHQORFDO DQG5HPRWHFOLHQWLQWHUIDFHVGHFLGHLIWKHIROORZLQJVWDWHPHQWV DUHWUXHRUIDOVH<RX¶OOKDYHWRPDNHVRPHLQIHUHQFHVDQGVPDUW JXHVVHVIRUVRPHRIWKHP 6HOHFWDOOWKDWDUHWUXH R7KHRQO\ZD\WRUHPRYHDORFDOVHVVLRQEHDQLVWKURXJKWKHFRPSRQHQWLQWHUIDFH R(QWLW\EHDQVFDQEHUHPRYHGWKURXJKDORFDOKRPHLQWHUIDFH R,I\RXVHHDQLV,GHQWLFDO R,I\RXVHHDJHW+DQGOH FDOOWKLVPXVWEHDORFDOEHDQ FDOOWKLVPXVWEHD5HPRWHEHDQ R,IWKHFOLHQWLVFDWFKLQJD5HPRWH([FHSWLRQRQDKRPHPHWKRGWKHEHDQ¶VKRPH LQWHUIDFHPXVWH[WHQG(-%/RFDO+RPH R,IWKHFOLHQWLVQRWKDQGOLQJD5HPRWH([FHSWLRQRQDEXVLQHVVPHWKRGWKHEHDQ¶V FRPSRQHQWLQWHUIDFHPXVWH[WHQG(-%2EMHFW R,I\RXVHHDFDOOWRJHW(-%0HWD'DWD (-%/RFDO2EMHFW WKHEHDQ¶VFRPSRQHQWLQWHUIDFHPXVWH[WHQG R,I\RXGRD-1',ORRNXSRQDORFDOKRPH\RXPXVWQDUURZWKHREMHFWUHWXUQHGIURP-1', R7KHUHDUHWKUHHPHWKRGVLQWKH(-%/RFDO2EMHFWLQWHUIDFH R7KHUHDUHWZRPHWKRGVLQWKH(-%/RFDO+RPHLQWHUIDFH \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 46 ORFDOLQWHUIDFHV ?ZQ\QVO\PMTWKITKTQMV\QV\MZNIKM[ .OW THAT WEVE COVERED WHAT THE CLIENT SEES IN A LOCAL INTERFACE LETS LOOK AT YOUR RESPONSIBILITY AS A "EAN 0ROVIDER )N OTHER WORDS WHAT YOU HAVE TO DO TO WRITE THE LOCAL INTERFACES FOR YOUR BEAN ocalObject extend EJBLJBObject instead of E &RPSRQHQWLQWHUIDFH SDFNDJHKHDG¿UVW LPSRUWMDYD[HME SXEOLFLQWHUIDFH$GYLFH/RFDOH[WHQGV(-%/RFDO2EMHFW^ SXEOLF6WULQJJHW$GYLFH ` this return type doesnÕt need to be RMIIIOP compliant (although it certainly can be, of course, like String) No RemoteExcep tion!! +RPHLQWHUIDFH ocalHome extend EJBLJBHome E of instead SDFNDJHKHDG¿UVW LPSRUWMDYD[HME SXEOLFLQWHUIDFH$GYLFH+RPH/RFDOH[WHQGV(-%/RFDO+RPH^ SXEOLF$GYLFH/RFDOFUHDWH ` This MUST be the loc WKURZV&UHDWH([FHSWLRQ al component interface ! still needs a CreateException but no RemoteException 5XOHVIRUORFDOLQWHUIDFHV ,PSRUWMDYD[HME RUXVHIXOO\TXDOL¿HGQDPHV ([WHQG(-%/RFDO2EMHFW IRUWKHFRPSRQHQWLQWHUIDFH RU (-%/RFDO+RPH IRUWKHKRPHLQWHUIDFH 'HFODUHRQHRUPRUHEXVLQHVVPHWKRGVLQWKHFRPSRQHQWLQWHUIDFH $OOFUHDWHPHWKRGVLQWKHORFDOKRPHPXVWUHWXUQWKHORFDOFRPSRQHQWLQWHUIDFH DQGGHFODUHD&UHDWH([FHSWLRQ $Q\PHWKRG\RXGHFODUHLQWKHKRPHRUFRPSRQHQWLQWHUIDFHFDQGHFODUH\RXU RZQDSSOLFDWLRQH[FHSWLRQVZKLFKPXVWEHFRPSLOHUFKHFNHGH[FHSWLRQV LH QRWVXEFODVVHVRI5XQWLPH([FHSWLRQ <RXPXVW127GHFODUHD5HPRWH([FHSWLRQIRUDQ\PHWKRGV &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Page 47 Return to Table of Contents WKHFOLHQWYLHZ AW]KIVPI^MJW\PI:MUW\M IVLTWKITKTQMV\^QM_NWZIJMIV J]\aW]XZWJIJTa_WV¼\ 7E MENTIONED EARLIER THAT LOCAL INTERFACES ARE A VERY SPECIAL CASE FOR A CLIENT VIEW 4HEY WERE INTRODUCED WITH VERSION OF THE %*" SPEC WHICH THIS BOOK IS BASED ON AND THE ORIGINAL INTENT WAS TO SUPPORT CONTAINER MANAGED RELATIONSHIPS IN ENTITY BEANS "UT ENOUGH CUSTOMERS AND VENDORS ASKED FOR THE ABILITY TO HAVE NON 2EMOTE INTERFACES FOR BEANS SO THE *%% TEAM DECIDED TO MAKE IT AVAILABLE FOR SESSION BEANS AS WELL AS ENTITY BEANS "UT REGARDLESS OF WHICH YOU CHOOSE ITS VERY UNLIKELY YOULL HAVE A DESIGN THAT REQUIRES BOTH A LOCAL AND 2EMOTE CLIENT VIEW )F YOUR BEAN IS IN A CONTAINER MANAGED RELATIONSHIP WITH ANOTHER ENTITY BEAN YOULL LEARN ALL ABOUT THIS IN THE ENTITY CHAPTERS YOU HAVE NO CHOICE 4HE BEAN MUST EXPOSE ITSELF LOCALLY !ND IN THAT CASE ITS ALMOST IMPOSSIBLE TO THINK OF A REASON TO ALSO HAVE THAT SAME BEAN EXPOSED TO 2EMOTE CLIENTS FOR OTHER PURPOSES *UST KNOW THAT IT IS LEGAL TO HAVE BOTH "UT YOU CAN NEVER EVER EVER MIX AND MATCH ! 2EMOTE HOME INTERFACE CAN GIVE OUT ONLY 2EMOTE COMPONENT INTERFACE REFERENCES IN OTHER WORDS A STUB TO THE 2EMOTE %*"/BJECT ! LOCAL HOME CAN GIVE OUT ONLY LOCAL COMPONENT INTERFACE REFERENCES IN OTHER WORDS A REGULAR *AVA HEAP REFERENCE TO THE %*",OCAL/BJECT <RXFDQ·WPL[ORFDODQG 5HPRWHLQWHUIDFHVWRJHWKHU IRUWKHVDPHEHDQHYHQ WKRXJKDJLYHQEHDQFDQ H[SRVHERWKD5HPRWHDQG ORFDOFOLHQWYLHZ %XWD5HPRWHKRPHZLOOJLYH RXWRQO\5HPRWHFRPSRQHQW LQWHUIDFHUHIHUHQFHV VWXEV DQGDORFDOKRPHZLOOJLYH RXWRQO\ORFDOFRPSRQHQW LQWHUIDFHUHIHUHQFHV UHJXODU-DYDUHIHUHQFHV \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 48 ORFDOFOLHQWV 6KDUSHQ \RXU SHQFLO &KDQJHWKH$GYLFH&OLHQWIURPD5HPRWHFOLHQWWRDORFDOFOLHQW XVLQJWKHORFDOLQWHUIDFHVIRU$GYLFH/RFDODQG$GYLFH+RPH/RFDO 'R127WXUQWKHQH[WSDJH LPSRUWMDYD[QDPLQJ LPSRUWMDYDUPL LPSRUWMDYD[UPL LPSRUWKHDG¿UVW LPSRUWMDYD[HME SXEOLFFODVV$GYLFH&OLHQW^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ QHZ$GYLFH&OLHQW JR ` SXEOLFYRLGJR ^ WU\^ &RQWH[WLF QHZ,QLWLDO&RQWH[W 2EMHFWR LFORRNXS ³$GYLVRU´ $GYLFH+RPHKRPH $GYLFH+RPH 3RUWDEOH5HPRWH2EMHFWQDUURZ R$GYLFH+RPHFODVV $GYLFHDGYLVRU KRPHFUHDWH 6\VWHPRXWSULQWOQ DGYLVRUJHW$GYLFH `FDWFK ([FHSWLRQH[ ^ H[SULQW6WDFN7UDFH ` ` ` :KDWKDVWRFKDQJH":ULWHWKHFKDQJHGOLQH RUOLQHV EHORZ &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 49 WKHFOLHQWYLHZ -`KMX\QWV[QVKTQMV\QV\MZNIKM["_PI\\PMKTQMV\UQOP\OM\ ! 2EMOTE INTERFACE MUST HAVE 2EMOTE%XCEPTION DECLARED ON EVERY METHOD 4HAT MEANS THE CLIENT USING A 2EMOTE INTERFACE MUST DEAL WITH 2EMOTE%XCEPTION FOR EVERY 2EMOTE METHOD CALL "UT LOCAL INTERFACES DONT HAVE THAT RESTRICTION 4HE ONLY METHODS IN A LOCAL CLIENT INTERFACE THAT MUST DECLARE EXCEPTIONS ARE THE CREATE AND REMOVE METHODS FOR SESSION BEANS ENTITY BEANS ALSO HAVE A lNDER METHOD THAT DECLARES A &INDER%XCEPTION 7E HAVE A WHOLE CHAPTER DEVOTED TO EXCEPTIONS IN %*" SO WE WONT GO INTO THE DETAILS NOW BUT THE ESSENCE IS THIS IF A BEAN OR THE #ONTAINER GENERATES A RUNTIME EXCEPTION 2EMOTE CLIENTS SEE THE EXCEPTION AS A CHECKED 2EMOTE%XCEPTION BUT LOCAL CLIENTS SEE IT AS AN UNCHECKED %*"%XCEPTION )N ADDITION TO WHATEVER OTHER CHECKED EXCEPTIONS CALLED APPLICATION EXCEPTIONS IN %*" THE INTERFACE METHODS DECLARE ALL 2EMOTE INTERFACE METHODS CAN THROW A 2EMOTE%XCEPTION AND LOCAL CLIENT INTERFACE METHODS CAN ALWAYS THROW AN %*"%XCEPTION 3O 2EMOTE CLIENTS MUST WRAP ALL CALLS TO A HOME OR COMPONENT INTERFACE METHOD IN A TRYCATCH WHILE LOCAL CLIENTS USE A TRYCATCH ONLY IF THE INTERFACE METHOD DECLARES AN APPLICATION EXCEPTION WHICH INCLUDES #REATE%XCEPTION 2EMOVE%XCEPTION &INDER%XCEPTION AND ANY OTHER EXCEPTIONS THE "EAN 0ROVIDER DECLARES IN THE METHODS OF THE BEANS CLIENT INTERFACES LNDICATES A COMPILER CHECKED EXCEPTION IE NON 2UNTIME%XCEPTION 5(027(FOLHQWYLHZ /2&$/FOLHQWYLHZ $//PHWKRGV MDYD[HME5HPRWH([FHSWLRQ &5($7(PHWKRGV MDYD[HME5HPRWH([FHSWLRQ MDYD[HME&UHDWH([FHSWLRQ MDYD[HME&UHDWH([FHSWLRQ 5(029(PHWKRGV MDYD[HME5HPRWH([FHSWLRQ MDYD[HME5HPRYH([FHSWLRQ MDYD[HME5HPRYH([FHSWLRQ MDYD[HME(-%([FHSWLRQ MDYD[HME(-%([FHSWLRQ MDYD[HME(-%([FHSWLRQ \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 50 ORFDOFOLHQWV 4WKITKTQMV\KWLM #OMPARE THIS TO THE CODE MODIlCATIONS YOU MADE ON THE PREVIOUS SHARPEN 4O HELP SHOW THAT THE CALLS TO THE HOME AND COMPONENT INTERFACE ARE NO LONGER 2EMOTE WEVE MADE THE EXCEPTION HANDLING MORE lNE GRAINED .OTICE THAT WERE NOT CATCHING A 2EMOTE%XCEPTION LPSRUWMDYD[QDPLQJ LPSRUWKHDG¿UVW LPSRUWMDYD[HME we got rid of javax.rmi and java.rmi imports SXEOLFFODVV$GYLFH/RFDO&OLHQW^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ QHZ$GYLFH/RFDO&OLHQW JR ` SXEOLFYRLGJR ^ 2EMHFWR QXOO WU\^ &RQWH[WLF QHZ,QLWLDO&RQWH[W R LFORRNXS ³$GYLVRU/RFDO´ e lookup the DI, and do the to a real JN h ug ro th to go ferenc You still have ly this time you get a cere of EJBLocalHome), usual way, onon the heap (an instan Home object.. Java object a stub to a Remote EJB instead of `FDWFK 1DPLQJ([FHSWLRQQH[ ^ QH[SULQW6WDFN7UDFH ` $GYLFH+RPH/RFDOKRPH $GYLFH+RPH/RFDO R $GYLFH/RFDODGYLVRU QXOO WU\^ DGYLVRU KRPHFUHDWH `FDWFK &UHDWH([FHSWLRQFH[ ^ FH[SULQW6WDFN7UDFH ` od still declares a ception The create() methbut not a RemoteEx , ion CreateExcept 6\VWHPRXWSULQWOQ DGYLVRUJHW$GYLFH ` ` HereÕs a big ch ge! No narrowing old cast (we stan ! Just a pla return type of illloohave to cast because the in ku p is Object, t we have to narrow it donÕt since it isnÕt a bu stub.) The business method call. method call is no longer and since getAJust a normal local met a Remote hod call, dvice() doesnÕ excpetions, th t de have to be w e business method caclllare any rapped in a tr do y/catch. esnÕt &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 51 WKHFOLHQWYLHZ YiX`e gfn\i ?PI\PI[\WKPIVOMQV[QLM\PMJMIVKTI[[' 7EVE SEEN HOW THE INTERFACES CHANGE AND HOW THE CLIENT CODE HAS TO CHANGE WHEN YOU GO FROM A 2EMOTE TO LOCAL CLIENT VIEW "UT WHAT ABOUT THE BEAN CLASS ITSELF 7HAT DO YOU THINK $OES THE BEAN CODE NEED TO CHANGE IF YOURE GOING TO DEPLOY IT WITH A LOCAL CLIENT VIEW INSTEAD OF A 2EMOTE CLIENT VIEW 7HAT IF YOU PLAN TO DEPLOY IT WITH BOTH A LOCAL AND 2EMOTE CLIENT VIEW &OR NOW LETS ASSUME THAT THE ONLY METHOD THAT MATTERS IS THE BEANS BUSINESS METHOD (ERES HOW IT LOOKS IN THE ORIGINAL BEAN CLASS SXEOLF6WULQJJHW$GYLFH ^ 6\VWHPRXWSULQWOQ ³LQJHWDGYLFH´ LQWUDQGRP LQW 0DWKUDQGRP DGYLFH6WULQJVOHQJWK UHWXUQDGYLFH6WULQJV>UDQGRP@ ` $O YOU SEE ANYTHING IN THAT METHOD THAT LOOKS SPECIlC TO A 2EMOTE CLIENT VIEW 7OULD YOU NEED TO DO ANYTHING DIFFERENT WITH A LOCAL CLIENT .O DONT THINK SO !NYTHING THAT WORKS AS A RETURN TYPE OR ARGUMENT FOR A 2EMOTE METHOD IS GUARANTEED TO WORK FOR A LOCAL METHOD AS WELL SO WERE /+ THERE +IND OF A NO BRAINER WHEN THE RETURN TYPE IS 3TRING THOUGH /+ THERE IS ONE EXCEPTIONREMEMBER ACCORDING TO "EAN LAW YOU MUST NOT RETURN A BEANS 2EMOTE INTERFACE FROM A LOCAL INTERFACE METHOD 3O IT LOOKS LIKE AT LEAST WITH THIS BEAN WE SHOULD NEVER HAVE TO KNOW OR CARE 7E SHOULD BE ABLE TO DEPLOY THE BEAN AS WRITTEN AND THE BEAN SHOULD BE KEPT UNAWARE OF WHETHER ITS CLIENTS ARE 2EMOTE OR LOCAL 3OUNDS GOOD DOESNT IT 3IMPLE CLEAN OBJECT ORIENTED "UT THINK ABOUT IT SOME MORE )MAGINE A BEAN WITH MORE COMPLEX LOGIC -ORE BUSINESS METHODS !RGUMENTS TO THOSE METHODS !RGUMENTS THE METHOD MIGHT EVEN NEED TO ACT ON (MMMMM CAN YOU THINK OF ANYTHING THAT THE BEAN MIGHT WANT TO TREAT DIFFERENTLY IF IT KNEW THE CLIENT WERE LOCAL INSTEAD OF 2EMOTE \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 52 SDVVLQJREMHFWVORFDOO\ :DLWDPLQXWH-DYD SDVVHVREMHFWVORFDOO\E\SDVVLQJ DFRS\RIWKHREMHFWUHIHUHQFHQRW WKHREMHFWLWVHOI%XWZHNQRZWKDW 5HPRWHPHWKRGDUJXPHQWVDQGUHWXUQ YDOXHVDUHSDVVHGDVD6HULDOL]HG FRS\RIWKHDFWXDOREMHFW ,QRUGLQDU\ORFDOPHWKRGFDOOV-DYDSDVVHV DQREMHFWUHIHUHQFHE\YDOXHDVDFRS\RIWKH UHIHUHQFHYDULDEOH 7KHREMHFWLWVHOILVQHYHUSDVVHG %XWZLWK5HPRWHFDOOVWKHREMHFWLWVHOILVFRSLHG :LWK5HPRWHFDOOVWKHFDOOHGPHWKRGLVDOZD\V ZRUNLQJRQDFRS\RIWKHFDOOHU¶VREMHFW :LWKORFDOFDOOVWKHFDOOHGPHWKRGLVDOZD\VZRUNLQJ ZLWKWKHFDOOHU¶VRULJLQDOREMHFW²QRWDFRS\ &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 53 WKHFOLHQWYLHZ )ZO]UMV\[\W:MUW\M^[TWKITUM\PWL[ /2&$/PHWKRGFDOO 'RJ¿GR QHZ'RJ 'RJ REMHFW WKLVWUDLQ3HW ¿GR ^a\g a copy of the reference (the remote control) not the Dog object 'RJ %()25(GRJ UHIHUHQFH YRLGWUDLQ3HW 'RJDUJ ^` 'RJ REMHFW r and the ÔÞdoÕ Now the ÔargÕ paramel te ies. Both cop ica nt ide variable are object. g Do e reference the sam Yj_ ^a\g 'RJ %()25(GRJ UHIHUHQFHV 'RJ 5(027(PHWKRGFDOO 'RJ¿GR QHZ'RJ UHPRWH6WXEWUDLQ3HW ¿GR 'RJ REMHFW ue! NOT the reference val ItÕs a serialized copy tof the actual Dog objec 'RJ REMHFW ^a\g 'RJ %()25(GRJ UHIHUHQFH y The server Dog is a cop g Do nt clie e th of YRLGWUDLQ3HW 'RJDUJ ^` 'RJ REMHFW 'RJ REMHFW ^a\g 'RJ Yj_ &OLHQW $)7(5GRJV UHIHUHQFHV 'RJ 6HUYHU \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 54 5HPRWHYVORFDO WKHUH DUH QR 'XPE 4XHVWLRQV Q: 3O )M STILL NOT CLEAR IF THE BEAN -534 ALWAYS KNOW IF THE CLIENT IS 2EMOTE OR LOCAL A: )TS NOT THAT THE BEAN MUST BUT RATHER THAT THE BEAN MIGHT HAVE TO KNOW )F IT MATTERS THAT THE BEAN IS WORKING ON A THE CALLERS OBJECT VIA A COPY OF THE CALLERS REFERENCE AS OPPOSED TO A COPY OF THE CALLERS OBJECT YOUR BEAN CODE MIGHT HAVE TO CHANGE !ND THAT GOES FOR RETURN VALUES TOO )F IT MATTERS THAT THE CALLING METHOD GETS BACK A COPY OF A REFER ENCE VS A COPY OF AN OBJECT THE BEAN CODE MIGHT HAVE TO CHANGE Q: "UT ) THOUGHT THAT CHOOSING TO DEPLOY A BEAN WITH LOCAL VS 2EMOTE CLIENT VIEWS WAS JUST A MATTER OF SWITCHING A SWITCH AT DEPLOY TIME A: ./ ./ ./ ,ETS IMAGINE THAT YOU DID WRITE TWO SETS OF INTERFACES ONE FOR A LOCAL CLIENT VIEW AND ONE FOR A 2EMOTE CLIENT VIEW )T IS TRUE THAT AT DEPLOYMENT YOU COULD DECIDE WHICH OF THE TWO VIEWS YOU WANTED TO EXPOSE OR BOTH "UT THAT WORKS ONLY IF THE BEAN CODE DOESNT CARE WHERE THE CLIENT IS ! BEAN METHOD WITH NO ARGUMENTS OR RETURN VALUES MIGHT BE SAFE REGARDLESS OF HOW THE CLIENT IS ACCESSING IT /NE SOLUTION MIGHT BE TO WRITE THE BEAN CODE ASSUMING THE BEAN IS ALWAYS GETTING A COPY AND THEN IF THE CLIENT IS LOCAL HAVE THE CLIENT MAKE A COPY CLONE OF THE OBJECT BEFORE PASSING IT /R FOR RETURN VALUES YOU MIGHT ALWAYS HAVE THE BEAN MAKE A COPY BEFORE HANDING IT BACK 4HAT WAY THE BEAN NEVER HAS TO WORRY THAT A LOCAL CLIENT MIGHT BE MODIFYING THE BEANS OBJECT !ND A LOCAL CLIENT WONT HAVE CODE TO HANDLE THE 2EMOTE%XCEPTIONS OR NARROW THE STUBS Q: 4HE BOTTOM LINE IS THAT DEPLOYING A BEAN WITH A 2EMOTE VS LOCAL CLIENT VIEW IS A "IG $EAL )TS A COMMITMENT 4HE CLIENT HAS TO KNOW IN ADVANCE 4HEN ITS JUST ABOUT ARGUMENTS AND RETURN VALUES )S THERE ANY OTHER REASON YOU COULDNT DEPLOY A BEAN AND MAKE THE DECISION FOR 2EMOTE VS LOCAL VIEW AT DEPLOY TIME A: 4HERE IS ANOTHER REASON 4HE CLIENT CODE %VEN IF THE BEAN DOESNT NEED TO KNOW HOW ITS CLIENT IS ACCESSING IT THE CLIENT MUST KNOW ! CLIENT WRITTEN TO ACCESS A BEAN LOCALLY WOULDNT WORK IF THE BEANS 2EMOTE CLIENT INTERFACES AND VICE VERSA Q: A: 7HY NOT 4HE CLIENT MUST KNOW IN ADVANCE WHETHER ITS ACCESSING A BEANS 2EMOTE OR LOCAL CLIENT VIEW BECAUSE THE INTERFACES THEMSELVES ARE DIFFERENT 0OLYMORPHICALLY YOU CANT USE THE 2EMOTE AND LOCAL VIEWS INTERCHANGEABLY BECAUSE THE INTERFACES THEMSELVES ARE DIFFERENT 4HERES NO WAY THE CLIENT CAN BE KEPT BLISSFULLY IGNORANT BECAUSE THE BEHAVIOR OF THE BEAN IS DIFFERENT 2EMEMBER A 2EMOTE CLIENT MUST HANDLE 2EMOTE%XCEPTIONS AND NARROW THE 2EMOTE STUB COMING BACK FROM THE LOOKUP !ND A 2EMOTE CLIENT IS EXPOSED TO METHODS IN THE BEANS CLIENT INTERFACESMETHODS THAT DONT EXIST IN THE LOCAL INTERFACES 3O A 2EMOTE CLIENT MIGHT FOR EXAMPLE TRY TO CALL A GET(ANDLE METHOD ON THE LOCAL COMPONENT INTERFACE A METHOD CALL THAT WOULD NEVER WORK Q: #AN YOU GET AROUND THIS BY DECLARING YOUR 2EMOTE%XCEPTIONS ON YOUR LOCAL INTERFACE !ND COULD YOU MAKE AN INTERFACE THAT IS BOTH 2EMOTE AND LOCAL BY HAVING YOUR COMPONENT INTERFACE LIKE !DVICE EXTEND BOTH 2EMOTE AND %*",OCAL/BJECT 7HATS THE HARM IF THE CLIENT SIMPLY ALWAYS HANDLES 2EMOTE%XCEPTIONS AND ALWAYS DOES THE NARROW 4HAT WAY THE CLIENT SHOULDNT HAVE TO KNOW A: 3TILL WONT WORK &OR ONE THING ACCORDING TO BEAN LAW YOURE NOT ALLOWED TO DECLARE 2EMOTE%XCEPTIONS ON LOCAL METHODS 3O THERES NO GUARANTEE THAT YOUR SERVER WOULD EVEN LET YOU DEPLOY A BEAN WITH A LOCAL INTERFACE THAT DECLARES 2EMOTE%XCEPTIONS !ND THERE IS NO GUARANTEE THAT THE NARROW METHOD WOULD NOT CAUSE PROBLEMS !ND THEN THERE ARE HANDLES AND ALL THAT OTHER STUFF 9OU NEED TO JUST LET THIS GO &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 55 WKHFOLHQWYLHZ %(WKH&RQWDLQHU (DFKRIWKHFRGHVQLSSHWVRQWKLVSDJHUHSUHVHQWVFRGHIURPHLWKHU DQLQWHUIDFHRUDFOLHQW<RXUMRELVWRSOD\&RQWDLQHUDQGGHFLGH ZKHWKHUHDFKLVOHJDODFFRUGLQJWRERWK-DYDODZDQG%HDQ ODZ,QRWKHUZRUGVHYHQLIWKHFRGHFRPSLOHVLWPLJKWVWLOOEH :521*WRWKH&RQWDLQHUEHFDXVHLWGRHVQ·WFRPSO\ZLWKWKH UXOHVRIWKH(-%VSHF$VVXPHWKDWHYHU\WKLQJ\RXGR127VHHLV OHJDODQGFRUUHFW)LJXUHRXWLIWKHSUREOHPLVDFRPSLOHUHUURURUD SUREOHPWRWKH&RQWDLQHUDQGILJXUHRXWKRZWRIL[LW ([HUFLVH %,QD5HPRWHFOLHQW $,QDORFDOFOLHQW SXEOLFYRLGJR ^ 2EMHFWR QXOO WU\^ &RQWH[WLF QHZ,QLWLDO&RQWH[W R LFORRNXS ³$GYLVRU/RFDO´ SXEOLFYRLGJR ^ WU\^ ORRNXSWKH$GYLFHEHDQDVVLJQLW WRDGYLVRU `FDWFK ([FHSWLRQH[ ^ H[SULQW6WDFN7UDFH ` 6\VWHPRXWSULQWOQ DGYLVRUJHW$GYLFH ` `FDWFK 1DPLQJ([FHSWLRQQH[ ^ QH[SULQW6WDFN7UDFH ` $GYLFH+RPH/RFDOKRPH $GYLFH+RPH/RFDO R $GYLFH/RFDODGYLVRU QXOO PRUHVWXII ` &,QDEHDQ·VKRPHLQWHUIDFH SDFNDJHKHDG¿UVW LPSRUWMDYD[HME SXEOLFLQWHUIDFH$GYLFH+RPHH[WHQGV(-%+RPH^ SXEOLF$GYLFHFUHDWH WKURZV&UHDWH([FHSWLRQ ` ',QDEHDQ·VFRPSRQHQWLQWHUIDFH SDFNDJHKHDG¿UVW LPSRUWMDYD[HME SXEOLFLQWHUIDFH$GYLFH/RFDOH[WHQGV(-%/RFDO2EMHFW^ SXEOLF6WULQJJHW$GYLFH ` \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 56 ORFDOLQWHUIDFHV :MDD=LHGAFLK <RXFDQH[SRVH\RXUEHDQWRORFDOFOLHQWVXVLQJDORFDOFOLHQWYLHZ /RFDOFRPSRQHQWLQWHUIDFHVPXVWH[WHQGMDYD[HME(-%/RFDO2EMHFW /RFDOKRPHLQWHUIDFHVPXVWH[WHQGMDYD[HME(-%/RFDO+RPH 0HWKRGVLQORFDOFOLHQWLQWHUIDFHVGR127GHFODUH 5HPRWH([FHSWLRQ 6RPHRIWKHLQWHUIDFHPHWKRGVH[SRVHGWR5HPRWHFOLHQWVDUHQRW H[SRVHGWRORFDOFOLHQWV /RFDOFOLHQWVFDQQRWJHWKDQGOHVVLQFHKDQGOHVDUHXVHGWRUH HVWDEOLVKDFRQQHFWLRQWRWKH5HPRWHREMHFW (-%0HWD'DWDLVQRWXVHGZLWKORFDOFOLHQWVVLQFHDORFDOFOLHQWFDQ XVHUHIOHFWLRQWRLQWHUURJDWHWKH(-%REMHFWDQG+RPHREMHFW /RFDOKRPHLQWHUIDFHVKDYHRQO\RQHUHPRYH PHWKRG³WKHRQH WKDWWDNHVDSULPDU\NH\7KHUHPRYH WKDWWDNHVD+DQGOHGRHVQ·W H[LVWLQWKHORFDOKRPHLQWHUIDFHVLQFH+DQGOHVDUHQ·WXVHGZLWKD ORFDOFOLHQWYLHZ %HFDXVHWKHRQO\UHPRYH LQWKHORFDOKRPHLQWHUIDFHUHTXLUHVD SULPDU\NH\DUJXPHQWORFDOVHVVLRQEHDQFOLHQWVFDQQRWUHPRYH DEHDQXVLQJWKHEHDQ·VKRPHWKH\FDQFDOOUHPRYH RQO\RQWKH EHDQ·VFRPSRQHQWLQWHUIDFH (-%/RFDO+RPHKDVRQO\RQHPHWKRGUHPRYH WKDWWDNHVD SULPDU\NH\EHFDXVHWKHJHW+RPH+DQGOH JHW(-%0HWD'DWD DQGUHPRYH +DQGOH PHWKRGVWKDWDUHLQ(-%+RPHGRQ·WDSSO\WRD ORFDOFOLHQWYLHZ 7KHRQO\PHWKRGLQ(-%2EMHFWWKDWLVQRWDOVRLQ(-%/RFDO2EMHFWLV JHW+DQGOH :DWFKLW <RXKDYHWR UHFRJQL]HD ORFDOYV5HPRWH FOLHQWYLHZ DQFRGH %HSUHSDUHGWRORRNDWEH RGHDQG FOLHQWFRGHRULQWHUIDFHF JDWD NQRZZKHWKHU\RX¶UHORRNLQ Z WYLH FOLHQ FDO RUOR RWH 5HP $QGLWPLJKWEHVXEWOH WKH )RUH[DPSOH\RXPLJKWVHH HWKRGFDOO FOLHQWPDNHDEXVLQHVVP WKRG DQGWKHDUJXPHQWWRWKHPH 6LQFH LVDQRQ6HULDOL]DEOHREMHFW PHWKRG \RXDUHWRDVVXPHWKDWWKH 2: LVOHJDODQGFRUUHFW\RX.1 UIDFH² OLQWH ORFD HD VWE PX WKLV WKDW DEOH ULDOL] 6H QRQ VVD ¶WSD \RXFDQ XUQ UUHW QWR XPH DUJ VDQ REMHFWD HWKRG YDOXHWRRUIURPD5HPRWHP OO\RX 6RPHRIRWKHUJRWFKDVWRWH U5H ZKHWKHUWKHEHDQLVORFDOR PRWHLQFOXGH +RPH &OLHQWGRHVQ¶WQDUURZWKH DQ\ QGOH ¶WKD HVQ WGR OLHQ & XVLQHVV FKHFNHGH[FHSWLRQVRQDE PHWKRGFDOO GOH RQ &OLHQWFDOOVUHPRYH +DQ DEHDQ¶VKRPH $UJXPHQWVDQGUHWXUQYDOXHVDUHSDVVHGE\YDOXHZKHQXVLQJD ORFDOFOLHQWYLHZ,QRWKHUZRUGVWKH\·UHSDVVHGLQWKHQRUPDO-DYD ZD\ REMHFWVSDVVHGE\DFRS\RIWKHUHIHUHQFHSULPLWLYHVSDVVHG E\DFRS\RIWKHYDOXH /RFDOFOLHQWVGRQRWQHHGWRQDUURZWKH+RPHUHIHUHQFHEHFDXVH LW·VDQRUPDO-DYDUHIHUHQFHQRWDVWXEWRD5HPRWHREMHFW /RFDOFOLHQWVGRQRWQHHGWRFDWFK5HPRWH([FHSWLRQVVLQFHORFDO LQWHUIDFHPHWKRGVGRQ·WGHFODUH5HPRWH([FHSWLRQV &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 57 WKHFOLHQWYLHZ %(WKH&RQWDLQHU ([HUFLVH 6ROXWLRQV %,QD5HPRWHFOLHQW $,QDORFDOFOLHQW SXEOLFYRLGJR ^ 2EMHFWR QXOO WU\^ &RQWH[WLF QHZ,QLWLDO&RQWH[W R LFORRNXS ³$GYLVRU/RFDO´ SXEOLFYRLGJR ^ WU\^ ORRNXSWKH$GYLFHEHDQDVVLJQ LWWRDGYLVRU `FDWFK ([FHSWLRQH[ ^ H[SULQW6WDFN7UDFH ` 6\VWHPRXWSULQWOQ DGYLVRUJHW$GYLFH ` `FDWFK 1DPLQJ([FHSWLRQQH[ ^ QH[SULQW6WDFN7UDFH ` $GYLFH+RPH/RFDOKRPH $GYLFH+RPH/RFDO R $GYLFH/RFDODGYLVRU QXOO PRUHVWXII ` remote Advice() is hea t ge r! ro er Compiler t it isnÕt handling t last line method, bu ption. If we moved theork. ce w x ld eE wou Remot try block, it INSIDE the A is Þne. Because itÕs local, it does not need to narrow the Hom e reference. Only a cast is needed . &,QDEHDQ·VKRPHLQWHUIDFH SDFNDJHKHDG¿UVW LPSRUWMDYD[HME Container error! EJBHome is a Remote interface, and the rule is that you must declare RemoteException on each method in the interface. The compiler doesnÕt care, but the Container will. At some point in the deploy process, this will fail. SXEOLFLQWHUIDFH$GYLFH+RPHH[WHQGV(-%+RPH^ SXEOLF$GYLFHFUHDWH WKURZV&UHDWH([FHSWLRQ ` needs RemoteException (look at the imports; no java.rmi.*) ',QDEHDQ·VFRPSRQHQWLQWHUIDFH Õs a local D works Þne.. Itfa ce, and it er int t componen clare any de to doesnÕt need e business th on s ion pt exce methods. SDFNDJHKHDG¿UVW LPSRUWMDYD[HME SXEOLFLQWHUIDFH$GYLFH/RFDOH[WHQGV(-%/RFDO2EMHFW^ SXEOLF6WULQJJHW$GYLFH OK, needs no exceptions declared ` \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 58 FRIIHHFUDPPRFNH[DP 0RFN([DP 1 7HICH CAPABILITIES ARE PROVIDED BY BOTH REMOTE AND LOCAL HOME INTERFACES FOR SESSION BEANS #HOOSE ALL THAT APPLY R! R" R# R$ 2 #REATING A SESSION OBJECT 2EMOVING A SESSION OBJECT 'ETTING A SESSION OBJECTS %*"-ETA$ATA 'ETTING A SESSION OBJECTS HANDLE 7HEN LOCATING A SESSION BEANS REMOTE HOME INTERFACE WHICH ARE STEPS THAT MUST OCCUR TO CREATE A VALID HOME INTERFACE REFERENCE #HOOSE ALL THAT APPLY R! 4HE SESSION CONTEXT MUST BE NARROWED AND THE NARROWED RESULT CAST R" 4HE RESULT OF THE *.$) LOOKUP MUST BE NARROWED AND THE NARROWED RESULT CAST R# 4HE INITIAL CONTEXT MUST BE NARROWED AND THE NARROWED RESULT CAST R$ 4HE RESULT OF THE *.$) LOOKUP MUST BE CAST TO AN INITIAL CONTEXT AND THENNARROWED 3 'IVEN A REMOTE CLIENT @2 THAT HAS VALID REFERENCES TO SESSION BEANS @! AND @" AND GIVEN THAT ! IS A LOCAL CLIENT TO " WHICH STATEMENTS ARE TRUE #HOOSE ALL THAT APPLY R! R" R# R$ 2 CANNOT PASS HIS REFERENCE FOR ! TO " ! CANNOT PASS HIS REFERENCE FOR " TO 2 ! CANNOT INVOKE METHODS ON " " CANNOT INVOKE METHODS ON 2 &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Page 59 Return to Table of Contents WKHFOLHQWYLHZ 4 7HEN COMPARING TWO SESSION OBJECTS WHAT IS TRUE #HOOSE ALL THAT APPLY R! 5SING THE IS)DENTICAL METHOD STATELESS SESSION BEANS FROM THE SAME HOME WILL ALWAYS RETURN TRUE R" 5SING THE IS)DENTICAL METHOD STATEFUL SESSION BEANS FROM THE SAME HOME WILL ALWAYS RETURN TRUE R# 4HE IS)DENTICAL METHOD CAN BE USED FOR ONLY REMOTE OBJECT REFERENCES R$ 5SING THE EQUALS METHOD STATELESS SESSION BEANS FROM THE SAME HOME ARE GUARANTEED TO RETURN TRUE R% 5SING THE EQUALS METHOD STATEFUL SESSION BEANS FROM THE SAME HOME ARE GUARANTEED TO RETURN TRUE 5 7HICH STATEMENTS ABOUT SESSION BEANS ARE TRUE #HOOSE ALL THAT APPLY R! 4HE BEAN PROVIDER MUST WRITE THE METHOD PUBLIC VOID REMOVE IN BOTH STATELESS AND STATEFUL SESSION CLASSES R" ,OCAL CLIENTS CAN REMOVE SESSION BEANS BY CALLING A METHOD ON THE BEANS HOME R# 4HE REMOVE METHOD IN THE COMPONENT INTERFACE CAN BE USED ONLY BY REMOTE CLIENTS R$ 4O ASK THE %*"(OME TO REMOVE A SESSION BEAN THE CLIENT MUST PROVIDE THE BEANS HANDLE \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 60 FRIIHHFUDPPRFNH[DP 0RFN([DP$QVZHUV 1 7HICH CAPABILITIES ARE PROVIDED BY BOTH REMOTE AND LOCAL HOME INTERFACES FOR SESSION BEANS #HOOSE ALL THAT APPLY ere nÕt be removed R! R" R# R$ 2 session beanlos cacal home interface becausye kethy through a ove() that takes a primar only a rem 2EMOVING A SESSION OBJECT 'ETTING A SESSION OBJECTS %*"-ETA$ATA - not in a local home 'ETTING A SESSION OBJECTS HANDLE - not in a local home is #REATING A SESSION OBJECT 7HEN LOCATING A SESSION BEANS REMOTE HOME INTERFACE WHICH ARE STEPS THAT MUST OCCUR TO CREATE A VALID HOME INTERFACE REFERENCE #HOOSE ALL THAT APPLY (spec: 57) R! 4HE SESSION CONTEXT MUST BE NARROWED AND THE NARROWED RESULT CAST R" 4HE RESULT OF THE *.$) LOOKUP MUST BE NARROWED AND THE NARROWED RESULT CAST R# 4HE INITIAL CONTEXT MUST BE NARROWED AND THE NARROWED RESULT CAST R$ 4HE RESULT OF THE *.$) LOOKUP MUST BE CAST TO AN INITIAL CONTEXT AND THENNARROWED 3 'IVEN A REMOTE CLIENT @2 THAT HAS VALID REFERENCES TO SESSION BEANS @! AND @" AND GIVEN THAT ! IS A LOCAL CLIENT TO " WHICH STATEMENTS ARE TRUE #HOOSE ALL THAT APPLY R! R" R# R$ 2 CANNOT PASS HIS REFERENCE FOR ! TO " ! CANNOT PASS HIS REFERENCE FOR " TO 2 ! CANNOT INVOKE METHODS ON " local remote client a You canÕt giAveseaes B through a local reference, reference " CANNOT INVOKE METHODS ON 2 &KDSWHU Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 3. Exposing Yourself Return to Table of Contents Page 61 WKHFOLHQWYLHZ 4 7HEN COMPARING TWO SESSION OBJECTS WHAT IS TRUE #HOOSE ALL THAT APPLY (spec: 65-66) R! 5SING THE IS)DENTICAL METHOD STATELESS SESSION BEANS FROM THE SAME HOME WILL ALWAYS RETURN TRUE R" 5SING THE IS)DENTICAL METHOD STATEFUL SESSION BEANS FROM THE SAME HOME WILL ALWAYS RETURN TRUE R# 4HE IS)DENTICAL METHOD CAN BE USED FOR ONLY REMOTE OBJECT REFERENCES of R$ 5SING THE EQUALS METHOD STATELESS SESSION BEANS FROM THE SAME HOME The behavior iÞed equals() is not spec ARE GUARANTEED TO RETURN TRUE R% 5SING THE EQUALS METHOD STATEFUL SESSION BEANS FROM THE SAME HOME ARE GUARANTEED TO RETURN TRUE 5 7HICH STATEMENTS ABOUT SESSION BEANS ARE TRUE #HOOSE ALL THAT APPLY e() ove(), not remov R! 4HE BEAN PROVIDER MUST WRITE THE METHOD PUBLIC VOID REMOVE IN- itÕs ejbRem BOTH STATELESS AND STATEFUL SESSION CLASSES - local homes have only a remove() that takes a BEANS HOME primary key 4HE REMOVE METHOD IN THE COMPONENT INTERFACE CAN BE USED ONLY BY - no, only for local REMOTE CLIENTS R" ,OCAL CLIENTS CAN REMOVE SESSION BEANS BY CALLING A METHOD ON THE R# R$ 4O ASK THE %*"(OME TO REMOVE A SESSION BEAN THE CLIENT MUST PROVIDE THE BEANS HANDLE - but not true for EJBLocalHo me \RXDUHKHUH Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Table of Contents Chapter 4. Being a Session Bean.................................................................. 1 Section 4.1. OBJECTIVES: Session Beans...................................................................................................................................... 2 Section 4.2. Exploring the session bean lifecycle........................................................................................................................... 4 Section 4.3. You remember how it all works, right ? Getting and using a stateful session bean.................................................. 6 Section 4.4. Stateful Session Bean, CREATION............................................................................................................................. 7 Section 4.5. There's obviously more to the bean's lifecycle than just creation and business methods......................................... 8 Section 4.6. Container Callbacks, for the special moments in a bean's life................................................................................... 9 Section 4.7. Container Callbacks come from TWO places:.......................................................................................................... 10 Section 4.8. Implementing the container callbacks...................................................................................................................... 11 Section 4.9. There are no Dumb Questions.................................................................................................................................. 14 Section 4.10. Sharpen your pencil................................................................................................................................................. 14 Section 4.11. Sharpen your pencil.................................................................................................................................................. 15 Section 4.12. Bean creation: when an object becomes a bean...................................................................................................... 16 Section 4.13. Bean things you can do during creation: stateful session bean)............................................................................ 24 Section 4.14. Bean Use: what happens AFTER creation.............................................................................................................. 26 Section 4.15. Bean things you can do within business methods:................................................................................................. 27 Section 4.16. Passivation: a stateful bean's chance at scalability................................................................................................. 28 Section 4.17. There are no Dumb Questions................................................................................................................................ 30 Section 4.18. Your job for passivation: make your state passivatable!........................................................................................ 31 Section 4.19. There are no Dumb Questions................................................................................................................................ 33 Section 4.20. Sharpen your pencil................................................................................................................................................ 33 Section 4.21. Bean things you can do in ejbActivate and ejbPassivate()..................................................................................... 35 Section 4.22. Bean Removal: when beans die.............................................................................................................................. 36 Section 4.23. Complaints about bean removal............................................................................................................................. 41 Section 4.24. Bean things you can do in ejbRemove()................................................................................................................. 44 Section 4.25. Implementing the AdviceBean as a stateFUL bean............................................................................................... 45 Section 4.26. BRAIN POWER....................................................................................................................................................... 45 Section 4.27. Things you can add if the bean is stateFUL............................................................................................................ 45 Section 4.28. There are no Dumb Questions................................................................................................................................ 45 Section 4.29. AdviceStatefulBean code........................................................................................................................................ 46 Section 4.30. AdviceStatefulBean CLIENT code.......................................................................................................................... 47 Section 4.31. Sharpen your pencil................................................................................................................................................ 48 Section 4.32. Deploying a stateful bean....................................................................................................................................... 49 Section 4.33. Compared to stateful beans, stateless beans have a simple life............................................................................. 50 Section 4.34. Bean things you can do from stateless bean methods............................................................................................ 56 Section 4.35. Writing a Session Bean:your job as Bean Provider................................................................................................ 58 Section 4.36. Sharpen your pencil................................................................................................................................................ 59 Section 4.37. Sharpen your pencil: Who does What?................................................................................................................... 67 Section 4.38. SessionContext : You need it more than it needs you............................................................................................ 68 Section 4.39. Overheard at THE TiKiBEAN LoUNGE................................................................................................................. 70 Section 4.40. What's allowed?....................................................................................................................................................... 71 Section 4.41. COFFEE CRAM: Mock Exam.................................................................................................................................. 72 Section 4.42. COFFEE CRAM: Mock Exam Answers................................................................................................................... 79 Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 1 session bean lifecycle 5 4 4 %HLQJD6HVVLRQ%HDQ ,KDWHLWZKHQ,JHW FRQWDLQHUFDOOEDFNV,MXVWNQRZ LW·VJRQQDEHDQHME5HPRYH EXW, ZDVRQO\MXVWFUHDWHG,ZLVK,ZDV VWDWHOHVV7KH\ZRQ·WHYHQOHW PHJRLQWKHSRRO 6HVVLRQEHDQVDUHFUHDWHGDQGUHPRYHG,I\RX¶UHOXFN\\RX¶UHD VWDWHOHVVEHDQ%HFDXVHWKHOLIHRIDVWDWHIXOEHDQLVWLHGWRWKHZKLPVRIDKHDUWOHVV FOLHQW6WDWHIXOEHDQVDUHFUHDWHGDWWKHFOLHQW¶VLQVLVWHQFHDQGH[LVWRQO\WRVHUYHWKDW RQHFOLHQW$VDVWDWHIXOEHDQWKHEHVW\RXFDQKRSHIRULVWKDWWKHFOLHQWFUDVKHVRU IRUJHWVWRFDOOUHPRYH LWPLJKWWDNHWKH&RQWDLQHUDZKLOHWR¿JXUHRXW\RX¶YHEHFRPH XVHOHVV%XWDKKKKWKHOLIHRIDVWDWHOHVVEHDQLVIDEXORXV3RROVWKRVHOLWWOHXPEUHOOD GULQNVDQGQRERUHGRPVLQFH\RXJHWWRPHHWVRPDQ\GLIIHUHQWFOLHQWV2IFRXUVHHYHQ DVWDWHOHVVEHDQGLHVZLWKDQXQFKHFNHGH[FHSWLRQ WKLVLVDQHZFKDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 2 H[DPREMHFWLYHV 6HVVLRQ%HDQV 2IILFLDO ,GHQWLI\FRUUHFWDQGLQFRUUHFWVWDWHPHQWVRU H[DPSOHVDERXWVHVVLRQEHDQVLQFOXGLQJ FRQYHUVDWLRQDOVWDWHWKH6HVVLRQ%HDQ LQWHUIDFHDQGFUHDWHPHWKRGV :KDWLWUHDOO\PHDQV 7KLVREMHFWLYHFDQKLW\RXRQDQ\WKLQJUHODWHGWR VHVVLRQEHDQVVR\RXSUHWW\PXFKKDYHWRNQRZLW DOOLQFOXGLQJWKHGHWDLOVRIERWKVWDWHOHVVDQGVWDWHIXO EHDQOLIHF\FOHVWKHFRQWDLQHUFDOOEDFNPHWKRGV RI6HVVLRQ%HDQZKDW\RXPXVWZULWHLQDEHDQ FODVVDQGZKDWDEHDQFDQJHWIURPLWV(-%&RQWH[W $Q\WKLQJFRYHUHGE\WKHRWKHUVHVVLRQEHDQREMHFWLYHV LVIDLUJDPH Copyright Safari Books Online #896963 ,GHQWLI\WKHXVHRIDQGWKHEHKDYLRURIWKH HME3DVVLYDWHPHWKRGLQDVHVVLRQEHDQ LQFOXGLQJWKHUHVSRQVLELOLWLHVRIERWKWKH FRQWDLQHUDQGWKHEHDQSURYLGHU )LUVW\RXKDYHWRNQRZWKDWSDVVLYDWLRQLVIRURQO\ VWDWHIXOVHVVLRQEHDQV6WDWHOHVVEHDQVJREDFNWR WKHEHDQSRROZLWKRXWJRLQJWKURXJKSDVVLYDWLRQ6R HME3DVVLYDWH ZLOOQHYHUEHFDOOHGRQDVWDWHOHVV EHDQ<RXKDYHWRNQRZWKDW\RXUUHVSRQVLELOLW\LQ HME3DVVLYDWH LVWRPDNHVXUHWKDWZKHQWKHPHWKRG HQGV\RXUEHDQLVUHDG\IRUSDVVLYDWLRQDQGWKDW SDVVLYDWLRQPD\RUPD\QRWLQYROYHVHULDOL]DWLRQ<RX PXVWNQRZWKHVWDWHWKDW\RXULQVWDQFHYDULDEOHVPXVW EHLQDWWKHWLPHHME3DVVLYDWH HQGV$QGHYHQWKRXJK WKLVREMHFWLYHGRHVQ¶WPHQWLRQHME$FWLYDWH \RXKDYH WRXQGHUVWDQGKRZHME$FWLYDWH ZRUNVDVZHOO ,GHQWLI\WKHLQWHUIDFHDQGPHWKRGIRU <RXPXVWEHDEOHWRORRNDWDGHVFULSWLRQRIDGHVLUHG HDFKRIWKHIROORZLQJUHWULHYHWKHVHVVLRQ EHKDYLRUOLNH¿JXUHRXWWKHVHFXULW\SULQFLSDORIWKH EHDQ¶VUHPRWHKRPHLQWHUIDFHUHWULHYHWKH FOLHQWDQGNQRZZKLFKPHWKRG\RXFDQFDOODQGRQ VHVVLRQEHDQ¶VORFDOFRPSRQHQWLQWHUIDFH ZKLFKLQWHUIDFHWRJHWWKDWEHKDYLRU LQWKLVFDVH GHWHUPLQHLIWKHVHVVLRQEHDQ¶VFDOOHUKDV JHW&DOOHU3ULQFLSDO FDOOHGRQWKH6HVVLRQ&RQWH[W DSDUWLFXODUUROHDOORZWKHLQVWDQFHWRPDUN LQWHUIDFH WKHFXUUHQWWUDQVDFWLRQDVDUROOEDFNUHWULHYH 7RDQVZHUWKHVHTXHVWLRQV\RXKDYHWRNQRZHYHU\ WKH8VHU7UDQVDFWLRQLQWHUIDFHSUHSDUHWKH PHWKRGLQWKHWKUHHNH\LQWHUIDFHV6HVVLRQ%HDQ WKH LQVWDQFHIRUUHXVHIROORZLQJSDVVLYDWLRQ LQWHUIDFH\RXUEHDQLPSOHPHQWV 6HVVLRQ&RQWH[W UHOHDVHUHVRXUFHVSULRUWRUHPRYDO WKHLQWHUIDFH\RXUEHDQKDVDUHIHUHQFHWR DQG LGHQWLI\WKHLQYRNHURIWKHEHDQLQVWDQFH¶V 6HVVLRQ6\QFKURQL]DWLRQ DQRSWLRQDOLQWHUIDFHWKDWD FRPSRQHQWLQWHUIDFHEHQRWL¿HGWKDWDQHZ VWDWHIXOEHDQFDQLPSOHPHQW WUDQVDFWLRQKDVEHJXQEHQRWL¿HGWKDWWKH ,QWKLVFKDSWHUZH¶OOFRYHUHYHU\WKLQJDERXWVHVVLRQ FXUUHQWWUDQVDFWLRQKDVFRPSOHWHG EHDQLQWHUIDFHVH[FHSW6HVVLRQ6\QFKURQL]DWLRQ ZKLFKLVFRYHUHGLQWKHWUDQVDFWLRQVFKDSWHU &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 3 VHVVLRQEHDQOLIHF\FOH 2EMHFWLYH 6HVVLRQ%HDQV 2IILFLDO :KDWLWUHDOO\PHDQV 0DWFKFRUUHFWGHVFULSWLRQVDERXWSXUSRVH <RXKDYHWRNQRZWKHGLIIHUHQFHVEHWZHHQVWDWHOHVV DQGIXQFWLRQZLWKZKLFKVHVVLRQEHDQ W\SHWKH\DSSO\WRVWDWHOHVVVWDWHIXORU ERWK DQGVWDWHIXOEHDQVLQFOXGLQJZKHQ\RX¶GFKRRVHRQH RYHUWKHRWKHU<RXPXVWNQRZWKHEHDQUXOHVLQFOXGLQJ WKHUHFDQEHRQO\RQHFUHDWHPHWKRGLQDVWDWHOHVVEHDQ DQGLWPXVWKDYHQRDUJXPHQWVRQO\VWDWHIXOEHDQVFDQ LPSOHPHQW6HVVLRQ6\QFKURQL]DWLRQERWKVWDWHOHVVDQG VWDWHIXOEHDQVZLOOQRWVXUYLYHDFRQWDLQHUFUDVKVWDWH OHVVEHDQVZLOOQHYHUEHSDVVLYDWHGVWDWHOHVVEHDQ FUHDWLRQDQGUHPRYDOLVQRWWLHGWRWKHFOLHQW *LYHQDOLVWRIUHVSRQVLELOLWLHVUHODWHGWR <RXKDYHWRNQRZZKDWLVDQGLVQ¶WJXDUDQWHHGLQWKH VSHF)RUH[DPSOHWKHFRQWDLQHUZRQ¶WDOORZFRQFXUUHQW DFFHVVWRDVLQJOHVHVVLRQEHDQ LIFOLHQW$LVXVLQJWKH EHDQFOLHQW%ZLOOJHWDQH[FHSWLRQLILWFDOOVDPHWKRG RQWKDWEHDQ $QGWKHFRQWDLQHULVQ¶WUHTXLUHGWRDOORZ DEHDQLQRQH($5¿OHWREHDORFDOFOLHQWWRDEHDQLQ DGLIIHUHQW($5$QG\RXKDYHWRNQRZH[DFWO\ZKLFK FODVVHVDQGPHWKRGVDUHLPSOHPHQWHGE\\RX WKHEHDQ WKHHME5HPRYH DQGHME&UHDWH PHWKRGVHWF DQG ZKLFKDUHLPSOHPHQWHGE\WKHFRQWDLQHU WKHKDQGOHV WKHKRPHDQGFRPSRQHQWREMHFWVWKHVWXEV IRU5HPRWH FOLHQWLQWHUIDFHV WKHJHW(-%2EMHFW PHWKRGHWF 0RVW RIZKDW¶VLQWKLVREMHFWLYHRYHUODSVZLWKDQG VHVVLRQEHDQVLQGHQWLI\WKRVHZKLFKDUH WKHUHVSRQVLELOLW\RIWKHVHVVLRQEHDQ SURYLGHUDQGWKRVHZKLFKDUHWKH UHVSRQVLELOLW\RIWKH(-%FRQWDLQHU SURYLGHU *LYHQDOLVWRIUHTXLUHPHQWVLGHQWLI\ WKRVHZKLFKDUHWKHUHTXLUHPHQWVIRUD VHVVLRQEHDQFODVVUHPRWHFRPSRQHQW LQWHUIDFHUHPRWHKRPHLQWHUIDFHFUHDWH PHWKRGVEXVLQHVVPHWKRGVORFDO FRPSRQHQWLQWHUIDFHUHPRWHFRPSRQHQW LQWHUIDFH 7KLVLVDERXWNQRZLQJEHDQODZ²WKHUXOHV\RXKDYHWR IROORZDFFRUGLQJWRWKHVSHFHYHQLQWKHFDVHVZKHUH FRPSLOHUZRQ¶WFDUHLI\RXGRQ¶W)RUH[DPSOH\RXPXVW KDYHDPDWFKLQJHME&UHDWH PHWKRGIRUHDFKFUHDWH PHWKRGLQWKHKRPHEXWWKHFRPSLOHUZRQ¶WVWRS\RXLI \RXOHDYHLWRXW$QG\RXPXVWNQRZWKDWORFDOLQWHUIDFH PHWKRGVPXVWQRWGHFODUHD5HPRWH([FHSWLRQEXW 5HPRWHLQWHUIDFHPHWKRGVPXVW$QG\RXPXVWNQRZ WKDWREMHFWW\SHDUJXPHQWVWRD5HPRWHLQWHUIDFH PHWKRGDUHVHQWDVVHULDOL]HGFRSLHVRIWKHREMHFWZKLOH DUJXPHQWVWRORFDOLQWHUIDFHVDUHVHQWDVFRSLHVRIWKH UHIHUHQFH \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 4 VHVVLRQEHDQV -`XTWZQVO\PM[M[[QWVJMIVTQNMKaKTM )MAGINE YOURE A SESSION BEAN 7HAT MATTERS TO YOU 7HAT ARE THE KEY MOMENTS IN YOUR LIFE 7HAT DO YOU NEED TO KNOW ABOUT THE CLIENT 7HAT DO YOU NEED TO KNOW ABOUT THE #ONTAINER 7HAT DO YOU NEED TO GET FROM THE #ONTAINER .OW IMAGINE YOURE THE CLIENT 7HATS YOUR MOTIVATION 7HAT DO YOU NEED TO KNOW ABOUT HOW THE #ONTAINER WORKS !ND WHAT IF YOURE THE #ONTAINER 7HAT DO YOU HAVE TO DO TO MANAGE A BEANS LIFE AND WHEN DO YOU HAVE TO DO IT )F YOURE THE "EAN 0ROVIDER WHAT DO YOU HAVE TO KNOW ABOUT THE BEANS LIFECYCLE IN ORDER TO WRITE CODE THAT WORKS 7HAT IF YOU WANT THAT CODE TO BE EFlCIENT )N THIS CHAPTER WE LOOK AT THE ENTIRE WORLD OF A SESSION BEAN AND WE DO IT FROM DIFFERENT POINTS OF VIEW 7E TAKE AN // APPROACH TO THIS CHAPTER ITERATING THROUGH DEEPER LEVELS OF DETAIL 3O DONT PANIC IF THE lRST SEVERAL PAGES FEEL A LITTLE TOO HIGH LEVEL OR LEAVE YOU WITH QUESTIONS 9OULL BE DOWN IN THE DIRT BEFORE THE CHAPTERS DONE "EFORE WE START LETS INTRODUCE THE PLAYERS +WV\IQVMZ *MIV8ZW^QLMZ +TQMV\ *MIV *RDOFDOOEXVLQHVV PHWKRGVRQDEHDQ *RDOXQGHUVWDQGWKH WUXHPHDQLQJRILWVOLIH :KHUHLWFDPHIURP ZKDWLWVSXUSRVHLV KRZZLOOLWDOOHQG *RDOZULWHGHSOR\DEOH ZHOOEHKDYHGEHDQV .QRZWKHEHDQ$3,ZHOO HQRXJKWRSXWWKHULJKW FRGHLQWKHULJKWSODFH *RDOEHWKHEHDQ JRG0DQDJHWKHOLIH DQGGHDWKRIEHDQV LQWHUFHSWLQJDOORIWKH EHDQ·VPHWKRGFDOOV &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 5 VHVVLRQEHDQOLIHF\FOH ,FUHDWHXVHDQGUHPRYH EHDQVE\FDOOLQJPHWKRGVRQWKH KRPHDQGFRPSRQHQWLQWHUIDFHVWXEV 6RPHGD\,ZLOONLOOWKH%HDQ3URYLGHU ZLWKP\EDUHKDQGVIRUQHYHUJLYLQJ PHWKHULJKWVWXEV +TQMV\ ,·PDQREMHFWDQG,UXQ EXVLQHVVPHWKRGVEXW,·PDOVR DEHDQ$VDEHDQ,SD\DWWHQWLRQWRNH\ PLOHVWRQHVLQP\OLIHOLNHFRQVWUXFWLRQ ERULQJ EHDQ FUHDWLRQ YHU\LPSRUWDQW SDVVLYDWLRQ JRLQJWR VOHHS DFWLYDWLRQ ZDNLQJXS DQGUHPRYDO GHDWK QRWJRRGLI\RX·UHPH 7KHFRQWDLQHUWHOOVPH HYHU\WLPHRQHRIWKHVHVSHFLDOPRPHQWVRFFXUV 0RVWKDSSHQMXVWRQFH FRQVWUXFWLRQFUHDWLRQ UHPRYDO EXWDFWLYDWLRQSDVVLYDWLRQDQG EXVLQHVVPHWKRGVFDQKDSSHQRYHU DQGRYHUDJDLQ *MIV ,ZULWHWKHEHDQFRGH%HVLGHVWKH EXVLQHVVPHWKRGV,KDYHWRLPSOHPHQW WKHFRQWDLQHUFDOOEDFNV WKHRQHVWKH FRQWDLQHUFDOOVZKHQVRPHWKLQJKDSSHQVLQWKH EHDQ·VOLIH ,DOVRKDYHWRZULWHWKHHME&UHDWH PHWKRGVWRPDWFKWKHKRPHFUHDWH PHWKRGV $QG,·PXQGHUSDLGDQGWKH\VWLOOKDYHQ·W JLYHQPHRQHRIWKRVH$HURQFKDLUV *MIV8ZW^QLMZ ,FRQVWUXFWWKHEHDQ, JLYHWKHEHDQLWVEHDQQHVV VSHFLDOEHDQFDSDELOLWLHV DQG,FDOOFUHDWH RQWKHEHDQSDVVLQJLQFOLHQWDUJXPHQWVLI LW·VDVWDWHIXOEHDQ,GHFLGHLIDQGZKHQWKH EHDQVKRXOGVOHHSDQGZDNHXSDQG,·PWKH RQO\RQHWKDWFDQNLOO UHPRYH DEHDQ, GHFLGHKRZDQGLIDEXVLQHVVPHWKRGLV FDOOHGRQDEHDQIDFWRULQJLQWKLQJV OLNHVHFXULW\DQGWUDQVDFWLRQV 7RDEHDQ,·PDJRG +WV\IQVMZ \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Page 6 Return to Table of Contents VHVVLRQEHDQV AW]ZMUMUJMZPW_Q\ITT_WZS[ZQOP\ ' /M\\QVOIVL][QVOI[\I\MN]T[M[[QWVJMIV 4HIS PICTURE SHOWS WHAT HAPPENS AFTER THE CLIENT DOES A LOOKUP IN *.$) AND GETS THE HOME STUB )N OTHER WORDS IT SHOWS THE CLIENT GETTING AND THEN USING A BEAN 4HIS PICTURE IS STILL TOO HIGH LEVEL THOUGH SO WELL GO A LOT DEEPER LATER IN THIS CHAPTER &OR EXAMPLE WELL COVER WHAT REALLY HAPPENS BETWEEN STEP TWO HOME OBJECT GETS THE CLIENTS CREATE CALL AND STEP FOUR HOME MAKES THE BEAN 4HE EXAM EXPECTS YOU TO KNOW JUST EXACTLY WHATS INVOLVED IN hMAKES THE BEANv &OLHQWFDOOVFUHDWH RQWKHKRPHVWXE +RPHREMHFWJHWVWKHFUHDWH FDOO creating a bean +RPHFRQWDLQHUPDNHVWKH(-%2EMHFW FRPSRQHQWLQWHUIDFH IRUWKHEHDQ +RPHFRQWDLQHUPDNHVWKHEHDQ +RPHUHWXUQVWKH(-%2EMHFW FRPSRQHQWLQWHUIDFH VWXEWRWKHFOLHQW &OLHQWFDOOVDEXVLQHVVPHWKRGRQWKHFRPSRQHQWLQWHUIDFHVWXE JHW$GYLFH using a bean (-%2EMHFWJHWVWKHJHW$GYLFH FDOO &RQWDLQHUVWHSVLQDQGJHW$GYLFH LVFDOOHGRQWKHEHDQ &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 7 VHVVLRQEHDQOLIHF\FOH 6WDWHIXO6HVVLRQ%HDQ &5($7,21 (-% REMHFW VWXE FU HD WH UHW XUQ VWX E H PDN +RPH REMHFW VWXE HV YLF VHU FOLHQW PDNH EHDQ H FUHDW 86( EXVLQHVVPHWKRGFDOO LFH GY W$ H J VWXE JHW$GYLFH VHUYLFHV (-% REMHFW JHW$GYLFH EHDQ FOLHQW +RPH REMHFW \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 8 VHVVLRQEHDQV <PMZM¼[WJ^QW][TaUWZM\W\PMJMIV¼[TQNMKaKTM \PIVR][\KZMI\QWVIVLJ][QVM[[UM\PWL[ 7HAT ABOUT WHEN A CLIENT WANTS TO REMOVE A BEAN 7HAT ABOUT ACTIVATION AND PASSIVATION OF STATEFUL SESSION BEANS !ND WHAT REALLY DOES HAPPEN WHEN A BEAN IS CREATED 7HAT MAKES BEAN CREATION DIFFERENT FROM PLAIN OLD OBJECT CONSTRUCTION )N THIS CHAPTER WELL LOOK AT ALL THE STAGES OF A SESSION BEANS LIFE FOR BOTH STATEFUL AND STATELESS SESSION BEANS 7KHEHDQOLIHF\FOH VSHFLDOPRPHQWVLQDEHDQ·VOLIH 6WDWHIXOVHVVLRQEHDQV %HDQFUHDWLRQ ZKHQWKHFOLHQWZDQWVDEHDQ %HDQXVH ZKHQWKHFOLHQWFDOOVDEXVLQHVVPHWKRG %HDQSDVVLYDWLRQ WKHEHDQLVSXWWRVOHHSWRFRQVHUYH UHVRXUFHV %HDQDFWLYDWLRQ WKHEHDQZDNHVXSWRVHUYLFHDEXVLQHVV PHWKRGIURPWKHFOLHQW %HDQUHPRYDO ZKHQWKHFOLHQWLVILQLVKHGZLWKWKHEHDQRU WKHEHDQWLPHVRXW 6WDWHOHVVVHVVLRQEHDQV %HDQFUHDWLRQ ZKHQWKHFRQWDLQHUZDQWVWRPDNHDEHDQ %HDQXVH ZKHQWKHFOLHQWFDOOVDEXVLQHVVPHWKRG %HDQUHPRYDO ZKHQWKHFRQWDLQHUGHFLGHVWKHUHDUHWRR PDQ\EHDQVLQWKHSRRO &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 9 VHVVLRQEHDQOLIHF\FOH +WV\IQVMZ+ITTJIKS[ NWZ\PM[XMKQITUWUMV\[QVIJMIV¼[TQNM +H\LWKDSSHQVRQO\ RQFHLQDOLIHWLPHVR \RXEHWWHUPDNHWKH PRVWRILW )RUPH"7KLV LVVXFKDVSHFLDO PRPHQW $VD%HDQ3URYLGHU<28PXVWLPSOHPHQW FRQWDLQHUFDOOEDFNVLQ\RXUEHDQFODVV 7HEN A BEAN HAS A SPECIAL MOMENT IT DOESNT KNOW UNTIL THE #ONTAINER CALLS ONE OF THE BEANS CONTAINER CALLBACK METHODS 4HESE ARE SPECIAL METHODS THE #ONTAINER KNOWS ABOUT THAT YOU THE "EAN 0ROVIDER MUST IMPLEMENT IN YOUR BEAN CLASS 9OU CAN THINK OF THE CONTAINER CALLBACKS AS BEING KIND OF LIKE EVENT HANDLERS #ONTAINER CALLBACKS COME FROM TWO PLACES YOUR BEANS HOME INTERFACE AND THE 3ESSION"EAN INTERFACE YOUR SESSION BEAN CLASS MUST IMPLEMENT &OR A SESSION BEAN THE HOME RELATED CONTAINER CALLBACKS ARE MATCHING CREATION METHODS EJB#REATE FOR EACH CREATE METHOD DECLARED IN THE BEANS HOME INTERFACE 4HE 3ESSION"EAN INTERFACE DECLARES FOUR CONTAINER CALLBACK METHODS FOR GIVING THE BEAN ITS BEAN CONTEXT ACTIVATING IT PASSIVATING IT AND REMOVING IT 7ELL LOOK AT THE ACTUAL METHODS ON THE NEXT PAGE \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 10 FRQWDLQHUFDOOEDFNV +WV\IQVMZ+ITTJIKS[KWUMNZWU<?7XTIKM[" <RXUKRPHLQWHUIDFH 6HVVLRQ%HDQLQWHUIDFH MDYD[HME6HVVLRQ%HDQ LQWHUIDFH!! $GYLFH+RPH FUHDWH 6WULQJFOLHQW1DPH for eve the homry create() in a matc e, there must in the bhing ejbCreate( be ean ) $GYLFH%HDQ LQWHUIDFH!! 6HVVLRQ%HDQ VHW6HVVLRQ&RQWH[W 6HVVLRQ&RQWH[WVF VHW6HVVLRQ&RQWH[W 6HVVLRQ&RQWH[WVF HME$FWLYDWH HME$FWLYDWH HME3DVVLYDWH HME3DVVLYDWH HME5HPRYH HME5HPRYH HME&UHDWH 6WULQJFOLHQW1DPH JHW$GYLFH m the ethod fro Business nmt interface, not a compone r callback containe you must always implement these FOUR container callbac methods in your session bean k class &OR ANY SESSION BEAN YOU WILL ALWAYS ALWAYS ALWAYS HAVE AT LEAST lVE CONTAINER CALLBACKSFOUR FROM THE 3ESSION"EAN INTERFACE IMPLEMENTATIONS AND AT LEAST ONE EJB#REATE TO MATCH THE CREATE METHOD DECLARED IN THE HOME INTERFACE &OR STATELESS SESSION BEANS THERE WILL BE EXACTLY lVE CONTAINER CALLBACKS SINCE STATELESS SESSION BEANS CAN HAVE ONLY ONE CREATE METHOD !ND SINCE THE STATELESS BEANS CREATE IS ALWAYS A NO ARG YOU KNOW THAT THE BEANS EJB#REATE WILL ALWAYS BE A NO ARG &OR STATEFUL BEANS REMEMBER THERE MUST BE AT LEAST ONE CREATE BUT THERE CAN BE MORE INCLUDING BOTH OVERLOADED CREATE METHODS AND CREATESOMETHING METHODS &OR EXAMPLE A STATEFUL BEAN HOME MIGHT HAVE THREE CREATE METHODS CREATE CREATE3TRING S CREATE"IG#USTOMER3TRING S 4HAT WOULD MEAN THE BEAN WOULD HAVE THREE CONTAINER CALLBACKS FROM THE HOME EJB#REATE EJB#REATE3TRING S EJB#REATE"IG#USTOMER3TRING S &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 11 VHVVLRQEHDQOLIHF\FOH 1UXTMUMV\QVO\PMKWV\IQVMZKITTJIKS[ 4HIS !DVICE"EAN CODE IS COMPLETELY LEGAL )T COMPILES AND RUNS 2IGHT NOW WE DONT NEED ANYTHING IN THE CALLBACK METHODS BECAUSE THE BEANS BUSINESS LOGIC DOESNT DEPEND ON ANYTHING ELSE THAT HAPPENS AT OTHER TIMES IN THE BEANS LIFE 9OULL NOTICE THAT WE ARE SAVING THE 3ESSION#ONTEXT PARAMETER WE GET FROM THE SET3ESSION#ONTEXT CONTAINER CALLBACK BUT WE ARENT USING IT IN THIS CODE ,ATER IN THIS CHAPTER YOULL LEARN WHY YOU MIGHT WANT TO ASSIGN THE CONTEXT TO AN INSTANCE VARIABLE AND THE CIRCUMSTANCES UNDER WHICH YOU WOULD PUT CODE IN EACH OF THESE CALLBACK METHODS ent SessionBean You MUST implem SDFNDJHKHDG¿UVW LPSRUWMDYD[HME SXEOLFFODVV$GYLFH%HDQLPSOHPHQWV6HVVLRQ%HDQ^ instance variable to hold the context we SULYDWH6HVVLRQ&RQWH[WFRQWH[W get from a container callback method. SULYDWH6WULQJQDPH SXEOLFYRLGHME$FWLYDWH ^ ` thods SXEOLFYRLGHME3DVVLYDWH ` SXEOLFYRLGHME5HPRYH of SessionBean the four me implement that you must ^ ^ ` SXEOLFYRLGVHW6HVVLRQ&RQWH[W 6HVVLRQ&RQWH[WFW[ ^ FRQWH[W FW[ ` SXEOLFYRLGHME&UHDWH 6WULQJFOLHQW1DPH ^ QDPH FOLHQW1DPH ` business meth This must match the ate method from the home interfcre ace od from the component interface... not callback a container SXEOLF6WULQJJHW$GYLFH ^ UHWXUQ³$GYLFHIRU³QDPH³WKHUHLVQRVSRRQ´ ` ` \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 12 FRQWDLQHUFDOOEDFNV :KHQDUHFRQWDLQHUFDOOEDFNVLQYRNHG" )RUVWDWHIXOVHVVLRQEHDQVZKHQWKHEHDQWUDQVLWLRQV IURPRQHVWDWHWRDQRWKHU EHDQWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW GRHVQRWH[LVW FRQVWUXFWRU VHW6HVVLRQ&RQWH[W HME&UHDWH HME5HPRYH RUWLPHRXW WLPHRXW === === HME3DVVLYDWH SDVVLYDWHG PHWKRGUHDG\ PHWKRGFDOOV HME$FWLYDWH GRHVQRWH[LVW %HIRUHDEHDQLVDEHDQ RUHYHQDQREMHFW LWGRHVQRWH[LVW7KHVSHFPDNHVDELJSRLQWRI WKLVMXVWLQFDVHWKHUH·VDQ\FRQIXVLRQDERXWVRPHWKLQJH[LVWLQJEHIRUHLWH[LVWV6RWKHVWDWH RIQRQH[LVWHQFHLVLQGHHGDVWDWHWKDWDEHDQFDQEHLQHYHQWKRXJKLWGRHVQRW\HWH[LVW :KHQDVHVVLRQEHDQPRYHVRXWRIWKLVVWDWHWKHEHDQ·VFRQVWUXFWRUUXQVWKHQVHW6HVVLRQ &RQWH[W DQGILQDOO\WKHHME&UHDWH PHWKRG7KLVLVDOVRWKHVWDWHDEHDQUHWXUQVWRDIWHU DQ\RQHRIWKHVHWKLQJVRFFXUVEHDQWLPHVRXWEHDQWKURZVDV\VWHPH[FHSWLRQRUDFOLHQW FDOOVUHPRYH 7KHEHDQZLOOJHWDQHME5HPRYH FDOOLIWKHFOLHQWFDOOVUHPRYH RUWLPHVRXW ZKLOHLQWKHPHWKRGUHDG\ DFWLYH VWDWH PHWKRGUHDG\ $EHDQLQWKHPHWKRGUHDG\VWDWHLVHLWKHUH[HFXWLQJDFOLHQW·VPHWKRGRUZDLWLQJIRUWKHFOLHQW WRPDNHDQRWKHUEXVLQHVVPHWKRGFDOO$VWDWHIXOEHDQPLJKWRUPLJKWQRWEHLQDQDFWLYH WUDQVDFWLRQZKLOHLQWKLVVWDWH GHSHQGVRQWKHGHSOR\PHQWWUDQVDFWLRQVHWWLQJVDQGWKHFDOOHU·V WUDQVDFWLRQVWDWXV EXWLIWKHEHDQLVLQDWUDQVDFWLRQWKHEHDQFDQQRWEHSDVVLYDWHG SDVVLYDWHG $SDVVLYDWHGEHDQLVWHPSRUDULO\VDYHGLQVRPHNLQGRIVHFRQGDU\VWRUDJHWRFRQVHUYH UHVRXUFHVEHWZHHQFOLHQWFDOOV3DVVLYDWLRQPLJKWEHVHULDOL]DWLRQDOWKRXJKWKH&RQWDLQHU FDQXVHDQ\WKLQJLWZDQWVDVORQJDVLWEHKDYHVOLNHVHULDOL]DWLRQ ZLWKRQHVPDOOH[FHSWLRQ ZH·OOVHHODWHULQWKLVFKDSWHU $OWKRXJKDSDVVLYDWHGEHDQLVQRORQJHUDQDFWLYHREMHFWRQ WKHKHDSWKH&RQWDLQHUZLOOUHDFWLYDWHWKHEHDQ FDOOLQJHME$FWLYDWH ZKHQWKHFOLHQWFDOOVD EXVLQHVVPHWKRG,IDSDVVLYDWHGEHDQWLPHVRXWWKHEHDQZLOOVLPSO\GLHZLWKRXWILUVWEHLQJ UHDFWLYDWHG &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 13 VHVVLRQEHDQOLIHF\FOH :HKDYHWRORRNDWWKHWUDQVLWLRQV .OW THAT YOUVE SEEN THE OVERALL STATE DIAGRAM WE HAVE TO DRILL DOWN AND lND OUT WHAT REALLY HAPPENS AND WHY WHEN A STATEFUL SESSION BEAN TRANSITIONS FROM ONE STATE TO ANOTHER 0RYLQJIURPGRHVQRWH[LVW WRPHWKRGUHDG\ EHDQWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW GRHVQRWH[LVW FRQVWUXFWRU FRQVWUXFWRU VHW6HVVLRQ&RQWH[W HME&UHDWH HME5HPRYH RUWLPHRXW WLPHRXW VHW6HVVLRQ&RQWH[W HME&UHDWH HME3DVVLYDWH SDVVLYDWHG PHWKRGUHDG\ HME$FWLYDWH 0RYLQJIURPPHWKRGUHDG\ WRSDVVLYDWHGDQGYLFHYHUVD EHDQWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW GRHVQRWH[LVW FRQVWUXFWRU HME3DVVLYDWH VHW6HVVLRQ&RQWH[W HME&UHDWH HME5HPRYH RUWLPHRXW HME$FWLYDWH WLPHRXW HME3DVVLYDWH PHWKRGUHDG\ SDVVLYDWHG HME$FWLYDWH 0RYLQJIURPSDVVLYDWHGRU PHWKRGUHDG\WRGRHVQRWH[LVW EHDQWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW GRHVQRWH[LVW FRQVWUXFWRU WLPHRXWV\VWHPH[FHSWLRQ VHW6HVVLRQ&RQWH[W HME&UHDWH HME5HPRYH RUWLPHRXW WLPHRXW RU HME5HPRYH HME3DVVLYDWH PHWKRGUHDG\ SDVVLYDWHG HME$FWLYDWH \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Page 14 Return to Table of Contents VHVVLRQEHDQV WKHUH DUH QR Q: 'XPE 4XHVWLRQV )F ITS SO COMMON TO LEAVE THE METHODS EMPTY WHY DONT THEY HAVE ADAPTER CLASSES LIKE THEY HAVE FOR EVENT HANDLERSTHAT IMPLEMENT ALL THE METHODS FROM THE INTERFACE )S THERE ANY REASON WHY YOUR BEAN CLASS CANT EXTEND A CLASS THAT IMPLEMENTS THE 3ESSION"EAN INTERFACE A: 4HE !0) DOESNT HAVE ADAPTER CLASSES FOR 3ESSION"EAN IMPLEMENTATIONS IE A CLASS THAT IMPLEMENTS ALL OF THE METHODS "UT THERES NO REASON YOU CANT MAKE ONE YOURSELF +EEP IN MIND THOUGH THAT WITH REAL WORLD BEANS YOU PROBABLY WILL HAVE CODE IN ONE OR MORE OF THE METHODS !ND YOU MIGHT EVEN BE WORKING WITH A BEAN AWARE )$% THAT PUTS THE METHODS IN FOR YOU ANYWAY 3TILL IT MIGHT BE HANDY TO MAKE YOURSELF A GENERIC BEAN THAT YOU TYPICALLY EXTEND FROM THAT HAS ALL OF THE METHODS FROM 3ESSION"EAN 7ITH STATELESS BEANS ESPECIALLY YOU HAVE TO IMPLEMENT EJB!CTIVATE AND EJB0ASSIVATE EVEN THOUGH THEYLL NEVER BE CALLED 3TATELESS BEANS ARE NEVER PASSIVATED YOULL SEE MORE ON THAT LATER IN THE CHAPTER Q: ) JUST REMEMBERED THAT ) READ SOMEWHERE THAT ENTERPRISE BEANS DONT SUPPORT INHERITANCE 7HATS THAT ABOUT A: !H A COMMON MISCONCEPTION 7ELL SORT OF %*" SUPPORTS REGULAR *AVA CLASS INHERITANCE BUT HAS NO CONCEPT OF BEAN INHERITANCE !ND NOW YOURE ASKING h7HAT THE HECK IS THE DIFFERENCEv 9OU ALREADY KNOW WHAT CLASS INHERITANCE IS ITS THE THING YOU DO IN *AVA WHEN ONE CLASS EXTENDS ANOTHER !ND YOU CAN DO THAT WITH A BEAN JUST LIKE ANY OTHER CLASS "UT BEAN INHERITANCE IF IT WERE SUPPORTED WOULD MEAN THAT A BEAN CLASS COULD EXTEND ANOTHER BEAN CLASS AND INHERIT NOT JUST THE CLASS INHERITABLE MEMBERS BUT ALSO ITS BEANNESS 7HAT KIND OF BEANNESS MIGHT BE INHERITABLE *UST IN CASE THEY DO DECIDE TO SUPPORT THIS IN THE FUTURE WHICH IS A POSSIBILITY 2EGULAR OLD NON ENTERPRISE BEANS DO SUPPORT BEAN INHERITANCE /NE IDEA MIGHT BE TO HAVE YOUR BEAN SUBCLASS INHERIT SOME OF THE DEPLOYMENT DESCRIPTOR SETTINGS OF ITS SUPERCLASS AND THEN OVERRIDE THE ONES IT WANTS TO CHANGE WITH A MUCH SMALLER INCOMPLETE DEPLOYMENT DESCRIPTOR 4HAT MIGHT BE COOL WERE NOT SURE "UT RIGHT NOW ITS JUST OUR LITTLE FANTASY )N THE MEANTIME GO AHEAD AND LET YOUR BEAN EXTEND ANOTHER CLASS IF IT MAKES SENSE FOR YOUR // DESIGN 6KDUSHQ \RXU SHQFLO )RUWKHH[DP\RXKDYHWRNQRZH[DFWO\ ZKLFKPHWKRGVDUHLQWKH6HVVLRQ%HDQ LQWHUIDFHVRQRZLVDJRRGWLPHWRVWDUW PHPRUL]LQJWKHP6HHLI\RXFDQUHPHPEHU WKHQDPHRIWKHPHWKRGWKDWPDWFKHVWKH EHKDYLRUGHVFULEHG:H·YHLQFOXGHGVRPH SUHWW\ELJKLQWVKHUHEHFDXVHLW·V\RXUILUVW WLPHEXWWKHPRFNH[DPTXHVWLRQVZLOOEH PXFKOHVVREYLRXV 7KLVPHWKRGLVFDOOHGZKHQWKHFOLHQWWHOOV WKH&RQWDLQHUWKDWKH·VGRQHXVLQJDVWDWHIXO VHVVLRQEHDQ7KHEHDQLV127KDSS\ BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB 7KLVPHWKRGLVFDOOHGZKHQWKHEHDQ LVSXWWRVOHHSWRWHPSRUDULO\FRQVHUYH UHVRXUFHV BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB 7KLVPHWKRGLVFDOOHGZKHQWKHSUHYLRXVO\ VOHHSLQJEHDQLVFDOOHGEDFNWRDFWLYHGXW\ WRVHUYLFHDEXVLQHVVPHWKRG BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB 7KLVPHWKRGLVFDOOHGQHDUWKHEHJLQQLQJ RIWKHEHDQ·VOLIHZKHQWKH&RQWDLQHUKDQGV WKHEHDQDUHIHUHQFHWRWKHEHDQ·VVSHFLDO OLQNWRWKH&RQWDLQHU BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 15 VHVVLRQEHDQOLIHF\FOH 6KDUSHQ \RXU SHQFLO )LOOLQWKHPLVVLQJPHWKRGV'RQ·WZRUU\LI\RXGRQ·WJHWWKH QDPHH[DFWO\ULJKWMXVWWU\WRZRUNRXWZKDWKDSSHQVDWHDFK RIWKHWUDQVLWLRQVLQWKHVWDWHIXOVHVVLRQEHDQVWDWHGLDJUDP GRHVQRWH[LVW EHDQWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW RUWLPHRXW === WLPHRXW PHWKRGUHDG\ === SDVVLYDWHG PHWKRGFDOOV <RXKDYHWR NQRZWKLV OLIHF\FOH KDQN ,I\RXWDNHWKHH[DP\RX¶OOW HFDXVH XVIRUEHDWLQJLWWRGHDWKE NZDUGV \RXUHDOO\PXVWNQRZLWEDF DUG DQGIRUZ HUWKLV %XWHYHQDVDEHDQGHYHORS \RXU LVVRIXQGDPHQWDOWRZULWLQJ QWWR EHDQFRGHWKDW\RX'2ZD LVQ¶W KDYHWKLVPHPRUL]HG7KLV L]DWLRQ OLNHRQHRIWKRVH$3,PHPRU N³,VQ¶W TXHVWLRQVZKHUH\RXWKLQ WKDWZKDWWKHGRFVDUHIRU"´ WSXWV (YHQLI\RXKDYHDQ,'(WKD WR\RXU WKHFRQWDLQHUFDOOEDFNVLQ WR FRGHIRU\RX\RXVWLOOKDYH KDW NQRZZKDWWKH\PHDQDQGZ RLQ \RXVKRXOGRUVKRXOGQRWG HDFKRQH \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 16 EHDQFUHDWLRQ GRHVQRWH[LVW FRQVWUXFWRU *MIV+ZMI\QWV" _PMVIVWJRMK\JMKWUM[IJMIV VHW6HVVLRQ&RQWH[W HME&UHDWH PHWKRGUHDG\ ejbCreate() text() andssion beanÕs life, on C on si es S t se se once in a to do happen onlylow it ! And donÕt tryor is too b ct Õt so don soon... the constru things too o any bean things. early to d 7KHSURXGHVWPRPHQWRIP\OLIH LVZKHQWKH*UDQG0DVWHU&RQWDLQHU PDNHVPHDEHDQE\JLYLQJPHDFRQWH[WDQG FDOOLQJP\FUHDWH %HIRUHWKDW,·PMXVWDQ RUGLQDU\REMHFW%XWDVDEHDQ,KDYHVSHFLDO SULYLOHJHV EHVLGHVWKHVHFUHWKDQGVKDNH OLNHWKHDELOLW\WRJHWVHFXULW\ RUWUDQVDFWLRQLQIR ! BEAN MOVES FROM DOES NOT EXIST TO METHOD READY STILL FEELS CREEPY DOESNT IT HAVE TO EXIST BEFORE IT CAN MOVE BEGINNING WITH A CONSTRUCTOR "UT THE CONSTRUCTOR MAKES AN OBJECT NOT A BEAN 4O BE A BEAN THE OBJECT NEEDS TO BE GRANTED BEANNESS 7HEN AN OBJECT BECOMES A BEAN IT GETS ALL THE UNIQUE PRIVILEGES THAT COME WITH BEANNESS LIKE THE ABILITY TO GET SECURITY INFO ABOUT THE CLIENT OR LOOK UP SPECIAL DEPLOY TIME PROPERTIES IN THE BEANS SPECIAL SPACE IN *.$) OR GET A *$"# #ONNECTION FROM A POOL MANAGED BY THE CONTAINER 7HY DO YOU CARE ABOUT CREATION DETAILS "ECAUSE SOMEWHERE BETWEEN THE CONSTRUCTOR AND THE CREATE METHOD THE BEAN IS IN A 3CHROEDINGERS BEAN STATE 9OU MIGHT HAVE BEAN INITIALIZATION CODE LIKE GETTING A *$"# #ONNECTION OR LOOKING UP A REFERENCE TO ANOTHER BEAN THAT WILL FAIL IF YOU RUN IT TOO EARLY IN THE BEANS LIFE 7RITING GOOD BEAN CODE MEANS YOU MUST KNOW THE POINT AT WHICH AN OBJECT BECOMES A CARD CARRYING BEAN AND WHAT THAT MEANS TO YOU AS THE DEVELOPER *WeÕre not explaining the whole Schroedinger reference here, except to say that it involves cats (animal-lovers be warned) and subatomic physics. For more info, look for Head First Quantum Physics in the future. WeÕre serious. Serious for us, anyway. &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 17 VHVVLRQEHDQOLIHF\FOH :KDWGRHV´EHDQQHVVµEX\\RX" WRWKLV" :KDWKDSSHQVZKHQD EHDQJRHVIURPWKLV card-carrying bean object ! BEANS CONTEXT SOMETIMES CALLED %*"#ONTEXT REFERRING TO THE SUPERINTERFACE OF 3ESSION#ONTEXT IS THE BEANS ONLY LIFELINE TO THE #ONTAINER AND IT LETS THE BEAN DO THINGS LIKE GET SECURITY INFORMATION ABOUT THE CALLING CLIENT FORCE A TRANSACTION TO ROLLBACK GET A REFERENCE TO THE BEANS OWN HOME OR %*" OBJECT AND MORE 'RQ·WFRQIXVH DEHDQFRQWH[W ZLWKD-1', FRQWH[W VDOOVR -XVWZKHQ\RXWKRXJKWLWZD RUWKDWWKH FOHDU\RX¿QGWR\RXUKRUU G ZRUG³FRQWH[W´LVRYHUORDGH VLRQ&RQWH[W $EHDQ¶VFRQWH[WOLNH6HV 'ULYHQ DJH HVV RU0 WH[W &RQ QWLW\ RU( UHIHU FLDO VSH DQ¶V HEH LVWK &RQWH[W W IRU QWH[ %&R V(WHQG KH[ HQFH ZKLF QHU JHWWLQJLQIRIURPWKH&RQWDL $VSHFLDO-1',&RQWH[W %VERY BEAN GETS ITS VERY OWN *.$) CONTEXT WHERE IT CAN lND THINGS INCLUDING RESOURCE MANAGER CONNECTION FACTORIES OBJECTS THAT GIVE YOU CONNECTIONS TO RESOURCES LIKE A DATABASE OTHER BEANS AND DEPLOY TIME CONSTANT VALUES THAT YOU CAN USE TO CUSTOMIZE YOUR BEANS VARIABLES 9OULL LEARN A LOT MORE ABOUT THAT IN #HAPTER WHERE WE LOOK AT THE ENTERPRISE BEAN ENVIRONMENT THATS WHAT THE SPEC CALLS THE BEANS SPECIAL *.$) CONTEXT LWKD-1', 7KLVKDV127+,1*WRGRZ HPEHULV FRQWH[W$-1',FRQWH[WUHP ,YLUWXDO DSDUWLFXODU³QRGH´RQD-1' [WLVWKH GLUHFWRU\WUHH$Q,QLWLDO&RQWH UHQWU\ \RX DV UYHV WVH WWKD QWH[ -1',FR WKHU ,QR XUH UXFW HVW HWUH SRLQWLQWRWK \RXVWDUW ZRUGVWKHSODFHIURPZKLFK QDYLJDWLQJWKURXJKWKHWUHH RUG³FRQWH[W´ :KHQHYHU\RXVHHWKHZ WH[WRIWKH ORRNFORVHO\DWWKHXPFRQ KFRQWH[W ZKLF WWR HRX JXU ¿ QWR VWLR TXH (-% DQ¶V HEH ²WK IHUV QUH VWLR WKHTXH K HHV 6K WH[W FRQ 1', &RQWH[WRU- $6HVVLRQ&RQWH[WUHIHUHQFH $FFHVVWREHDQVDQGUHVRXUFHV *UST BECAUSE YOU GOT A CONNECTION TO A DATABASE DOESNT MEAN YOU CAN ALWAYS USE IT 0ART OF BEING A BEAN IS THE ABILITY TO ACCESS A RESOURCE LIKE A DATABASE OR CALL METHODS ON ANOTHER ENTERPRISE BEAN \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 18 VSHFLDOEHDQWKLQJV 6SHFLDO%HDQ7KLQJV $6HVVLRQ&RQWH[WUHIHUHQFH 7KLQJV\RXFDQGRZLWK\RXUFRQWH[W (-%&RQWH[W JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\EHHQVHWWRUROOEDFN &07 JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOOPHWKRGVRQLW %07 $VSHFLDO-1',&RQWH[W 7KLQJV\RXFDQORRNXSZLWK\RXUVSHFLDO -1',FRQWH[W )RR DUHIHUHQFHWRDQRWKHUEHDQ DUHIHUHQFHWRDUHVRXUFHPDQDJHUFRQQHFWLRQIDFWRU\ OLNH 'DWD6RXUFH WKDW\RXFDQXVHWRJHWIRUH[DPSOHDGDWDEDVH FRQQHFWLRQ 0\3ULYDWH3ODFH %DU GHSOR\WLPHFRQVWDQWYDOXHV NLQGRIOLNHSURSHUWLHV IRUWKHEHDQ YDOXHVVHWE\WKHGHSOR\HUWKDWWKHEHDQFDQORRNXSDQGXVH DVYDULDEOHVDWUXQWLPH a beanÕs special JNDI reference DUHIHUHQFHWRDQ´DGPLQLVWHUHGREMHFWµUHVRXUFH ZKLFKXVXDOO\ PHDQVD-06GHVWLQDWLRQ '% $FFHVVWR DQRWKHUEHDQ DUHVRXUFHPDQDJHU OLNHDGDWDEDVH &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Page 19 Return to Table of Contents VHVVLRQEHDQOLIHF\FOH <RXFDQ·WDOZD\VXVHDOORI \RXUEHDQQHVV *UST BECAUSE YOUVE BECOME A FULL mEDGED BEAN DOESNT NECESSARILY MEAN YOU CAN DO ALL OF THE "EAN 4HINGS ON THE OPPOSITE PAGE LOOKUP A RESOURCE FORCE A TRANSACTION ROLLBACK ETC 4HE THINGS YOU CAN DO CAN VARY DEPENDING ON WHAT KIND OF BEAN YOU ARE 3ESSION %NTITY -ESSAGE$RIVEN YOUR TRANSACTION STATUS AND WHAT KIND OF METHOD YOURE IN <RXPXVWNQRZ :+(1\RXFDQ GRVSHFLÀF%HDQ 7KLQJVDQGZKHQ \RXFDQ·W VFHQDULR %HSUHSDUHGIRUFRGHRUD DQGDVNV WKDWVKRZV\RXDPHWKRG ODU%HDQ ZKHWKHU\RXFDQGRDSDUWLFX WKH LYHQ GJ HWKR DWP PWK 7KLQJIUR HV FLUFXPVWDQF VKRZQ )RUH[DPSOH\RXPLJKWEH WKRGRID WKHVHW6HVVLRQ&RQWH[W PH OORZHGWR EHDQDQGDVNHGLI\RX¶UHD WDEDVH DFFHVVDUHVRXUFH OLNHDGD RW<RX IURPWKDWPHWKRG <RX¶UHQ DFWLRQ DUHQ¶WLQD³PHDQLQJIXOWUDQV QW SRL WKDW ´DW FRQWH[W &OR EXAMPLE IF YOURE A STATEFUL SESSION BEAN YOUR CREATION IS A DIRECT RESULT OF THE CLIENT CALLING CREATE ON YOUR HOME )F YOURE A STATEFUL BEAN AND YOURE IN YOUR EJB#REATE METHOD THERE CAN BE ONLY ONE REASON A CLIENT HAS ASKED FOR YOU TO BE CREATED !ND THAT MEANS YOU CAN lND OUT WHO THE CLIENT IS BY ASKING YOUR %*"#ONTEXT LIKE 3ESSION#ONTEXT FOR CLIENT SECURITY INFORMATION "UT IF YOURE A STATELESS BEAN YOUR CREATION ISNT TIED TO ANY PARTICULAR CLIENTS REQUEST YOULL LEARN ALL ABOUT THIS IN A FEW MORE PAGES )N FACT IF THE #ONTAINER WANTS TO IT CAN CREATE A BUNCH OF STATELESS BEANS FOR THE POOL BEFORE THERE ARE ANY CLIENTS !ND THAT MEANS A STATELESS BEAN CANNOT GET SECURITY INFORMATION ABOUT THE CLIENT DURING EJB#REATE "ECAUSE THERES NO CLIENT 4HERES ONLY THE #ONTAINER INVOKING A CONTAINER CALLBACK THATS NOT PART OF A CLIENT CALL AND WE DONT CONSIDER THE #ONTAINER TO BE A CLIENT 4HE #ONTAINER IS THE BOSS NOT THE CLIENT !ND THERE ARE SOME "EAN 4HINGS THAT CAN BE DONE ONLY WHILE THE BEAN IS IN WHAT THE #ONTAINER CONSIDERS hA MEANINGFUL TRANSACTION CONTEXTv 9OU CANT FOR EXAMPLE ACCESS A DATABASE FROM A METHOD THAT MIGHT NOT HAVE A TRANSACTION 3OME "EAN 4HINGS ARE MUTUALLY EXCLUSIVE )F YOURE USING CONTAINER MANAGED TRANSACTIONS #-4 #HAPTER YOU MUST NOT ASK YOUR %*"#ONTEXT TO GIVE YOU YOUR OWN TRANSACTION OBJECT /N THE OTHER HAND IF YOURE USING BEAN MANAGED TRANSACTIONS "-4 YOU CAN ASK FOR A TRANSACTION OBJECT BUT THEN YOU MUST NOT ASK YOUR CONTEXT TO ROLLBACK YOUR TRANSACTION 9OULL HAVE TO ROLLBACK THE TRANSACTION YOURSELF USING THE TRANSACTION OBJECT YOU GOT FROM YOUR CONTEXT \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 20 EHDQFUHDWLRQ %HDQFUHDWLRQRYHUYLHZ VWDWHIXOVHVVLRQEHDQ &OLHQWFDOOVFUHDWHRQWKHKRPH FOLHQW +RPH REMHFW VWXE FUHDWH FUHDWH &RQWDLQHUPDNHVWKH(-%REMHFWDQG 6HVVLRQ&RQWH[W (-% REMHFW 6HVVLRQ &RQWH[W &RQWDLQHUFRQVWUXFWVWKH EHDQLQVWDQFH EHDQ &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Page 21 Return to Table of Contents VHVVLRQEHDQOLIHF\FOH &RQWDLQHUOLQNVWKHEHDQWRLWV FRQWH[WDQG(-%REMHFW FDOOLQJ VHW6HVVLRQ&RQWH[W DQGHME&UHDWH VH W6 H HM VVLR E& Q& UH RQ DW WH H [W EHDQ 4HE LINKING HAPPENS WHEN THE CONTAINER CALLS SET3ESSION#ONTEXT AND EJB#REATE ON THE BEAN /NCE EJB#REATE RETURNS THE BEAN IS READY FOR @ACTIVE DUTY IN OTHER WORDS READY FOR BUSINESS METHOD CALLS FROM THE CLIENT &RQWDLQHUVHQGVWKHFOLHQWDVWXE WRWKH(-%REMHFW VWXE \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 22 EHDQFUHDWLRQ 2EMHFW,QWHUDFWLRQ'LDJUDP 2,' IRUEHDQFUHDWLRQ VWDWHIXOVHVVLRQEHDQ FOLHQW +RPH REMHFW (-% REMHFW 6HVVLRQ &RQWH[W EHDQ FUHDWH QHZ QHZ QHZ EHDQ·VFRQVWUXFWRUUXQV VHW6HVVLRQ&RQWH[W HME&UHDWH E TO NT HAV 9OU DO , TO PASS KNOW 5 M THE EXA TO U UIRE YO -, IS SNT REQ AM DOE OUGH A LITTLE 5 ST X E E H 4 E H B LT R A U , O F K NOW 5 SPEC 3OME O )T S NOTHING *" , % E 5 TH IN VE R N OW Y (OWE G DONT K IN FRIENDS AMED OF 2EALL IN A M N RE SH TO BE A ONE OF THE TE T K NOW 5-, O RE N A O U IF YO FAC T D BLIC WHO IN RISK PU PEOPLE ONT WANT TO YOU MIGHT D LE U AND YO ON AND RIDICU ST 5-, TI EAD &IR HUMILIA ONSIDER OUR ( C WANT TO S BOOK RN 0ATTE &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 23 VHVVLRQEHDQOLIHF\FOH :ULWLQJWKHWKUHHFUHDWLRQUHODWHGWKLQJV VWDWHIXOVHVVLRQEHDQ FRQVWUXFWRU 'RQ·WSXWDQ\WKLQJLQWKHFRQVWUXFWRU7KHUH·VQRWKLQJ\RXZRXOGGRLQ WKHFRQVWUXFWRUWKDW\RXFDQ·WGRLQHME&UHDWH VRXQOHVV\RXU,'(SXWV RQHLQIRU\RX\RX·UHEHWWHURIIOHDYLQJWKHFRQVWUXFWRURXWRI\RXUFRGH SXEOLFFODVV$GYLFH%HDQLPSOHPHQWV6HVVLRQ%HDQ^ ld look nstructor shouput a co e th t ha w SXEOLF$GYLFH%HDQ ^ Õt This is y. Better yet, don ve QRFRGHKHUH like!! Empt in and let the compiler gi ` r to uc tr ns co nstructor. the default co ` you VHW6HVVLRQ&RQWH[W 6HVVLRQ&RQWH[WVF 6DYH\RXUFRQWH[W<RXJHWRQO\RQHFKDQFHWRJUDEWKLVUHIHUHQFHWR \RXU6HVVLRQ&RQWH[WVR\RXEHWWHUDVVLJQLWWRDQLQVWDQFHYDULDEOH SULYDWH6HVVLRQ&RQWH[WFRQWH[W SXEOLFYRLGVHW6HVVLRQ&RQWH[W 6HVVLRQ&RQWH[WFW[ ^ FRQWH[W FW[ save the SessionContext by assigning ` it to an instance variable. You will NEVER get another chan a reference to your context, so you bett ce to have do it here! Anything else you would put in this methoder prob ably belongs in ejbCreate(). HME&UHDWH 3XWDOO\RXULQLWLDOL]DWLRQFRGHKHUH,ILW·VDVWDWHIXOEHDQDQGWKHFUHDWH PHWKRGKDVDUJXPHQWVWKH&RQWDLQHUZLOOSDVVWKRVHDUJXPHQWVLQWR\RXU PDWFKLQJHME&UHDWH PHWKRG<RXKDYHIXOOEHDQVWDWXVQRZVR\RXFDQ GRDQ\WKLQJ\RXQHHGWRIURPWKLVPHWKRGLQFOXGLQJWKLQJV\RXFDQ·WGRLQ VHW6HVVLRQ&RQWH[W OLNHJHWDUHIHUHQFHWR\RXURZQ(-%REMHFW SULYDWH6WULQJQDPH SXEOLFYRLGHME&UHDWH 6WULQJFOLHQW1DPH ^ QDPH FOLHQW1DPH RWKHUFRGH ` YouÕre a full member of the bean society now, so this is where youÕll usually put ALL your initialization code. \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 24 EHDQFUHDWLRQ *MIV\PQVO[aW]KIVLWL]ZQVOKZMI\QWV" VWDWHIXOVHVVLRQEHDQ WLPHOLQH FRQVWUXFWRU VHW6HVVLRQ&RQWH[W HME&UHDWH QRWKLQJ 8VH\RXU6HVVLRQ&RQWH[WWR 8VH\RXU6HVVLRQ&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 ItÕs too early to DO anything! YouÕre an object, but not yet a bean. EHDQV EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQV PHWKRGVRQLW %07EHDQV $FFHVV $FFHVV \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH 1RWHWKHZRUG³DFFHVV´PHDQV³GRWKLQJVZLWK´VRZKHQWKHVSHFXVHV WKHSKUDVH³UHVRXUFHPDQDJHUDFFHVV´LWPHDQVXVLQJWKHUHVRXUFHWR GRZKDWHYHUWKDWUHVRXUFHLVIRU6RLQVHW6HVVLRQ&RQWH[W IRUH[DPSOH \RXFDQXVH-1',WRORRNXSDUHVRXUFHPDQDJHUFRQQHFWLRQIDFWRU\OLNH MDYD[VTO'DWD6RXUFHDQG\RXFDQDVNWKH'DWD6RXUFHIRUDFRQQHFWLRQ P\'DWD6RXUFHJHW&RQQHFWLRQ EXW\RXFDQ¶WPDNHD-'%&FDOORQWKH FRQQHFWLRQUHIHUHQFH<RXFDQXVH\RXUFRQQHFWLRQLQHME&UHDWH &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Page 25 Return to Table of Contents VHVVLRQEHDQOLIHF\FOH 8PJRRGWR NQRZDOOWKDWEXW WKLVPDWWHUVWRPH+2:" :KDWGRHVWKLVPHDQ WR0(DQGKRZGRHVLW DIIHFWP\FRGH" 6KHGRHVQ·WJHWLWIRURQHWKLQJ,GRQ·WSXW $1<7+,1*LQP\FRQVWUXFWRU2QHWLPH,WULHGWR GRD-1',ORRNXSLQDFRQVWUXFWRUWRJHWDGDWDEDVH UHVRXUFHIDFWRU\,WFRPSLOHGDQGGLGQ·WWKURZDQ H[FHSWLRQGXULQJWKHORRNXSEXWDWUXQWLPHZKHQ,WULHG WRJHWDFRQQHFWLRQLWEOHZXS7KHEHDQKDGQ·WJRWWHQLWV VSHFLDOEHDQ-1',FDSDELOLWLHV\HWVRWKHUHVRXUFHIDFWRU\ ZDVQ·WUHDOO\WKHUH7KHUHLV127+,1*\RXZRXOGGRLQ DFRQVWUXFWRUWKDW\RXFDQ·WGRLQHME&UHDWH LQVWHDG 0\EHVWDGYLFHLVWRWKLQNRIHME&UHDWH DVWKH FRQVWUXFWRUIRUVHVVLRQEHDQV HQWLW\EHDQVDUH DQRWKHUVWRU\ DQDVSLULQJ%HDQ3URYLGHU WKHPRUHH[SHULHQFHG %HDQ3URYLGHU \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 26 EHDQXVH GRHVQRWH[LVW PHWKRGUHDG\ the ready state,usiness od h et m e h In t running a bhe bean is either ting for t ai w method or ll one... client to ca *MIV=[M" _PI\PIXXMV[).<-:KZMI\QWV 7KHRQO\UHDVRQ,FDOOHG FUHDWH RQWKHKRPHLVVR,FRXOG JHWDUHIHUHQFHWRDEHDQ·VFRPSRQHQW LQWHUIDFHDQGGRZKDW,5($//<ZDQW FDOODEXVLQHVVPHWKRG)DUDV,·P FRQFHUQHGWKDW·VWKH21/<UHDVRQ WKHEHDQ·VDOLYH7RVHUYLFH 0(VR,FDQVKRS $WODVW,·PIXOÀOOLQJ P\GHVWLQ\0\SXUSRVH 0\UDLVRQG·rWUH )N THE METHOD READY STATE THE BEAN CAN RUN BUSINESS METHODS !ND GIVEN THAT BUSINESS METHODS ARE THE BEANS HIGHEST PURPOSE THE BEAN CAN BRING ALL OF ITS BEANNESS TO BEAR )N OTHER WORDS THE BEAN CAN DO MORE BEAN THINGS FROM WITHIN A BUSINESS METHOD THAN AT ANY OTHER POINT IN ITS BRIEF BUT MEANINGFUL LIFE :HFKDOOHQJH\RXWR¿QGDQRWKHUFRPSXWHUERRNWKDWXVHV UDLVRQG¶rWUHDVDSSURSULDWHO\DVZHGR$QGZLWKWKDWOLWWOH XSVLGHGRZQYWKLQJRYHUWKHrDQGHYHU\WKLQJ &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 27 VHVVLRQEHDQOLIHF\FOH *MIV\PQVO[aW]KIVLW_Q\PQVJ][QVM[[UM\PWL[" 8VH\RXU6HVVLRQ&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQV $FFHVV \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 28 EHDQSDVVLYDWLRQ HME3DVVLYDWH PHWKRGUHDG\ SDVVLYDWHG HME$FWLYDWH 8I[[Q^I\QWV"I[\I\MN]TJMIV¼[KPIVKM I\[KITIJQTQ\a $UH\RXVXUHWKLV FDQ·WZDLWXQWLOODWHU", ZDVULJKWLQWKHPLGGOHRI VKRSSLQJRKZHOO,JXHVV P\VKRSSLQJFDUWEHDQZLOO VWLOOEHWKHUHZKHQ ZH·UHGRQH 7KDWFOLHQWGRHVQ·WVHHPWREH GRLQJDQ\WKLQJ,W·VWRRHDUO\WRNLOO WKHEHDQFRPSOHWHO\VKHVWLOOPLJKWFRPH EDFN%XW,GRQ·WZDQWWKHEHDQZDVWLQJ 5$0,·OOVHULDOL]HWKHEHDQIRUQRZDQG LIWKHFOLHQWFRPHVEDFN,·OOMXVW GHVHULDOL]HLW /RRNV OLNH,·PEHLQJ SDVVLYDWHGVR,EHWWHU PDNHVXUH,QXOORXWP\ QRQ6HULDOL]DEOHLQVWDQFH YDULDEOHV === === d directly beans are tie StateFUL , remember, so until ut to a client ner decides to timeoas the Contai(because the client w n the bean ay too long), the bea inactive w ed for anything (or canÕt be usse. anyone) el ave this eans donÕt h a pool b S S E eL at St go back toll, since a problem, they method ca ch ea between tied to a client. bean isnÕt 7HEN THE #ONTAINER DECIDES A STATEFUL BEAN IS WASTING RESOURCES ITLL CALL THE BEANS EJB0ASSIVATE METHOD AND THEN SAVE THE BEAN INTO TEMPORARY STORAGE 7HY DO YOU CARE ABOUT PASSIVATION DETAILS "ECAUSE THE #ONTAINER USES A SPECIAL SET OF RULES NEARLY IDENTICAL TO 3ERIALIZATION TO PASSIVATE YOUR BEAN AND ITS YOUR JOB TO MAKE SURE YOUR INSTANCE VARIABLES ARE IN A STATE THAT WORKS FOR PASSIVATION !ND ITS JUST A TINY BIT MORE SUBTLE THAN SIMPLY MAKING NON 3ERIALIZABLE VALUES TRANSIENT &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 29 VHVVLRQEHDQOLIHF\FOH /LIHF\FOHRYHUYLHZEHDQSDVVLYDWLRQDFWLYDWLRQ &OLHQWGRHVQ·WFDOODQ\PHWKRGV IRUDZKLOHVRFRQWDLQHUFDOOV HME3DVVLYDWH RQWKHEHDQ (-% REMHFW FOLHQW VWXE EHDQ WH VLYD 3DV HME &RQWDLQHUFDOOVHME3DVVLYDWH RQ WKHEHDQWKHQVDYHVWKHEHDQWR WHPSRUDU\VWRUDJH HLWKHUWKURXJK VHULDOL]DWLRQRUVRPHWKLQJOLNHLW === === EHDQ (-% REMHFW FOLHQW EHDQLVDVOHHS SDVVLYDWHGDQG WHPSRUDULO\VDYHG VWXE &OLHQWFDOOVDEXVLQHVVPHWKRGVR FRQWDLQHUDFWLYDWHVWKHEHDQ WKURXJK VRPHWKLQJOLNHGHVHULDOL]DWLRQ FDOOV HME$FWLYDWH WKHQLQYRNHVWKHEXVLQHVV PHWKRG JHW$GYLFH RQWKHEHDQ JHW$GYLFH JHW$GYLFH FOLHQW VWXE H GYLF JHW$ (-% REMHFW EHDQ DWH FWLY HME$ \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 30 EHDQSDVVLYDWLRQ WKHUH DUH QR 'XPE 4XHVWLRQV Q: (OW DOES THE #ONTAINER KNOW WHEN TO PASSIVATE A BEAN A: 4HE #ONTAINER LOOKS FOR INAC TIVITY )F THE CLIENT HASNT BEEN DOING ANYTHING FOR SOME PERIOD OF TIME THE #ONTAINER CAN CHOOSE TO PASSIV ATE THE BEAN )F IT NEEDS TO Q: 'EE CAN YOU BE ANY MORE NON COMMITTAL (OW LONG IS hSOME PERIOD OF TIMEv AND WHAT DETERMINES WHETHER THE #ONTAINER @NEEDS TO PASSIVATE THE BEAN A: 4HERE IS NOTHING IN THE SPEC THAT SAYS YOURE ALLOWED TO TUNE THE PARAMETERS FOR THE #ONTAINERS PASSIVATION DECISIONS "UT MANY CONTAINERS DO GIVE YOU A WAY TO SET A VARIETY OF VALUES 4HE DECISION MIGHT BE BASED ON AVAILABILITY OF RE SOURCES LIKE hPASSIVATE STATEFUL SES SION BEANS ONLY WHEN AVAILABLE 2!HITS THIS LEVELv OR hPASSIVATE BEANS ONLY WHEN THE NUMBER OF ACTIVE STATEFUL SESSION BEANS REACHESv !ND YOU MIGHT BE ABLE TO SET THE INACTIVITY VALUE FOR THE AMOUNT OF ELAPSED TIME AT WHICH THE #ONTAINER SHOULD PASSIVATE Q: 7HAT IF THE CLIENT CRASHES #AN THE SERVER TELL )S THERE ANY KIND OF DISTRIBUTED GARBAGE COL LECTION OR LEASING LIKE THERE IS WITH 2-) /R IS THE SERVER REALLY JUST BASING EVERYTHING ON INACTIVITY A: !CCORDING TO THE SPEC THERE IS NO GUARANTEE OF DISTRIBUTED GAR BAGE COLLECTION WHICH MEANS YOU SHOULD ASSUME THAT IT ISNT HAPPEN ING 0ART OF THE REASON IS BECAUSE AN %*" CLIENT COULD AT LEAST IN THEORY BE A #/2"! CLIENT RATHER THAN A *AVA 2-) CLIENT 3O THE WHOLE CONCEPT OF 2-)S DISTRIBUTED GARBAGE COLLECTION WOULDNT APPLY !ND JUST A QUICK OFF PATH HERE DISTRIBUTED GARBAGE COLLECTION DGC OR DISTRIBUTED LEASING ARE WAYS IN WHICH A SERVER CAN FIGURE OUT IF A CLIENT HAS WITH DGC NULLED OUT THEIR REFERENCE TO THE STUB OR WITH LEASING IF THE CLIENT HAS SIMPLY GONE AWAY EITHER BY SHUTTING DOWN THE APP CRASHING OR DISCONNECTING FROM THE NETWORK 4HE BOTTOM LINE IS THAT YOU HAVE TO ASSUME THAT THE %*" SERVER DOESNT SUPPORT THIS SO YOURE STUCK WITH INACTIVITY AND PERHAPS SOME KIND OF ,25 ,EAST 2ECENTLY 5SED ALGO RITHMS FOR SELECTING WHO SHOULD BE PASSIVATED IF RESOURCES BECOME SCARCE AND THE #ONTAINER NEEDS TO BRING DOWN SOME BEANS 7KHUH¶VQRWKLQJRQWKH H[DPDERXWYHQGRU VSHFL¿FSDVVLYDWLRQ VHWWLQJVRUEHKDYLRU 7KHRQO\WKLQJRQWKHH[DPDERXW SDVVLYDWLRQLVZKDW\RX¶UHUHVSRQVLEOH IRUDVD%HDQ3URYLGHU²JHWWLQJ \RXULQVWDQFHYDULDEOHYDOXHVLQD SDVVLYDWDEOHVWDWH ZH¶OOWDONDERXW WKDWQH[W 1RWKLQJDERXWSDVVLYDWLRQ JRHVLQWRWKHGHSOR\PHQWGHVFULSWRU VR\RXGRQ¶WQHHGWRNQRZKRZ SDVVLYDWLRQSDUDPHWHUVDUHVHWIRU DQ\SDUWLFXODUVHUYHU &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 31 VHVVLRQEHDQOLIHF\FOH AW]ZRWJNWZXI[[Q^I\QWV" UISMaW]Z[\I\MXI[[Q^I\IJTM :KHQ,ZULWHP\EHDQFODVV, KDYHWRPDNHVXUHWKDWE\WKH WLPHHME3DVVLYDWH FRPSOHWHVP\ LQVWDQFHYDULDEOHYDOXHVDUH$//UHDG\ IRUSDVVLYDWLRQ7KDWPHDQVWKHYDOXHV KDYHWREH6HULDOL]DEOH RUSULPLWLYH QXOO RUUHIHUHQFHVWRRQHRIWKHVSHFLDO WKLQJVWKH&RQWDLQHUKDVWRWDNH FDUHRIQRPDWWHUZKDW :KHQHME3DVVLYDWH FRPSOHWHVHYHU\QRQWUDQVLHQWLQVWDQFH YDULDEOH0867EHDUHIHUHQFHWRRQHRIWKHIROORZLQJ D6HULDOL]DEOHREMHFW DQXOOYDOXH DEHDQ·VUHPRWHFRPSRQHQWRUKRPHLQWHUIDFHHYHQLIWKHVWXE FODVVLVQRW6HULDOL]DEOH LQRWKHUZRUGV\RXGRQ·WKDYHWRZRUU\ DERXWLW DEHDQ·VORFDOFRPSRQHQWRUKRPHLQWHUIDFHHYHQLILW·VQRW 6HULDOL]DEOH DJDLQ\RXGRQ·WKDYHWRZRUU\ D6HVVLRQ&RQWH[WREMHFWHYHQLILW·VQRW6HULDOL]DEOH WKHEHDQ·VVSHFLDO-1',FRQWH[WRUDQ\RILWVVXEFRQWH[WV WKH8VHU7UDQVDFWLRQLQWHUIDFH VRPHWKLQJ\RXFDQJHWIURP\RXU 6HVVLRQ&RQWH[W³ZH·OOVHHWKDWLQWKHWUDQVDFWLRQVFKDSWHU DUHVRXUFHPDQDJHUFRQQHFWLRQIDFWRU\ OLNHDQLQVWDQFHRI MDYD[VTO'DWD6RXUFH <RXKDYHWR NQRZWKHVH IRUWKHH[DP DW <RXPLJKWVHHDTXHVWLRQWK VKRZV\RXDFODVVDQGDQ DVNV HME3DVVLYDWH PHWKRGDQG \RXLILWZRXOGZRUNRUQRW KDYH ,IWKHPHWKRGLVHPSW\\RX ULDEOHVWR WRORRNDWWKHLQVWDQFHYD DUH VHHLIWKH\¶UHDOO2.DVWKH\ \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 32 DFWLYDWLRQSDVVLYDWLRQ ,PSOHPHQWLQJHME$FWLYDWH DQGHME3DVVLYDWH HME3DVVLYDWH 0DNHVXUH\RXULQVWDQFHYDULDEOHVDUHUHDG\IRUSDVVLYDWLRQ0RVW RIWKHWLPH\RXSUREDEO\ZRQ·WKDYHDQ\FRGHLQHME3DVVLYDWH VLPSO\ EHFDXVHDOORI\RXULQVWDQFHYDULDEOHVPHHWWKHFULWHULDGHILQHGRQWKH SUHYLRXVSDJH HJUHIHUHQFHWRD6HVVLRQ&RQWH[WRUDEHDQ·VFRPSRQHQW LQWHUIDFHRUD6HULDOL]DEOHREMHFWHWF SXEOLFYRLGHME3DVVLYDWH FRQQHFWLRQ QXOO ` ^ ection is notve one, you a JDBC conn so if you ha assivate() Serializable, to null in ejbP it t must se HME$FWLYDWH 5HDFTXLUH\RXUQRQ6HULDOL]DEOHUHVRXUFHVRUGRZKDWHYHULWWDNHV WRUHVWRUH\RXUVWDWHIRUXVH,I\RX·UHUXQQLQJLQHME$FWLYDWH WKHUH FDQEHRQO\RQHUHDVRQWKHFOLHQWFDOOHGDEXVLQHVVPHWKRG6RLQ\RXU HME$FWLYDWH PHWKRG\RXPXVWPDNHVXUH\RXJHWUHDG\IRUWKHEXVLQHVV PHWKRGFDOOWKDW·VDERXWWRKDSSHQ&KDQFHVDUHWKLVPHWKRGZLOOEH HPSW\IRUWKHVDPHUHDVRQDVHME3DVVLYDWH %XWLI\RXGLGQXOORXWQRQ 6HULDOL]DEOHUHIHUHQFHVWKHQHME$FWLYDWH LVWKHSODFHWRJHWWKHPEDFN SXEOLFYRLGHME$FWLYDWH ^ WU\^ FRQQHFWLRQ P\'DWD6UFJHW&RQQHFWLRQ `FDWFK ([FHSWLRQH[ ^` ` now we have to get the JDBC connection back again. Notice that the DataSource did not have to be resto itÕs one of the things on the ContainerÕs Òapprred, because (Because itÕs a resource manager connection oved listÓ. factory, and the Container is required to passivate it.) &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 33 VHVVLRQEHDQOLIHF\FOH WKHUH DUH QR 'XPE 4XHVWLRQV Q: #ANT YOU JUST MARK YOUR NON 3ERIALIZABLE VARIABLES TRANSIENT )SNT THAT WHAT TRANSIENT IS FOR A: 9OU COULD YES BECAUSE THE #ONTAINER IS REQUIRED TO FOLLOW THE RULES FOR 3ERIALIZATION WHEN IT PASSIVATES A BEAN EVEN IF THE #ONTAINER CHOOSES TO USE SOMETHING OTHER THAN SERIALIZATION TO GET THE JOB DONE "UT THERES A LITTLE TEENY COMMENT IN THE SPEC WHICH SAYS THAT THERES AN EXCEPTION TO THE RULE THAT PASSIVATION BEHAVE JUST LIKE SERIALIZATION 4HE EXCEPTION IS THAT WHILE SERIALIZATION IS REQUIRED TO BRING BACK TRANSIENT FIELDS WITH DEFAULT VALUES PASSIVATION DOESNT GUARANTEE THAT 7HAT DOES THIS MEAN 4HINK ABOUT IT )T MEANS YOU CANT RELY ON TRANSIENT TO GIVE YOU BACK YOUR DEFAULTS SO AFTER ACTIVATION YOU COULD END UP WITH WELL ANYTHING IN A VARIABLE THATS MARKED TRANSIENT 3O YOU ARE FREE TO USE TRANSIENT AND IT CAN MAKE PASSIVATION MORE EFFICIENT BUT THE IMPLICATION ISRESET YOUR TRANSIENT VARIABLES YOURSELF IN EJB!CTIVATE Q: 7HAT IF ) HAVE A NON 3ERIALIZABLE VALUE THAT ) REALLY NEED TO MAINTAIN DURING PASSIVATION )F ) CANT SAVE IT HOW WILL ) KNOW WHAT TO SET IT BACK TO IN EJB!CTIVATE A: 4HIS IS A CLASSIC SERIALIZATION ISSUE NOT SPECIFIC TO PASSIVATION 4HE USUAL TRICK IS TO INTERROGATE THE NON 3ERIALIZABLE OBJECT TO GET ALL THE IMPORTANT STATE OUT OF IT AND STICK THAT STATE INTO INSTANCE VARIABLES THAT ARE 3ERIALIZABLE 4HEN DURING EJB!CTIVATE USE THE VALUES YOU WERE ABLE TO SAVE TO RECONSTRUCT THE NON 3ERIALIZABLE OBJECT SO THAT ITS IDENTICAL TO THE ONE YOU HAD BEFORE PASSIVATION &OR EXAMPLE IMAGINE YOU HAVE A $OG BEAN WITH A #OLLAR VARIABLE AND THE #OLLAR ISNT 3ERIALIZABLE )N EJB0ASSIVATE CALL GETTERS ON THE #OLLAR TO RETRIEVE THE VALUES THAT MATTER LIKE GET#OLOR GET3IZE ETC 3AVE THOSE VALUES IN INSTANCE VARIABLES IN THE BEAN THEN IN EJB!CTIVATE INSTANTIATE A NEW #OLLAR AND USE THE #OLLAR ATTRIBUTES YOU SAVED AS ARGUMENTS TO SETTERS ON THE NEW #OLLAR OBJECT 6KDUSHQ \RXU SHQFLO <RX·UHUHVSRQVLEOHIRUPDNLQJVXUHWKDWZKHQ HME3DVVLYDWH FRPSOHWHV\RXULQVWDQFH YDULDEOHVDUHLQRQHRIWKHVWDWHVZHOLVWHGD FRXSOHRISDJHVDJR'RQ·WORRNQRZ6HHLI \RXFDQZRUNRXWZKLFKRIWKHVHZLOOEHVDIHO\ SDVVLYDWHG UHIHUHQFHWRDMDYDQHW6RFNHWREMHFW UHIHUHQFHWRDMDYD[VTO'DWD6RXUFH UHIHUHQFHWRDEHDQ·V5HPRWHFRPSRQHQW LQWHUIDFH UHIHUHQFHWRDEHDQ·V-1',FRQWH[W UHIHUHQFHWRDMDYDVTO&RQQHFWLRQ UHIHUHQFHWRDMDYD[HME6HVVLRQ&RQWH[W REMHFW DWUDQVLHQWYDULDEOHZLWKDQXOOYDOXH DQRQWUDQVLHQW6HULDOL]DEOHYDULDEOHZLWK DQXOOYDOXH DWUDQVLHQWYDULDEOHZLWKDQRQQXOOYDOXH DQRQWUDQVLHQWQRQ6HULDOL]DEOHYDULDEOH ZLWKDQXOOYDOXH DQRQWUDQVLHQWQRQ6HULDOL]DEOHYDULDEOH ZLWKDQRQQXOOYDOXH :HZHUHJRLQJIRUDSHUVRQDOEHVW 3% LQWKH QXPEHURIWLPHVLQZKLFKZHFRXOGXVHWKH ZRUG¶QRQ·LQDVLQJOHVWDWHPHQW \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Page 34 Return to Table of Contents SDVVLYDWLRQDQGWUDQVDFWLRQV ,ZRXOGPDUU\WKLVPDQLIRQO\ KHZDVQRWVXFKDQLGLRWDERXW VWDWHIXOVHVVLRQEHDQVFDODELOLW\ +HOHWVKLVVWDWHIXOEHDQVVWD\LQRSHQ WUDQVDFWLRQV(YHQ%REP\ORVHUH[ ER\IULHQGNQRZVWKDWVHVVLRQEHDQV LQDWUDQVDFWLRQFDQQHYHUEH SDVVLYDWHG :$51,1* $VWDWHIXOVHVVLRQ EHDQZLOO1(9(5EH SDVVLYDWHGZKLOH WKHEHDQLVVWLOOLQD WUDQVDFWLRQ ,KDYHEHHQ VLWWLQJKHUHOLNH IRUHYHUZDLWLQJIRUWKLV VWXSLGWUDQVDFWLRQWR ÀQLVK 7KHVSHFOHWV\RXEHJLQDWUDQVDFWLRQLQRQHPHWKRGRIDVWDWHIXO VHVVLRQEHDQEXWHQGWKHPHWKRGZLWKRXWHQGLQJWKHWUDQVDFWLRQ 7KLVLVDOPRVWDOZD\VDUHDOO\VWXSLGWKLQJWRGR)RUVWDUWHUV\RX FRXOGQHYHUJXDUDQWHHWKDWMXVWEHFDXVHDFOLHQWFDOOVWKHPHWKRG WKDWEHJLQVWKHWUDQVDFWLRQWKHFOLHQWZLOODWVRPHSRLQWFDOOWKH PHWKRGWKDWHQGVWKHWUDQVDFWLRQ LQHLWKHUDFRPPLWRUUROOEDFN %XWWKHPDLQUHDVRQLVWKDWWKHORQJHU\RXUWUDQVDFWLRQVWKHEHWWHU \RXUFKDQFHVRIEULQJLQJ\RXUVHUYHUWRLWVNQHHV DQGWKDWKROGV UHJDUGOHVVRIWKHEHDQW\SH )RUVWDWHIXOVHVVLRQEHDQVOHDYLQJD WUDQVDFWLRQRSHQPHDQVWKHEHDQZLOOQRWEHSDVVLYDWHGQRPDWWHU KRZORQJLW·VEHHQVLQFHWKHFOLHQWGLGDQ\WKLQJZLWKLW:H·OOORRNDW WKLVPRUHLQWKHWUDQVDFWLRQVFKDSWHUEXWIRUQRZXQGHUVWDQGWKDW VWDWHIXOEHDQVLQDWUDQVDFWLRQZRQ·WEHSDVVLYDWHG &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 35 VHVVLRQEHDQOLIHF\FOH *MIV\PQVO[aW]KIVLWQVMRJ)K\Q^I\MIVLMRJ8I[[Q^I\M HME3DVVLYDWH HME$FWLYDWH 8VH\RXU6HVVLRQ&RQWH[WWR 8VH\RXU6HVVLRQ&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQV PHWKRGVRQLW %07EHDQV $FFHVV $FFHVV \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 36 EHDQUHPRYDO *MIV:MUW^IT" _PMVJMIV[LQM $VDVWDWHIXOEHDQ ,·PDSHUVRQDOVODYHWR WKHFOLHQWDQGZKHQVKH·VGRQH ZLWKPH,·PWRVVHGRXWOLNH VRPDQ\$2/GLVNV6RPHWLPHV WKH\GRQ·WHYHQOHWPHVD\ JRRGE\H GRHVQRWH[LVW EHDQWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW HME5HPRYH RUWLPHRXW PHWKRGUHDG\ WLPHRXW SDVVLYDWHG $VHVVLRQEHDQVWRSVH[LVWLQJIRU RQHRIWKUHHUHDVRQV WKHFOLHQWFDOOVUHPRYH WKHEHDQWLPHVRXW WKHEHDQWKURZVDV\VWHPH[FHSWLRQ %XWWKHUH·VDQRWKHUTXHVWLRQWRDVNZKHQWKHEHDQWLPHV RXWZDVLWDFWLYHRUSDVVLYH",IWKHEHDQLVDFWLYHWKH &RQWDLQHUGHDOVZLWKLWLQWKHVDPHZD\LWGHDOVZLWKFOLHQW UHPRYH FDOOV³WKHEHDQJHWVDQHME5HPRYH FDOODQGLV WKHQNLOOHG%XWLIWKHEHDQZDVSDVVLYDWHGZKHQLWWLPHVRXW WKH&RQWDLQHUVHQGVLWVWUDLJKWWRWKHGRHVQRWH[LVWVWDWH ZLWKRXWFDOOLQJHME5HPRYH &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 37 VHVVLRQEHDQOLIHF\FOH /LIHF\FOHRYHUYLHZUHPRYLQJDVWDWHIXOEHDQ &OLHQWFDOOVUHPRYH RQDQDFWLYH LHQRQSDVVLYDWHG EHDQ &OLHQWFDOOVUHPRYHRQWKH FRPSRQHQWLQWHUIDFH RUFDOOV WKHUHPRYH PHWKRGLQWKHKRPH LQWHUIDFHWKDWWDNHVD+DQGOH FOLHQW UHPRYH UHPRYH EHDQ (-% REMHFW VWXE &RQWDLQHUFDOOVHME5HPRYH RQWKHEHDQ (-% REMHFW FOLHQW VWXE EHDQ RYH 5HP HME &RQWDLQHU´NLOOVµWKHEHDQDQG(-% REMHFW DVVXPHWKH\·UHQRZHOLJLEOH IRUJDUEDJHFROOHFWLRQ 7KHFOLHQW ZLOOJHWDQH[FHSWLRQLIVKHWULHVWR XVHWKHVWXEDJDLQ FOLHQW PLJKWVWLOOEHKHUH EXW ZLOOQRORQJHUZRUN \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 38 EHDQUHPRYDO /LIHF\FOHRYHUYLHZUHPRYLQJDVWDWHIXOEHDQ %HDQWLPHVRXWZKLOHDFWLYH &OLHQWGRHVQ·WPDNHDQ\FDOOVWRWKH EHDQ·VFRPSRQHQWLQWHUIDFHIRUD ORQJWLPH ZKDWHYHUWKH&RQWDLQHU FRQVLGHUVD´ORQJµWLPH EHDQ (-% REMHFW FOLHQW VWXE 1RDFWLYLW\ &RQWDLQHUGHFLGHVWRNLOOWKH EHDQDQGFDOOVHME5HPRYH RQWKHEHDQ (-% REMHFW FOLHQW VWXE EHDQ RYH 5HP HME &RQWDLQHU´NLOOVµWKHEHDQDQG(-% REMHFW DVVXPHWKH\·UHQRZHOLJLEOH IRUJDUEDJHFROOHFWLRQ 7KHFOLHQW ZLOOJHWDQH[FHSWLRQLIVKHWULHVWR XVHWKHVWXEDJDLQ FOLHQW PLJKWVWLOOEHKHUH EXW ZLOOQRORQJHUZRUN &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 39 VHVVLRQEHDQOLIHF\FOH /LIHF\FOHRYHUYLHZUHPRYLQJDVWDWHIXOEHDQ %HDQWLPHVRXWZKLOHSDVVLYDWHG 7KHFOLHQWGRHVQ·WFDOODQ\PHWKRGV RQWKHEHDQ·VFRPSRQHQWLQWHUIDFH IRUDORQJWLPH$)7(5WKHEHDQKDV DOUHDG\EHHQSDVVLYDWHG === === EHDQ (-% REMHFW FOLHQW VWXE 1RDFWLYLW\ &RQWDLQHUGHFLGHVWRNLOOWKHEHDQ EXWGRHV127FDOOHME5HPRYH === (-% REMHFW FOLHQW VWXE === EHDQ RJF GW VHQ QQHYHU WKHEHD H FDOO Y <,.(6 R P H HME5 JHWVDQ &RQWDLQHU´NLOOVµWKHEHDQDQG(-% REMHFW DVVXPHWKH\·UHQRZHOLJLEOH IRUJDUEDJHFROOHFWLRQ 7KHFOLHQW ZLOOJHWDQH[FHSWLRQLIVKHWULHVWR XVHWKHVWXEDJDLQ FOLHQW PLJKWVWLOOEHKHUH EXW ZLOOQRORQJHUZRUN \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 40 EHDQUHPRYDO /LIHF\FOHRYHUYLHZUHPRYLQJDVWDWHIXOEHDQ %HDQWKURZVDV\VWHPH[FHSWLRQ 7KHEHDQWKURZVDV\VWHP XQFKHFNHG H[FHSWLRQZKLOH H[HFXWLQJDPHWKRG ([FHSWLRQ EHDQ (-% REMHFW FOLHQW VWXE &RQWDLQHUGHFLGHVWRNLOOWKHEHDQ EXWGRHV127FDOOHME5HPRYH (-% REMHFW FOLHQW VWXE RJF GW VHQ EHDQ &RQWDLQHU´NLOOVµWKHEHDQDQG(-% REMHFW DVVXPHWKH\·UHQRZHOLJLEOH IRUJDUEDJHFROOHFWLRQ 7KHFOLHQW ZLOOJHWDQH[FHSWLRQLIVKHWULHVWR XVHWKHVWXEDJDLQ FOLHQW PLJKWVWLOOEHKHUH EXW ZLOOQRORQJHUZRUN &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 41 VHVVLRQEHDQOLIHF\FOH +WUXTIQV\[IJW]\JMIVZMUW^IT :K\ZRXOG\RX JHWULGRIP\EHDQ ZLWKRXWZDLWLQJIRUPH WRFDOOUHPRYH "7KDW LVVRUXGH ,GRQ·WVHHZK\ \RX FDQ·WFDOOHME5HPRYH RQDEHDQZKHQ\RX·UHJRLQJWR NLOOLWUHJDUGOHVVRIWKHUHDVRQ :KDWJRRGLVHME5HPRYH IRU SXWWLQJLQFOHDQXSFRGHLILW PLJKWQRWEHFDOOHG" +RZDP,VXSSRVHGWR NQRZ\RX·UHFRPLQJEDFN",I, GRQ·WXVHWLPHRXWVEHIRUHORQJ,·OOEH )8//RIUXVW\ROGDEDQGRQHGVKRSSLQJ FDUWV6RRQHURUODWHU,KDYHWR DVVXPHWKDW\RX·UHQHYHUFRPLQJ EDFN6RUU\EXW,UHDOO\ KDYHQRFKRLFH )LUVWRIDOO\RXKRQHVWO\H[SHFWPHWREULQJ DEHDQ287RISDVVLYDWLRQMXVWWRNLOOLW"7DON DERXWDZDVWHRIRYHUKHDG6KHHVK\RXVKRXOG NQRZEHWWHULI\RXFDUHDERXWSHUIRUPDQFH$QG LW·VQRWWKDWELJRIDGHDOMXVWSXW\RXUFOHDQXSFRGHLQ ERWKHME5HPRYH $1'HME3DVVLYDWH DQG\RX·UHVDIH $QGZLWKH[FHSWLRQVWKLQNDERXWLWGR\RXUHDOO\ZDQW WRUXQ\RXUFOHDQXSFRGHRQDEHDQZKRVHVWDWHFRXOG EHVHULRXVO\FRUUXSWDWWKLVSRLQW",IDEHDQWKURZV DV\VWHPH[FHSWLRQ,·PMXVWJRQQDORJLWDQG SXWLWRXWRILWVPLVHU\ \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 42 LPSOHPHQWLQJHME5HPRYH ,PSOHPHQWLQJHME5HPRYH HME5HPRYH 5HOHDVHDQ\UHVRXUFHVRUGRZKDWHYHUFOHDQXS\RXQHHGWRGREH IRUHWKHEHDQGLHVIRUHYHU0XFKRIWKHWLPH\RXUHME5HPRYH PHWKRG ZLOOEHHPSW\EHFDXVHLI\RXUEHDQGRHVXVHUHVRXUFHVLWPRVWOLNHO\ZLOO DFTXLUHDQGUHOHDVHWKHUHVRXUFHVLQHDFKEXVLQHVVPHWKRG%XWLI\RXU GHVLJQGRHVFDOOIRUNHHSLQJUHVRXUFHVRSHQWKURXJKRXWWKHEHDQ·VOLIH WKHQ\RXQHHGWRIUHHWKHPXSKHUH SXEOLFYRLGHME5HPRYH ^ WU\^ P\5HVRXUFHFORVH `FDWFK ([FHSWLRQH[ ^` ` ces e resour gracefully clos to t an w ! u re yo he if open, do it that are still DQDOWHUQDWLYH &DOODFOHDQ8S PHWKRGIURPERWKHME3DVVLYDWH DQGHME5HPRYH *LYHQWKDW\RXUEHDQFRXOGEHUHPRYHGZLWKRXWDQHME5HPRYH FDOOLILW WLPHVRXWZKLOHSDVVLYDWHG\RXVKRXOGKDYHERWK\RXUHME3DVVLYDWH DQG HME5HPRYH PHWKRGVSHUIRUPWKHVDPHFOHDQXS6LQFHVFDUFHV\VWHP UHVRXUFHVDUHVHOGRP6HULDOL]DEOHDQ\ZD\\RX·UHSUREDEO\DOUHDG\WDNLQJ FDUHRIWKHPLQHME3DVVLYDWH SXEOLFYRLGHME5HPRYH WKLVFOHDQ8S ` ^ SXEOLFYRLGHME3DVVLYDWH WKLVFOHDQ8S ` ^ now both ejbRemove( do the same thing, so) and ejbPassivate() will while passivated, you doeven if you time out nÕt have to worry. SULYDWHYRLGFOHDQ8S ^ WU\^ P\5HVRXUFHFORVH `FDWFK ([FHSWLRQH[ ^` ` &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 43 VHVVLRQEHDQOLIHF\FOH 0LVVHGHME5HPRYH FDOOV :DLWDPLQXWHWKHUH·V VWLOOVRPHWKLQJZURQJKHUH :KDWKDSSHQVLIWKHEHDQWKURZV DQH[FHSWLRQ"7KHQ,VWLOOZRQ·W JHWP\FOHDQXSFRGHWRUXQ :RQ·W,VWLOOJHWLQWRWURXEOH" :HOOXP\HV<HV\RXFRXOGVWLOO EHLQWURXEOHLQWKDWFDVH%XWWKLVLVUHDOO\<285 UHVSRQVLELOLW\WRGHDOZLWK/HW·VVD\\RXWHPSRUDULO\ VDYHVRPHRQH·VVKRSSLQJLQIRWRDGDWDEDVHVRWKDWLI WKHVHUYHUFUDVKHVZKLOHWKHSHUVRQ·VVKRSSLQJ\RXKDYHD PHDQVRIVWLOOUHFRYHULQJWKHLUFDUW XQWLOWKHEHDQWLPHVRXW ,Q HME5HPRYH \RX·GGHOHWHWKHURZLQWKHGDWDEDVHIRUWKLVFOLHQW %XWVLQFH\RXPLJKWQRWJHWWKDWHME5HPRYH FDOOVRRQHURU ODWHU\RX·UHJRQQDQHHGVRPHZD\WRJRWKURXJKDQGGRSHULRGLF FOHDQXSVRQWKDWGDWD7KHUH·VQRWKLQJLQWKHVSHFWKDWFDQ KHOS\RXKHUH,W·VXSWR<28WRGHDOZLWKWKHSRWHQWLDO FRQVHTXHQFHVRIPLVVHGHME5HPRYH FDOOV <RXPXVWEHDEOHWR LQ UHFRJQL]HVFHQDULRV :DWFKLW ZKLFKHME5HPRYH ZLOO EHPLVVHG QRZWKDWWKHUHDUH 7KHH[DPH[SHFWV\RXWRN KLFKDEHDQZLOO HUZ XQG WKUHHFLUFXPVWDQFHV FDOO 127JHWDQHME5HPRYH 6HUYHUFUDVKHV VLYDWHG %HDQWLPHVRXWZKLOHSDV HSWLRQ H[F WHP V\V ZVD WKUR %HDQ UFOHDQXSFRGH ,IDQ\RIWKHVHKDSSHQ\RX HVXJJHVWLRQVRQ ZLOOQRWUXQ,I\RXIROORZWK XWFOHDQXSFRGHLQ WKHSUHYLRXVSDJHDQGS 5HPRYH WKHQ ERWKHME3DVVLYDWH DQGHME HP WLPHRXW \RX¶UHSUREDEO\2.IRUSUREO WLOOKDYHWRGHDO X¶OOV W\R EX DWHG VVLY HSD ZKLO \VWHPH[FHSWLRQ DQV UEH VKR UFUD HUYH ZLWKDV EYLRXVTXHVWLRQ $QGGRQ¶WH[SHFWDVLPSOHR PRYH LI E5H QHM LVVD OLNH³:LOODEHDQP Q"´<RXPLJKW DV\VWHPH[FHSWLRQLVWKURZ HQDULRIRU KDYHWRFRQVLGHUDGHVLJQVF KHUHFRXOGEHD H[DPSOHDQG¿JXUHRXWLIW RZOHGJHRIPLVVHG SUREOHPEDVHGRQ\RXUNQ OV FDO WKRG PH YH HME5HPR \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 44 HME5HPRYH EHDQWKLQJV *MIV\PQVO[aW]KIVLWQVMRJ:MUW^M 8VH\RXU6HVVLRQ&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQV 7KHEHDQWKLQJV\RXFDQ GRLQHME5HPRYH DUH H[DFWO\WKHVDPHDVWKH EHDQWKLQJV\RXFDQGRLQ HME&UHDWH HME3DVVLYDWH HME$FWLYDWH $FFHVV \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 45 VHVVLRQEHDQOLIHF\FOH 1UXTMUMV\QVO\PM)L^QKM*MIVI[I[\I\M.=4JMIV 3O FAR THE !DVICE"EAN WEVE WRITTEN HASNT NEEDED TO BE STATEFUL )T DOESNT KEEP OR USE ANY CLIENT SPECIlC STATE SO IT DOESNT NEED A CREATE METHOD WITH ARGUMENTS "UT WHAT IF WE DID WANT TO MAKE IT A STATEFUL BEAN 7HAT IF THE BUSINESS LOGIC NEEDED TO SAY KEEP A RECORD OF THE CONVERSATION ITS HAVING SO THAT IT NEVER GIVES OUT THE SAME ADVICE MORE THAN ONCE IN A SESSION %VEN IF THE CHOICE OF AN ADVICE STRING IS PURELY RANDOM IF YOU WANT TO ENSURE THE ADVICE ISNT REPEATED DURING A SESSION YOULL HAVE TO KEEP TRACK OF IT IN AN INSTANCE VARIABLE !ND YOU MIGHT HAVE OTHER CHANGES TOO LIKE MAKING THE CREATE METHODS TAKE ARGUMENTS THAT CONTAIN THE TYPE OF ADVICE THE CLIENT IS LOOKING FOR OR SOME OTHER KIND OF PREFERENCE )N THAT CASE EACH TIME THE CLIENT MADE A METHOD CALL YOUD WANT TO CHECK THE STATUS OF THAT CLIENT SUPPLIED CREATION INITIALIZATION PREFERENCE AND TAILOR YOUR ADVICE BASED ON THE VALUE SET DURING THE BEANS EJB#REATE ,ATER IN THE BOOK WELL LOOK AT A MORE ELABORATE VERSION OF THIS !DVICE"EAN BUT FOR NOW WELL MAKE JUST A SUBTLE CHANGE TO MAKE THE BEAN STATEFUL YiX`e gfn\i <RXNQRZWKDWWKHEHDQFODVVLVJRLQJWR FKDQJHEXWZKDWDERXWWKHFOLHQWDQGWKH WZRLQWHUIDFHV"'RRQHRUPRUHRIWKRVH KDYHWRFKDQJH"5HDOO\WKLQNWKURXJKWKH LPSOLFDWLRQVEHIRUH\RXWXUQWKHSDJH <PQVO[aW]KIVILLQN\PMJMIVQ[[\I\M.=4 <RXFDQKDYHPRUHWKDQRQHFUHDWHPHWKRG 7KHFUHDWHPHWKRGFDQKDYHDUJXPHQWV 7KHEHDQFDQEHSDVVLYDWHGVR\RX FDQZULWHFRGHLQHME3DVVLYDWH DQG HME$FWLYDWH WKHUH DUH QR 'XPE 4XHVWLRQV Q: 7HAT HAPPENS IF YOU PUT CODE IN EJB0ASSIVATE OR EJB!CTIVATE AND THE BEAN IS STATE,%33 7ILL IT COMPILE 7ILL IT DEPLOY A: 9OU CAN CERTAINLY PUT CODE IN EJB!CTIVATE OR EJB0ASSIVATE REGARDLESS OF WHETHER THE BEAN IS STATELESS OR STATEFUL EVEN THOUGH IT WILL NEVER BE CALLED IF THE BEAN IS STATELESS 4HINK ABOUT IT ,OOK AT THE 3ESSION"EAN INTERFACE .OTICE THAT THERE ISNT A SEPARATE 3TATEFUL3ESSION"EAN INTERFACE AND 3TATELESS3ESSION"EAN INTERFACE 4HERES JUST 3ESSION "EAN AND BOTH STATELESS AND STATEFUL BEANS IMPLEMENT IT 3O THERES NOTHING IN YOUR CLASS THAT SPECIFICALLY SAYS YOUR BEAN IS STATELESS )TS ONLY AT DEPLOY TIME WHEN YOU TELL THE DEPLOYMENT DESCRIPTOR THAT THE BEAN IS STATELESS OR STATEFUL THAT IT ACTUALLY MATTERS )N FACT YOU COULD WRITE A BEAN THAT HAS ONLY A NO ARG CRE ATE AND KEEPS NO CLIENT STATE AND YOU CAN DEPLOY IT WITH EITHER SETTINGSTATELESS OR STATEFULAND IT WILL WORK AS LONG AS YOUVE TAKEN CARE OF PASSIVATION AND ACTIVATION \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 46 VWDWHIXOVHVVLRQEHDQFRGH )L^QKM;\I\MN]T*MIVKWLM SDFNDJHKHDG¿UVW LPSRUWMDYD[HME SXEOLFFODVV$GYLFH6WDWHIXO%HDQLPSOHPHQWV6HVVLRQ%HDQ^ SULYDWH6HVVLRQ&RQWH[WFRQWH[W ent Session We still implem Bean SULYDWH6WULQJXVHU1DPH This time, we can keep client-speciÞc state (in this case, the argument the client sends to the create method) SXEOLFYRLGHME$FWLYDWH ` ^ SXEOLFYRLGHME3DVVLYDWH ` SXEOLFYRLGHME5HPRYH ` ^ ^ of the four SessionBean tai ner callbacks, the only thing weÕre doing is savingcon context, even though in this version of the class weÕthe not using it. WeÕre saving the context because we wanre t YO your context, because in t U to get used to saving real-world beans youÕre gonna want to use it latemos r. (La examples of using the context) ter in the book, youÕll see SXEOLFYRLGVHW6HVVLRQ&RQWH[W 6HVVLRQ&RQWH[WFW[ ^ FRQWH[W FW[ ` SXEOLFYRLGHME&UHDWH 6WULQJQDPH ^ XVHU1DPH QDPH ` an, because a stateFUL be Y L N O be ything n an This ca must NOT have stateless beanÕs-arg create() method. except the no SXEOLF6WULQJJHW$GYLFH ^ UHWXUQXVHU1DPH³P\DGYLFHLV³$GYLVRUJHW$GYLFH ` In the busin ess method, speciÞc stat weÕre parameter. e, that we saved fusroing the clientstateless beaThis must be a stat m the create eful ns arenÕt tie d to a singlebean, because client. ` static helper assume this is claass, Advisor, method on a t see here. WeÕll that you donÕ that one. let you write &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 47 VHVVLRQEHDQOLIHF\FOH )L^QKM;\I\MN]T*MIV+41-6<KWLM 4HE STATEFUL VERSION OF THE CLIENT ADDS TWO THINGS NOT IN THE STATELESS VERSION 7ERE PASSING AN ARGUMENT TO THE CREATE METHOD ! STATEFUL BEAN CAN AND OFTEN DOES HAVE ARGUMENTS TO CREATE METHODS SO THAT THE CLIENT CAN PASS IN CLIENT SPECIlC STATE FOR THE BEAN TO SAVE AND USE IN LATER hCONVERSATIONSv WITH THE CLIENT IE LATER METHOD CALLS FROM THAT CLIENT 7ERE CALLING REMOVE ON THE COMPONENT INTERFACE )TS POLITE FOR THE CLIENT TO TELL THE #ONTAINER THAT SHES DONE WITH A STATEFUL SESSION BEAN !ND NOW YOU KNOW WHY 7ITHOUT THE REMOVE CALL THE #ONTAINER WILL HOLD ON TO THE BEAN WASTEFULLY UNTIL THE BEAN TIMES OUT 3O IN THIS CASE hPOLITEv REALLY MEANS hIMPROVES SCALABILITYv LPSRUWMDYD[QDPLQJ LPSRUWMDYD[UPL LPSRUWKHDG¿UVW LPSRUWMDYD[HME SXEOLFFODVV$GYLFH6WDWHIXO&OLHQW^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^ QHZ$GYLFH6WDWHIXO&OLHQW JR ` <RXPXVWEHDEOHWR UHFRJQL]HZKHWKHU WKHFOLHQWLVIRUD VWDWHIXOEHDQ OHWRORRNDWFOLHQWFRGH HDE VWE <RXPX RZZKHWKHUWKHFOLHQW¶V GNQ OLNHWKLVDQ WFDOOVD EHDQLVVWDWHIXO,IWKHFOLHQ ZD\WRNQRZ QRDUJFUHDWH WKHUH¶VQR DWOHDVW\RX ZKHWKHULW,6VWDWHOHVVEXW HDWHKDV NQRZLW0,*+7EH,IWKHFU QFUHDWH DUJVRUDQDPHRWKHUWKD XNQRZWKH \R XQW FFR DWH$ FUH OLNH IXO WDWH EHV XVW EHDQP SXEOLFYRLGJR ^ WU\^ &RQWH[WLF QHZ,QLWLDO&RQWH[W 2EMHFWR LFORRNXS ³6WDWHIXO$GYLVRU´ $GYLFH6WDWHIXO+RPHKRPH $GYLFH6WDWHIXO+RPH 3RUWDEOH5HPRWH2EMHFWQDUURZ R $GYLFH6WDWHIXO+RPHFODVV $GYLFH6WDWHIXODGYLVRU KRPHFUHDWH ³FORYHU´ because bean, t be a statefwulith arguments ! us m s hi T 6\VWHPRXWSULQWOQ DGYLVRUJHW$GYLFH eate weÕre calling cr DGYLVRUUHPRYH weÕre calling remove(), to tell the Cont ainer weÕre `FDWFK ([FHSWLRQH[ ^ done with the bean (so the Container can kill it). H[SULQW6WDFN7UDFH ` this is a poor way to handle exceptions! You would probably write ` ` a real-world client to catch the different kinds of exceptions that matter... NamingException, CreateException, etc. \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 48 VHVVLRQEHDQV 6KDUSHQ \RXU SHQFLO 'RWKHLQWHUIDFHVKDYHWRFKDQJHZKHQLWJRHVIURP VWDWHOHVVWRVWDWHIXO" /RRNDWWKHWZRLQWHUIDFHVEHORZIRUWKHVWDWHOHVVYHUVLRQ RIWKH$GYLFHEHDQ,IQHHGHGPDNHDQ\DGMXVWPHQWVWR WKHFRGHLQHLWKHURUERWKRIWKHLQWHUIDFHVIRUZKDW LIDQ\ WKLQJ QHHGVWRFKDQJHWRPDNHWKLVZRUNZLWKWKHUHYLVHG VWDWHIXOYHUVLRQRIWKHEHDQ SDFNDJHKHDG¿UVW LPSRUWMDYD[HME LPSRUWMDYDUPL5HPRWH([FHSWLRQ SXEOLFLQWHUIDFH$GYLFH+RPHH[WHQGV(-%+RPH^ SXEOLF$GYLFHFUHDWH WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ ` SDFNDJHKHDG¿UVW LPSRUWMDYD[HME LPSRUWMDYDUPL5HPRWH([FHSWLRQ SXEOLFLQWHUIDFH$GYLFHH[WHQGV(-%2EMHFW^ SXEOLF6WULQJJHW$GYLFH WKURZV5HPRWH([FHSWLRQ ` &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 49 VHVVLRQEHDQOLIHF\FOH :MDD=LHGAFLK ,MXTWaQVOI[\I\MN]TJMIV 4HE ONLY DIFFERENCE BETWEEN DEPLOYING THE STATEFUL !DVICE BEAN AND THE STATELESS VERSION BESIDES THE lLE NAMES AND *.$) NAME IS IN THE DEPLOYMENT DESCRIPTOR SETTING THAT DElNES WHETHER THE BEAN IS STATELESS OR STATEFUL 5SING THE 2) DEPLOYTOOL REMEMBER YOU SET THIS USING THE DEPLOYTOOLS .EW %NTERPRISE "EAN 7IZARDTHE THING THAT WALKS YOU THROUGH THE SETTINGS THAT IT THEN USES TO OUTPUT THE 8-, DEPLOYMENT DESCRIPTOR &RQWDLQHUFDOOEDFNVLQGLFDWHNH\PLOHVWRQHVLQDEHDQ·V OLIH $VD%HDQ3URYLGHU\RX·UHUHVSRQVLEOHIRULPSOHPHQW LQJWKHFRQWDLQHUFDOOEDFNVLQ\RXUEHDQFODVV &RQWDLQHUFDOOEDFNVFRPHIURPWZRSODFHVWKH 6HVVLRQ%HDQLQWHUIDFHDQGWKHKRPHLQWHUIDFH7KH FRPSLOHUIRUFHV\RXWRLPSOHPHQWWKH6HVVLRQ%HDQ LQWHUIDFHEXWWKHFDOOEDFNVUHODWHGWRWKHKRPHDUH\RXU UHVSRQVLELOLW\DQGWKHFRPSLOHUZRQ·WNQRZVLQFH\RXU EHDQFODVVGRHVQ·WLPSOHPHQW\RXUKRPHLQWHUIDFH $VWDWHIXOVHVVLRQEHDQFDQEHLQRQHRIWKUHHVWDWHV GRHVQRWH[LVW \HVWKDW·VDVWDWH PHWKRGUHDG\DQG SDVVLYDWHG :KHQDEHDQWUDQVLWLRQVIURPGRHVQRWH[LVWWRPHWKRG UHDG\LWVFRQVWUXFWRULVFDOOHGIROORZHGE\VHW6HVVLRQ &RQWH[W DQGILQDOO\WKHEHDQ·VHME&UHDWH $EHDQLQVWDQFHKDVVSHFLILF%HDQ7KLQJVWKDWLWFDQ GREXWQRQHDUHDYDLODEOHGXULQJWKHEHDQ·VFRQVWUXF WRUEHFDXVHDWWKDWSRLQWLWLVDQREMHFWEXWQRW\HWDIXOO EHDQ,WGRHVQ·W\HWKDYHLWVEHDQQHVV 6RPHRIWKH%HDQ7KLQJVDEHDQFDQGRLQFOXGHJHWD UHIHUHQFHWRLWVKRPHRU(-%REMHFWOHDUQRUDIIHFWWKH VWDWXVRIWKHWUDQVDFWLRQJHWVHFXULW\LQIRUPDWLRQDERXW WKHFOLHQWDQGDFFHVVDUHVRXUFHVXFKDVDGDWDEDVH :KHQDVWDWHIXOEHDQLVSDVVLYDWHGLW·VSXWLQWRVHFRQG YouÕll have to change this to Sta teful!! DU\VWRUDJHSRVVLEO\WKURXJKVHULDOL]DWLRQ<RXPXVW EHVXUHE\WKHHQGRI\RXUHME3DVVLYDWH PHWKRGWKDW \RXULQVWDQFHYDULDEOHVDUHUHDG\IRUSDVVLYDWLRQ 7KH&RQWDLQHUFDOOVHME5HPRYH RQDEHDQZKHQWKH FOLHQWFDOOVUHPRYH IRUDVWDWHIXOEHDQRUZKHQWKH &RQWDLQHUZDQWVWRUHGXFHWKHVL]HRIWKHSRROIRU VWDWHOHVVEHDQV,IDSDVVLYDWHGEHDQWLPHVRXWWKH &RQWDLQHUZLOONLOOWKHEHDQZLWKRXWLQYRNLQJHME5H PRYH $EHDQFDQDOVRPLVVDQHME5HPRYH FDOOLIWKHUH·VD FRQWDLQHUFUDVKRUWKHEHDQWKURZVDUXQWLPHH[FHSWLRQ \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 50 VWDWHIXOYVVWDWHOHVV +WUXIZML\W[\I\MN]TJMIV[[\I\MTM[[JMIV[ PI^MI[QUXTMTQNM 3TATELESS BEANS HAVE A MUCH SIMPLER EXISTENCE 4HEYRE BORN CREATED THEYRE THROWN INTO A POOL WITH OTHERS OF THEIR KIND THEY RUN BUSINESS METHODS FOR ANY CLIENT WHO ASKS AND THEY MIGHT EVENTUALLY DIE 4HEY ARENT PASSIVATED THEY DONT KEEP CLIENT SPECIlC STATE AND THEIR CREATION AND DESTRUCTION REMOVAL ARENT TIED TO THE WHIMS OF THE CLIENT #OMPARE THE DIFFERENCE BETWEEN THE LIFECYCLE OF A STATEFUL VS STATELESS BEAN 7KHEHDQOLIHF\FOH VSHFLDOPRPHQWVLQDEHDQ·VOLIH 6WDWHIXOVHVVLRQEHDQV %HDQFUHDWLRQ ZKHQWKHFOLHQWZDQWVDEHDQ %HDQXVH ZKHQWKHFOLHQWFDOOVDEXVLQHVVPHWKRG %HDQSDVVLYDWLRQ WKHEHDQLVSXWWRVOHHSWRFRQVHUYH UHVRXUFHV %HDQDFWLYDWLRQ WKHEHDQZDNHVXSWRVHUYLFHDEXVLQHVV PHWKRGIURPWKHFOLHQW %HDQUHPRYDO ZKHQWKHFOLHQWLVILQLVKHGZLWKWKHEHDQRU WKHEHDQWLPHVRXW 6WDWHOHVVVHVVLRQEHDQV %HDQFUHDWLRQ ZKHQWKHFRQWDLQHUZDQWVWRPDNHDEHDQ %HDQXVH ZKHQWKHFOLHQWFDOOVDEXVLQHVVPHWKRG %HDQUHPRYDO ZKHQWKHFRQWDLQHUGHFLGHVWKHUHDUH WRRPDQ\EHDQVLQWKHSRRORUWKHEHDQWKURZVDV\VWHP H[FHSWLRQ &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 51 VHVVLRQEHDQOLIHF\FOH &RPSDULQJWKHOLIHF\FOHVRIVWDWHIXO DQGVWDWHOHVVVHVVLRQEHDQV 6WDWHIXO EHDQWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW GRHVQRWH[LVW FRQVWUXFWRU VHW6HVVLRQ&RQWH[W HME&UHDWH HME5HPRYH RUWLPHRXW WLPHRXW === === HME3DVVLYDWH PHWKRGUHDG\ SDVVLYDWHG HME$FWLYDWH PHWKRGFDOOV 6WDWHOHVV GRHVQRWH[LVW FRQVWUXFWRU VHW6HVVLRQ&RQWH[W HME&UHDWH EHDQWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW HME5HPRYH PHWKRGUHDG\ PHWKRGFDOOV \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 52 VWDWHOHVVEHDQV &OLHQWFDOOVFUHDWHRQDVWDWHOHVVVHVVLRQEHDQKRPH &OLHQWFDOOVFUHDWHRQWKHKRPH FOLHQW +RPH REMHFW VWXE FUHDWH FUHDWH &RQWDLQHUPDNHVWKH(-%REMHFW (-% REMHFW &RQWDLQHUVHQGVWKHFOLHQWDVWXE WRWKH(-%REMHFW VWXE See whatÕs missing? No bean !! N o context ! &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 53 VHVVLRQEHDQOLIHF\FOH 6HVVLRQEHDQFUHDWLRQLVQRW UHODWHGWRWKHFOLHQW &RQWDLQHUFRQVWUXFWVWKH 6HVVLRQ&RQWH[WREMHFWDQG WKHEHDQLQVWDQFHWKHQFDOOV VHW6HVVLRQ&RQWH[W RQWKHEHDQ EHDQ VH W6 HV VLR Q& RQ WH [W &RQWH[W &RQWDLQHUSXWVWKHEHDQ ZKLFKLV QRZOLQNHGWRLWVRZQFRQWH[W LQ WKHSRROIRUWKDWEHDQW\SH &RQWH[W EHDQ %HDQ3RRO \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 54 EXVLQHVVPHWKRGV %XVLQHVVPHWKRGFDOO &OLHQWLQYRNHVDEXVLQHVVPHWKRG RQKHUSUHYLRXVO\DFTXLUHG(-% REMHFWVWXE JR (-% REMHFW JR VWXE FOLHQW %HDQ3RRO &RQWDLQHUSXOOVDEHDQRXWRIWKH SRRODQGOLQNVLWZLWKWKHFOLHQW·V (-%REMHFW (-% REMHFW EHDQ &RQWH[W %HDQ3RRO &RQWDLQHULQYRNHVEXVLQHVVPHWKRG RQWKHEHDQ WKHEHDQUHWXUQV IURPWKHPHWKRG WKHQ&RQWDLQHU VHQGVEHDQEDFNWRWKHSRRO JR EHDQ %HDQ3RRO &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 55 VHVVLRQEHDQOLIHF\FOH 2EMHFW,QWHUDFWLRQ'LDJUDP 2,' IRUEHDQFUHDWLRQ VWDWHOHVVVHVVLRQEHDQ :KHQWKHFOLHQWFDOOVFUHDWH FOLHQW +RPH REMHFW (-% REMHFW 6HVVLRQ &RQWH[W EHDQ FUHDWH QHZ DQGDWDFRPSOHWHO\GLIIHUHQWXQUHODWHGWLPH QHZ QHZ EHDQFRQVWUXFWRUUXQV VHW6HVVLRQ&RQWH[W HME&UHDWH 7KHFOLHQWFDQFDOO FUHDWHDOOGD\ORQJEXW ,ZLOO127WLHDEHDQWRWKH FOLHQW·V(-%REMHFWXQWLOWKH FOLHQWJHWVVHULRXVDQGFDOOVD EXVLQHVVPHWKRG \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 56 VHVVLRQEHDQWKLQJV *MIV\PQVO[aW]KIVLWNZWU[\I\MTM[[JMIVUM\PWL[ FRQVWUXFWRU QRWKLQJ VHW6HVVLRQ&RQWH[W EXVLQHVVPHWKRG 8VH\RXU6HVVLRQ&RQWH[WWR 8VH\RXU6HVVLRQ&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQV PHWKRGVRQLW %07EHDQV $FFHVV $FFHVV \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH HME&UHDWH HME5HPRYH 8VH\RXU6HVVLRQ&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQV $FFHVV eLESS Unlike stateFUL beans, a statinfo in bean canÕt get caller security nt clie no IS re the ause bec ), te( ejbCrea nÕs bea s eles stat the h associated wit Container creation. Remember, itÕs thebea n, and the ate cre to ides that dec nt. clie any its not tied to \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 57 VHVVLRQEHDQOLIHF\FOH +RZFRPH HME3DVVLYDWH DQG HME$FWLYDWH DUHQ·WFDOOHG ZKHQWKHEHDQJRHVLQDQG RXWRIWKHSRRO" 7KHUH·VQRQHHG%HDQVVWD\ DZDNHZKHQWKH\·UHLQWKHEHDQ SRRO)RUSHUIRUPDQFH,NHHS WKHEHDQVDVZKROHOLYLQJREMHFWVRQWKH KHDSQRWVHULDOL]HGREMHFWVWKDW,·G MXVWKDYHWRUHDFWLYDWHHDFKWLPHD FOLHQWFDOOVDPHWKRGDQG,QHHG DEHDQWRVHUYLFHLW :HOOWKHQKRZGR \RXJHWVFDODELOLW\LI DOOWKHEHDQVLQWKHSRRO DUHWDNLQJXSVSDFHRQ WKHKHDS" 5HPHPEHUZLWKVWDWHOHVV EHDQV,GRQ·WQHHGRQHSHU FOLHQW,QHHGRQO\RQHSHUFOLHQWLQWKH PLGGOHRIDEXVLQHVVPHWKRGFDOO,QRWKHUZRUGV ,QHHGMXVWHQRXJKVWDWHOHVVEHDQVWRVHUYLFHWKH PHWKRGVDFWXDOO\H[HFXWLQJDQGQRWRQHSHUHYHU\ FOLHQWZKRKDSSHQVWRKDYHDUHIHUHQFHWRDQ(-% REMHFW&OLHQWVVSHQGIDUPRUHWLPHEHWZHHQ PHWKRGFDOOVWKDQDFWXDOO\LQPHWKRGFDOOV \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 58 ZULWLQJVHVVLRQEHDQV ?ZQ\QVOI;M[[QWV*MIV" aW]ZRWJI[*MIV8ZW^QLMZ ,KDYHDV\VWHPZKHQ, VLWGRZQWRZULWHD6HVVLRQ EHDQ,DOZD\VKDYHWRSXW LQWKUHHNLQGVRIWKLQJVKRPH VWXIIEXVLQHVVPHWKRGVDQGWKH 6HVVLRQ%HDQPHWKRGV <RXSXW7+5((NLQGVRIPHWKRGV LQWKHEHDQFODVV LQWHUIDFH!! $GYLFH+RPH +20(WKLQJVHME&UHDWH PHWKRGV :ULWHDQHME&UHDWH PHWKRGLQWKHEHDQWRPDWFKHDFK FUHDWH PHWKRGLQWKHKRPHLQWHUIDFH FUHDWH LQWHUIDFH!! $GYLFH JHW$GYLFH &20321(17WKLQJVEXVLQHVVPHWKRGV :ULWHDEXVLQHVVPHWKRGLQWKHEHDQWRPDWFKHDFK PHWKRGLQ\RXUEHDQ¶VFRPSRQHQWLQWHUIDFH LQWHUIDFH!! 6HVVLRQ%HDQ VHW6HVVLRQ&RQWH[W HME3DVVLYDWH HME$FWLYDWH HME5HPRYH 6(66,21%($1WKLQJVFRQWDLQHUFDOOEDFNV IURPWKH6HVVLRQ%HDQLQWHUIDFH ,PSOHPHQWHDFKRIWKHIRXUPHWKRGVIURPWKH 6HVVLRQ%HDQLQWHUIDFHZKLFK\RXUEHDQPXVWLPSOHPHQW LQWKHRI¿FLDO-DYDZD\ LHXVLQJWKHLPSOHPHQWV 6HVVLRQ%HDQGHFODUDWLRQHLWKHULQ\RXUEHDQFODVVRURQH RILWVVXSHUFODVVHV 6KDUSHQ \RXU SHQFLO 2IWKHWKUHHW\SHVRIPHWKRGV\RX SXWLQ\RXUEHDQFKHFNRIIWKHRQHV WKHFRPSLOHUFDUHVDERXW &RPSLOHUFKHFNHG" 0HWKRGVWRPDWFKWKH+RPHLQWHUIDFH 0HWKRGVWRPDWFKWKH&RPSRQHQWLQWHUIDFH 0HWKRGVIURPWKH6HVVLRQ%HDQLQWHUIDFH &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 59 VHVVLRQEHDQOLIHF\FOH 6KDUSHQ \RXU SHQFLO *LYHQWKHIROORZLQJLQWHUIDFHVZULWHWKHEHDQFODVVFRGH \RXFDQOHDYH WKHPHWKRGHPSW\ DWWKHERWWRPRIWKHSDJH3D\VSHFLDODWWHQWLRQWR WKH+RPHFUHDWHPHWKRGZKDWGRHVLWWDNHWRµPDWFK¶WKLVLQWKHEHDQ" :LOOLWKDYHWKHVDPHUHWXUQW\SH"+LQWVDUHDWWKHERWWRPRIWKHSDJH LPSRUWMDYD[HME LPSRUWMDYDUPL5HPRWH([FHSWLRQ SXEOLFLQWHUIDFH.HQQHO+RPHH[WHQGV(-%+RPH^ SXEOLF.HQQHOFUHDWH 6WULQJFXVW,' WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ ` LPSRUWMDYD[HME LPSRUWMDYDUPL5HPRWH([FHSWLRQ SXEOLFLQWHUIDFH.HQQHOH[WHQGV(-%2EMHFW^ SXEOLF.HQQHO/HDVHSODFH3HW 3HWS WKURZV5HPRWH([FHSWLRQ SXEOLFYRLGUHQHZ/HDVH ./HDVHOHDVH WKURZV5HPRWH([FHSWLRQ([SLUHG([FHSWLRQ SXEOLF3HWJHW3HW ./HDVHOHDVH WKURZV5HPRWH([FHSWLRQ'HDG3HW([FHSWLRQ ` :ULWHWKHEHDQFODVVKHUH \RXDUHKHUH +LQWV7KHKRPHPHWKRGLQWKHEHDQKDVDVOLJKWO\GLIIHUHQWQDPHWKDQWKHRQHLQWKHKRPHLQWHU IDFH7KH&RQWDLQHUGRHVQ¶WQHHGDQ\WKLQJEDFNIURPWKHEHDQGXULQJFUHDWLRQ'RHV-DYDKDYHD UXOHWKDWVD\VWKHLPSOHPHQWHURIDQLQWHUIDFHPXVWGHFODUHWKHVDPHH[FHSWLRQVLQWKHLPSOHPHQWD WLRQFODVVWKDWDUHGHFODUHGRQWKHPHWKRGVLQWKHLQWHUIDFH" Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 60 KRPHLQWHUIDFH 5XOHVIRUWKH+20(PHWKRGVKRPHLQWHUIDFH /RFDOKRPHLQWHUIDFHVPXVWUHWXUQWKHORFDOFRPSRQHQWLQWHUIDFHDQG 5HPRWHKRPHLQWHUIDFHVPXVWUHWXUQWKH5HPRWHFRPSRQHQWLQWHUIDFH SXEOLFLQWHUIDFH$GYLFH/RFDO+RPHH[WHQGV(-%/RFDO+RPH^ SXEOLF$GYLFH/RFDOFUHDWH WKURZV&UHDWH([FHSWLRQ ` SXEOLFLQWHUIDFH$GYLFH+RPHH[WHQGV(-%+RPH^ SXEOLF$GYLFHFUHDWH WKURZV&UHDWH([FHSWLRQ 5HPRWH([FHSWLRQ ` (YHU\FUHDWHPHWKRGLQWKHKRPHLQWHUIDFHPXVWGHFODUHD &UHDWH([FHSWLRQUHJDUGOHVVRIZKHWKHUWKHLQWHUIDFHLVORFDORU5HPRWH <RXFDQDOVRGHFODUH\RXURZQDSSOLFDWLRQ FKHFNHG H[FHSWLRQV SXEOLF$GYLFHFUHDWH WKURZV&UHDWH([FHSWLRQ 1R$GYLFH([FHSWLRQ /RFDOKRPHLQWHUIDFHVPXVWH[WHQG(-%/RFDO+RPHDQGPXVW127 GHFODUH5HPRWH([FHSWLRQV SXEOLFLQWHUIDFH$GYLFH/RFDO+RPHH[WHQGV(-%/RFDO+RPH^ SXEOLF$GYLFH/RFDOFUHDWH WKURZV&UHDWH([FHSWLRQ ` 5HPRWHKRPHLQWHUIDFHVPXVWH[WHQG(-%+RPHDQGPXVWGHFODUH 5HPRWH([FHSWLRQVRQHYHU\PHWKRG SXEOLFLQWHUIDFH$GYLFH+RPHH[WHQGV(-%+RPH^ SXEOLF$GYLFHFUHDWH WKURZV&UHDWH([FHSWLRQ 5HPRWH([FHSWLRQ ` LQWHUIDFH!! .HQQHO+RPH .HQQHOFUHDWH 6WULQJLG This must be the component interface type. Remote homes must return Remote component interface references, local homes must return local component interface references. 6WDWHOHVVEHDQVFDQKDYHRQO\RQHFUHDWH PHWKRGDQGLWPXVW127 KDYHDUJXPHQWV SXEOLF$GYLFHFUHDWH The return type of a home create method must ALWAYS be the component interface type. WKURZV&UHDWH([FHSWLRQ 6WDWHIXOEHDQVPXVWKDYHRQHRUPRUHFUHDWH PHWKRGVDQGDUH127 UHTXLUHGWRKDYHDQRDUJFUHDWH 7KHFUHDWH PHWKRGVPXVWVWDUW ZLWKWKHVWULQJ³FUHDWH´DQGFDQEHRYHUORDGHG )RRFUHDWH%LJ)RR )RRFUHDWH $UJXPHQWVDQGUHWXUQW\SHVIRU5HPRWHKRPHLQWHUIDFHPHWKRGVPXVW EHOHJDO50,,,23W\SHV 6HULDOL]DEOHSULPLWLYH5HPRWHRUFROOHF WLRQVRUDUUD\VRIDQ\RIWKRVH &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 61 VHVVLRQEHDQOLIHF\FOH 5XOHVIRUWKH+20(PHWKRGVEHDQFODVV (YHU\FUHDWHPHWKRGLQWKHKRPHPXVWKDYHDPDWFKLQJHME&UHDWH PHWKRGLQWKHEHDQFODVV7KHHME&UHDWHPHWKRGVLQWKHEHDQPXVW KDYHDYRLGUHWXUQW\SH SXEOLFYRLGHME&UHDWH ^` 7KHHME&UHDWHPHWKRGVPXVWEHSXEOLFDQGPXVW127EHPDUNHG ¿QDORUVWDWLF <RXGR127KDYHWRGHFODUHWKHH[FHSWLRQVGHFODUHGLQWKHKRPHLQWHU IDFHXQOHVV\RXPLJKWDFWXDOO\WKURZWKRVHH[FHSWLRQVIURP\RXURZQ PHWKRGVDOWKRXJKLWLVRIWHQJRRGSUDFWLFHWRGHFODUH&UHDWH([FHSWLRQ <RX0867QRWGHFODUH5HPRWH([FHSWLRQLQ\RXUEHDQFODVV(9(5 SXEOLFYRLGHME&UHDWH ^`QRGHFODUHGH[FHSWLRQV EHFDXVHZHGRQ¶WDFWXDOO\WKURZDQ\ <RXPXVW127GHFODUHDQ\DSSOLFDWLRQH[FHSWLRQV LHFKHFNHG H[FHSWLRQV WKDWZHUHQRWGHFODUHGLQWKHKRPHLQWHUIDFHIRUWKDW PDWFKLQJFUHDWHPHWKRG SXEOLFYRLGHME&UHDWH WKURZV)LUH([FHSWLRQ^` QRWOHJDOXQOHVV)LUH([FHSWLRQLVGHFODUHGRQWKH KRPHLQWHUIDFHFUHDWHPHWKRG 6WDWHOHVVEHDQVPXVWKDYHRQO\RQHHME&UHDWH DQGLWPXVWKDYHQR DUJXPHQWV SXEOLFYRLGHME&UHDWH Every create in the home must have a matching ejbCreate in the bean class (void return type). .HQQHO%HDQ YRLGHME&UHDWH 6WULQJLG The bean home methods must match the home interface methods, except you preÞx the bean class methods with ÔejbÕ, and capitalize the ÔÔcÕ in create to ÔejbCreateÕ ^`PXVWORRNOLNHWKLV 6WDWHIXOEHDQVPXVWKDYHRQHRUPRUHHME&UHDWHPHWKRGV PDWFK LQJWKHPHWKRGVRIWKHKRPHLQWHUIDFH DQGPXVWVWDUWZLWKWKHVWULQJ ³HME&UHDWH´ +20( SXEOLF)RRFUHDWH%LJ)RR WKURZV&UHDWH([FHSWLRQ %($1 SXEOLFYRLGHME&UHDWH%LJ)RR ^` \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Page 62 Return to Table of Contents FRPSRQHQWLQWHUIDFH 5XOHVIRUWKH%86,1(66PHWKRGVFRPSRQHQWLQWHUIDFH %XVLQHVVPHWKRGQDPHVPXVWQRWEHJLQZLWKWKHVWULQJ³HME´ $UJXPHQWVDQGUHWXUQW\SHVIRU5HPRWHFRPSRQHQWLQWHUIDFHPHWKRGV PXVWEHOHJDO50,,,23W\SHV7KDWPHDQV6HULDOL]DEOHSULPLWLYH 5HPRWHRUDQDUUD\RUFROOHFWLRQRIDQ\RIWKRVH DVORQJDVWKHFROOHFWLRQ LPSOHPHQWDWLRQFODVVLVLWVHOI6HULDOL]DEOH SXEOLF6WULQJJHW$GYLFH WKURZV5HPRWH([FHSWLRQ 6WULQJLV6HULDOL]DEOHVRWKLVPHWKRGFRXOGEH LQD5HPRWHFRPSRQHQWLQWHUIDFH SXEOLF6RFNHWJHW7KH6RFNHW 6RFNHWLV1276HULDOL]DEOHVRWKLVPHWKRGPXVW 127EHLQD5HPRWHLQWHUIDFH ORFDOZRXOGEH2. Remote component interface methods must have RMI-IIOP types as arguments and return types. 6WULQJGR6RPHWKLQJ LQWL /RFDOFRPSRQHQWLQWHUIDFHVPXVWH[WHQG(-%/RFDO2EMHFWDQGPXVW127 GHFODUH5HPRWH([FHSWLRQV SXEOLFLQWHUIDFH$GYLFH/RFDOH[WHQGV(-%/RFDO2EMHFW^ SXEOLF6WULQJJHW$GYLFH ` This method would be legal in either a Remote or local interface, because the argument and return type are legal RMI-IIOP types. 5HPRWHFRPSRQHQWLQWHUIDFHVPXVWH[WHQG(-%2EMHFWDQGPXVW GHFODUH5HPRWH([FHSWLRQVRQHYHU\PHWKRG SXEOLFLQWHUIDFH$GYLFHH[WHQGV(-%2EMHFW^ SXEOLF6WULQJJHW$GYLFH WKURZV5HPRWH([FHSWLRQ ` <RXPXVWQRWH[SRVHWKHORFDOKRPHRUFRPSRQHQWLQWHUIDFHRIDEHDQ WKURXJKD5HPRWHFRPSRQHQWLQWHUIDFHPHWKRG,QRWKHUZRUGV\RX FDQ¶WKDYHDORFDOLQWHUIDFHDVWKHUHWXUQW\SHRUDUJXPHQWW\SHLQD 5HPRWHFRPSRQHQWLQWHUIDFHPHWKRG &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 63 VHVVLRQEHDQOLIHF\FOH 5XOHVIRUWKH%86,1(66PHWKRGVEHDQFODVV %XVLQHVVPHWKRGVPXVWEHGHFODUHGDVSXEOLFDQGPXVW127EH GHFODUHGDV¿QDORUVWDWLF SXEOLFYRLGGR%LJ7KLQJV ^` 0HWKRGQDPHVPXVWQRWEHJLQZLWKWKHVWULQJ³HME´ <RXPXVW127SDVV³WKLV´DVDQDUJXPHQWRUUHWXUQYDOXH5HPHPEHU \RXGRQ¶WZDQWWRJLYHRXWDUHIHUHQFHWRWKHEHDQ(YHU\ERG\PXVWJR WKURXJKWKH(-%REMHFW ODWHUZH¶OOVHHKRZWRGRWKDW SXEOLFYRLGGR6WXII WKLV QRWOHJDO IURPZLWKLQDEHDQPHWKRG $UJXPHQWVDQGUHWXUQW\SHVIRU5HPRWHFRPSRQHQWLQWHUIDFHPHWKRGV PXVWEHOHJDO50,,,23W\SHV7KDWPHDQV6HULDOL]DEOHSULPLWLYH 5HPRWHRUDQDUUD\RUFROOHFWLRQRIDQ\RIWKRVH DVORQJDVWKHFROOHFWLRQ LPSOHPHQWDWLRQFODVVLVLWVHOI6HULDOL]DEOH SXEOLF6WULQJJHW$GYLFH ^` 6WULQJLV6HULDOL]DEOHVRWKLVPHWKRGFRXOGEH LQD5HPRWHFRPSRQHQWLQWHUIDFH SXEOLF6RFNHWJHW7KH6RFNHW ^` 6RFNHWLV1276HULDOL]DEOHVRWKLVPHWKRGPXVW 127EHLQD5HPRWHLQWHUIDFH ORFDOZRXOGEH2. <RXGR127KDYHWRGHFODUHWKHH[FHSWLRQVGHFODUHGLQWKHFRPSRQHQW LQWHUIDFHXQOHVV\RXPLJKWDFWXDOO\WKURZWKRVHH[FHSWLRQVIURP\RXU RZQPHWKRGV<RX0867QRWGHFODUH5HPRWH([FHSWLRQLQ\RXUEHDQ FODVV(9(5 SXEOLF6WULQJJHW$GYLFH ^`QRGHFODUHGH[FHSWLRQV EHFDXVHZHGRQ¶WDFWXDOO\WKURZDQ\ Business methods must not throw application (checked) exceptions that arenÕt declared in the component interface. 6HVVLRQ%HDQ H[WHQGV (QWHUSULVH%HDQ KHUH%87 LQW 7KHUH¶VQRWKLQJ DEOH (QWHUSULVH%HDQLV6HULDOL] 6RWKDWPHDQV\RXUEHDQLV ZLWKRXW DXWRPDWLFDOO\6HULDOL]DEOH \RXUKDYLQJVDLGVR \RX ,I\RXKDYHDVFHQDULRZKHUH QLV EHD LID L]H RJQ UHF QHHGWR XJK 6HULDOL]DEOH,7,6HYHQWKR VD\ \RXGRQ¶WVHHWKHEHDQFODVV ³LPSOHPHQWV6HULDOL]DEOH´ <RXPXVW127GHFODUHDQ\DSSOLFDWLRQH[FHSWLRQV LHFKHFNHG H[FHSWLRQV WKDWZHUHQRWGHFODUHGLQWKHFRPSRQHQWLQWHUIDFHIRUWKDW PDWFKLQJEXVLQHVVPHWKRG SXEOLFYRLGJHW$GYLFH WKURZV$GYLFH([FHSWLRQ^` QRWOHJDOXQOHVV$GYLFH([FHSWLRQLVGHFODUHGRQWKH FRPSRQHQWLQWHUIDFHYHUVLRQRIWKHPHWKRG \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Page 64 Return to Table of Contents UXOHVIRUWKHEHDQFODVV 2WKHUUXOHVIRU\RXUEHDQFODVV 7KHFODVVPXVWLPSOHPHQWMDYD[HME6HVVLRQ%HDQHLWKHUGLUHFWO\ RULQGLUHFWO\ SXEOLFFODVV$GYLFH%HDQLPSOHPHQWV6HVVLRQ%HDQ 7KHFODVVPXVWEHSXEOLFDQGFDQQRWEH¿QDORUDEVWUDFW 7KHFODVVPXVWKDYHDSXEOLFQRDUJFRQVWUXFWRU 7KDW¶VZKDWWKH &RQWDLQHUKDVWRXVH :HUHFRPPHQGMXVWOHWWLQJWKHFRPSLOHULQVHUW WKHGHIDXOWFRQVWUXFWRUVLQFH\RXGR127ZDQWWRSXWFRGHLQWKH FRQVWUXFWRU SXEOLF$GYLFH ^`YDOLGEHDQFRQVWUXFWRU <RXPXVW127KDYHD¿QDOL]H PHWKRGLQ\RXUFODVV 7KHFODVVLVQRWUHTXLUHGWR EXWLVDOORZHGWR LPSOHPHQWWKHEHDQ¶V FRPSRQHQWLQWHUIDFH :HWDONHGHDUOLHUUHPHPEHUDERXWZK\\RX SUREDEO\GRQ¶WZDQWWR³RI¿FLDOO\´LPSOHPHQW\RXUFRPSRQHQWLQWHUIDFH <RXUFODVV0867LPSOHPHQWWKHPDWFKLQJKRPHDQGFRPSRQHQWLQWHUIDFH PHWKRGV LHDQHME&UHDWHIRUHYHU\FUHDWHLQWKHKRPHDQGDPDWFKLQJ EXVLQHVVPHWKRGIRUHYHU\PHWKRGLQWKHFRPSRQHQWLQWHUIDFH ,IWKHEHDQLVVWDWHIXOLWFDQRSWLRQDOO\LPSOHPHQWWKH 6HVVLRQ6\QFKURQL]DWLRQLQWHUIDFH 7KLVLQWHUIDFHJLYHVWKHEHDQ WKUHHPRUHFDOOEDFNVUHODWHGWRWUDQVDFWLRQVZKLFKZH¶OOORRNDWLQWKH WUDQVDFWLRQVFKDSWHU <RXUEHDQFODVVFDQKDYHDVXSHUFODVV,QRWKHUZRUGV\RXFDQXVH QRUPDO-DYDLQKHULWDQFHZLWK\RXUEHDQFODVV <RXMXVWGRQ¶WJHWDQ\ VSHFLDOEHDQVSHFL¿FLQKHULWDQFHUHPHPEHU <RXUEHDQFODVV FDQKDYHRWKHU PHWKRGV WRKDYHWKH KDV ODVV <RXUEHDQF IIURP WKUHHW\SHVRIPHWKRGVVWXI DWFK WKHKRPH HME&UHDWHVWRP GVIURP FUHDWHV EXVLQHVVPHWKR QGWKH WKHFRPSRQHQWLQWHUIDFHD GV%XW IRXU6HVVLRQ%HDQPHWKR J\RXU QRWKLQJVWRSV\RXIURPKDYLQ GV HWKR U¶´P HOSH G³K RVH H[S RZQQRQ G QHH 27 R1 GVG HWKR DQGWKRVHP WREHSXEOLF &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 65 VHVVLRQEHDQOLIHF\FOH 7KHGLIIHUHQFHEHWZHHQFUHDWHDQGHME&UHDWH KRPH LQWHUIDFH the client needs to get back the component interface SXEOLF.HQQHOFUHDWH EHDQ FODVV WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ3HW([FHSWLRQ donÕt forget the name differences! SXEOLFYRLGHME&UHDWH ace declares an just because the interf YOU must. If an me Õt esn do n exceptio declare it. nÕt do it, you donÕt throw WKURZV3HW([FHSWLRQ^` but the container doesnÕt need anything from the bean 7KH&RQWDLQHU PLJKWQRWQHHG DQ\WKLQJIURPWKHEHDQEXW WKHKRPHGDPQZHOOEHWWHU JLYH0(WKHFRPSRQHQW LQWHUIDFH7KDW·VWKHRQO\ UHDVRQ,·PKHUH ,·PQRWVXUH, XQGHUVWDQGZK\WKH UHWXUQW\SHVDUHGLIIHUHQW EHWZHHQWKHEHDQDQG WKHLQWHUIDFH :KHQ,FDOOWKH EHDQ·VHME&UHDWHPHWKRG, GRQ·W1(('DQ\WKLQJIURPWKH EHDQWKDQN\RXYHU\PXFKVRXVH YRLGIRUWKHUHWXUQW\SH 7KLVZLOODOO FKDQJHZLWKHQWLWLHVWKRXJKVR EHSUHSDUHG \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 66 VHVVLRQEHDQZDWFKLWV 3D\DWWHQWLRQWR WKH ´HMEµDVLQHME5 HPRYH YVMXVWUHPRYH DQG HME&UHDWHYVF UHDWH 7UXHRUIDOVHWKH& RQWDLQHUFDOOVDEH DQ¶V UHPRYH PHWKRGLIW KHVWDWHIXOEHDQWLPH V RXWZKLOHDFWLYH )$/6(7KHEHDQG RHVQ¶W+$9(D UHPRYH PHWKRGR QO\DQHME5HPRYH PHWKRG :DWFKIRUWKLQJVOL NH³7KH%HDQ3URYL GHULV UHVSRQVLEOHIRUSXWW LQJWKHFUHDWH P HWKRG LQWKHEHDQ´1RWWU XHEHFDXVHWKHUH ,6 QRFUHDWH PHWKRG LQWKHEHDQRQO\DQ HME&UHDWH %HVXUHWREXUQLWLQ WR\RXUKHDGWKDW DQ\WKLQJVWDUWLQJZ LWKWKHOHWWHUVµHME¶ LVLQ WKHEHDQ2IFRXUV HLWZRXOGEHHDV\ LIWKDW ZHUHWKH21/<UX OHOLNHLIHYHU\WKLQ JLQ WKHEHDQVWDUWVZL WK³HME´DQGQRWKLQJ LQWKH LQWHUIDFHVVWDUWVZL WKµ³HME´EXWRIFRX UVH WKDW¶VQRWWUXH %XVLQHVVPHWKRGVD UHLGHQWLFDOO\QDPH GLQ ERWKWKHFRPSRQHQ WLQWHUIDFHDQGWKHE HDQ FODVVDQGWKH\GRQ ¶WHYHUVWDUWZLWK³HM E´LQ HLWKHUWKHLQWHUIDFH RUWKHEHDQFODVV <RXDOVRQHHGWRNH HSWUDFNRI:+2 LQYRNHV:+$7)RU H[DPSOHZKR LQYRNHVWKHFUHDWH PHWKRG"7KHFOLHQ W RUWKH&RQWDLQHU" FOLHQW :KRLQYRNH V HME&UHDWH WKH&R QWDLQHU :KRLQYRNH V VHW6HVVLRQ&RQWH[W " &RQWDLQHU :KR LQYRNHVUHPRYH " FOLHQW <RXFDQKDYH DUHWXU QLQDQ HME&UHDWH HME&UHDWH WDQ <RX.12:WKD UHWXUQ PHWKRGPXVWKDYHDYRLG DVLF W\SHEXWGRQ¶WIRUJHW\RXUE RXVHHD -DYDUXOHVMXVWEHFDXVH\ GRHV UHWXUQVWDWHPHQWUHWXUQ H YDOX JD UQLQ UHWX X¶UH Q\R QRWPHD LQJ LGHU RQV E\F OHG IRR 'RQ¶WEH XDO DQ\WKLQJRWKHUWKDQWKHDFW GHFODUHGUHWXUQW\SHRIWKH HME&UHDWH PHWKRG FFHVV 2QO\21(FOLHQWFDQD H DVHVVLRQEHDQDWDWLP WKHEHDQ UHJDUGOHVVRIZKHWKHU LVVWDWHOHVVRUVWDWHIXO DQRQH (5EHDFFHVVHGE\PRUHWK 6HVVLRQEHDQVPXVW1(9 WKHU DQR DQG LHQW DFO E\ XVH FOLHQW,IDEHDQLVFXUUHQWO\LQ WJHWV WKHEHDQWKDWVHFRQGFOLHQ FOLHQWLQYRNHVDPHWKRGRQ RPPHQWLQWKHVSHFWKDW DF UHLV WKH 87 Q% SWLR DQH[FH QVDV RLPSOHPHQWVWDWH/(66EHD VD\VWKH&RQWDLQHULVIUHHW &RQWDLQHUWKHQUXQV WKH KLFK QZ EHD WKH HRI DVLQJOHLQVWDQF DWLPH WKHFOLHQWVWKURXJKRQHDW HU ZRUFDUHKRZWKH&RQWDLQ <RXDUHQ¶WVXSSRVHGWRNQR KDW\RX WLVW ERX UHD RFD YHW XKD GRHVLWWKHRQO\WKLQJ\R WRSURWHFW\RXUEHDQIURP GR127KDYHWRZULWHFRGH RX RXNQRZDQ\ZD\VLQFH\ FK\ :KL HVV DFF HQW FXUU FRQ WKUHDGUHODWHGFRGHLQ\RXU 6( Q8 HYH DQ¶W RXF DW\ NQRZWK LWQRWLI\HWF EHDQOLNHV\QFKURQL]HGZD &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 67 VHVVLRQEHDQOLIHF\FOH 6KDUSHQ \RXU SHQFLO :KRGRHV:KDW" )URPWKHOLVWRIZRUGVEHORZDUUDQJHWKHPLQWKHDSSURSULDWHOLVWVDFFRUGLQJWR ZKHWKHULW¶VDUHVSRQVLELOLW\RIWKH%HDQ3URYLGHUWKH&RQWDLQHURUWKH&OLHQW KZMI\QVO\PM0WUMWJRMK\KTI[[ QUXTMUMV\QVO\PM;M[[QWV+WV\M`\KTI[[ QV^WSQVO[M\;M[[QWV+WV\M`\ QV^WSQVOMRJ+ZMI\M QV^WSQVOMRJ:MUW^M QUXTMUMV\QVO\PM-2*7JRMK\KTI[[ QUXTMUMV\QVO\PM0IVLTMKTI[[ QV^WSQVOIJ][QVM[[UM\PWL WV\PMKWUXWVMV\QV\MZNIKM KZMI\QVO\PMPWUMQV\MZNIKM QV^WSQVOKZMI\M QUXTMUMV\QVO;M[[QWV*MIV QUXTMUMV\QVO\PMKZMI\MUM\PWL QUXTMUMV\QVO\PMMRJ+ZMI\MUM\PWL QUXTMUMV\QVO\PMMRJ)K\Q^I\MUM\PWL QV^WSQVOMJR8I[[Q^I\M %HDQ3URYLGHU &OLHQW &RQWDLQHU \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 68 VHVVLRQFRQWH[W ;M[[QWV+WV\M`\ AW]VMMLQ\UWZM\PIVQ\VMML[aW] !T THE BEGINNING OF A BEANS LIFE AND ONLY ONCE IN THE BEANS LIFE THE #ONTAINER CALLS THE BEANS SET3ESSION#ONTEXT CONTAINER CALLBACK METHOD 4HAT CONTEXT A SUBCLASS OF %*"#ONTEXT IS THE BEANS LIFELINE TO THE #ONTAINER AND ITS THE ONLY THING THE BEAN CAN CALL METHODS ON TO GET REFERENCES TO HIS OWN HOME AND %*" OBJECT TO GET SECURITY INFORMATION ABOUT THE CLIENT OR TO DO TRANSACTION RELATED THINGS 7E WONT LOOK AT THE SECURITY AND TRANSACTION INFO NOW THEYRE BOTH COVERED IN SEPARATE CHAPTERS <RXDUH62IULFNLQ·OXFN\WRKDYHPH,·P DVFORVHDV\RX·OOHYHUJHWWRWKH&RQWDLQHU EDE\VR\RXEHWWHUWDNHJRRGFDUHRIPH$QG \RXRQO\JHWRQHVKRWDWLWLI\RXGRQ·WVDYHPHZKHQ WKH&RQWDLQHUFDOOVVHW6HVVLRQ&RQWH[W \RX·UH VFUHZHG$QGGRQ·WGRDQ\WKLQJVWXSLGOLNHQXOORXW P\UHIHUHQFH6RRQHURUODWHU\RX·UHJRQQD QHHGPHIRUVRPHWKLQJ 8POHW·VJHWWKLVVWUDLJKW³ \RX·UHKHUHWRVHUYH0(6R\RXEHWWHU JHWXVHGWRKHDULQJ´2K&RQWH[W%R\JHW PHDWUDQVDFWLRQµ´2K&RQWH[W%R\JHWPH WKHFOLHQW·VVHFXULW\LQIRµ$QG,DOPRVW IRUJRW,WDNHRQHVXJDUDQGQRFUHDP LQP\FRIIHH %HDQ 6HVVLRQ&RQWH[W 0HPRUL]HWKH 6HVVLRQ&RQWH[W LQWHUIDFH DOLVWRI <RXPXVWEHDEOHWRORRNDW EHORQJWRWKH PHWKRGVDQGNQRZZKLFK QGUHPHPEHU 6HVVLRQ&RQWH[WLQWHUIDFH$ HGWROLIHF\FOH DORWRIWKHTXHVWLRQVUHODW KRGVIURPZKLFK DUHDERXWNQRZLQJWKHPHW VRQ\RXU WKRG PH UWDLQ OFH FDO FDQ \RX DYHWRNQRZ RXK OH\ DPS UH[ )R FRQWH[W GLQIRIURP ODWH QWUH WFOLH ¶WJH FDQ WKDW\RX PHWKRG H UHDW HME& WKH WLQ \RXUFRQWH[ VHWKHUH¶VQR RIDVWDWHOHVVEHDQ%HFDX WHOHVVEHDQ¶V FOLHQWDVVRFLDWHGZLWKDVWD JHWFOLHQWLQIR HME&UHDWH %XW\RX&$1 WHIXOEHDQ¶V IURP\RXUFRQWH[WIURPDVWD QWLQLWLDWHGLW FOLH VHD FDX EH H UHDW HME& &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 69 VHVVLRQEHDQOLIHF\FOH 7KLQJV\RXFDQGRZLWK\RXU6HVVLRQ&RQWH[W PHWKRGVFDWHJRULHV JHWDUHIHUHQFHWR\RXUKRPH JHW(-%+RPH JHW(-%/RFDO+RPH KRPHUHI LQWHUIDFH!! (-%&RQWH[W JHW&DOOHU3ULQFLSDO LV&DOOHU,Q5ROH 6WULQJV JHW(-%+RPH JHW(-%/RFDO+RPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHW(-%2EMHFW JHW(-%/RFDO2EMHFW (-%REMHFWUHI JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW FOLHQWVHFXULW\ JHW&DOOHU3ULQFLSDO LV&DOOHU,Q5ROH 6WULQJV JHW5ROOEDFN2QO\ JHW8VHU7UDQVDFWLRQ VHW5ROOEDFN2QO\ LQWHUIDFH!! 6HVVLRQ&RQWH[W JHW(-%/RFDO2EMHFW JHW(-%2EMHFW IRUFHDWUDQVDFWLRQWRUROOEDFN &07RQO\ VHW5ROOEDFN2QO\ ERROHDQE WUDQVDFWLRQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\EHHQ VHWWRUROOEDFN &07RQO\ JHW5ROOEDFN2QO\ JHWDWUDQVDFWLRQUHIHUHQFHDQGVWDUW\RXU RZQWUDQVDFWLRQ %07RQO\ JHW8VHU7UDQVDFWLRQ <RXGRQ¶WQHHG WRNQRZWKHWKUHH GHSUHFDWHGPHWKRGV %HVLGHVWKHRQHVVKRZQRQWKLVSDJH WKH(-%&RQWH[WLQWHUIDFHKDVWKUHH RWKHUPHWKRGVDOORIZKLFKKDYH EHHQGHSUHFDWHG<RX¶UHQRWWHVWHG RQGHSUHFDWHGPHWKRGVVRLI\RXVHH WKHPLQWKH$3,GRQ¶WSDQLF7KHRQO\ UHDVRQ\RXQHHGWRNQRZDERXWWKHP LVLQFDVH\RX¶UHZRUNLQJZLWKROGHU (-%FRGH7KHPHWKRGVDUH LV&DOOHU,Q5ROH ,GHQWLW\LG JHW&DOOHU,GHQWLW\ JHW(QYLURQPHQW \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 70 7LNLEHDQ/RXQJH 6WDWHOHVVEHDQ,GRQ·WVHHKRZ\RXFDQVWDQG LWEHLQJDVWDWHIXOEHDQ<RXUZKROHOLIHLVWLHGWR VHUYLFLQJRQHFOLHQW 6WDWHIXOEHDQ:KLFKLVDOVRZK\\RX·UHQRWXVHGIRU DQ\WKLQJELJRUVHULRXV 6WDWHOHVVEHDQ$QGWKDWZRXOGEHZURQJEHFDXVH 6WDWHOHVVEHDQ1RZZDLWMXVWDPLQXWHKHUH³WKDWLV 62QRWWUXH,QIDFWLI\RXZDQWWRWDONSHUIRUPDQFH EXGG\,·PZD\PRUHVFDODEOHWKDQ\RX$QGEHIRUH \RXVWDUWDUJXLQJGRQ·WHYHQJLYHPHWKDWQRQVHQVH DERXWSDVVLYDWLRQ,·YHKHDUGDOOWKHDUJXPHQWV%XW SDVVLYDWLRQZLOO1(9(5PDNH\RXDVVFDODEOHDVPH 6WDWHIXOEHDQ,W·VDSHUVRQDOLQWLPDWHUHODWLRQVKLS$ UHODWLRQVKLSWKDW\RXFRXOGQ·WSRVVLEO\FRPSUHKHQG 6WDWHIXOEHDQ2.PD\EH,·PQRWTXLWH$6VFDODEOH EXWWKHUHDUHPRUHLPSRUWDQWWKLQJVLQOLIHWRFRQVLGHU 6WDWHOHVVEHDQ:KDWLV7+$7VXSSRVHGWRPHDQ" 6WDWHOHVVEHDQ)RUH[DPSOH" 6WDWHIXOEHDQ%HFDXVH\RXUUHODWLRQVKLSVDUHVR WUDQVLHQW<RXGRQ·WUHDOO\FDUH:+2\RXVHUYLFH$Q\ FOLHQWLQDVWRUPDVWKH\VD\ 6WDWHIXOEHDQ:HOO,·PHDV\WRXVH(DV\DQGTXLFN WREXLOG0D\EHWKDW·VPRUHLPSRUWDQWWRWKHGHYHORSHU $QGLQVRPHFDVHV,·PVWLOOJRQQDSHUIRUPEHWWHUZKHQ \RXUHDOO\GRQHHGFOLHQWFRQYHUVDWLRQDOVWDWH7KH RSWLRQVFDQUHDOO\VXFNVRPHWLPHVKDYLQJWRVWRUH WKHVWDWHLQDGDWDEDVHZLWKHDFKFDOO DQGWKHQJRDQG ORRNLWDOOXSDJDLQZLWKWKHYHU\1(;7FDOO ",NQRZ VRPHWLPHV\RXKDYHWRGRWKDWEXWVWLOO2UVXUH\RX FRXOGMXVWNHHSVHQGLQJWKHFRQYHUVDWLRQDOVWDWHEDFNWR WKHFOLHQWVRWKDWWKH\FDQWXUQDURXQGDQGVHQGLWEDFN WR<28ZLWKHDFKFDOODQG³ 6WDWHIXOEHDQ,·PQRWVXUH,OLNHWKHZD\\RXSKUDVH WKDW´VHUYLFLQJRQHFOLHQWµ0DNHVLWVRXQGOLNH,·PD SHUVRQDOVODYHRUVRPHWKLQJ 6WDWHOHVVEHDQ7KHUH·VQRWKLQJZURQJZLWKWKDW,·P DSHRSOHSHUVRQ,OLNHEHLQJLQWKHSRROZLWKWKHRWKHU EHDQVWKHQFRQVWDQWO\PHHWLQJQHZFOLHQWV,W·VQHYHU ERULQJDQG,EDUHO\HYHUVHHWKHVDPHFOLHQWWZLFH DQG³ 6WDWHIXOEHDQ0\SRLQWH[DFWO\<RX·UHQRWHYHQ 3+<6,&$//<DEOHWRVXVWDLQDPHDQLQJIXORQJRLQJ UHODWLRQVKLS 6WDWHOHVVEHDQ([FXVHPH" 6WDWHIXOEHDQ,W·VWKDWVKRUWWHUPPHPRU\ORVV WKLQJ<RXFDQ·WKROGDQ\FOLHQWLQIRUPDWLRQEHWZHHQ PHWKRGFDOOVZKLFKLVZK\LWGRHVQ·WPDWWHU:+,&+ FOLHQWFDOOV<RX·GQHYHUEHDEOHWRUHPHPEHUHYHQLI\RX JRWWKHVDPHFOLHQWFDOOLQJWHQRI\RXUPHWKRGVLQDURZ 6WDWHOHVVEHDQ<HDKEXWWKDW·VQRWDSUREOHP 5HPHPEHU,·PXVHGIRUVHUYLFHVWKDWGRQ·W1(('PH WRUHPHPEHUDQ\WKLQJIURPFOLHQWVEHWZHHQPHWKRG FDOOV 6WDWHOHVVEHDQ:KDW·VZURQJZLWKWKDW" 6WDWHIXOEHDQ+HUH·VDQHZZRUGIRU\RXU YRFDEXODU\´EDQGZLGWKµ3URQRXQFHLWZLWKPH ´EDDDQGZLLLLGWKµ'R\RXUHDOO\ZDQWDOOWKDWJRLQJEDFN DQGIRUWK" 6WDWHOHVVEHDQ2N\HDKJRWLW%XWWHOOPHKRZ LVWKLVZRUVHIRUSHUIRUPDQFHWKDQWKHKLWIURP SDVVLYDWLRQDFWLYDWLRQ"$QGZKLOHZH·UHDWLW³ZDLWGLG \RXKHDUWKDW",WKLQN,KHDU\RXUFOLHQWFDOOLQJ$QG LWVRXQGOLNHQRZDLW\HV,·PDIUDLGLWVRXQGVOLNH KH·VFDOOLQJUHPRYH ,W·VEHHQQLFHWDONLQJWR\RX,·OO EHWKLQNLQJDERXW\RXZKLOH,·PRXWDWWKHSRRO)ORDWLQJ RQDLUPDWWUHVV*HWWLQJDWDQ'ULQNLQJPDUJDULWDVZLWK VKDYHGLFH:RZ,/29(EHLQJPH &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 71 VHVVLRQEHDQOLIHF\FOH WhatÕs allowed? 3XWDFKHFNQH[WWRWKHPHWKRGVWKDW\RXFDQFDOORQ\RXU6HVVLRQ&RQWH[WLILW·VDOORZHGIURPWKH PHWKRGVLQWKHOHIWFROXPQ)RUH[DPSOHLQHME&UHDWH \RXFDQJHWDUHIHUHQFHWR\RXUKRPHIRUERWK VWDWHOHVVDQGVWDWHIXOVHVVLRQEHDQVVR\RXFDQSXWDFKHFNLQWKHWRSER[LQERWKFROXPQV$OORIWKH DQVZHUVDUHLQWKLVFKDSWHUEXWWU\WRPDNH\RXUEHVWJXHVVEHIRUHJRLQJEDFNIRUWKHDQVZHUV<RX PLJKWZDQWWRPDNHDQRWKHURQHRIWKHVH\RXUVHOIWRLQFOXGH-1',UHVRXUFHDQGEHDQDFFHVV %HDQPHWKRGV HME&UHDWH HME5HPRYH 6WDWHIXO 6WDWHOHVV 8VH\RXU6HVVLRQ&RQWH[WWR 8VH\RXU6HVVLRQ&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQV PHWKRGVRQLW %07EHDQV 8VH\RXU6HVVLRQ&RQWH[WWR HME3DVVLYDWH HME$FWLYDWH JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 'RHVQRWDSSO\ VWDWHOHVVEHDQVDUH QHYHUSDVVLYDWHG EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQV EXVLQHVV PHWKRGV 8VH\RXU6HVVLRQ&RQWH[WWR 8VH\RXU6HVVLRQ&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQV PHWKRGVRQLW %07EHDQV \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 72 FRIIHHFUDPPRFNH[DP 0RFN([DP 1 7HICH STATEMENTS ABOUT SESSION BEANS ARE TRUE #HOOSE ALL THAT APPLY R! ! STATEFUL SESSION BEAN IS TYPICALLY USED TO REPRESENT A ROW IN A DATABASE R" 4HE CLIENT MUST CALL THE EJB!CTIVATE METHOD BEFORE CALLING ANY BUSINESS METHODS R# ! STATEFUL SESSION BEANS lELDS CAN CONTAIN CLIENT CONVERSATIONAL STATE R$ 4HEY ARE TYPICALLY USED AS ASYNCHRONOUS MESSAGE CONSUMERS 2 7HICH LISTS CORRECTLY SEQUENCE SOME OF THE STEPS IN A SESSION BEANS LIFECYCLE #HOOSE ALL THAT APPLY R! R" R# R$ R% 3 QHZ,QVWDQFH QHZ,QVWDQFH HME&UHDWH VHW6HVVLRQ&RQWH[W VHW6HVVLRQ&RQWH[W VHW6HVVLRQ&RQWH[W QHZ,QVWDQFH HME&UHDWH VHW6HVVLRQ&RQWH[W HME&UHDWH QHZ,QVWDQFH VHW6HVVLRQ&RQWH[W QHZ,QVWDQFH HME&UHDWH 7HICH TYPES OF SESSION BEAN INSTANCE lELDS CAN BE SUCCESSFULLY PASSIVATED AND REACTIVATED #HOOSE ALL THAT APPLY R! R" R# R$ R% HME&UHDWH ! REFERENCE TO THE REMOTE HOME INTERFACE ! REFERENCE TO THE 5SER4RANSACTION INTERFACE ! REFERENCE TO A DATABASE CURSOR ! REFERENCE TO THE 3ESSION#ONTEXT OBJECT ! REFERENCE TO A SOCKET &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 73 VHVVLRQEHDQOLIHF\FOH 4 7HICH ARE VALID REMOTE COMPONENT INTERFACES FOR A SESSION BEAN #HOOSE ALL THAT APPLY R! SXEOLFLQWHUIDFH0\%HDQH[WHQGVMDYD[HME(-%2EMHFW^ YRLGP\0HWKRG ` R" SXEOLFLQWHUIDFH0\%HDQH[WHQGVMDYD[HME(-%2EMHFW WKURZV5HPRWH([FHSWLRQ^ YRLGP\0HWKRG ` R# SXEOLFLQWHUIDFH0\%HDQH[WHQGV MDYD[HME(-%+RPH2EMHFW^ YRLGP\0HWKRG WKURZV5HPRWH([FHSWLRQ ` R$SXEOLFLQWHUIDFH0\%HDQH[WHQGVMDYD[HME(-%2EMHFW^ YRLGP\0HWKRG WKURZV5HPRWH([FHSWLRQ ` 5 )F A CLIENT MAKES A CALL TO A SESSION OBJECT THAT HAS BEEN REMOVED BY THE CONTAINER WHICH EXCEPTIONS CAN BE THROWN #HOOSE ALL THAT APPLY R! R" R# R$ R% 6 MDYDUPL5HPRWH([FHSWLRQ MDYD[HME5HPRYH([FHSWLRQ MDYDUPL1R6XFK2EMHFW([FHSWLRQ MDYD[HME1R6XFK(QWLW\([FHSWLRQ MDYD[HME2EMHFW1RW)RXQG([FHSWLRQ 'IVEN 0\%HDQFUHDWH 6WULQJQDPH WKURZV&UHDWH([FHSWLRQ 5HPRWH([FHSWLRQ 7HICH SESSION BEAN INTERFACE CAN CONTAIN THIS METHOD R! R" R# R$ /NLY STATEFUL SESSION BEANS /NLY STATELESS SESSION BEANS "OTH STATEFUL AND STATELESS SESSION BEANS .EITHER STATEFUL NOR STATELESS SESSION BEANS \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 74 FRIIHHFUDPPRFNH[DP 7 7HAT IS TRUE ABOUT A SESSION BEANS LIFECYCLE #HOOSE ALL THAT APPLY R! )F A BUSINESS METHOD THROWS A SYSTEM EXCEPTION THE BEAN WILL BE PASSIVATED R" ! PASSIVATED BEAN MUST BE ACTIVATED BEFORE IT CAN BE REMOVED R# ! STATELESS SESSION BEANS REMOVAL MUST BE INITIATED BY THE CLIENT NOT THE CONTAINER R$ 3TATELESS SESSION BEANS CANNOT IMPLEMENT THE 3ESSION3YNCHRONIZATION INTERFACE 8 7HICH ARE REQUIRED OF THE BEAN PROVIDER TO ENSURE THAT A SESSION BEAN IS SUCCESSFULLY PASSIVATED #HOOSE ALL THAT APPLY R! 4HE PROVIDER MUST CALLHME3DVVLYDWH R" 4HE PROVIDER MUST ALWAYS ADD BUSINESS LOGIC TO THEHME3DVVLYDWH METHOD IF THE BEAN IS STATEFUL R# 4HE PROVIDER MUST CLOSE ANY DATABASE CONNECTIONS BEFORE HME3DVVLYDWH COMPLETES R$ 4HE PROVIDER MUST ASSUME THAT ANY STATE STORED IN INSTANCE lELDS MARKED TRANSIENT WILL BE LOST R% 4HE PROVIDER MUST ASSUME THAT ANY REFERENCE TO THE 3ESSION#ONTEXT OBJECT WILL BE NOT SURVIVE PASSIVATION IF THE 3ESSION#ONTEXT OBJECT IS NOT SERIALIZABLE 9 'IVEN A STATELESS SESSION BEAN WITH CONTAINER MANAGED TRANSACTION DEMARCATION FROM WHICH METHODS CAN YOU ACCESS ANOTHER BEAN #HOOSE ALL THAT APPLY R! R" R# R$ HME&UHDWH HME5HPRYH A BUSINESS METHOD VHW6HVVLRQ&RQWH[W &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 75 VHVVLRQEHDQOLIHF\FOH 10 .OTE 4HE REAL EXAM HAS SEVERAL TYPES OF @DRAG AND DROP QUESTIONS THAT WERE GOING TO DO A LAME JOB OF SIMULATING WITH THIS QUESTION -ATCH THE METHODS ON THE LEFT WITH THE INTERFACES IN WHICH THOSE METHODS CAN BE FOUND ON THE RIGHT ! MATCH IS CORRECT IF THE METHOD IS EITHER DECLARED IN OR INHERITED BY THE INTERFACE .OTE 4HERE MAY BE SOME MANY TO ONE AND ONE TO MANY RELATIONSHIPS IN YOUR ANSWER ! AFTER#OMPLETION 3ESSION3YNCHRONIZATION " GET5SER4RANSACTION 3ESSION#ONTEXT # AFTER"EGIN 3ESSION"EAN $ IS#ALLER)N2OLE 5SER4RANSACTION % GET2OLL"ACK/NLY & SET3ESSION#ONTEXT ' SET2OLLBACK/NLY 11 7HAT IS TRUE ABOUT A SESSION BEANS LIFECYCLE #HOOSE ALL THAT APPLY R! 4HE CONTAINER WILL ALWAYS CREATE A NEW SESSION BEAN INSTANCE WHEN THE CLIENT INVOKES THE FUHDWH STATELESS BEAN METHOD ON THE HOME INTERFACE OF A R" 4HE CONTAINER WILL ALWAYS CALL HME5HPRYH THE UHPRYH WHEN THE CLIENT INVOKES METHOD ON A STATELESS BEANS HOME INTERFACE R# ! SESSION BEAN CANNOT BE PASSIVATED WHILE IT IS IN A TRANSACTION R$ 4HE VHW6HVVLRQ&RQWH[W METHOD IS NOT INVOKED DURING A STATELESS BEANS LIFECYCLE 12 7HICH OF THE FOLLOWING METHODS ARE CONTAINER CALLBACK METHODS #HOOSE ALL THAT APPLY R! HME3DVVLYDWH R" VHW5ROOEDFN2QO\ R# VHW6HVVLRQ&RQWH[W R$ JHW5ROOEDFN2QO\ R% JHW8VHU7UDQVDFWLRQ \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 76 FRIIHHFUDPPRFNH[DP 13 )N WHAT CASES WILL THE CONTAINER FAIL TO CALL EJB2EMOVE ON A SESSION BEAN #HOOSE ALL THAT APPLY R! R" R# R$ 14 )F AN APPLICATION EXCEPTION IS THROWN FROM WITHIN A TRANSACTION HME&UHDWH HME5HPRYH A BUSINESS METHOD VHW6HVVLRQ&RQWH[W ONLY STATEFUL SESSION BEANS ONLY STATELESS SESSION BEANS BOTH STATEFUL AND STATELESS SESSION BEANS NEITHER STATEFUL NOR STATELESS SESSION BEANS &OR SESSION BEANS WHICH ARE THE RESPONSIBILITY OF THE #ONTAINER #HOOSE ALL THAT APPLY R! R" R# R$ )F A TIMEOUT OCCURS WHILE THE BEAN IS IN THE METHOD READY STATE &OR WHICH TYPE OF BEAN CAN THE CONTAINER PASSIVATE AN INSTANCE THAT IS IN A TRANSACTION R! R" R# R$ 16 )F AN APPLICATION EXCEPTION IS THROWN FROM A BUSINESS METHOD 7HICH METHODS ALLOW BOTH STATEFUL AND STATELESS SESSION BEANS WITH BEAN MANAGED TRANSACTION DEMARCATION TO ACCESS 5SER4RANSACTION METHODS #HOOSE ALL THAT APPLY R! R" R# R$ 15 )F THE CONTAINER CRASHES )NVOKING THE LOCAL INTERFACE FUHDWH METHOD )NVOKING THE JHW6HVVLRQ&RQWH[W METHOD )NVOKING THE HME&UHDWH METHOD %NSURING THAT THE HME5HPRYH METHOD IS ALWAYS INVOKED &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 77 VHVVLRQEHDQOLIHF\FOH 17 &OR THIS DRAG AND DROP TYPE QUESTION YOU CAN USE EACH ELEMENT ONLY ONCE 7HICH INTERFACE SHOULD BE MATCHED WITH WHICH FACT SO THAT ALL FOUR MATCHES ARE CORRECT REMOTE COMPONENT A (AS A JHW+RPH+DQGOH REMOTE HOME B EXTENDS µMDYD[HME(-%2EMHFW¶ METHOD LOCAL COMPONENTC METHODS MUST ./4 THROW LOCAL HOME 18 µMDYDUPL5HPRWH([FHSWLRQ¶ D CAN BE USED TO RETRIEVE AN %*",OCAL/BJECT 7HICH CAN BE CALLED BY THE CONTAINER DURING THE LIFECYCLE OF A SESSION BEAN #HOOSE ALL THAT APPLY R! FUHDWH R" BEAN CONSTRUCTOR R# VHW5ROOEDFN2QO\ R$ JHW8VHU7UDQVDFWLRQ R% HME5HPRYH 19 20 7HICH STATEMENTS ABOUT A SESSION BEAN CLASS ARE TRUE #HOOSE ALL THAT APPLY R! 4HEY CAN BE MARKED @lNAL R" 4HEY DO NOT NEED A NO ARGUMENT CONSTRUCTOR R# 4HEIR COMPONENT INTERFACE METHODS CAN BE @PRIVATE R$ 4HEIR BUSINESS METHOD NAMES MUST START WITH hEJBv R% 4HEIR @EJB#REATE METHODS MUST NOT BE DECLARED AS @lNAL &OR WHICH TYPE OF BEAN CAN THE 3ESSION3YNCHRONIZATION INTERFACE BE IMPLEMENTED R! ONLY STATEFUL SESSION BEANS R" ONLY STATELESS SESSION BEANS R# BOTH STATEFUL AND STATELESS SESSION BEANS R$ NEITHER STATEFUL NOR STATELESS SESSION BEANS \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Page 78 Return to Table of Contents FRIIHHFUDPPRFNH[DP 21 7HICH STATEMENTS ARE TRUE FOR STATELESS SESSION BEAN INSTANCES #HOOSE ALL THAT APPLY R! !NY INSTANCE CAN BE USED FOR ANY CLIENT R" #ONVERSATIONAL STATE MUST BE RETAINED ACROSS METHODS R# #ONVERSATIONAL STATE MUST BE RETAINED ACROSS TRANSACTIONS R$ 4HEY CAN BE PASSIVATED R% 4HEY DO NOT SUPPORT TRANSACTIONS &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 79 VHVVLRQEHDQOLIHF\FOH 0RFN([DP$QVZHUV (spec: 71) 1 7HICH STATEMENTS ABOUT SESSION BEANS ARE TRUE #HOOSE ALL THAT APPLY s what entity R! ! STATEFUL SESSION BEAN IS TYPICALLY USED TO REPRESENT A ROW IN A DATABASE -thatÕs do bean R" 4HE CLIENT MUST CALL THE EJB!CTIVATE METHOD BEFORE CALLING ANY BUSINESS - only the container calls ejbActivate() METHODS R# ! STATEFUL SESSION BEANS lELDS CAN CONTAIN CLIENT CONVERSATIONAL STATE R$ 4HEY ARE TYPICALLY USED AS ASYNCHRONOUS MESSAGE CONSUMERS 2 R! R" R# R$ R% 3 (spec: 77) 7HICH LISTS CORRECTLY SEQUENCE SOME OF THE STEPS IN A SESSION BEANS LIFECYCLE #HOOSE ALL THAT APPLY HME&UHDWH QHZ,QVWDQFH QHZ,QVWDQFH HME&UHDWH VHW6HVVLRQ&RQWH[W VHW6HVVLRQ&RQWH[W VHW6HVVLRQ&RQWH[W QHZ,QVWDQFH HME&UHDWH VHW6HVVLRQ&RQWH[W HME&UHDWH QHZ,QVWDQFH VHW6HVVLRQ&RQWH[W QHZ,QVWDQFH HME&UHDWH 7HICH TYPES OF SESSION BEAN INSTANCE lELDS CAN BE SUCCESSFULLY PASSIVATED AND REACTIVATED #HOOSE ALL THAT APPLY R! R" R# R$ R% (spec: 71) ! REFERENCE TO THE REMOTE HOME INTERFACE lization on could be seria ti iva ss pa , er mb ! REFERENCE TO A DATABASE CURSOR - reme ! REFERENCE TO THE 5SER4RANSACTION INTERFACE ! REFERENCE TO THE 3ESSION#ONTEXT OBJECT ! REFERENCE TO A SOCKET - remember, passivation could be seri alization \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 80 PRFNH[DPDQVZHUV 4 7HICH ARE VALID REMOTE COMPONENT INTERFACES FOR A SESSION BEAN #HOOSE ALL THAT APPLY (spec: 97) R! SXEOLFLQWHUIDFH0\%HDQH[WHQGVMDYD[HME(-%2EMHFW^ YRLGP\0HWKRG - ` needs RemoteException R" SXEOLFLQWHUIDFH0\%HDQH[WHQGVMDYD[HME(-%2EMHFW WKURZV5HPRWH([FHSWLRQ^ YRLGP\0HWKRG - illegal Java ! ` ect R# SXEOLFLQWHUIDFH0\%HDQH[WHQGV - nope, gotta be EJBObj MDYD[HME(-%+RPH2EMHFW^ YRLGP\0HWKRG WKURZV5HPRWH([FHSWLRQ ` R$SXEOLFLQWHUIDFH0\%HDQH[WHQGVMDYD[HME(-%2EMHFW^ YRLGP\0HWKRG WKURZV5HPRWH([FHSWLRQ ` 5 )F A CLIENT MAKES A CALL TO A SESSION OBJECT THAT HAS BEEN REMOVED BY THE CONTAINER WHICH EXCEPTIONS CAN BE THROWN #HOOSE ALL THAT APPLY R! R" R# R$ R% 6 MDYDUPL5HPRWH([FHSWLRQ MDYD[HME5HPRYH([FHSWLRQ - this happens only (spec: 79) on remove() MDYDUPL1R6XFK2EMHFW([FHSWLRQ e this - clients donÕt se MDYD[HME2EMHFW1RW)RXQG([FHSWLRQ - this is for entity Þnders MDYD[HME1R6XFK(QWLW\([FHSWLRQ (spec: 98) 'IVEN 0\%HDQFUHDWH 6WULQJQDPH WKURZV&UHDWH([FHSWLRQ 5HPRWH([FHSWLRQ 7HICH SESSION BEAN INTERFACE CAN CONTAIN THIS METHOD R! R" R# R$ /NLY STATEFUL SESSION BEANS /NLY STATELESS SESSION BEANS ans must haeavete Stateless berg ent cr only a no-a um "OTH STATEFUL AND STATELESS SESSION BEANS .EITHER STATEFUL NOR STATELESS SESSION BEANS &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 81 VHVVLRQEHDQOLIHF\FOH 7 7HAT IS TRUE ABOUT A SESSION BEANS LIFECYCLE #HOOSE ALL THAT APPLY R! )F A BUSINESS METHOD THROWS A SYSTEM EXCEPTION THE BEAN WILL BE - (spec: 79, 88) lled no, the bean is ki PASSIVATED R" ! PASSIVATED BEAN MUST BE ACTIVATED BEFORE IT CAN BE REMOVED - nope R# ! STATELESS SESSION BEANS REMOVAL MUST BE INITIATED BY THE CLIENT NOT THE CONTAINER R$ 3TATELESS SESSION BEANS CANNOT IMPLEMENT THE 3ESSION3YNCHRONIZATION INTERFACE 8 7HICH ARE REQUIRED OF THE BEAN PROVIDER TO ENSURE THAT A SESSION BEAN IS SUCCESSFULLY PASSIVATED #HOOSE ALL THAT APPLY (spec: 71-72) R! 4HE PROVIDER MUST CALLHME3DVVLYDWH R" 4HE PROVIDER MUST ALWAYS ADD BUSINESS LOGIC TO THEHME3DVVLYDWH METHOD IF THE BEAN IS STATEFUL R# 4HE PROVIDER MUST NOT CLOSE ANY DATABASE CONNECTIONS BEFORE HME3DVVLYDWH COMPLETES R$ 4HE PROVIDER MUST ASSUME THAT ANY STATE STORED IN INSTANCE lELDS MARKED TRANSIENT WILL BE LOST R% 4HE PROVIDER MUST ASSUME THAT ANY REFERENCE TO THE 3ESSION#ONTEXT OBJECT WILL BE NOT SURVIVE PASSIVATION IF THE 3ESSION#ONTEXT OBJECT IS NOT SERIALIZABLE no matter what! Container MUST passivate your context, 9 'IVEN A STATELESS SESSION BEAN WITH CONTAINER MANAGED TRANSACTION DEMARCATION FROM WHICH METHODS CAN YOU ACCESS ANOTHER BEAN #HOOSE ALL THAT APPLY R! R" R# R$ (spec: 90) HME&UHDWH HME5HPRYH A BUSINESS METHOD - also true for stateless BMT beans VHW6HVVLRQ&RQWH[W \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 82 PRFNH[DPDQVZHUV 10 (API docs) .OTE 4HE REAL EXAM HAS SEVERAL TYPES OF @DRAG AND DROP QUESTIONS THAT WERE GOING TO DO A LAME JOB OF SIMULATING WITH THIS QUESTION -ATCH THE METHODS ON THE LEFT WITH THE INTERFACES IN WHICH THOSE METHODS CAN BE FOUND ON THE RIGHT ! MATCH IS CORRECT IF THE METHOD IS EITHER DECLARED IN OR INHERITED BY THE INTERFACE .OTE 4HERE MAY BE SOME MANY TO ONE AND ONE TO MANY RELATIONSHIPS IN YOUR ANSWER ! AFTER#OMPLETION " GET5SER4RANSACTION # AFTER"EGIN $ IS#ALLER)N2OLE % GET2OLL"ACK/NLY & SET3ESSION#ONTEXT ' SET2OLLBACK/NLY 11 1 2 1 2 2 3 2,4 3ESSION3YNCHRONIZATION 3ESSION#ONTEXT 3ESSION"EAN 5SER4RANSACTION (spec: 70) 7HAT IS TRUE ABOUT A SESSION BEANS LIFECYCLE #HOOSE ALL THAT APPLY R! 4HE CONTAINER WILL ALWAYS CREATE A NEW SESSION BEAN INSTANCE WHEN THE CLIENT INVOKES THE FUHDWH METHOD ON THE HOME INTERFACE OF A STATELESS BEAN - Container makes stateless beans whenever it wants R" 4HE CONTAINER WILL ALWAYS CALL HME5HPRYH THE UHPRYH WHEN THE CLIENT INVOKES METHOD ON A STATELESS BEANS HOME INTERFACE - Container removes a stateless bean ONLY when it wants to shrink the pool R# ! SESSION BEAN CANNOT BE PASSIVATED WHILE IT IS IN A TRANSACTION R$ 4HE VHW6HVVLRQ&RQWH[W METHOD IS NOT INVOKED DURING A STATELESS BEANS LIFECYCLE 12 7HICH OF THE FOLLOWING METHODS ARE CONTAINER CALLBACK METHODS #HOOSE ALL THAT APPLY (spec: 73) R! HME3DVVLYDWH R" VHW5ROOEDFN2QO\ R# VHW6HVVLRQ&RQWH[W R$ JHW5ROOEDFN2QO\ R% JHW8VHU7UDQVDFWLRQ &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 83 VHVVLRQEHDQOLIHF\FOH 13 )N WHAT CASES WILL THE CONTAINER FAIL TO CALL EJB2EMOVE ON A SESSION BEAN #HOOSE ALL THAT APPLY R! R" R# R$ 14 15 )F AN APPLICATION EXCEPTION IS THROWN FROM A BUSINESS METHOD )F A TIMEOUT OCCURS WHILE THE BEAN IS IN THE METHOD READY STATE )F AN APPLICATION EXCEPTION IS THROWN FROM WITHIN A TRANSACTION HME&UHDWH HME5HPRYH - for stateful on (spec: 80, 90) ly A BUSINESS METHOD VHW6HVVLRQ&RQWH[W &OR WHICH TYPE OF BEAN CAN THE CONTAINER PASSIVATE AN INSTANCE THAT IS IN A TRANSACTION R! R" R# R$ 16 )F THE CONTAINER CRASHES 7HICH METHODS ALLOW BOTH STATEFUL AND STATELESS SESSION BEANS WITH BEAN MANAGED TRANSACTION DEMARCATION TO ACCESS 5SER4RANSACTION METHODS #HOOSE ALL THAT APPLY R! R" R# R$ (spec: 82) (spec: 70) ONLY STATEFUL SESSION BEANS ONLY STATELESS SESSION BEANS BOTH STATEFUL AND STATELESS SESSION BEANS NEITHER STATEFUL NOR STATELESS SESSION BEANS &OR SESSION BEANS WHICH ARE THE RESPONSIBILITY OF THE #ONTAINER #HOOSE ALL THAT APPLY R! R" R# R$ )NVOKING THE LOCAL INTERFACE FUHDWH )NVOKING THE JHW6HVVLRQ&RQWH[W )NVOKING THE HME&UHDWH client calls it METHOD - Container calls setSessionContext METHOD - METHOD %NSURING THAT THE HME5HPRYH METHOD IS ALWAYS INVOKED - might be passivated bemisansed if times out, or bean thro exception ws a runtime \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Return to Table of Contents Page 84 PRFNH[DPDQVZHUV 17 &OR THIS DRAG AND DROP TYPE QUESTION YOU CAN USE EACH ELEMENT ONLY ONCE 7HICH INTERFACE SHOULD BE MATCHED WITH WHICH FACT SO THAT ALL FOUR MATCHES ARE CORRECT b A (AS A JHW+RPH+DQGOH METHOD B EXTENDS µMDYD[HME(-%2EMHFW¶ a LOCAL COMPONENTC METHODS MUST ./4 THROW c REMOTE COMPONENT REMOTE HOME LOCAL HOME 18 20 µMDYDUPL5HPRWH([FHSWLRQ¶ d 7HICH CAN BE CALLED BY THE CONTAINER DURING THE LIFECYCLE OF A SESSION BEAN #HOOSE ALL THAT APPLY R! R" R# R$ R% 19 D CAN BE USED TO RETRIEVE AN %*",OCAL/BJECT FUHDWH (spec: 90, 97) - client calls BEAN CONSTRUCTOR VHW5ROOEDFN2QO\ - bean calls these JHW8VHU7UDQVDFWLRQ HME5HPRYH 7HICH STATEMENTS ABOUT A SESSION BEAN CLASS ARE TRUE #HOOSE ALL THAT APPLY (spec: 95) R! 4HEY CAN BE MARKED @lNAL R" 4HEY DO NOT NEED A NO ARGUMENT CONSTRUCTOR R# 4HEIR COMPONENT INTERFACE METHODS CAN BE @PRIVATE R$ 4HEIR BUSINESS METHOD NAMES MUST START WITH hEJBv R% 4HEIR @EJB#REATE METHODS MUST NOT BE DECLARED AS @lNAL &OR WHICH TYPE OF BEAN CAN THE 3ESSION3YNCHRONIZATION INTERFACE BE IMPLEMENTED (spec: 75) R! ONLY STATEFUL SESSION BEANS R" ONLY STATELESS SESSION BEANS R# BOTH STATEFUL AND STATELESS SESSION BEANS R$ NEITHER STATEFUL NOR STATELESS SESSION BEANS &KDSWHU Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 4. Being a Session Bean Page 85 Return to Table of Contents VHVVLRQEHDQOLIHF\FOH 21 7HICH STATEMENTS ARE TRUE FOR STATELESS SESSION BEAN INSTANCES #HOOSE ALL THAT APPLY (spec: 88) R! !NY INSTANCE CAN BE USED FOR ANY CLIENT R" #ONVERSATIONAL STATE MUST BE RETAINED ACROSS METHODS R# #ONVERSATIONAL STATE MUST BE RETAINED ACROSS TRANSACTIONS R$ 4HEY CAN BE PASSIVATED R% 4HEY DO NOT SUPPORT TRANSACTIONS \RXDUHKHUH Chapter 4. Being a Session Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Table of Contents Chapter 5. Entities Are Persistent................................................................ 1 Section 5.1. OBJECTIVES: Entity Beans........................................................................................................................................ 2 Section 5.2. What's an entity bean?................................................................................................................................................ 3 Section 5.3. Entities vs. Entity Beans............................................................................................................................................. 4 Section 5.4. There are no Dumb Questions.................................................................................................................................... 5 Section 5.5. There are no Dumb Questions.................................................................................................................................... 6 Section 5.6. Entity beans from the client's point of view............................................................................................................... 7 Section 5.7. Entity bean overview................................................................................................................................................... 8 Section 5.8. Entity bean overview................................................................................................................................................... 9 Section 5.9. A very simple Customer entity bean......................................................................................................................... 10 Section 5.10. An entity bean's client view...................................................................................................................................... 11 Section 5.11. Entity bean Remote component interface............................................................................................................... 12 Section 5.12. Entity bean Remote component interface............................................................................................................... 13 Section 5.13. Rules for the Remote component interface............................................................................................................. 14 Section 5.14. Entity bean Remote home interface........................................................................................................................ 15 Section 5.15. What does the client really want from an entity bean home?................................................................................. 16 Section 5.16. Entity bean Remote component interface............................................................................................................... 17 Section 5.17. When finders have a dark side................................................................................................................................. 18 Section 5.18. There are no Dumb Questions................................................................................................................................. 19 Section 5.19. Home business methods to the rescue................................................................................................................... 20 Section 5.20. Rules for the Remote home interface..................................................................................................................... 21 Section 5.21. Sharpen your pencil................................................................................................................................................. 22 Section 5.22. Session bean create() vs. entity bean create()........................................................................................................ 23 Section 5.23. Session bean remove() vs. entity bean remove().................................................................................................... 24 Section 5.24. Entity/bean/instance death.................................................................................................................................... 27 Section 5.25. Client calls remove()............................................................................................................................................... 28 Section 5.26. Entity bean client view............................................................................................................................................ 29 Section 5.27. COFFEE CRAM....................................................................................................................................................... 30 Section 5.28. COFFEE CRAM....................................................................................................................................................... 33 Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 1 entity bean intro 5 4 4 (QWLWLHVDUH3HUVLVWHQW DQGWKHQ,VDLG´<RXZDQWDSLHFH RIPH"*RDKHDGWDNH\RXUEHVWVKRWEXGG\µ +HGLGQ·WNQRZKHZDVPHVVLQJZLWKDQHQWLW\EHDQ6R KHWKUHZDQH[FHSWLRQWKHQKHFUDVKHGWKHVHUYHU EXW,·PVWLOOKHUH,ZRQ·WJRGRZQWKDWHDV\QR VLUHH$VORQJDV,·PLQWKHGDWDEDVH,·OOMXVW NHHSFRPLQJEDFNVRGR\RXUZRUVW (QWLW\EHDQVSHUVLVW(QWLW\EHDQVH[LVW(QWLW\EHDQVDUH7KH\DUHREMHFW UHSUHVHQWDWLRQVRIVRPHWKLQJLQDQXQGHUO\LQJSHUVLVWHQWVWRUH 7KLQNGDWDEDVH EHFDXVHPRVWHQWLW\EHDQVUHSUHVHQWVRPHWKLQJIURPDUHODWLRQDOGDWDEDVH ,I\RX KDYHD&XVWRPHUHQWLW\EHDQWKHQRQHEHDQPLJKWUHSUHVHQWWKHHQWLW\7\OHU'XUGHQ ,'ZKLOHDQRWKHULVWKHHQWLW\'RQQ\'DUNR,'7ZREHDQVUHSUHVHQWLQJWZR UHDOHQWLWLHV$QHQWLW\EHDQLVVLPSO\DUHDOL]DWLRQRIVRPHWKLQJWKDWDOUHDG\H[LVWVLQD SHUVLVWHQWVWRUH6RPHWKLQJWKDWDOUHDG\LV$QGWKHVHVXFNHUVDUHKDUGWRNLOO$VORQJDV WKHGDWDLVLQWKHGDWDEDVHLWFDQNHHSFRPLQJEDFNLQWKHIRUPRIDQHQWLW\EHDQ WKLVLVDQHZFKDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 2 H[DPREMHFWLYHV (QWLW\%HDQV 2IILFLDO ,GHQWLI\FRUUHFWDQGLQFRUUHFWVWDWHPHQWVRU H[DPSOHVDERXWWKHFOLHQWYLHZRIDQHQWLW\ EHDQ¶VORFDODQG5HPRWHKRPHLQWHUIDFH LQFOXGLQJWKHFRGHXVHGWRORFDWHDQHQWLW\ EHDQ¶VKRPHLQWHUIDFHDQGWKHKRPH LQWHUIDFHPHWKRGVSURYLGHGWRWKHFOLHQW :KDWLWUHDOO\PHDQV <RXKDYHWRNQRZWKHUXOHVIRUDQHQWLW\EHDQKRPH LQWHUIDFHDQGHVSHFLDOO\KRZWKH\¶UHGLIIHUHQWIURPD VHVVLRQEHDQ<RXPXVWNQRZWKDW\RX¶UHQRWUHTXLUHG WRKDYHDFUHDWH PHWKRGEXWWKDW\RXPXVWKDYH DWOHDVWRQH¿QGHUPHWKRG¿QG%\3ULPDU\.H\ <RX KDYHWRNQRZWKDWWKHUHWXUQW\SHRIVLQJOHHQWLW\ ¿QGHUVDQGFUHDWHPHWKRGVPXVWDOZD\VEHWKH FRPSRQHQWLQWHUIDFHW\SHEXWWKDWPXOWLHQWLW\¿QGHUV PXVWUHWXUQD&ROOHFWLRQ<RXPXVWNQRZWKDWVLQJOH HQWLW\¿QGHUVWKURZDQ2EMHFW1RW)RXQG([FHSWLRQLI WKHUH¶VQRPDWFKLQWKHGDWDEDVHEXWWKDWPXOWLHQWLW\ ¿QGHUVDOZD\VUHWXUQD&ROOHFWLRQHYHQLILWVHPSW\ <RXDOVRKDYHWRNQRZWKDWKRPHEXVLQHVVPHWKRGV DUHQ¶WUHTXLUHGWRUHWXUQWKHFRPSRQHQWLQWHUIDFH² WKH\FDQUHWXUQDQ\WKLQJWKDWFDQEHOHJDOO\SDVVHG <RXPXVWDOVRNQRZWKDWLIDQHQWLW\LVGHOHWHGIURPWKH GDWDEDVHLWFDQQRORQJHUEHUHSUHVHQWHGDVDQHQWLW\ EHDQEXWWKDWLIDQHQWLW\EHDQLQVWDQFHGLHV WKURXJK DQH[FHSWLRQRUHYHQDVHUYHUFUDVK WKHHQWLW\LWVHOI SHUVLVWV$VORQJDVWKHHQWLW\LVLQWKHGDWDEDVHLWFDQ EHUHSUHVHQWHGE\DQHQWLW\EHDQ Copyright Safari Books Online #896963 ,GHQWLI\FRUUHFWDQGLQFRUUHFWVWDWHPHQWV RUH[DPSOHVDERXWWKHFOLHQWYLHZRIDQ HQWLW\EHDQ¶VORFDOFRPSRQHQWLQWHUIDFH (-%/RFDO2EMHFW DQG5HPRWHFRPSRQHQW LQWHUIDFH (-%2EMHFW ,GHQWLI\WKHXVHV\QWD[DQGEHKDYLRURIWKH IROORZLQJHQWLW\EHDQKRPHPHWKRGW\SHV IRU&03¿QGHUPHWKRGVFUHDWHPHWKRGV UHPRYHPHWKRGVDQGKRPHEXVLQHVV PHWKRGV 7KHUXOHVIRUKRZ\RXGH¿QHWKLQJVLQDQHQWLW\ FRPSRQHQWLQWHUIDFHDUHLGHQWLFDOWRWKHUXOHVIRUD VHVVLRQEHDQFRPSRQHQWLQWHUIDFH%XWLQREMHFWLYH ZH¶OOORRNDWKRZWKHPHWKRGVEHKDYHGLIIHUHQWO\ 7KHEHKDYLRURIUHPRYH LVSURIRXQGO\GLIIHUHQWIRU DQHQWLW\EHDQDQG\RXPXVWXQGHUVWDQGWKDWFDOOLQJ UHPRYH UHDOO\PHDQV³GHOHWHWKLVHQWLW\IURPWKH XQGHUO\LQJSHUVLVWHQWVWRUH´<RXPXVWNQRZWKDW FDOOLQJFUHDWH RQDQHQWLW\EHDQPHDQV³LQVHUWD QHZURZLQWKHGDWDEDVH´ 2.WHFKQLFDOO\LWPHDQV ³FUHDWHDQHZHQWLW\LQWKHXQGHUO\LQJSHUVLVWHQWVWRUH´ <RXPXVWNQRZWKDWLIDQHQWLW\LVGHOHWHGIURPWKH XQGHUO\LQJSHUVLVWHQWVWRUHWKHHQWLW\EHDQµGLHV¶HYHQ WKRXJKWKHEHDQLQVWDQFHJRHVEDFNWRWKHSRRO &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 3 HQWLW\EHDQLQWUR ?PI\¼[IVMV\Q\aJMIV' /DVW'DUNR )LUVW'RQQ\ &86720(57$%/( /DVW )LUVW ,' 'DUNR 'RQQ\ -RQHV ,QGLH /LQ 7DP ,' Q &XV WRPHU%HD /DVW-RQHV )LUVW,QGLH ,' ent store. nt data in a persiy stbean obese pr re s an be y tit En means that entit That almost alwaysional database rows (although it lat re instance jects map to mplex than just one co re mo lot a be n ca maps to one row...) Q &XV WRPHU%HD /DVW/LQ )LUVW7DP ,' Q &XV WRPHU%HD !N ENTITY BEAN IS AN OBJECT ORIENTED WAY OF LOOKING AT IN A PERSISTENT STORE 4HE SPEC SAYS hPERSISTENT STOREv AND DOESNT SPECIFY WHAT THAT PERSISTENT STORE MUST BE )T COULD BE ANYTHING THAT SATISlES THE REQUIREMENTS OF @PERSISTENCE INCLUDING A RELATIONAL DATABASE AN OBJECT DATABASE OR EVEN SOMETHING AS LAME AND INEFlCIENT AS STORING SERIALIZED OBJECTS TO lLES "UT IN MOST REAL WORLD SCENARIOS WERE TALKING A RELATIONAL DATABASE !ND THAT MEANS AN INDIVIDUAL ROW IN A TABLE MAPS TO A UNIQUE ENTITY BEAN 4HE OBJECT TO RELATIONAL /2 MAPPING COULD BE A LOT MORE COMPLEX THAN A SIMPLE ONE ROW EQUALS ONE BEAN SCENARIO BUT WELL GET INTO THAT IN THE NEXT CHAPTER %NTITY BEANS ARE DATA OBJECTS 4HEY ARETHINGS .OUNS !S OPPOSED TO SESSION BEANS WHICH ARE PROCESSES 6ERBS %NTITY BEANS MIGHT REPRESENT THINGS LIKE PEOPLE PRODUCTS ORDERS BOOKINGS INVENTORY ANIMALSTHINGS %NTITY BEANS WOULD NOT REPRESENT THINGS LIKE CREDIT CARD VERIlCATION ADVISOR SERVICE ORDER SUBMISSION MEMBERSHIP REGISTRATION PROCESSES /F COURSE IN VIRTUALLY ALL WELL DESIGNED %*" APPLICATIONS ENTITY BEANS WILL BE COMBINED WITH SESSION BEANS WHERE THE CLIENT INTERACTS WITH THE PROCESS SESSION BEAN AND THE SESSION BEAN USES ENTITY BEANS WHEN IT NEEDS DATA AS PART OF THE PROCESS \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 4 HQWLW\EHDQV -V\Q\QM[^[-V\Q\a*MIV[ !N ENTITY IS THE REAL THING THAT THE ENTITY BEAN REPRESENTS )T DOESNT WORK IN REVERSE 4HE ENTITY DOES NOT REPRESENT THE ENTITY BEAN 4HAT MIGHT SEEM LIKE A SUBTLE POINT BUT ITS NOT 4HE DIFFERENCE IS IN KNOWING WHICH ONE IS REAL AND WHICH ONE IS SIMPLY A VIEW OF THE REAL THING )N OTHER WORDS IF YOU DELETE THE REAL ENTITY THE THING IN THE DATABASE THE ENTITY BEAN DISINTEGRATES 0OOF "UT IF THE ENTITY BEAN DIES AS AN INSTANCE ON THE HEAP IT DOESNT KILL THE REAL ENTITY IN THE DATABASE "Y THE WAY WERE NOW GOING TO SAY @DATABASE INSTEAD OF @UNDERLYING PERSISTENT STORE BUT YOU AND ) BOTH KNOW THAT THESE TWO ARE NOT NECESSARILY SYNONYMOUS AND THAT THE @UNDERLYING PERSISTENT STORE NEED NOT BEBUT USUALLY ISA DATABASE 4HERE WEVE SAID IT 3O YOU WONT HAVE THE RIGHT PERSPECTIVE IF YOU THINK OF THE ENTITY BEAN AS SOME DATA OBJECT THAT HAPPENS TO BE BACKED UP TO PERSISTENT STORAGE )N NEARLY ALL APPLICATIONS THE VAST MAJORITY OF ENTITIES USED IN THE APP ARE NOT REALIZED AS ENTITY BEANS AT ANY GIVEN TIME )NSTEAD MOST ENTITY BEANS BECOME A REPRESENTATION OF AN UNDERLYING ENTITY ONLY WHEN THAT PARTICULAR ENTITY IS NEEDED IN THE APPLICATION LOGIC +L,·OOEHSOD\LQJ\RX IRUWKLVWUDQVDFWLRQVR,·G OLNHWRNQRZDOLWWOHPRUH DERXW\RX 'DUNR 'RQQ\ entity entity b e an &OR EXAMPLE IMAGINE YOUR DATABASE HAS CUSTOMERS "UT AT ANY GIVEN TIME CLIENTS THROUGH BUSINESS PROCESSES IN SESSION BEANS ARE USING ONLY OF THOSE CUSTOMERS )N THAT CASE YOU PROBABLY HAVE ONLY ENTITY BEANS ACTIVELY REPRESENTING CUSTOMER ENTITIES 4HE REST OF THE CUSTOMER ENTITIES ARE JUST SITTING THERE IN THE DATABASE WAITING FOR A CLIENT PROCESS TO NEED THEM 4HINK OF ENTITY BEANS AS ACTORS )N THE SCENARIO WE JUST DESCRIBED YOU HAVE ACTORS EACH PLAYING A DIFFERENT ROLE OF SOMEONE IN THE DATABASE "UT THE OTHER ENTITIES IN THE DATABASETHE OTHER CUSTOMERSDO NOT HAVE ANYONE PLAYING THEM "UT AS SOON AS A CLIENT COMES ALONG AND TRIES TO GET ONE OF THOSE OTHER SAY TO CHANGE THAT CUSTOMERS ADDRESS OR CHECK ITS CREDIT LIMIT AN ACTOR ENTITY BEAN WILL BE CHOSEN TO PLAY THE ROLE OF THAT SELECTED CUSTOMER &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Page 5 Return to Table of Contents HQWLW\EHDQLQWUR WKHUH DUH QR 'XPE 4XHVWLRQV Q: *UST SO )VE GOT THIS STRAIGHT YOUR ENTIRE DATABASE DOESNT GET LOADED IN AS ENTITY BEANS RIGHT ) MEAN THAT WOULD BE INSANE (QWLW\EHDQVDUHDQ22ZD\RI ORRNLQJDWGDWDLQDSHUVLVWHQWVWRUH A: $QHQWLW\LVDUHDOXQLTXHO\ LGHQWLILDEOHWKLQJWKDWH[LVWV VRPHZKHUHRXWVLGHRI(-%DQG WKHHQWLW\EHDQ·VMRELVVLPSO\WR %(&20(DQ22YLHZRIWKDWUHDO SHUVLVWHQWHQWLW\ 9OURE RIGHT AT LEAST ABOUT THE FIRST PART .O YOUR ENTIRE DATABASE IS ./4 LOADED IN AS ENTITY BEANS 4HINK OF ENTITY BEANS AS JUST IN TIME REPRESENTATIONS OF ONLY THE ENTITIES IN THE DATABASE THAT ARE ACTIVELY NEEDED BY CLIENT BUSINESS PROCESSES "UT DOES THAT MEAN IT WOULD BE INSANE TO LOAD THEM ALL IN .OT NECESSARILY 7HILE IT WOULD CERTAINLY UP YOUR RESOURCE REQUIREMENTS ESPECIALLY IF THE DATA IN THE UNDERLYING DATABASE CONTINUES TO GROW JUST THINK OF HOW &!34 IT WOULD BE !LTHOUGH THERE IS NOTHING IN THE SPEC THAT REQUIRES THIS A VENDOR CAN CHOOSE TO LET YOU CONFIGURE THE APP IN SUCH A WAY THAT IT DOES PRE LOAD ALL THE ENTITIES IN THE DATABASE INTO BEANS 4HAT GIVES YOU ESSENTIALLY A LIGHTENING FAST IN MEMORY DATABASE THAT STILL SYNCHRONIZES ITSELF WITH THE REAL UNDERLYING STORE YOULL SEE HOW IN A MOMENT !ND TO TAKE IT A STEP FURTHER IF YOUR SERVER GIVES YOU A WAY TO TELL IT THAT YOUR ENTITY BEANS ARE YOUR ONLY ACCESS TO THE DATABASE THEN THE SERVER CAN EVEN ELIMINATE THE SYNCHRONIZATION AND JUST KEEP THE DATABASE IN MEMORY SAVING ONLY WHAT IT NEEDS AS A BACKUP IN CASE OF A CRASH 7KHHQWLW\EHDQFDQQRWH[LVW ZLWKRXWWKHUHDOHQWLW\EXWWKHUHDO HQWLW\FDQH[LVWZLWKRXWWKHEHDQ $QHQWLW\EHDQLV127WKHUHDO HQWLW\LW·VMXVWDUHSUHVHQWDWLRQRU ´UHDOL]DWLRQµRIWKHHQWLW\ \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Page 6 Return to Table of Contents HQWLW\EHDQV WKHUH DUH QR 'XPE 4XHVWLRQV Q: 7HY DO YOU EVEN .%%$ ENTITY BEANS 7HY NOT JUST GO STRAIGHT TO THE DATABASE FROM A SESSION BEAN A: 9OU CAN GO FROM A SESSION BEAN DIRECTLY TO THE DATABASE (ECK YOU CAN ALWAYS GO FROM THE CLIENT TO THE DATABASE BUT THEN WERE BACK TO THE OLD NON SCALABLE CLIENT SERVER TWO TIER ARCHITECTURE AND WE ALL KNOW WHY THAT IS USUALLY A BAD IDEA DOESNT SCALE BUSINESS LOGIC IS IN THE CLIENT HARD TO MAINTAIN ETC )F YOU USE ENTITY BEANS INSTEAD OF DIRECT CALLS TO THE DATABASE YOU GET TO TAKE ADVANTAGE OF ALL THE #ONTAINERS SERVICES INCLUDING THE ABILITY TO WRAP SEVERAL DATABASE TRIPS IN ONE TRANSACTION "UT AS YOULL SEE ONE OF THE BIGGEST BENEFITS OF USING ENTITY BEANS IS THAT THE #ONTAINER AUTOMATICALLY SYNCHRONIZES BETWEEN THE DATABASE AND THE ENTITY BEAN "UT THE SINGLE MOST COMPELLING REASON FOR ENTITY BEANS IS THAT THEY TAKE YOU FROM THE RELATIONAL WORLD TO THE OBJECT WORLD )N OTHER WORDS YOU GET TO STAY WITH OBJECTS ALL THE WAY DOWN IN YOUR APP RATHER THAN MAPPING BACK AND FORTH IN YOUR CODE !ND IF YOURE USING #-0 WHICH YOU ALMOST CERTAINLY WILL BE FOR REASONS THAT WILL BECOME OBVIOUS A LITTLE LATER YOU WONT HAVE A SHRED OF 31, IN YOUR BEAN CODE )N FACT WITH #-0 ENTITY BEANS YOU GET TO PRETEND THAT YOUR ENTIRE DATABASE EXISTS SOLELY AS OBJECTS ON THE HEAP QRZ QHHGWRN <RXGRQ¶W H[DP %&IRUWKH 64/RU-' WRNQRZ X'2QHHG R64/ RVWSDUW<R UW P LOD P KH VL UW V IR :HOO RPHZD\ RXW KLFKLVLQV DQ\WKLQJDE YHU Z (-%4/Z QR N WR HG FR QH ¶W ¶W VQ RQ RH G EXW\RX HH[DPG $QGZKLOHWK DWDEHDQ WK Z QR 64/LWVHOI N WR '2QHHG RPZKLFK -'%&\RX D6RXUFHIU YD[VTO'DW TO&RQQHFWLRQ<RX¶OO ORRNXSDMD V YD XSVLQWKH DQJHWDMD F RN OR DQ H EH UF H RX WK D6 DERXW'DW HQW OHDUQPRUH -%HQYLURQP ( Q UR WH FKDS 3URE MAYBE YOURE COMFORTABLE WITH *$"# BUT WHAT ABOUT THE REST OF THE TEAM !ND LETS FACE ITTHINK ING IN // MAKES A LOT MORE SENSE THAN HAVING TO SHIFT BETWEEN // AND ENTITY CODE &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 7 HQWLW\EHDQLQWUR -V\Q\aJMIV[NZWU\PMKTQMV\¼[XWQV\WN^QM_ :LWKWKHFXVWRPHUEHDQ ,ZDQWWR LQSXWQHZFXVWRPHUVUHPRYHFXVWRPHUV DQGXSGDWHFXVWRPHULQIRUPDWLRQ,DOVR ZDQWWRGRTXHULHVWRVHOHFWFXVWRPHUV EDVHGRQVRPHFULWHULD$QG,·PVRUU\EXW ,MXVW5()86(WRWKLQNLQ64/<RXNQRZ ZKDWWKH\VD\RQFH\RX·YHWULHGDQ REMHFW\RX·OOQHYHUJREDFN 7KHFOLHQWZDQWVWRGRGDWDEDVH VWXIIEXWLQD-DYD22ZD\ 0DNHDQHZHQWLW\ DQ64/,16(57 'HOHWHDQHQWLW\ DQ64/'(/(7( 8SGDWHHQWLW\VWDWH DQ64/83'$7( 6HDUFKTXHU\RQHQWLWLHV DQ64/6(/(&7 4HE CLIENT INTERFACE FOR AN ENTITY BEAN IS A LITTLE DIFFERENT FROM THAT OF A SESSION BEAN &OR EXAMPLE WHEN A SESSION BEAN CLIENT WANTS TO GET A BEAN SO THAT IT CAN CALL THE BEANS BUSINESS METHODS THE CLIENT CALLS CREATE AND THE #ONTAINER ALLOCATES A NEW %*" OBJECT "UT WHAT IF A CLIENT WANTS TO USE AN EXISTING ENTITY BEAN &OR EXAMPLE WHAT IF THE CLIENT DOESNT WANT JUST SOME RANDOM ENTITY BUT WANTS THE %*" OBJECT OF A SPECIlC ENTITY SAY "ART 3IMPSON )N THAT CASE A CREATE WONT WORK 4HE CLIENT DOESNT WANT A NEW ENTITY BUT WANTS A REFERENCE TO AN EXISTING ENTITY 3O AS YOULL SEE IN A MINUTE THE CLIENT INTERFACE FOR AN ENTITY BEAN ADDS AND MUST HAVE ONE OR MORE lNDER METHODS 4HE NEXT TWO PAGES ARE HIGH LEVEL PICTURES OF HOW ENTITY BEANS ARE CREATED INSERT AND FOUND SELECT 4HE SCENARIOS IN THESE PICTURES WILL BE lLLED IN WITH A LOT MORE DETAIL AS WE GO THROUGH THIS CHAPTER BUT FOR NOW YOU CAN RELAX \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 8 HQWLW\EHDQV (QWLW\EHDQRYHUYLHZ 6FHQDULRFOLHQWZDQWVDUHIHUHQFHWRDQH[LVWLQJHQWLW\ FRQWH[W EHDQ FOLHQW EHDQ ÀQ G% \3 ULP DU \. H\ +RPH REMHFW VWXE ´ µ RROWR WKHS HDQLQ H[LVWV E D N V D WKDW YHULI\ ´µ U\.H\ 3ULPD \ % G ÀQ FRQWH[W %HDQ3RRO '% $IWHUGRLQJD-1',ORRNXSRQWKHHQWLW\EHDQKRPHDQGJHWWLQJDKRPHLQWHUIDFHUHIHUHQFH WKHFOLHQWFDOOVILQG%\3ULPDU\.H\ ´µ RQWKHKRPHVWXE 7KHILQG%\3ULPDU\.H\ ´µ PHWKRGLQYRFDWLRQLVSDVVHGWRWKHKRPHREMHFW 7KH&RQWDLQHUDVNVDEHDQLQWKHSRROWRYHULI\WKDWH[LVWVLQWKHGDWDEDVH VWXE (-% REMHFW UHWX UQ(%RE MHFWV WXE +RPH REMHFW µ [LVWV H ´<HV EHDQ FRQWH[W %HDQ3RRO '% 7KHEHDQFKHFNVIRUDQHQWLW\LQWKHGDWDEDVHZLWKSULPDU\NH\ 7KHEHDQWHOOVWKHKRPHWKDWLVLQWKHGDWDEDVH 7KH&RQWDLQHUPDNHVRUILQGVDQ(-%REMHFWIRU WKHUHPLJKWDOUHDG\EHRQH 7KH&RQWDLQHUUHWXUQVWKHVWXEIRU &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 9 HQWLW\EHDQLQWUR (QWLW\EHDQRYHUYLHZ 6FHQDULRFOLHQWZDQWVWRFUHDWHDQHZHQWLW\ FRQWH[W EHDQ EHDQ FOLHQW FU HD WH FRQWH[W +RPH REMHFW ´% LOO\ % RE µ VWXE RRO RPS QIU EHD W H J Eµ O\%R H ´%LO FUHDW %HDQ3RRO '% $IWHUGRLQJD-1',ORRNXSRQWKHHQWLW\EHDQKRPHDQGJHWWLQJDKRPHLQWHUIDFHUHIHUHQFH WKHFOLHQWFDOOVFUHDWH ´%LOO\%REµ RQWKHKRPHVWXE 7KHFUHDWH ´%LOO\%REµ PHWKRGLQYRFDWLRQLVSDVVHGWRWKHKRPHREMHFW 7KH&RQWDLQHUSXOOVDEHDQIURPWKHSRROIRUWKLVEHDQW\SH FRXOGEHDQ\UDQGRPEHDQLQWKH SRRO 1RWLFHWKDWWKHEHDQDOUHDG\KDVLWVRZQFRQWH[WWKDWVWLFNVZLWKWKHEHDQ (-% REMHFW VWXE EHDQ FRQWH[W +RPH REMHFW ,QV HU ZLW WDQH %HDQ3RRO KD Z QH URZ ZS LQ ULP WKH DU\ GD NH WDE \ DVH '% 7KH&RQWDLQHU RUWKHEHDQ LQVHUWVDQHZURZLQWKHGDWDEDVHDQGWKHEHDQ JHQHUDWHVDQHZSULPDU\NH\ 7KHEHDQLVOLQNHGWRDQ(-%REMHFWDQGERWKWKHFRQWH[WDQG(-%REMHFWJHWWKHQHZSULPDU\NH\ 7KH&RQWDLQHUUHWXUQVDVWXEIRUWKHQHZO\FUHDWHGHQWLW\ %LOO\%RE ,IWKHFOLHQW·VRQO\ JRDOZDVWRLQVHUWWKHQHZURZWKHFOLHQWPLJKWQRWHYHQFDUHDERXWWKHUHWXUQHGVWXE \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 10 &XVWRPHUHQWLW\EHDQ )^MZa[QUXTM+][\WUMZMV\Q\aJMIV Bean ean) B the Entity implement (instead of Session SXEOLFFODVV&XVWRPHU%HDQLPSOHPHQWV(QWLW\%HDQ^ interface map to es the Þelds that cas SULYDWH6WULQJODVW1DPH ity beanÕs state includaba e-ent e s Th thi in le, tab se SULYDWH6WULQJ¿UVW1DPH in the Customer dat s umn col ) key ry ID (prima SULYDWH6WULQJSULPDU\.H\ Þrst name, last name, and ortant when youÕre n eve MORE impsion SULYDWH(QWLW\&RQWH[WFRQWH[W Your context is inst bean ses a of ead an entity bean SDFNDJHDQGLPSRUWVKHUH SXEOLF6WULQJHME&UHDWH 6WULQJODVW6WULQJ¿UVW ^ ntÕs or? We take the clie ODVW1DPH ODVW a lot like a construct k the loo s ent thi res Õt rep esn t Do tha les iab ¿UVW1DPH ¿UVW ance var abase table). args and assign themstatote inst SULPDU\.H\ WKLVJHW3. (i.e. columns in the dat ent sist per Õs Õt worry about ity don ent -key '%,16(57 ry ma pri the s The ejbCreate() return UHWXUQSULPDU\.H\ it right now. ` SXEOLF6WULQJJHW/DVW1DPH ^ UHWXUQODVW1DPH ` SXEOLFYRLGVHW/DVW1DPH 6WULQJQDPH ^ ODVW1DPH QDPH ` SXEOLF6WULQJJHW)LUVW1DPH ^ UHWXUQODVW1DPH ` SXEOLFYRLGVHW)LUVW1DPH 6WULQJQDPH ^ ¿UVW1DPH QDPH ` SXEOLFYRLGHME$FWLYDWH ^` SXEOLFYRLGHME3DVVLYDWH ^` SXEOLFYRLGHME5HPRYH ^'(/(7(` Plain old Java getters and setter s for the persistent Þelds. The mag as youÕll see in a minute, is that the result ic, se setters will ultimately lead to a databaofse the update ! n, that were Three container callbacks from EntityBea Y VERY VER have they pt... Exce n. nBea also in Sessio chapter. this in later see youÕll different meanings, as SXEOLFYRLGVHW(QWLW\&RQWH[W (QWLW\&RQWH[WFW[ ^ ntext, except for an FRQWH[W FW[ this is just like setSessionhCoentity beans, the context is wit t ` EntityContext. Bu session beans. SXEOLFYRLGXQVHW(QWLW\&RQWH[W ^` SXEOLFYRLGHME/RDG ^6(/(&7` SXEOLFYRLGHME6WRUH ^83'$7(` SULYDWH6WULQJJHW3. ^ UHWXUQ³´ LQW 0DWKUDQGRP ` than it is for usually WAY more important these are three NEW container call backs from the EntityBean interface (not found in SessionB ean). TheyÕre IMPORTANT! WorldÕs worst primary key algorithm.Normally weÕd use clientsupplied info or perhaps the databaseÕs auto-generated key. (ignore these last two 6WULQJSN ^6(/(&7UHWXUQSN` methods for now) SXEOLF6WULQJHME)LQG3ULPDU\.H\ SXEOLF&ROOHFWLRQHME)LQG%\&LW\ 6WULQJFLW\ ^6(/(&7UHWXUQFROOHFWLRQRINH\V` ` &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 11 HQWLW\EHDQLQWUR )VMV\Q\aJMIV¼[KTQMV\^QM_ /UR #USTOMER BEAN ISNT lNISHED SO DONT GET TOO ATTACHED TO IT !ND IN FACT THE CODE IS AN EXAMPLE OF BEAN MANAGED PERSISTENCE "-0 WHICH WE REALLY WONT BE USING IN THIS BOOK 7ELL TALK A LITTLE ABOUT "-0 AND A LOT ABOUT ITS MUCH MORE POPULAR COUNTERPART CONTAINER MANAGED PERSISTENCE #-0 IN THIS CHAPTER AND THE NEXT &OR NOW THOUGH WELL FOCUS ON THE CLIENT VIEW OF AN ENTITY BEAN AND THIS SIMPLE BEAN IS JUST TO GET YOU STARTED LOOKING AT ENTITY BEAN CODE 'IVEN THAT A #USTOMER BEAN REPRESENTS A #USTOMER ENTITY IE A REAL CUSTOMER IN THE UNDERLYING DATABASE WHAT BEHAVIORS SHOULD THE ENTITY BEAN HAVE )N OTHER WORDS WHAT KINDS OF THINGS MIGHT THE CLIENT WANT TO DO WITH EITHER A SINGLE #USTOMER OR MULTIPLE #USTOMERS 4HINGS YOUD DO WITH A DATABASE RECORD 4HE THINGS WE MENTIONED EARLIER INCLUDING MAKE A NEW #USTOMER DELETE A #USTOMER UPDATE A #USTOMERS lELDS COLUMNS AND QUERYSEARCH ON THE #USTOMER DATABASE YiX`e gfn\i MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ KRPH MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ KRPH MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ 0DNHDQHZFXVWRPHU FRPSRQHQW MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ &KDQJHDQH[LVWLQJFXVWRPHU¶VSKRQHQXPEHU FRPSRQHQW LPSRUWMDYD UPL5HPRWH([ FHSWLRQ LPSRUWMDYD UPL5HPRWH([ FHSWLRQ KRPH FRPSRQHQW MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ 7KLQNDERXWWKHIROORZLQJRSHUDWLRQVDQGILJXUHRXWZKLFKRI WKHWZRFOLHQWLQWHUIDFHV FRPSRQHQWRUKRPH LVEHWWHUVXLWHG IRUHDFKRSHUDWLRQ.HHSLQPLQGWKDWWKHUXOHVIRUHQWLW\EHDQ LQWHUIDFHVPLJKWEHGLIIHUHQWIURPVHVVLRQEHDQLQWHUIDFHV,I \RXWKLQNERWKLQWHUIDFHVDUHDSSURSULDWHFKHFNWKHPERWK :H·YHGRQHWKHILUVWRQHIRU\RX MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ KRPH FRPSRQHQW LPSRUWMDYD UPL5HPRWH([ FHSWLRQ LPSRUWMDYD UPL5HPRWH([ FHSWLRQ KRPH FRPSRQHQW MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ MDYD[HME LPSRUWMDYD UPL5HPRWH([ FHSWLRQ KRPH FRPSRQHQW )LQGDOOWKHFXVWRPHUVLQ3OHDVDQWYLOOH 'HOHWHDOOFXVWRPHUVSUHYLRXVO\GHFODUHGµLQDFWLYH¶ 'HOHWHDVSHFL¿FFXVWRPHU *HWWKHVWUHHWDGGUHVVRIDVSHFL¿FFXVWRPHU \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 12 HQWLW\5HPRWHFRPSRQHQWLQWHUIDFH -V\Q\aJMIV:MUW\MKWUXWVMV\QV\MZNIKM !N ENTITY BEANS COMPONENT INTERFACE IS JUST LIKE A SESSION BEANSIT HAS BUSINESS METHODS AND IT EXTENDS JAVAXEJB%*"/BJECT 4HAT MEANS A CLIENT CAN SEE THE METHODS YOUVE DECLARED IN YOUR COMPONENT INTERFACE AS WELL AS THE METHODS FROM %*"/BJECT GET(ANDLE REMOVE ETC "UT WHAT KINDS OF BUSINESS METHODS GO IN THE COMPONENT INTERFACE 4HE METHODS RELATED TO A SINGLE ENTITY 7HEN THE CLIENT HAS A REFERENCE TO AN ENTITY BEAN WHICH MEANS A REFERENCE TO THE BEANS %*" OBJECT OF COURSE THE CLIENT HAS A REFERENCE TO A SINGLE SPECIlC ENTITY &RED &LINTSTONE -ARGE 3IMPSON 2OY 2ODGERS 3O WHAT MIGHT A CLIENT WANT TO DO WITH A REFERENCE TO SAY -ARGE 3IMPSON $ELETE HER CHANGE HER LAST NAME GET HER HANDLE OR GET HER HOME SO THAT THE CLIENT CAN GET REFERENCES TO OTHER CUSTOMERS +EEP IN MIND THAT OUR SIMPLE #USTOMER BEAN ISNT VERY USEFUL YET WITH METHODS TO GET OR SET ONLY THE #USTOMER NAME ,ATER WELL BUILD IT OUT :KDW<28ZULWH LQWHUIDFH!! &XVWRPHU LQWHUIDFH!! &XVWRPHU JHW/DVW1DPH VHW/DVW1DPH 6WULQJV JHW/DVW1DPH VHW/DVW1DPH 6WULQJV JHW)LUVW1DPH VHW)LUVW1DPH 6WULQJV JHW)LUVW1DPH VHW)LUVW1DPH 6WULQJV LQWHUIDFH!! 5HPRWH Remember the actual hierarchy for Remote component interfacesthe component interface extends EJBObject, and EJBObject extends Remote :KDWWKH&/,(17VHHV LQWHUIDFH!! (-%2EMHFW LQWHUIDFH!! &XVWRPHU JHW3ULPDU\.H\ JHW(-%+RPH JHW+DQGOH UHPRYH LV,GHQWLFDO session e methods thcoatme from m sa e th e ar These e, because they bean clients seEJBObject interface! the javax.ejb. ss the client can acce mer Remember, the you declare in your Custo om at fr th ds ds ho ho met S the Þve met interface, PLUObject. JB javax.ejb.E &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Page 13 Return to Table of Contents HQWLW\EHDQLQWUR -V\Q\aJMIV:MUW\MKWUXWVMV\QV\MZNIKM SDFNDJHKHDG¿UVW LPSRUWMDYD[HME LPSRUWMDYDUPL5HPRWH([FHSWLRQ SXEOLFLQWHUIDFH&XVWRPHUH[WHQGV(-%2EMHFW^ SXEOLF6WULQJJHW/DVW1DPH WKURZV5HPRWH([FHSWLRQ SXEOLFYRLGVHW/DVW1DPH 6WULQJODVW1DPH WKURZV5HPRWH([FHSWLRQ SXEOLF6WULQJJHW)LUVW1DPH WKURZV5HPRWH([FHSWLRQ SXEOLFYRLGVHW)LUVW1DPH 6WULQJ¿UVW1DPH WKURZV5HPRWH([FHSWLRQ ` \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Page 14 Return to Table of Contents HQWLW\5HPRWHFRPSRQHQWLQWHUIDFH 5XOHVIRUWKH5HPRWHFRPSRQHQWLQWHUIDFH ,PSRUWMDYD[HME DQGMDYDUPL5HPRWH([FHSWLRQ ([WHQGMDYD[HME(-%2EMHFW 'HFODUHRQHRUPRUHEXVLQHVVPHWKRGVWKDWWKURZD5HPRWH([FHSWLRQ 4 $UJXPHQWVDQGUHWXUQW\SHVPXVWEH50,,,23FRPSDWLEOH 6HULDOL]DEOH SULPLWLYH5HPRWHRUDUUD\VRUFROOHFWLRQVRIDQ\RIWKRVH 4 4 (DFKPHWKRGPXVWGHFODUHD5HPRWH([FHSWLRQ 4 <RXFDQGHFODUH\RXURZQDSSOLFDWLRQH[FHSWLRQVEXWWKH\PXVW <RXFDQKDYHRYHUORDGHGPHWKRGV 127EHUXQWLPHH[FHSWLRQV LQRWKHUZRUGVWKH\PXVWEHFRPSLOHU FKHFNHGH[FHSWLRQV²VXEFODVVHVRI([FHSWLRQEXWQRWVXEFODVVHVRI 5XQWLPH([FHSWLRQ 4 0HWKRGVFDQKDYHDUELWUDU\QDPHVDVORQJDVWKH\GRQ¶WEHJLQZLWK³HME´ &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 15 HQWLW\EHDQLQWUR -V\Q\aJMIV:MUW\MPWUMQV\MZNIKM 0W_Q\¼[LQNNMZMV\NZWUI[M[[QWVJMIVPWUMQV\MZNIKM" <RX¶UHPRUHOLNHO\WR¿QGDQH[LVWLQJHQWLW\WKDQFUHDWHDQHZRQH !S A CLIENT YOULL PROBABLY SPEND A LOT MORE TIME USING REFERENCES TO EXISTING CUSTOMERS THAN YOULL SPEND CREATING NEW CUSTOMERS 7HETHER YOURE UPDATING A SPECIlC CUSTOMER OR DOING A BATCH OPERATION ON MANY CUSTOMERS YOULL NEED HOME INTERFACE CODE THAT LETS YOU SELECT CUSTOMERS MORE THAN YOULL NEED CREATION METHODS )N FACT THE CREATE METHOD IS OPTIONAL FOR ENTITY BEANS "ECAUSE YOU MIGHT HAVE A POLICY THAT SAYS NEW ENTRIES IN THE DATABASE MUST BE DONE DIRECTLY THROUGH A DATABASE ADMIN TOOL FOR EXAMPLE "UT YOURE REQUIRED TO PUT AT LEAST ONE lNDER METHOD FOR AN ENTITY BEAN HOMEYOU CAN HAVE AS MANY lNDERS AS YOU LIKE BUT YOU MUST HAVE lND"Y0RIMARY+EY3TRING PRIMARY+EY IN EVERY ENTITY HOME 7HICH MEANS IT MIGHT BE THE ONLY METHOD DECLARED IN THE HOME INTERFACE <RXPLJKWZDQWWRGRTXHULHVWKDWLQYROYHPRUHWKDQRQHHQWLW\ 3INCE SESSION BEANS REPRESENT PROCESS IT DOESNT MAKE SENSE TO SAY GET MULTIPLE INSTANCES OF THE SAME PROCESS "UT WITH ENTITY BEANS YOU MIGHT WANT TO DO THE SAME THINGS YOUD DO ON A DATABASE TABLE LIKE lND ALL THE CUSTOMERS WHO LIVE IN (ELSINKI AND ENJOY SURlNG :KDW<28ZULWH LQWHUIDFH!! &XVWRPHU+RPH FUHDWH 6WULQJODVW6WULQJILUVW ILQG%\3ULPDU\.H\ 6WULQJNH\ ILQG%\&LW\ 6WULQJFLW\ e the an client can iosen bean An entity beet ss se hods a same four m e, because both session se n ca nt aces clie an home interfHome. and entity beja JB E vax.ejb. must extends get to use BOTH an be y it Ent methods, while home remove()can use only the one s session bean Handle. that takes a :KDWWKH&/,(17VHHV LQWHUIDFH!! &XVWRPHU+RPH FUHDWH 6WULQJODVW6WULQJILUVW ILQG%\3ULPDU\.H\ 6WULQJNH\ ILQG%\&LW\ 6WULQJFLW\ JHW(-%0HWD'DWD JHW+RPH+DQGOH UHPRYH +DQGOHK UHPRYH 2EMHFWNH\ \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 16 HQWLW\KRPHLQWHUIDFH ?PI\LWM[\PMKTQMV\ZMITTa_IV\ NZWUIVMV\Q\aJMIVPWUM ' give ByPrimaryKeye th create and Þnd to e nc refere back a single te rface. in nt ne po m co row Þnder But a multiplllee-ction of references returns a co LQWHUIDFH!! &XVWRPHU+RPH &XVWRPHUFUHDWH 6WULQJODVW6WULQJILUVW &XVWRPHUILQG%\3ULPDU\.H\ 6WULQJNH\ &ROOHFWLRQILQG%\&LW\ 6WULQJFLW\ 7ITH SESSION BEANS THAT WAS EASYA REFERENCE TO THE COMPONENT INTERFACE !ND THATS EXACTLY WHAT THE CREATE METHODS HAVE TO GIVE BACK 7ITH ENTITY BEANS ITS THE SAME FOR CREATE METHODSTHEY MUST GIVE BACK A REFERENCE TO THE COMPONENT INTERFACE IN THIS CASE THE COMPONENT INTERFACE FOR THE ENTITY JUST CREATED "UT WHAT IF YOU WANT TO lND AN EXISTING ENTITY BEAN INSTEAD OF MAKING A NEW ONE 4HATS WHAT THE MANDATORY lND"Y0RIMARY+EY METHOD IS FOR AND IT TOO MUST GIVE BACK A REFERENCE TO THE COMPONENT INTERFACE FOR THE BEAN MATCHING THAT KEY "UT WHAT IF THERE ISNT A MATCHING ENTITY )F THERES NO ENTITY WITH THAT KEY IN THE DATABASE THE CLIENT GETS A JAVAXEJB/BJECT .OT&OUND%XCEPTION 3O THE RETURN TYPE OF lND"Y0RIMARY+EY IS ALWAYS THE SAME AS IT IS FOR CREATE THE COMPONENT INTERFACE FOR THAT BEAN TYPE !ND OF COURSE THE RULES FOR SESSION BEAN CLIENT INTERFACES APPLIES HERE AS WELLA 2EMOTE HOME INTERFACE MUST GIVE BACK THE 2EMOTE COMPONENT INTERFACE AND THE LOCAL HOME INTERFACE MUST GIVE BACK THE LOCAL COMPONENT INTERFACE 4HIS STILL LEAVES US WITH A METHOD THAT CANNOT RETURN THE COMPONENT INTERFACE A MULTIPLE ENTITY lNDER LIKE OUR lND"Y#ITY METHOD 7ELL THE CLIENTS GOAL DOESNT CHANGE WITH MULTIPLE ENTITY lNDERS THE CLIENT STILL WANTS A REFERENCE TO THE COMPONENT INTERFACE ONLY THIS TIME IT MIGHT BE A WHOLE COLLECTION OF THEM /NE FOR EVERY CUSTOMER ENTITY IN THE CITY NAMED IN THE METHODS ARGUMENT 7KHFUHDWHDQGILQGHU PHWKRGVLQDQHQWLW\EHDQ KRPHDOZD\VJLYHEDFN WKHEHDQ·VFRPSRQHQW LQWHUIDFH )RUFUHDWH DQG ILQG%\3ULPDU\.H\ WKH FOLHQWJHWVDUHIHUHQFHWR RQH(-%REMHFW )RUPXOWLSOHHQWLW\ ILQGHUVWKHFOLHQWPLJKW JHWDZKROH3,/(RI UHIHUHQFHVWR(-%REMHFWV³ RQHIRUHDFKEHDQWKDW PDWFKHVWKHTXHU\ .OTE A CLIENT WILL NOT GET AN EXCEPTION IF A MULTI ENTITY lNDER CANT lND ANY MATCHES )NSTEAD THE CLIENT WILL STILL GET A #OLLECTION BUT IT WILL SIMPLY BE EMPTY ! #OLLECTION WITH NO ELEMENTS /NLY SINGLE ENTITY lNDERS THROW EXCEPTIONS WHEN NOTHING MATCHES THE lND CRITERIA &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Page 17 Return to Table of Contents HQWLW\EHDQLQWUR -V\Q\aJMIV:MUW\MPWUMQV\MZNIKM SDFNDJHKHDG¿UVW LPSRUWMDYD[HME LPSRUWMDYDUPL5HPRWH([FHSWLRQ LPSRUWMDYDXWLO&ROOHFWLRQ SXEOLFLQWHUIDFH&XVWRPHU+RPHH[WHQGV(-%+RPH^ SXEOLF&XVWRPHUFUHDWH 6WULQJODVW6WULQJ¿UVW WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ SXEOLF&XVWRPHU¿QG%\3ULPDU\.H\ 6WULQJNH\ WKURZV)LQGHU([FHSWLRQ5HPRWH([FHSWLRQ SXEOLF&ROOHFWLRQ¿QG%\&LW\ 6WULQJFLW\ WKURZV)LQGHU([FHSWLRQ5HPRWH([FHSWLRQ ` \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 18 HQWLW\¿QGHUV ?PMVNQVLMZ[PI^MILIZS[QLM ,MXVWWKRXJKWRIDSUHWW\VFDU\ VFHQDULRLI,ZDQWP\FOLHQWWRGLVSOD\D OLVWRIDOOP\FXVWRPHUVLW·VMXVWRQHFDOO WRÀQG$OO EXW7+(1ZKDW"$UH\RXWHOOLQJPH ,ZRXOGJHWEDFND]LOOLRQUHPRWHVWXEV"$QG WKHQ,·GKDYHWRPDNHUHPRWHPHWKRGFDOOVRQ HDFKRQH"7KDWZRXOGWDNH)25(9(5QRWWR PHQWLRQDOOWKHEDQGZLGWK7KDWGRHVQ·W VRXQGJRRGDWDOO :LWKD5HPRWHLQWHUIDFH ILQGHUDQGFUHDWHPHWKRGV JLYHEDFN5HPRWHVWXEV 7KDWPHDQV\RXKDYHWR PDNHUHPRWHPHWKRGFDOOV RQHDFKRQHWRJHWWKHGDWD \RXZDQW &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 19 HQWLW\EHDQLQWUR :RXOGQ·WLWEHGUHDP\LIWKHUH ZHUHDZD\WRKDYHPHWKRGVLQWKH KRPHWKDWFRXOGJLYHEDFNVRPHWKLQJRWKHU WKDQ(-%REMHFWUHIHUHQFHV",IDOO,ZDQWLV WKHGDWDDERXWWKHFXVWRPHUVOLNHMXVWDEXQFK RI6WULQJVWRGLVSOD\ZRXOGQ·WLWEHJUHDWLI ,FRXOGKDYHDPHWKRGLQWKHKRPHWKDW FRXOGJLYHEDFNMXVWWKHGDWD"%XWLW·V SUREDEO\DIDQWDV\ WKHUH DUH QR 'XPE 4XHVWLRQV Q: 7AIT ) THOUGHT IT WAS BAD DESIGN TO MAKE ALL THOSE METHOD CALLS FROM THE CLIENT !.97!9 7OULDNT THE CLIENT USUALLY GO THROUGH A SESSION BEAN !ND THE SESSION BEAN WOULD THEN TALK TO THE ENTITY BEAN A: +IND OF 9OURE THINKING OF THE 3ES SION &ACADE *%% DESIGN PATTERN "UT EVEN IF YOU DO PUT A SESSION BEAN IN FRONT OF AN ENTITY BEAN THE SESSION BEAN IS STILL A CLIENT )T MIGHT BE A LOT MORE EFFICIENT BECAUSE THE SESSION BEAN MIGHT NOT HAVE AS FAR TO GO ON THE NETWORK AND MIGHT EVEN BE ON THE SAME SERVER AS THE ENTITY "UT IF YOURE KEEPING LOCATION INDEPENDENCE THEN YOUR SESSION BEAN IS STILL USING THE ENTITY BEANS 2EMOTE INTERFACES SO THERES STILL A LOT OF OVERHEAD Q: #OULDNT YOU JUST HAVE A BUSINESS METHOD IN THE COMPONENT INTERFACE THAT RETURNS DATA ,IKE RETURN A COLLECTION OF 3TRINGS A: 9OURE GETTING WARMER 9ES THATS THE WAY YOU MIGHT HAVE DONE IT IN %*" "UT THATS KLUDGEY BECAUSE YOU HAVE TO FIRST GET A REFERENCE TO SOME CUSTOMER JUST SO YOU CAN ASK THAT CUSTOMER TO GIVE YOU BACK THE DATA FOR ALL CUSTOMERS \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Page 20 Return to Table of Contents HQWLW\KRPHEXVLQHVVPHWKRGV 0WUMJ][QVM[[UM\PWL[\W\PMZM[K]M 4HATS RIGHT BUSINESS METHODS ARENT JUST FOR THE COMPONENT INTERFACE WHEN YOURE TALKING ABOUT ENTITY BEANS !S OF %*" AN ENTITY BEAN HOME CAN HAVE METHODS THATDRUM ROLLDONT HAVE TO RETURN COMPONENT INTERFACES (OME BUSINESS METHODS CAN RETURN ANYTHING WITH THE ONE RESTRICTION OF COURSE THAT 2EMOTE HOME METHODS RETURN VALUES THAT ARE 2-) ))/0 COMPLIANT (OME BUSINESS METHODS ARE GREAT FOR BATCH OPERATIONS OR FOR QUERY METHODS WHERE THE CLIENT DOESNT NEEDOR WANT%*" OBJECT REFERENCES BUT SIMPLY WANTS THE ENTITYS DATA IN OTHER WORDS THE DATA FOR ONE OR MORE OF THE ENTITYS PERSISTENT lELDS &OR EXAMPLE WE MIGHT PUT A HOME BUSINESS METHOD IN THE #USTOMER BEAN LIKE GET!LL#USTOMER)NFO THAT RETURNS A COLLECTION OF 3TRINGS WITH WHATEVER PIECES OF DATA YOUVE DECIDED MAKE UP THE CUSTOMERS INFO "ETTER YET YOU CAN SEND BACK A COLLECTION OF #USTOMER)NFO OBJECTS WHERE #USTOMER)NFO IS A CLASS THAT SIMPLY HOLDS THE DATA AND GETTERS FOR THE #USTOMERS PERSISTENT STATE 4HAT WAY THE CLIENT CAN MAKE LOCAL CALLS TO GET THE DATA IT NEEDS OUT OF THE #USTOMER)NFO OBJECTS WITHOUT HAVING THOSE CALLS BE REMOTE CALLS ON THE COMPONENT INTERFACE ! #USTOMER)NFO CLASS IS AN EXAMPLE OF A 6ALUE /BJECT CLASS WHICH IS IN A NUTSHELL JUST A CLASS WITH GETTERS AND POSSIBLY SETTERS DEPENDING ON THE DESIGN REPRESENTING THE ENTITYS PERSISTENT lELDS !ND IT TOO HAS A DARK SIDETHE DATA STARTS TO BECOME STALE THE MOMENT AFTER THE 6ALUE /BJECT IS CREATED 7E COULD TELL YOU NOW BUT THEN WED BE ROBBING YOU OF SUCH A VALUABLE OPPORTUNITY TO APPLY A LITTLE NEURAL EFFORT 3O FOR NOW WHY DONT YOU THINK OF WHY SENDING BACK #USTOMER)NFO OBJECTS THAT THE CLIENT COULD THEN INTERROGATE IE CALL METHODS ON AT ITS LEISURE COULD HAVE A DOWNSIDE 7ELL USE 6ALUE /BJECTS A LOT BUT YOU HAVE TO BE AWARE OF THE TRADEOFFS WHEN CHOOSING BETWEEN USING A HOME lNDER METHOD THAT RETURNS %*" OBJECT REFERENCES ESPECIALLY WHEN THE REFERENCES ARE 2EMOTE VS A HOME BUSINESS METHOD THAT RETURNS 6ALUE /BJECTS +RPHEXVLQHVVPHWKRGV FDQUHWXUQVRPHWKLQJ RWKHUWKDQ(-%REMHFW UHIHUHQFHV7KH\·UH SHUIHFWIRUTXHULHVZKHUH WKHFOLHQWMXVWZDQWVWKH HQWLW\GDWDQRWUHIHUHQFHV WRWKHHQWLWLHVWKHPVHOYHV 7KH\·UHDOVRJUHDWIRU EDWFKRSHUDWLRQVRU DQ\WKLQJHOVH\RXPLJKW ZDQWWRGRZLWKPRUH WKDQRQHVSHFLILFHQWLW\ ZKHQ\RXGRQ·WZDQWWR UHWXUQUHIHUHQFHVWRWKH FRPSRQHQWLQWHUIDFH &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 21 HQWLW\EHDQLQWUR 5XOHVIRUWKH5HPRWHKRPHLQWHUIDFH ,PSRUWMDYD[HME DQGMDYDUPL5HPRWH([FHSWLRQ ([WHQGMDYD[HME(-%+RPH 'HFODUH RSWLRQDOO\ RQHRUPRUHFUHDWH PHWKRGVZKLFK0867 UHWXUQWKH5HPRWHFRPSRQHQWLQWHUIDFHDQGGHFODUHERWKD 5HPRWH([FHSWLRQDQGD&UHDWH([FHSWLRQ(DFKFUHDWH PHWKRGPXVW EHJLQZLWKWKHSUH¿[³FUHDWH´ 'HFODUHWKH¿QG%\3ULPDU\.H\ PHWKRGZKLFK0867 UHWXUQWKH5HPRWHFRPSRQHQWLQWHUIDFHDQGGHFODUHERWKD 5HPRWH([FHSWLRQDQGD)LQGHU([FHSWLRQ 'HFODUH RSWLRQDOO\ RQHRUPRUHRWKHU¿QGHUPHWKRGVZKLFK 0867UHWXUQHLWKHUWKH5HPRWHFRPSRQHQWLQWHUIDFH IRU VLQJOHHQWLW\¿QGHUV RUMDYDXWLO&ROOHFWLRQ IRUPXOWLSOHHQWLW\ ¿QGHUV $OO¿QGHUVPXVWGHFODUHERWKD5HPRWH([FHSWLRQDQGD )LQGHU([FHSWLRQ 'HFODUHRQHRUPRUHKRPHEXVLQHVVPHWKRGV 4 $UJXPHQWVDQGUHWXUQW\SHVPXVWEH50,,,23FRPSDWLEOH 6HULDOL]DEOH SULPLWLYH5HPRWHRUDUUD\VRUFROOHFWLRQVRIDQ\RIWKRVH 4 <RXFDQKDYHRYHUORDGHGPHWKRGV 4 (DFKPHWKRGPXVWGHFODUHD5HPRWH([FHSWLRQ 4 <RXFDQGHFODUH\RXURZQDSSOLFDWLRQH[FHSWLRQVEXWWKH\PXVW 127EHUXQWLPHH[FHSWLRQV LQRWKHUZRUGVWKH\PXVWEHFRPSLOHU FKHFNHGH[FHSWLRQV²VXEFODVVHVRI([FHSWLRQEXWQRWVXEFODVVHVRI 5XQWLPH([FHSWLRQ 4 0HWKRGVFDQKDYHDUELWUDU\QDPHVDVORQJDVWKH\GRQ¶WEHJLQZLWK ³FUHDWH´³¿QG´RU³UHPRYH´ \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 22 HQWLW\EHDQV 6KDUSHQ \RXU SHQFLO )RUWKHIRXUGDWDEDVHRSHUDWLRQV 64/FRPPDQGV DFOLHQWPLJKW ZDQWWRGRZLWKDQHQWLW\EHDQOLVWWKHPHWKRGVLQWKHEHDQ¶V LQWHUIDFH V WKDWDUHUHODWHGWRWKRVHGDWDEDVHRSHUDWLRQV1R\RX GRQ¶WKDYHWRNQRZ64/EXW\RXGH¿QLWHO\KDYHWRXQGHUVWDQGWKH LPSOLFDWLRQVRIWKHIRXUGDWDEDVHRSHUDWLRQVDQG\RXPXVWNQRZ KRZWKH\FRUUHVSRQGWRPHWKRGVLQWKHEHDQFODVV )URPWKHOLVWRIWKHPHWKRGVLQWKHLQWHUIDFHV¿OOLQWKHPHWKRGRU PHWKRGVWKDWFRUUHVSRQGZLWKWKHGDWDEDVHRSHUDWLRQ ,16(57 '(/(7( 83'$7( 6(/(&7 LQWHUIDFH!! &XVWRPHU+RPH FUHDWH 6WULQJODVW6WULQJILUVW ILQG%\3ULPDU\.H\ 6WULQJNH\ ILQG%\&LW\ 6WULQJFLW\ JHW(-%0HWD'DWD JHW+RPH+DQGOH UHPRYH +DQGOHK UHPRYH 2EMHFWNH\ LQWHUIDFH!! &XVWRPHU JHW/DVW1DPH VHW/DVW1DPH 6WULQJV JHW)LUVW1DPH VHW)LUVW1DPH 6WULQJV JHW3ULPDU\.H\ JHW(-%+RPH JHW+DQGOH UHPRYH LV,GHQWLFDO &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 23 HQWLW\EHDQLQWUR ;M[[QWVJMIVKZMI\M^[MV\Q\aJMIVKZMI\M 6WDWHIXOVHVVLRQEHDQFUHDWH &OLHQWFDOOVLWWRJHWDQ(-%REMHFWUHIHUHQFHWRDQHZMXVWIRUPH VWDWHIXOVHVVLRQEHDQ ,WFDQ DQGIUHTXHQWO\GRHV KDYHDUJXPHQWVWKDWWKHEHDQXVHVWRGR FOLHQWVSHFLILFLQLWLDOL]DWLRQ EHIRUHUXQQLQJDQ\EXVLQHVVPHWKRGV 7KH&RQWDLQHUPDNHVDQHZVHVVLRQEHDQZKHQWKHFOLHQWFDOOVFUHDWH 6WDWHOHVVVHVVLRQEHDQFUHDWH &OLHQWFDOOVLWWRJHWDQ(-%REMHFWUHIHUHQFHWRDEHDQ ,WKDVQRDUJXPHQWVDQGWKHEHDQGRHVQRWGRDQ\FOLHQWVSHFLILF LQLWLDOL]DWLRQ VLQFHDWWKHWLPHWKHEHDQ·VHME&UHDWH LV FDOOHGWKHEHDQKDVQRDVVRFLDWLRQZLWKDFOLHQW 7KH&RQWDLQHUGRHVQRWPDNHDQHZVHVVLRQEHDQZKHQ WKHFOLHQWFDOOVFUHDWH DQGGRHVQRWSXOORQHRXWRIWKH SRROXQWLOWKHFOLHQWLQYRNHVDEXVLQHVVPHWKRG (QWLW\EHDQFUHDWH &OLHQWFDOOVLWWRLQVHUWDQHZURZLQWKHGDWDEDVH $OWKRXJKWKHHQGUHVXOWIRUWKHFOLHQWLVVWLOODQ(-%REMHFW UHIHUHQFH LQWKLVFDVHWRWKHQHZO\FUHDWHGHQWLW\ ,WZLOOYLUWXDOO\DOZD\VKDYHDUJXPHQWV DOWKRXJKWKH\ DUHQ·WPDQGDWRU\EXWLW·VNLQGDKDUGWRLPDJLQHDFUH DWH ZLWKRXWWKHPOLNH´+H\GDWDEDVHFUHDWHDQHZ FXVWRPHUQR,GRQ·WKDYHDQ\QDPHRU,'RUDQ\WKLQJ MXVWPDNHVRPHVWXIIXSµ RED 2OSES ARE FOR A DATE D O O AND G E W IS MAD A NEW RO CREATE W E N CH WITH EA CT %*" OBJE ER ROW NEW JECT 2EMEMB NEW W %*" OB TE E A N E N CR A S E B AN NEW ENTITY BE TE CT A JE E B CR O S BEAN NEW %*" STATEFUL EATE BEANS CR STATELESS 7KH&RQWDLQHUGRHVQRWPDNHDQHZHQWLW\EHDQEXWLWGRHVSXOORQH RXWRIWKHSRROWRUXQWKHHME&UHDWH PHWKRG5HPHPEHUWKHHME&UH DWH PHWKRGKDVWRWDNHWKHFUHDWH DUJXPHQWVDQGVRPHKRZFUHDWH DQHZHQWLW\LQWKHXQGHUO\LQJSHUVLVWHQWVWRUH RUDWOHDVWVXSSRUWWKH &RQWDLQHULQFUHDWLQJDQHZHQWLW\ \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Page 24 Return to Table of Contents VHVVLRQYVHQWLW\ ;M[[QWVJMIVZMUW^M^[MV\Q\aJMIVZMUW^M 6WDWHIXOVHVVLRQEHDQUHPRYH &OLHQWFDOOVLWWRWHOOWKH&RQWDLQHUWKDWKH·VGRQHZLWKWKHEHDQ &RQWDLQHUFDOOVWKHEHDQ·VHME5HPRYH XQOHVVWKHEHDQLVDOUHDG\SDVVLYDWHG DQG NLOOVWKHEHDQ WKLQNIRRGIRUWKHJDUEDJHFROOHFWRU &OLHQWZLOOJHWDQH[FHSWLRQLIKHWULHVWRXVHWKH(-%REMHFWUHIHUHQFHDIWHUUHPRYLQJ WKHEHDQ 6WDWHOHVVVHVVLRQEHDQUHPRYH &OLHQWFDOOVLWWRWHOOWKH&RQWDLQHUWKDWKH·VGRQHZLWKWKHEHDQ &RQWDLQHUJHWVWKHFDOODQGVD\V´/LNH,FDUH"'R\RXKRQHVWO\WKLQN\RX·UHWKDW LPSRUWDQW"7KLVEHDQLVDOUHDG\EDFNLQWKHSRROEDE\µ7KH&RQWDLQHUGRHVQRWFDOO DEHDQ·VHME5HPRYH 7KLQNDERXWLW³ZKLFKEHDQ·VHME5HPRYH ZRXOGLWFDOO" &OLHQWZLOOJHWDQH[FHSWLRQLIKHWULHVWRXVHWKH(-%REMHFWUHIHUHQFHDIWHUUHPRYLQJ WKHEHDQ (QWLW\EHDQUHPRYH &OLHQWFDOOVLWWRWHOOWKH&RQWDLQHUWRGHOHWHWKHHQWLW\ZLWKWKLVSULPDU\NH\ &RQWDLQHUFDOOVWKHEHDQ·VHME5HPRYH PHWKRGDQG³LIWKHEHDQVXSSRUWVFOLHQW WULJJHUHGUHPRYDO³WKHHQWLW\LVGHOHWHGIURPWKHXQGHUO\LQJSHUVLVWHQWVWRUH,QRWKHU ZRUGVWKHURZLQWKHGDWDEDVHLVKLVWRU\*RQH3RRI &OLHQWZLOOJHWDQH[FHSWLRQLIKHWULHVWRXVHWKH(-%REMHFWUHIHUHQFHDIWHUUHPRYLQJ WKHEHDQ ,QIDFW12FOLHQWZLOOEHDEOHWRXVHDQ(-%REMHFWUHIHUHQFHWRWKDWHQWLW\ &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 25 HQWLW\EHDQLQWUR ,KDWHWRGRWKLVUHDOO\EXW, KDYHQRFKRLFH0867EH UHPRYHGIURPWKHGDWDEDVH 6RLI\RX·YHJRWDQ\ODVWZRUGV\RX EHWWHUGRLWLQ\RXUHME5HPRYH UHPRYH RQDVHVVLRQEHDQ PHDQVWKHFOLHQWLVGRQHZLWK WKHEHDQ UHPRYH RQDQHQWLW\EHDQ PHDQV(9(5<21(LVGRQHZLWK WKHEHDQ 1R3OHDVH1R ,·OOJLYH\RXZKDWHYHU \RXZDQWMXVWGRQ·W FDOOUHPRYH \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Page 26 Return to Table of Contents HQWLW\UHPRYDO &XW /RRNVOLNHVRPHERG\ GLGQ·WUHDGWKHVFULSW<RX NQRZWKDW·VQRWKRZLWZRUNVLW·V QRWWKHEHDQLQVWDQFHWKDW·VNLOOHG EXWWKHHQWLW\LQGDWDEDVH<RX MXVWJREDFNWRWKHEHDQSRRO ZKHQWKHFOLHQWFDOOV UHPRYH %XW,QHYHUJHWWRGRDGHDWK VFHQH,NQRZLW·VRQO\WKHGDWDEDVH HQWLW\WKDWJHWVGHOHWHGEXW,MXVW ZDQWHGVRPHWKLQJDELWPRUHGUDPDWLF IRUDFKDQJH,NQRZ,·OOGLHLI,WKURZDQ XQFKHFNHGH[FHSWLRQEXWWKDWFRXOGWDNH IRUHYHUDQG,ZDVKRSLQJWRJHWD SDUWLQ´/RUGRIWKH%HDQVµDQG &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 27 HQWLW\EHDQLQWUR -V\Q\aJMIVQV[\IVKMLMI\P 3O WE ALL KNOW THAT AN ENTITY BEAN IS A REPRESENTATION OF SOME REAL ENTITY IN AN UNDERLYING PERSISTENT STORE USUALLY AS A ROW IN A DATABASE BLAH BLAH BLAH "UT THERES STILL SOME CONFUSION ABOUT WHAT DISTINGUISHES AN ENTITY FROM AN ENTITY BEAN FROM AN ENTITY BEAN INSTANCE %NTITY 4HE REAL THING IN THE UNDERLYING PERSISTENT STORE 4HE ROW IN THE DATABASE ALTHOUGH IT CAN BE MORE COMPLEX !N ENTITY DIES WHEN ITS ROW IS DELETED FROM THE UNDERLYING STORE EITHER THROUGH A DIRECT DATABASE DELETE LIKE SOMEONE USING A DATABASE ADMIN TOOL OR BECAUSE SOMEONE CALLS REMOVE ON THE BEANS HOME OR COMPONENT INTERFACE %NTITY BEAN 4HE COMPONENT THAT REPRESENTS THE UNDERLYING REAL ENTITY "UT THIS ONES TRICKY IS IT THE CLASS )S IT THE INTERFACE )S IT THE INSTANCE OF THE BEAN CLASS $URING DEVELOPMENT AND DEPLOYMENT THE ENTITY BEAN IS THE WHOLE COMPONENT THE TWO INTERFACES $$ AND BEAN CLASS "UT AT RUNTIME IT CAN GET A LITTLE FUZZY 3 OMETIMES WE USE hENTITY BEANv TO DESCRIBE THE POSSIBILITY OF REPRESENTING A PARTICULAR ENTITY AS A BEAN )N OTHER WORDS IF THERES AN ENTITY FOR "O 2ODGERS IN THE DATABASE THEN WE CAN SAY THAT THERE IS A "O 2ODGERS ENTITY BEAN EVEN IF THERES NO BEAN INSTANCE CURRENTLY REPRESENTING THAT ENTITY )F AN ENTITY EXISTS FOR A PARTICULAR BEAN TYPE LIKE #USTOMER &RED &OO THE ENTITY BEAN FOR THAT ENTITY IS SAID TO EXIST !N ENTITY BEAN IS SAID TO DIE WHEN ITS UNDERLYING ENTITY IS DELETED AS IN h4HERES NO &RED &OO ENTITY BEANv "UT THAT DOESNT MEAN THE INSTANCE ON THE HEAP DIES %NTITY BEAN INSTANCE 4HE INSTANCE OF THE BEAN CLASS ON THE HEAP "EAN DEATH IS INTIMATELY TIED TO THE DATABASE BUT BEAN INSTANCE DEATH AS IN hYOURE HEADIN FOR GARBAGE COLLECTION PALv IS TIED TO THE WHIMS OF THE #ONTAINER OR A SERVER CRASH 9ES IT REALLY IS THAT CONFUSING 9OU HAVE TO KNOW THE CONTEXT TO KNOW HOW THE WORD @BEAN IS BEING USED )F IT MEANS THE BEAN REPRESENTING THE ENTITY THEN THAT BEAN WILL DIE WHEN THE ENTITY DIES AND THE %*" OBJECT FOR THAT ENTITY GOES AWAY "UTAND HERES WHERE IT GETS WEIRDTHE ENTITY BEAN INSTANCE DOESNT DIE IT JUST GOES BACK TO THE POOL 4HINK OF THE PHRASE hENTITY BEANv AS MORE CONCEPTUAL THAN PHYSICAL )N MOST CASES WE WONT HAVE TO DISTINGUISH BETWEEN THE BEAN AND ITS INSTANCE OR THE DISTINCTION WILL BE SO OBVIOUS THAT ITS NOT AN ISSUE $FOLHQWFDQNLOODQHQWLW\ E\FDOOLQJUHPRYH RQD EHDQRUGHOHWLQJWKHGDWD IURPWKHGDWDEDVHGLUHFWO\ %XWRQO\WKH&RQWDLQHURUD VHUYHUFUDVKFDQNLOODEHDQ LQVWDQFH ,IZHVD\´HQWLW\EHDQ ZDVNLOOHGµWKHXQGHUO\LQJ HQWLW\LVJRQHDQGWKH(-% REMHFWIRULVJRQHEXW WKHEHDQLQVWDQFHWKDWKDG EHHQSOD\LQJVXUYLYHV :HOOLW·VEHHQJUHDW SOD\LQJ\RXUHDOO\EXWQRZ LW·VWLPHIRU\RXWRJRWRDEHWWHU SODFH,·OOWKLQNDERXW\RXZKLOH,·P ÁRDWLQJRQRQHRIWKRVHOLWWOH LQÁDWDEOHPDWWUHVV WKLQJV 'DUNR bean in LIVES stance if the e even it play ntity removeds is . 'RQQ\ entity DIES when a client calls remove() \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 28 HQWLW\UHPRYDO &OLHQWFDOOVUHPRYH FRQWH[W UHPRYH RYH UHP VWXE (-% REMHFW EHDQ FOLHQW EHDQ H RY HP 5 HME FRQWH[W %HDQ3RRO '% &OLHQWFDOOVUHPRYH RQWKH(-%REMHFWVWXEIRUHQWLW\ 7KHUHPRYH PHWKRGLQYRFDWLRQLVSDVVHGWRWKH(-%REMHFW 7KH&RQWDLQHUFDOOVHME5HPRYH RQWKHEHDQ FRQWH[W (-% REMHFW EHDQ EHDQ FRQWH[W %HDQ3RRO GHO SULP HWHUR Z DU\ NH\ IRU '% 7KH&RQWDLQHURUEHDQGHOHWHVWKHHQWLW\LQWKHGDWDEDVH 7KHEHDQORVHVLWVLGHQWLW\ LQRWKHUZRUGVLWLVQRORQJHUUHSUHVHQWLQJHQWLW\ DQGPRYHV EDFNWRWKHSRRO0HDQZKLOHWKH(-%REMHFWIRULVGHOHWHGVRWKHFOLHQW·VVWXEZLOOWKURZDQ H[FHSWLRQLIWKHFOLHQWXVHVLWWRLQYRNHDPHWKRG &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 29 HQWLW\EHDQLQWUR -V\Q\aJMIVKTQMV\^QM_ :MDD=LHGAFLK $QHQWLW\LVDUHDOWKLQJWKDWH[LVWVRXWVLGHRI(-%LQD SHUVLVWHQWVWRUHDQGDQHQWLW\EHDQLVDQ22UHSUH VHQWDWLRQRUUHDOL]DWLRQRIDQHQWLW\ &OLHQWVXVHHQWLW\EHDQVWRGRGDWDEDVHRSHUDWLRQVLQ DQ22ZD\2SHUDWLRQVLQFOXGHFUHDWLQJQHZHQWL WLHV GDWDEDVHLQVHUWV GHOHWLQJHQWLWLHV GDWDEDVH GHOHWHV XSGDWLQJHQWLW\VWDWH GDWDEDVHXSGDWHV DQG VHDUFKLQJIRURQHQWLWLHV GDWDEDVHVHOHFWV $QHQWLW\EHDQ5HPRWHFRPSRQHQWLQWHUIDFHH[WHQGV (-%2EMHFW7KHUH·VQRWDVHSDUDWHLQWHUIDFHIRUVHV VLRQEHDQVDQGHQWLW\EHDQV7KDWPHDQVWKDWWKH FOLHQWZLOOVHHDOORIWKHPHWKRGVLQ\RXUFRPSRQHQW LQWHUIDFHSOXVWKHILYHDGGLWLRQDOPHWKRGVIURP(- %2EMHFW (QWLW\EHDQFRPSRQHQWLQWHUIDFHVXVXDOO\FRQWDLQ JHWWHUVDQGVHWWHUVIRUILHOGYDOXHVWKDWFRUUHVSRQGWR FROXPQVLQDGDWDEDVHWDEOHVXFKDVJHW/DVW1DPH JHW+RPH3KRQH VHW)LUVW1DPH HWF (QWLW\EHDQFRPSRQHQWLQWHUIDFHPHWKRGVDUHXVXDOO\ PHDQWWREHUXQE\DVSHFLILFXQLTXHO\LGHQWLILDEOH HQWLW\)RUH[DPSOHFDOOLQJJHW/DVW1DPH RQWKH HQWLW\ZLWKSULPDU\NH\UHWXUQVWKHODVWQDPHRI WKHHQWLW\LQWKHGDWDEDVHZLWKWKHSULPDU\NH\ 'DQ'RRI 7KHUXOHVIRUKRZ\RXZULWHDQHQWLW\EHDQ5HPRWH FRPSRQHQWLQWHUIDFHDUHWKHVDPHDVWKHUXOHVIRU VHVVLRQEHDQVLQFOXGLQJH[WHQG(-%2EMHFWGHFODUH 5HPRWH([FHSWLRQVRQDOOPHWKRGVXVHRQO\50,,,23 W\SHVIRUDUJXPHQWVDQGUHWXUQYDOXHVGRQ·WEHJLQ PHWKRGQDPHVZLWKWKHSUHIL[´HMEµHWF $QHQWLW\EHDQKRPHLQWHUIDFHLVVXEVWDQWLDOO\GLIIHUHQW IURPWKDWRIDVHVVLRQEHDQEHFDXVHHQWLW\EHDQVDUH W\SLFDOO\IRXQGUDWKHUWKDQFUHDWHG,QRWKHUZRUGVWKH FOLHQWLVPRUHOLNHO\WRWU\WRDFFHVVDQH[LVWLQJHQWLW\ DVRSSRVHGWRPDNLQJDQHZHQWLW\ ZKLFKPHDQVD QHZURZLQWKHGDWDEDVH ,QDQHQWLW\KRPHDFUHDWH PHWKRGLVQRWUHTXLUHG VLQFHFUHDWH PHWKRGLQHQWLW\EHDQVDUHIRULQVHUWLQJ QHZHQWLWLHVLQWRWKHGDWDEDVHDQG\RX·UHQRWUHTXLUHG WRDOORZ\RXUFOLHQWVWRGRWKDW (QWLW\EHDQKRPHLQWHUIDFHVFDQKDYHVLQJOHURZRU PXOWLURZILQGHUPHWKRGV%RWKFUHDWHDQGILQGHUPHWK RGVUHWXUQWKHFRPSRQHQWLQWHUIDFHRIDEHDQDOWKRXJK PXOWLHQWLW\ILQGHUVUHWXUQD&ROOHFWLRQRIFRPSRQHQW LQWHUIDFHUHIHUHQFHV (YHU\HQWLW\EHDQKRPHLVUHTXLUHGWRKDYHDWOHDVW RQHPHWKRG³WKHILQG%\3ULPDU\.H\ PHWKRGWKDW VHDUFKHVIRUDSDUWLFXODUHQWLW\DQGUHWXUQVLWVFRP SRQHQWLQWHUIDFH LHDUHIHUHQFHWRWKDWHQWLW\·V(-% REMHFW RUWKURZVDQH[FHSWLRQ 0XOWLSOHHQWLW\ILQGHUVGRQRWWKURZDQH[FHSWLRQLIQR PDWFKLQJHQWLWLHVDUHIRXQG7KH\VLPSO\UHWXUQDQ HPSW\FROOHFWLRQ (QWLW\KRPHLQWHUIDFHFDQDOVRKDYHKRPHEXVLQHVV PHWKRGVIRURSHUDWLRQVWKDWDSSO\WRPRUHWKDQRQH HQWLW\DVRSSRVHGWRRQHVSHFLILFHQWLW\%DWFKXSGDWHV ZRXOGEHDJRRGXVHIRUDKRPHEXVLQHVVPHWKRG 7KHUHDOEHQHILWRIKRPHEXVLQHVVPHWKRGVLV WKDW³XQOLNHFUHDWHDQGILQGHUPHWKRGV³KRPHEXVL QHVVPHWKRGVFDQUHWXUQVRPHWKLQJRWKHUWKDQDQ(-% REMHFWUHIHUHQFH,IWKHFOLHQWZDQWVRQO\GDWDVD\D &ROOHFWLRQRI6WULQJVUHSUHVHQWLQJWKHQDPHDQGSKRQH QXPEHURIHDFKFXVWRPHUDKRPHEXVLQHVVPHWKRG FDQGRWKDWZKLOHDILQGHUFDQQRW $QHQWLW\EHDQFUHDWH LVYHU\GLIIHUHQWIURPDVHVVLRQ EHDQFUHDWH EHFDXVHDQHQWLW\EHDQFUHDWH LQVHUWV DQHZHQWLW\LQWRWKHXQGHUO\LQJSHUVLVWHQWVWRUH LH QHZURZLQWKHGDWDEDVH $QHQWLW\EHDQUHPRYH LVGUDPDWLFDOO\GLIIHUHQWIURPD VHVVLRQEHDQUHPRYH :KHQDFOLHQWFDOOVUHPRYH RQDQHQWLW\EHDQLW·VWRGHOHWHWKHHQWLW\IURPWKHGDWD EDVH7KDWPHDQVHYHU\ERG\LVGRQHZLWKWKHEHDQ \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 30 FRIIHHFUDPPRFNH[DP 0RFN([DP 1 7HAT IS TRUE CONCERNING LOCATING AN ENTITY BEANS HOME INTERFACE R! 4HE QDUURZ METHOD SHOULD BE USED FOR A LOCAL HOME INTERFACE R" 4HE QDUURZ METHOD SHOULD BE USED FOR A REMOTE HOME INTERFACE R# 4HE QDUURZ METHOD SHOULD BE USED FOR BOTH LOCAL AND REMOTE HOME INTERFACES R$ 4HE QDUURZ METHOD SHOULD BE USED FOR NEITHER LOCAL NOR REMOTE HOME INTERFACES 2 7HICH CAPABILITIES ARE FOUND IN AN ENTITY BEANS REMOTE COMPONENT INTERFACE #HOOSE ALL THAT APPLY R! R" R# R$ R% 3 lNDING EXISTING ENTITY OBJECTS REMOVING EXISTING ENTITY OBJECTS EXECUTING A HOME BUSINESS METHOD RETRIEVING THE %*"-ETA$ATA INTERFACE 7HICH ARE WAYS IN WHICH A CLIENT CAN GET A REFERENCE TO AN EXISTING ENTITY OBJECTS LOCAL COMPONENT INTERFACE #HOOSE ALL THAT APPLY R! R" R# R$ R% CREATING NEW ENTITY OBJECTS #ALL HME&UHDWH #ALL JHW6HVVLRQ&RQWH[W /BTAIN THE REFERENCE FROM THE HANDLE 2ECEIVE THE REFERENCE AS A PARAMETER IN A METHOD CALL 5SE A lNDER METHOD DElNED IN THE LOCAL HOME INTERFACE &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 31 HQWLW\EHDQLQWUR 4 (OW MANY CREATE METHODS CAN BE DElNED IN AN ENTITY BEANS LOCAL HOME INTERFACE R! R" R# R$ R% 5 TO TO MANY TO MANY #ALL HME&UHDWH /BTAIN THE REFERENCE FROM THE HANDLE #ALL A METHOD ON THE ENTITY OBJECTS PRIMARY KEY 2ECEIVE THE REFERENCE AS A PARAMETER IN A METHOD CALL 5SE A lNDER METHOD DElNED IN THE REMOTE HOME INTERFACE 7HICH APPROACHES CAN BE USED ON A PRIMARY KEY CLASS TO DETERMINE IF TWO KEYS REFER TO THE SAME ENTITY #HOOSE ALL THAT APPLY R! R" R# R$ 7 /NLY 7HICH ARE WAYS IN WHICH A CLIENT CAN GET A REFERENCE TO AN EXISTING ENTITY OBJECTS REMOTE COMPONENT INTERFACE #HOOSE ALL THAT APPLY R! R" R# R$ R% 6 5SING THE OPERATOR 5SING THE HTXDOV METHOD 5SING THE LV,GHQWLFDO METHOD NONE OF THE ABOVE 7HICH APPROACHES CAN DETERMINE WHETHER TWO ENTITY %*" OBJECT REFERENCES REFER TO THE SAME ENTITY OBJECT #HOOSE ALL THAT APPLY R! R" R# R$ 5SING THE OPERATOR 5SING THE HTXDOV METHOD 5SING THE LV,GHQWLFDO METHOD NONE OF THE ABOVE \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Page 32 Return to Table of Contents FRIIHHFUDPPRFNH[DP 8 7HATS TRUE ABOUT THE CLIENTS VIEW OF AN ENTITY BEANS REMOTE COMPONENT INTERFACE #HOOSE ALL THAT APPLY R! R" R# R$ 9 "USINESS METHODS CANNOT RETURN A REFERENCE TO THE ENTITY OBJECT /NLY TO TO MANY TO MANY SXEOLF&XVWFUHDWH LQW[ SXEOLFYRLGFUHDWH LQW[ WKURZV&UHDWH([FHSWLRQ SXEOLF&XVWFUHDWH LQW[ WKURZV&UHDWH([FHSWLRQ SXEOLF&XVWFUHDWH LQW[ WKURZV&UHDWH([FHSWLRQ 5HPRWH([FHSWLRQ 7HICH IS A LEGAL NAME FOR AN ENTITY BEAN HOME BUSINESS METHOD #HOOSE ALL THAT APPLY R! R" R# R$ R% %NTITY BEANS MAY NOT SURVIVE A CRASH OF THE CONTAINER 7HICH IS A LEGAL DECLARATION FOR A LOCAL HOME INTERFACES CREATE METHOD #HOOSE ALL THAT APPLY R! R" R# R$ 11` .EW ENTITY BEANS CAN BE CREATED USING A METHOD IN THIS INTERFACE (OW MANY lNDER METHODS CAN BE DECLARED WITHIN AN ENTITY BEANS LOCAL HOME INTERFACE R! R" R# R$ R% 10 -ULTIPLE CLIENTS CAN ACCESS THE SAME ENTITY OBJECT CONCURRENTLY CREATE CREATE#UST REMOVE!LL lND#UST SELECT#UST &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 33 HQWLW\EHDQLQWUR 0RFN([DP$QVZHUV 1 7HAT IS TRUE CONCERNING LOCATING AN ENTITY BEANS HOME INTERFACE (spec: 110) R! 4HE QDUURZ METHOD SHOULD BE USED FOR A LOCAL HOME INTERFACE R" 4HE QDUURZ METHOD SHOULD BE USED FOR A REMOTE HOME INTERFACE R# 4HE QDUURZ METHOD SHOULD BE USED FOR BOTH LOCAL AND REMOTE HOME INTERFACES R$ 4HE QDUURZ METHOD SHOULD BE USED FOR NEITHER LOCAL NOR REMOTE HOME INTERFACES 2 7HICH CAPABILITIES ARE FOUND IN AN ENTITY BEANS REMOTE COMPONENT INTERFACE #HOOSE ALL THAT APPLY R! R" R# R$ R% 3 CREATING NEW ENTITY OBJECTS - home methods lNDING EXISTING ENTITY OBJECTS REMOVING EXISTING ENTITY OBJECTS EXECUTING A HOME BUSINESS METHOD - home methods RETRIEVING THE %*"-ETA$ATA INTERFACE 7HICH ARE WAYS IN WHICH A CLIENT CAN GET A REFERENCE TO AN EXISTING ENTITY OBJECTS LOCAL COMPONENT INTERFACE #HOOSE ALL THAT APPLY R! R" R# R$ R% #ALL HME&UHDWH (spec: 119) - we said existing : ) #ALL JHW6HVVLRQ&RQWH[W /BTAIN THE REFERENCE FROM THE HANDLE aces r Remote interf - handles are fo 2ECEIVE THE REFERENCE AS A PARAMETER IN A METHOD CALL 5SE A lNDER METHOD DElNED IN THE LOCAL HOME INTERFACE \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 34 PRFNH[DPDQVZHUV 4 (OW MANY CREATE METHODS CAN BE DElNED IN AN ENTITY BEANS LOCAL HOME INTERFACE R! R" R# R$ R% 5 TO MANY TO MANY #ALL HME&UHDWH (spec: 119) - we said EXISTING /BTAIN THE REFERENCE FROM THE HANDLE #ALL A METHOD ON THE ENTITY OBJECTS PRIMARY KEY 2ECEIVE THE REFERENCE AS A PARAMETER IN A METHOD CALL 5SE A lNDER METHOD DElNED IN THE REMOTE HOME INTERFACE (spec: 120-121) st, theyÕre ss the equals(th) etesame entity. pa ys ke o tw if means 5SING THE HTXDOV METHOD the same key! Which 5SING THE LV,GHQWLFDO METHOD - isIdentical () is for paring component interfacecom NONE OF THE ABOVE references 5SING THE OPERATOR 7HICH APPROACHES CAN DETERMINE WHETHER TWO ENTITY %*" OBJECT REFERENCES REFER TO THE SAME ENTITY OBJECT #HOOSE ALL THAT APPLY R! R" R# R$ TO to allow - You donÕtcrhaeavete new entities clients to 7HICH APPROACHES CAN BE USED ON A PRIMARY KEY CLASS TO DETERMINE IF TWO KEYS REFER TO THE SAME ENTITY #HOOSE ALL THAT APPLY R! R" R# R$ 7 /NLY 7HICH ARE WAYS IN WHICH A CLIENT CAN GET A REFERENCE TO AN EXISTING ENTITY OBJECTS REMOTE COMPONENT INTERFACE #HOOSE ALL THAT APPLY R! R" R# R$ R% 6 (spec: 115) (spec: 120-121) ring is for compa - equals() pr ys ke y ar im METHOD entityÕs 5SING THE OPERATOR 5SING THE HTXDOV 5SING THE LV,GHQWLFDO METHOD NONE OF THE ABOVE &KDSWHU Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 5. Entities Are Persistent Return to Table of Contents Page 35 HQWLW\EHDQLQWUR 8 R! R" R# R$ 9 -ULTIPLE CLIENTS CAN ACCESS THE SAME ENTITY OBJECT CONCURRENTLY .EW ENTITY BEANS CAN BE CREATED USING A METHOD IN THIS INTERFACE %NTITY BEANS MAY NOT SURVIVE A CRASH OF THE CONTAINER "USINESS METHODS CANNOT RETURN A REFERENCE TO THE ENTITY OBJECT (OW MANY lNDER METHODS CAN BE DECLARED WITHIN AN ENTITY BEANS LOCAL HOME INTERFACE R! R" R# R$ R% 10 (spec: 108) 7HATS TRUE ABOUT THE CLIENTS VIEW OF AN ENTITY BEANS REMOTE COMPONENT INTERFACE #HOOSE ALL THAT APPLY /NLY TO TO MANY TO MANY ey() is required - ÞndByPrimaryK 7HICH IS A LEGAL DECLARATION FOR A LOCAL HOME INTERFACES CREATE METHOD #HOOSE ALL THAT APPLY R! R" R# R$ (spec: 116) SXEOLF&XVWFUHDWH LQW[ - eption needs CreateExc SXEOLFYRLGFUHDWH LQW[ WKURZV&UHDWH([FHSWLRQ SXEOLF&XVWFUHDWH LQW[ WKURZV&UHDWH([FHSWLRQ (spec: 115) canÕt return void, - must return component interface SXEOLF&XVWFUHDWH LQW[ WKURZV&UHDWH([FHSWLRQ 5HPRWH([FHSWLRQ local interface canÕt throw RemoteException - 11 7HICH IS A LEGAL NAME FOR AN ENTITY BEAN HOME BUSINESS METHOD #HOOSE ALL THAT APPLY R! R" R# R$ R% CREATE CREATE#UST REMOVE!LL (spec: 114) ÒÞndÓ & ÒcreateÓ ÒremoveÓ ed preÞxes are reserv lND#UST SELECT#UST \RXDUHKHUH Chapter 5. Entities Are Persistent Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Table of Contents Chapter 6. Being an Entity Bean.................................................................. 1 Section 6.1. OBJECTIVES............................................................................................................................................................... 2 Section 6.2. The real power of entity beans is synchronization..................................................................................................... 4 Section 6.3. Oh no!: The entity bean and the entity it represents have different data!................................................................. 5 Section 6.4. How the entity bean and the underlying entity stay synchronized............................................................................ 7 Section 6.5. The only question is WHO does the work when it's time to synchronize.................................................................. 8 Section 6.6. Container-managed vs. bean-managed persistence.................................................................................................. 9 Section 6.7. there are no Dumb Questions................................................................................................................................... 10 Section 6.8. Off the path: A brief history on the evolution of CMP 2.0........................................................................................ 11 Section 6.9. The EntityBean interface adds three new container callbacks (including two just for synchronization)............... 16 Section 6.10. Even the methods that are the same, don't behave the same................................................................................. 17 Section 6.11. But wait... there's more! Entity beans have new home container callbacks , too................................................... 18 Section 6.12. Writing a CMP entity bean: make it abstract ......................................................................................................... 19 Section 6.13. You put three kinds of things in your bean class:................................................................................................... 20 Section 6.14. PLUS... (ok, that's four things...)............................................................................................................................. 21 Section 6.15. Virtual fields are NOT instance variables!.............................................................................................................. 22 Section 6.16. Sharpen your penil.................................................................................................................................................. 23 Section 6.17. Complete code for the CustomerBeanCMP class.................................................................................................... 24 Section 6.18. Sharpen your pencil................................................................................................................................................ 25 Section 6.19. Entity bean instance lifecycle.................................................................................................................................. 26 Section 6.20. Entity bean instance transitions............................................................................................................................. 27 Section 6.21. there are no Dumb Questions................................................................................................................................. 28 Section 6.22. Sharpen your pencil................................................................................................................................................ 29 Section 6.23. Sharpen your pencil................................................................................................................................................ 30 Section 6.24. So how DID the client get a reference to the EJB object for #28?......................................................................... 31 Section 6.25. Bean things you can do during entity construction:.............................................................................................. 33 Section 6.26. CMP Entity bean creation....................................................................................................................................... 34 Section 6.27. Creating a new entity bean: CMP entity bean........................................................................................................ 35 Section 6.28. CMP Entity bean creation....................................................................................................................................... 36 Section 6.29. Implement your ejbCreate() methods.................................................................................................................... 37 Section 6.30. Object identity: the primary key............................................................................................................................. 38 Section 6.31. Implement your ejbPostCreate() methods............................................................................................................. 40 Section 6.32. BRAIN POWER....................................................................................................................................................... 41 Section 6.33. Bean things you can do during entity creation:..................................................................................................... 44 Section 6.34. CMP Entity finder................................................................................................................................................... 45 Section 6.35. Finding an existing entity....................................................................................................................................... 46 Section 6.36. CMP Entity bean finders......................................................................................................................................... 47 Section 6.37. YOU don't implement the Finder methods!........................................................................................................... 48 Section 6.38. CMP Home Business Methods................................................................................................................................ 51 Section 6.39. Client invokes a Home Business Method............................................................................................................... 52 Section 6.40. Bean things you can do in home business methods............................................................................................... 53 Section 6.41. Starting a business method (in a transaction)........................................................................................................ 54 Section 6.42. Completing a business method (and a transaction)............................................................................................... 55 Section 6.43. Being John Entity Bean.......................................................................................................................................... 56 Section 6.44. Activation/passivation of a CMP entity bean......................................................................................................... 58 Section 6.45. Sharpen your pencil................................................................................................................................................ 59 Section 6.46. Bean things you can do during activation and loading.......................................................................................... 60 Section 6.47. Bean things you can do during passivation and storing......................................................................................... 61 Section 6.48. Commit options: what REALLY happens to a bean after a transaction commits?............................................... 62 Section 6.49. There are no Dumb Questions................................................................................................................................ 63 Section 6.50. Exercise: CMP entity bean Creation....................................................................................................................... 65 Section 6.51. Exercise: Entity Bean Business Method (with activation & passivation).............................................................. 66 Section 6.52. Exercise................................................................................................................................................................... 67 Section 6.53. COFFEE CRAM....................................................................................................................................................... 68 Section 6.54. COFFEE CRAM....................................................................................................................................................... 73 Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 1 bean/entity synchronization 5 4 4 %HLQJDQ(QWLW\%HDQ $V DQHQWLW\EHDQ,OHDGWKH JODPRURXVOLIHRIDQDFWUHVV,VSHQGPRVW RIP\WLPHLQWKHSRROEXWHYHQZKHQ,GR ZRUN,·PQHYHUERUHG,DOZD\VJHWWRSOD\ GLIIHUHQWUROHV\HVWHUGD\,ZDV-HDQQH 5RVHWRGD\,·P0DULO\Q0DORQHDQG WRQLJKW,·OOEHSOD\LQJWKHSDUWRI 'RXJ$GDPV (QWLW\EHDQVDUHDFWRUV$VORQJDVWKH\¶UHDOLYHWKH\¶UHHLWKHULQWKHSRRORU WKH\¶UHEHLQJVRPHERG\6RPHERG\IURPWKHXQGHUO\LQJSHUVLVWHQWVWRUH,QRWKHUZRUGV DQHQWLW\IURPWKHGDWDEDVH:KHQDEHDQLVSOD\LQJDSDUWWKHEHDQDQGWKHXQGHUO\LQJ HQWLW\KDYHWRVWD\LQV\QF,PDJLQHWKHKRUURULIWKHEHDQLVSUHWHQGLQJWREHVD\$XGUH\ /HRQHDQGVRPHRQHORZHUV$XGUH\¶VFUHGLWOLPLWLQWKHGDWDEDVHEXWIRUJHWVWRWHOO WKHEHDQ7KHEHDQDFWLQJDV$XGUH\LVKDSSLO\DXWKRUL]LQJSXUFKDVHVIRUPRUHWKDQ $XGUH\¶VFXUUHQWOLPLW2UZKDWLIDFOLHQWXVHVWKHEHDQWRPRGLI\$XGUH\¶VDGGUHVVEXW WKHEHDQKDQJVRQWRWKHQHZLQIRZLWKRXWWHOOLQJWKHGDWDEDVH\LNHV WKLVLVDQHZFKDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 2 H[DPREMHFWLYHV (QWLW\%HDQ/LIHF\FOH 2IILFLDO ,GHQWLI\FRUUHFWDQGLQFRUUHFWVWDWHPHQWV RUH[DPSOHVDERXWWKH%HDQ3URYLGHU¶V YLHZDQGSURJUDPPLQJFRQWUDFWIRU&03 LQFOXGLQJWKHUHTXLUHPHQWVIRUD&03 HQWLW\EHDQ :KDWLWUHDOO\PHDQV 7KLVREMHFWLYHFDQKLW\RXRQDOPRVWDQ\WKLQJUHODWHG WRHQWLW\EHDQVVR\RXSUHWW\PXFKKDYHWRNQRZLW DOOLQFOXGLQJWKHGHWDLOVRID&03HQWLW\EHDQOLIHF\FOH WKHFRQWDLQHUFDOOEDFNPHWKRGVRIMDYD[HME(QWLW\%HDQ ZKDW\RXPXVWZULWHLQDEHDQFODVVDQGZKDWDEHDQ FDQJHWIURPLWV(-%&RQWH[W Copyright Safari Books Online #896963 ,GHQWLI\WKHLQWHUIDFH V DQGPHWKRGV D&03HQWLW\EHDQPXVWDQGPXVWQRW LPSOHPHQW ,GHQWLI\FRUUHFWDQGLQFRUUHFWVWDWHPHQWV RUH[DPSOHVDERXWWKHOLIHF\FOHRID&03 HQWLW\EHDQ )URPDOLVWLGHQWLI\WKHSXUSRVHEHKDYLRU <RXKDYHWRNQRZWKDWSDVVLYDWLRQLQHQWLW\EHDQV LVGLIIHUHQWIURPVHVVLRQEHDQSDVVLYDWLRQEHFDXVH HQWLW\EHDQVJREDFNWRDSRRODIWHUHME3DVVLYDWH EXWVWD\DVKHDOOLYLQJREMHFWV<RXPXVWNQRZWKDW HQWLW\EHDQFUHDWLRQLVDOVRFRPSOHWHO\GLIIHUHQWIRU HQWLW\EHDQVDQGWKDWDFUHDWH FDOORQDQHQWLW\ EHDQPHDQV³LQVHUWDQHZHQWLW\LQWRWKHXQGHUO\LQJ SHUVLVWHQWVWRUH´<RXKDYHWRNQRZWKDWUHPRYH RQ DQHQWLW\EHDQLVPRUHGUDVWLFIRUHQWLW\EHDQVWKDQ VHVVLRQEHDQV²DQHQWLW\EHDQUHPRYHGHOHWHVWKH HQWLW\IURPWKHGDWDEDVH DQGUHVSRQVLELOLWLHVRIWKH%HDQ3URYLGHU IRUD&03HQWLW\EHDQLQFOXGLQJEXWQRW <RXPXVWEHDEOHWRORRNDWDQHQWLW\EHDQPHWKRGDQG OLPLWHGWRVHW(QWLW\&RQWH[W NQRZWKHFLUFXPVWDQFHVXQGHUZKLFKWKDWPHWKRGLV XQVHW(QWLW\&RQWH[W HME&UHDWH HME FDOOHGDQGZKDW\RXVKRXOGGRLQWKDWPHWKRG)RUH[ 3RVW&UHDWH HME$FWLYDWH HME3DVVLYDWH DPSOH\RXQHHGWRNQRZWKDWLQDEHDQ¶VHME&UHDWH HME5HPRYH HME/RDG HME6WRUH HME PHWKRGWKHEHDQFDQQRWJHWDUHIHUHQFHWRLWV(-% )LQG HME+RPH DQGHME6HOHFW REMHFWEXWWKDWLWFDQJHWWKDWUHIHUHQFHLQHME3RVW&UH DWH )URPDOLVWLGHQWLI\WKHSXUSRVHEHKDYLRU <RXQHHGWRNQRZZKLFKPHWKRGV\RX¶UHUHTXLUHGWR ZULWHLQWR\RXUEHDQDQGZKLFKDUHOHIWWRWKH&RQ DQGUHVSRQVLELOLWLHVRIWKH&RQWDLQHUIRUD WDLQHU)RUH[DPSOH\RXQHHGWRNQRZWKDWWKH%HDQ &03HQWLW\EHDQLQFOXGLQJEXWQRWOLPLWHG WRVHW(QWLW\&RQWH[W XQVHW(QWLW\&RQWH[W 3URYLGHUPXVWZULWHDEVWUDFWJHWWHUVDQGVHWWHUVIRU HME&UHDWH HME3RVW&UHDWH HME$FWLYDWH SHUVLVWHQW¿HOGVEXWPXVWGH¿QH¿QGHUPHWKRGVRQO\ LQWKHKRPHLQWHUIDFHQRWWKHEHDQFODVV HME3DVVLYDWH HME5HPRYH HME/RDG HME6WRUH HME)LQG HME+RPH DQGHME 6HOHFW &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 3 HQWLW\EHDQV\QFKURQL]DWLRQ (QWLW\%HDQ/LIHF\FOH 2IILFLDO :KDWLWUHDOO\PHDQV )URPDOLVWRIEHKDYLRUVPDWFKWKHPZLWK <RXKDYHWRNQRZWKHPHWKRGVRI(QWLW\&RQWH[WDVZHOO WKHDSSURSULDWH(QWLW\&RQWH[WPHWKRG UHVSRQVLEOHIRUWKDWEHKDYLRU ,GHQWLI\FRUUHFWDQGLQFRUUHFWVWDWHPHQWV DERXWDQHQWLW\EHDQ¶VSULPDU\NH\DQG REMHFWLGHQWLW\ DVWKHFLUFXPVWDQFHVXQGHUZKLFK\RXFDQFDOOWKRVH PHWKRGV)RUH[DPSOH\RXVKRXOGNQRZWKDWLI\RX¶UH UXQQLQJLQWKHVHW(QWLW\&RQWH[W PHWKRG\RXFDQXVH \RXUVSHFLDO-1',QDPHVSDFHWRORRNXSDUHIHUHQFHWR D'DWD6RXUFHEXWWKDW\RX¶UHQRWDOORZHGWRDFFHVVWKH XQGHUO\LQJGDWDEDVHIURPWKDWPHWKRG WKHUH¶VQRPHDQ LQJIXOWUDQVDFWLRQVRWKH&RQWDLQHUZRQ¶WOHW\RXGRLW <RXVKRXOGNQRZWKDW\RXFDQ¶WFDOOJHW8VHU7UDQVDFWLRQ RQ\RXUFRQWH[WEHFDXVHHQWLW\EHDQVPXVWXVHFRQ WDLQHUPDQDJHGWUDQVDFWLRQVDQGJHW8VHU7UDQVDFWLRQLV IRUEHDQPDQDJHGWUDQVDFWLRQVRQO\ <RXKDYHWRNQRZWKDWDQHQWLW\EHDQPXVWKDYHD XQLTXHLGHQWLW\ QRWZREHDQVFDQKDYHWKHVDPH SULPDU\NH\ DQGWKDWD&03EHDQ¶VSULPDU\NH\PXVW EHHLWKHURQHRIWKHEHDQ¶VSHUVLVWHQW¿HOGVRUDFXVWRP SULPDU\NH\FODVVZKRVH¿HOGVDUHDOOIURPFRQWDLQHU PDQDJHG¿HOGVGH¿QHGLQWKHEHDQ<RXPXVWNQRZWKDW WKHSULPDU\NH\FODVVW\SHPXVWEH6HULDOL]DEOHDQGWKDW LWPXVWKDYHDYDOLGRYHUULGHRIHTXDOV DQGKDVKFRGH \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 4 HQWLW\EHDQV\QFKURQL]DWLRQ <PMZMITXW_MZWNMV\Q\aJMIV[Q[ [aVKPZWVQbI\QWV 4HE BEAN AND THE UNDERLYING ROW IN THE DATABASE MUST STAY IN SYNC 2EMEMBER THE BEAN IS NOT THE ENTITYTHE BEAN IS A REPRESENTATION OF THE REAL ENTITY 4HE BEAN IS AN // WAY OF WORKING WITH THE DATA BUT THE ENTITY IN THE DATABASE IS THE ONLY TRUE ENTITY )F THE ENTITY IN THE DATABASE DIES IE IS DELETED THE BEAN FOR THAT SPECIlC ENTITY DIES TOO ALTHOUGH THE BEAN INSTANCE STILL LIVES AS YOU SAW IN THE LAST CHAPTER 4HE #ONTAINERS JOB IS MAKE SURE THAT THE ENTITY AND THE BEAN STAY IN SYNC SO THAT NOBODYS LOOKING AT A STALE BEAN OR A STALE ROW IN THE DATABASE +EEP IN MIND THAT THE ENTITY BEAN MAY NOT BE THE ONLY WAY TO GET INTO THE DATABASE )N FACT IN MOST CASES THE "EAN 0ROVIDER YOU WILL HAVE NO WAY TO KNOW FOR CERTAIN DURING DEVELOPMENT IF YOUR BEAN IS THE ONLY WAY ANYONE WILL EVER BE ABLE TO GET TO THE ENTITY DATA 7KH&RQWDLQHUDOZD\VNQRZVZKHQWKH EHDQDQGWKHGDWDEDVH WKHXQGHUO\LQJ HQWLW\ PXVWEHV\QFKURQL]HGVRWKDW QHLWKHURQHKDVµVWDOH¶GDWD 2. ,KDYH WKHEHDQIRU&KULV 0DUWLQDQGQRZ,ZDQWWR FKDQJHKLVDGGUHVVWR &ORFN6WUHHW6R,·OO FDOOVHW$GGUHVV RQ WKHEHDQ &RRO,·YH XSGDWHGP\EHDQ VWDWHZLWKWKHQHZ DGGUHVV &OR EXAMPLE YOUR #USTOMER DATABASE MIGHT BE USED BY A BUNCH OF APPS IN YOUR COMPANY INCLUDING SOME THAT WORK DIRECTLY WITH THE DATABASE AND SOME THAT GO THROUGH THE #USTOMER BEAN IN AN %*" APPLICATION )F A CLIENT HAS A REFERENCE TO A BEAN THE CLIENT MIGHT CHANGE THE BEANS STATE BY SAY CALLING A SETTER METHOD )F THAT SETTER METHOD CORRESPONDS TO ONE OF THE BEANS PERSISTENT lELDS IN OTHER WORDS A COLUMN IN THE TABLE LIKE ADDRESS THE BEAN AND THE DATABASE WILL BE TEMPORARILY OUT OF SYNC 4HE DATABASE WONT HAVE THE CURRENT ADDRESS UNTIL THE BEAN UPDATES THE DATABASE .OW THINK WHAT A DISASTER IT WOULD BE IF THE BEAN CACHES THE NEW DATA FOR THE ENTITY WITHOUT TELLING THE DATABASE )F SOMEONE COMES ALONG WITH ANOTHER APPLICATION AND ASKS FOR THAT #USTOMERS ADDRESS FROM THE DATABASE 4HAT 7OULD "E "AD !ND THE OPPOSITE SCENARIO IS BAD AS WELL IF SOMEONE UPDATES THE DATABASE THE BEAN NEEDS TO KNOW /THERWISE THE BEAN IS OUT THERE IN THE %*" APP REPRESENTING THE ENTITY IN THE DATABASE BUT THE BEAN ISNT A TRUE REmECTION OF THE ENTITYS STATE )N OTHER WORDS THE BEAN IS STALE 7HICH MEANS PERHAPS USELESS EHDQFXUUHQWO\ SOD\LQJ +(//2([FXVHPH ,·PJODG\RX·UHDOOKDYLQJD QLFHFKDWDERXWLWEXWGRQ·W\RX WKLQNWKHGDWDEDVHPLJKWOLNH WRNQRZDERXWWKDWDGGUHVV FKDQJH"" "" '% &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 5 HQWLW\EHDQV\QFKURQL]DWLRQ 2KQR 7KHHQWLW\EHDQDQGWKHHQWLW\LW UHSUHVHQWVKDYHGLIIHUHQWGDWD )LUVW&KULV /DVW0DUWLQ .H\ $GGUHVV&ORFNVW 3KRQH Someone changed the bean state, but not the databa se ! )LUVW /DVW .H\ $GGUHVV 3KRQH &KULV )UDQ %HOD 0DUWLQ +HDO\ )OHFN )RRVW %DUUG 3LFNODQH 7KH&RQWDLQHU·VPRVWLPSRUWDQWHQWLW\EHDQMRELVWRPDNH VXUHWKDWWKLVVFHQDULR³ZKHUHWKHEHDQDQGWKHGDWDEDVH DUHRXWRIV\QF³GRHVQ·WFDXVHDQ\GDPDJH 7KH&RQWDLQHUKDVWRPDNHVXUHWKDW :KLOHVRPHERG\LVZRUNLQJZLWKWKHEHDQ DQGSRWHQWLDOO\FKDQJLQJ LWVVWDWH QRERG\FDQZRUNZLWKWKHUHDOHQWLW\LQWKHGDWDEDVH 2QFHDQHQWLW\EHDQ·VVWDWHKDVEHHQXSGDWHGWKHGDWDEDVHKDVWR EHXSGDWHGEHIRUHDQ\RQHHOVHFDQDFFHVVWKDWUHFRUGLQWKHGDWD EDVH %HIRUHWKHEHDQFDQUXQDQ\EXVLQHVVPHWKRGVRQDSDUWLFXODUHQWLW\·V EHKDOIWKHEHDQKDVWREHUHIUHVKHGZLWKWKHHQWLW\·VVWDWH,QRWKHU ZRUGVEHIRUHWKHHQWLW\EHDQIRU-RH%ORJJVFDQUXQDJHW&UHG LW/LPLW PHWKRGWKH&RQWDLQHUKDVWRORDGWKHHQWLW\EHDQXSZLWKWKH PRVWFXUUHQWGDWDIRU-RH%ORJJV2WKHUZLVHWKHEHDQPLJKWUHWXUQ WKHZURQJFUHGLWOLPLW³WKHOLPLWWKDWZDVLQSODFHWKHODVWWLPHWKH EHDQZDVORDGHGXSZLWK-RH·VGDWDIURPWKHGDWDEDVH \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 6 HQWLW\EHDQV\QFKURQL]DWLRQ 8KRK1RZ\RX·YHJRWPHHYHQ025( ZRUULHG'RHVQ·WWKLVPHDQWKHQWKDWWKH EHDQFDQJHWVWDOHLQEHWZHHQ(9(5<EXVLQHVV PHWKRGFDOO"$UH\RXWHOOLQJPH\RXKDYHWRPDNHD WULSWRWKHGDWDEDVHWRUHIUHVKWKHEHDQ·VGDWDZLWK WKHDFWXDOGDWDEDVHGDWDMXVWLQFDVHLWFKDQJHG HYHU\WLPHWKHFOLHQWFDOOVDPHWKRGRQWKHEHDQ" ,I7+$7·VWUXH\RXPLJKWDVZHOOMXVWJR VWUDLJKWWRWKHGDWDEDVH 1R7KDW·VQRWZKDW,·P VD\LQJEXW,FDQVHHKRZLWPLJKW ORRNOLNHWKDW7KHPLVVLQJSLHFHKHUH LVWKHWUDQVDFWLRQ$VORQJDVWKHEHDQ·V PHWKRGVDUHEHLQJFDOOHGDVSDUWRID VLQJOHWUDQVDFWLRQWKHEHDQGRHVQ·W KDYHWRV\QFKURQL]HLWVVWDWHZLWK GDWDLQWKHGDWDEDVH 6HHZKHQWKHFOLHQWFDOOVDEXVLQHVV PHWKRGDQGWKDWPHWKRGVWDUWVD WUDQVDFWLRQ,WHOOWKHGDWDEDVHWRORFNWKH HQWLW\ )RUQRZ\RXFDQWKLQNRILWDVORFNLQJ WKHURZDOWKRXJKLWPLJKWDFWXDOO\EHVRPHWKLQJD OLWWOHGLIIHUHQW :LWKWKHUHDOHQWLW\ORFNHGWKH EHDQFDQ·WEHFRPHVWDOHEHFDXVHQRERG\FDQJHW LQWRWKHHQWLW\WKURXJKWKHGDWDEDVH,ZRQ·WWHOO WKHGDWDEDVHWRUHOHDVHWKHORFNXQWLOWKH WUDQVDFWLRQLVRYHUVRWKHEHDQLVVDIH &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 7 HQWLW\EHDQV\QFKURQL]DWLRQ +RZWKHHQWLW\EHDQDQGWKHXQGHUO\LQJ HQWLW\VWD\V\QFKURQL]HG &OLHQWFDOOVD EXVLQHVVPHWKRG &RQWDLQHULQWHUFHSWVWKH FDOODQGVWDUWVDWUDQVDFWLRQ %()25(JHWWLQJWKHEHDQ &RQWDLQHUWHOOVWKHGDWDEDVHWRORFNWKH URZ WRDQ\RQHHOVHEXWWKH&RQWDLQHU '% /DVW )LUVW 3.H\ 3RO\ 0RUSKLVP 'HZH\ &KHDWHP &RQWDLQHUORDGVWKHEHDQ ZLWKWKHHQWLW\VWDWHIURPWKH GDWDEDVH1RZQRERG\FDQ H[FHSWWKHEHDQFDQFKDQJH WKHHQWLW\GDWD '% , %HDQUXQVPXOWLSOHEXVLQHVV PHWKRGVLQWKHVDPHWUDQVDFWLRQ NQRZLQJWKDWWKHXQGHUO\LQJ HQWLW\GDWDLQWKHGDWDEDVHFDQ¶W EHFKDQJHG EHFDXVHWKHURZIRU WKLVHQWLW\LVORFNHG &RQWDLQHUHQGVWKHWUDQVDFWLRQ EXW¿UVWLWXSGDWHVWKH GDWDEDVHZLWKZKDWHYHUQHZ VWDWHWKHEHDQPLJKWKDYH EHHQFDFKLQJRQEHKDOIRIWKDW HQWLW\ OLNHLIVRPHRQHFDOOHG VHW$GGUHVV RQWKHEHDQ &RQWDLQHUWHOOVWKHGDWDEDVHWRUHOHDVH WKHORFNRQWKHHQWLW\URZ '% /DVW '% )LUVW 3.H\ 3RO\ 0RUSKLVP 'HZH\ &KHDWHP \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Page 8 Return to Table of Contents HQWLW\EHDQV\QFKURQL]DWLRQ <PMWVTaY]M[\QWVQ[?07LWM[\PM_WZS _PMVQ\¼[\QUM\W[aVKPZWVQbM 4HE #ONTAINER ALWAYS KNOWS WHEN THE BEAN AND THE DATABASE ENTITY MUST BE SYNCHRONIZED )T KNOWS BASED ON TRANSACTIONS )F A CLIENT CALLS A METHOD ON A BEAN AND THAT METHOD STARTS A NEW TRANSACTION FOR THE BEAN THE #ONTAINER KNOWS THAT THE UNDERLYING ENTITY IN THE DATABASE MAY HAVE CHANGED SINCE THE LAST TIME THIS BEAN WAS LOADED UP WITH THIS ENTITYS DATA 3O THE #ONTAINER FORCES THE BEAN TO REFRESH ITS STATE BY LOADING IN THE ENTITYS DATA !ND OF COURSE THE REVERSE IS TRUE )F THE BEAN COMPLETES A METHOD AND THIS METHOD WAS THE END OF THE TRANSACTION THE #ONTAINER WILL TELL THE DATABASE TO RELEASE THE LOCK ON THIS ENTITY IN THE DATABASE "UT DURING THE TIME WHEN THE ROW WAS LOCKED THE BEAN MIGHT HAVE BEEN HAPPILY CACHING DATA THAT REPRESENTS THE NEWLY CHANGED STATE OF THE ENTITY LIKE A NEW ADDRESS OR PHONE NUMBER )N OTHER WORDS THE CLIENT MIGHT HAVE BEEN CALLING SETTER METHODS ON THE BEAN WITH THE INTENTION OF CAUSING AN UPDATE IN THE REAL DATABASE 4HE CLIENT WANTS TO UPDATE A RECORD 3O THE #ONTAINER KNOWS IT MUST FORCE THE BEAN TO UPDATE THE DATA IN THE DATABASE WITH THE BEANS STATE BEFORE THE #ONTAINER TELLS THE DATABASE TO RELEASE THE LOCK ON THAT ENTITY /+ SO THE WHEN IS NOT THE ISSUE !ND THATS IMPORTANT BECAUSE OTHERWISE YOU THE "EAN 0ROVIDER WOULD HAVE TO WORK OUT THE BUSINESS LOGIC TO KNOW EXACTLY WHEN THERE WAS A DANGER THAT THE BEAN AND ENTITY ARE OUT OF SYNC !ND YOU CAN JUST IMAGINE HOW QUICKLY YOU COULD CORRUPT THE STATE OF YOUR DATABASE 4HE REAL ISSUE IS WHO DOES THE DATABASE ACCESS !ND THERE ARE ONLY TWO CHOICES ITS EITHER YOU OR THE #ONTAINER )F YOU WRITE THE DATABASE ACCESS CODE YOU WRITE *$"# STATEMENTS IN THE CALLBACK METHODS THE #ONTAINER CALLS WHEN ITS TIME TO KICK YOU AND SAY h(EY TIME TO GO TO THE DATABASEv /N THE OTHER HAND IF THE #ONTAINER WRITES THE DATABASE CODE YOU SAVE YOURSELF A LOT OF CODING TIME AND EFFORT AND YOULL ALMOST ALWAYS GET BETTER PERFORMANCE &RQWDLQHU0DQDJHG 3HUVLVWHQFH &03 PHDQVWKH &RQWDLQHUWDNHVFDUHRIDOO WKHGDWDEDVHDFFHVVFRGHIRU V\QFKURQL]DWLRQLQFOXGLQJ DGGLQJDQGGHOHWLQJHQWLWLHV UHFRUGVURZVLQWKH GDWDEDVH %HDQ0DQDJHG3HUVLVWHQFH %03 PHDQV<28ZULWHWKH GDWDEDVHDFFHVVFRGH WKH -'%&VWDWHPHQWV IRUZKHQ WKH&RQWDLQHUWHOOV\RXLWV WLPHWRJRWRWKHGDWDEDVH ,Q(-%\RXVKRXOG XVH&03,WVDYHV\RXD ORWRIZRUNDQGYLUWXDOO\ DOZD\VJLYHV\RXEHWWHU SHUIRUPDQFH &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 9 HQWLW\EHDQV\QFKURQL]DWLRQ +WV\IQVMZUIVIOML^[JMIVUIVIOMLXMZ[Q[\MVKM &RQWDLQHUPDQDJHGSHUVLVWHQFH &03 :LPS\LQ(-%JUHDWO\HQKDQFHGLQ(-% &03 7KH%HDQ3URYLGHUGHVLJQVWKHHQWLW\EHDQFODVVFKRRVLQJZKLFKRI WKHEHDQ·VILHOGVDUHSDUWRIWKHEHDQ·VSHUVLVWHQWVWDWH3HUVLVWHQW ILHOGVPDSWRFROXPQVLQRQHRUPRUHGDWDEDVHWDEOHV 7KH&RQWDLQHUNHHSVWUDFNRIFKDQJHVWRWKHEHDQ·VVWDWHDQG XSGDWHVWKHUHDOHQWLW\DVQHHGHG)RUH[DPSOHLIDFOLHQWFDOOV VHW/DVW1DPH RQWKHEHDQWKH&RQWDLQHUNQRZVWKDWWKHUHDOHQWLW\ LQWKHGDWDEDVHKDVWREHXSGDWHGVRWKDWLIDQ\RQHHOVHDFFHVVHV WKHGDWDEDVHIRUWKDWHQWLW\ LQFOXGLQJQRQ(-%FOLHQWVXVLQJVRPH RWKHUPHDQVWRJHWWRWKHGDWDEDVH WKH\·OOVHHWKHFXUUHQWVWDWH RIWKHHQWLW\7KH&RQWDLQHUPDNHVWKHGHFLVLRQVRQKRZWRNHHS WKHEHDQDQGWKHUHDOHQWLW\V\QFKURQL]HGEDVHGRQWKHVWDWHRI WUDQVDFWLRQV 0RUHRQWKDWODWHU 7KH%HDQ3URYLGHUZULWHV(-%4/WRWHOOWKH&RQWDLQHUKRZWRGR '% 7KH&RQWDLQHUJRHVWRWKHGDWDEDVH ZKHQLWQHHGVWRLQVHUWDQHZHQWLW\ GHOHWHDQGHQWLW\XSGDWHWKHGDWDEDVH ZLWKQHZHQWLW\VWDWH LHRQHRUPRUH FROXPQVLQWKHHQWLW\URZKDYHFKDQJHG WKURXJKWKHEHDQ VHOHFWV(-%4/LVOLNHDVXEVHWRI64/EXWZLWKVRPH22IHDWXUHV (-%4/KHOSVEULGJHWKH22ZRUOGRI\RXUEHDQWRWKHUHODWLRQDOZRUOG RI\RXUUHDOHQWLW\ 8VLQJLQIRUPDWLRQLQWKHGHSOR\PHQWGHVFULSWRUWKH&RQWDLQHUZULWHV WKHDFWXDOLPSOHPHQWDWLRQRIWKH&03EHDQLQFOXGLQJLPSOHPHQWLQJ WKHILQGHUDQGVHOHFWPHWKRGVDQGDOORIWKHGDWDEDVHDFFHVVFRGH ,QRWKHUZRUGVLI\RX·UHD%HDQ3URYLGHUZULWLQJD&03EHDQ\RXGR QRWORRNXSDUHVRXUFHFRQQHFWLRQIDFWRU\IRUWKH'DWD6RXUFHJHW DQ64/&RQQHFWLRQRUZULWHDQ\-'%&FRGH1RWRQO\LVWKHGDWD DFFHVVFRGHWDNHQFDUHRIE\WKH&RQWDLQHUEXW\RXFDQWUXVWWKDWWKH &RQWDLQHUNQRZVH[DFWO\ZKHQWRJRWRWKHGDWDEDVH %HDQPDQDJHGSHUVLVWHQFH %03 %03 '% %HDQ3URYLGHUZULWHVWKHGDWDEDVHDFFHVVFRGHLQFOXGLQJORRNLQJXS D'DWD6RXUFHJHWWLQJD&RQQHFWLRQDQGVHQGLQJ-'%&VWDWHPHQWV WRWKHGDWDEDVH,W·VVWLOOEHWWHUWKDQLI\RXGLGQ·WXVHHQWLW\EHDQVDW DOOEHFDXVHWKH&RQWDLQHUZLOODWOHDVWWHOOD%03EHDQZKHQWRJR WRWKHGDWDEDVHVRZLWK%03\RXGRQ·WKDYHWRSXWLQORJLFWRNHHS WKHEHDQDQGWKHGDWDEDVHLQV\QF:KHQWKH&RQWDLQHUWHOOV\RXWR XSGDWHWKHGDWDEDVH\RXMXVWGRLW 7KH&RQWDLQHULQYRNHVDFRQWDLQHUFDOO EDFNRQWKHEHDQZKHQWKHEHDQQHHGV WRGRVRPHWKLQJZLWKWKHGDWDEDVHDQG WKHEHDQFRGHGRHVWKHDFWXDO-'%& ZRUN \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 10 HQWLW\EHDQV\QFKURQL]DWLRQ WKHUH DUH QR 'XPE 4XHVWLRQV Q: $OESNT THE #ONTAINER STILL HAVE TO GIVE YOU THE DATABASE #ONNECTION )SNT THAT THE WHOLE POINT OF LOOKING UP A $ATA3OURCE USING RESOURCE FACTORY REFERENCES )F THATS TRUE THEN HOW CAN YOU DO BEAN MANAGED PERSISTENCE /R IS "-0 A WAY TO BYPASS ALL THAT IN WHICH CASE YOUD BE BYPASSING THE #ONTAINER SERVICES FOR CONNECTION POOL ING AND A: 2ELAX 2EALLY 7ITH "-0 YOU STILL GET YOUR DATABASE CONNECTION FROM THE #ONTAINER BY LOOKING UP AS YOU SAID A RESOURCE FACTORY REFERENCE JAVAXSQL$ATA3OURCE 'EE YOU MUST HAVE BEEN READING AHEAD TO THE CHAPTER ON THE %*" ENVIRONMENT "ECAUSE YOURE RIGHT YOU NEED TO GET A CONNECTION FROM A CONNECTION POOL MANAGED BY THE #ONTAINER "UT ONCE YOU GET THE CONNEC TION FOR A "-0 BEAN YOURE ON YOUR OWN FOR SENDING IT STATEMENTS TO DO ).3%24 $%,%4% 50$!4% 3%,%#4 OPERATIONS !ND WHILE WERE HERE REMEMBER THAT DA TABASE ACCESS IS NOT JUST FOR ENTITY BEANS !.9 BEAN CAN GO TO THE DATABASE AS PART OF ITS BUSINESS LOGIC 3ESSION BEANS AND MESSAGE DRIVEN BEANS MIGHT FIND PLENTY OF REASONS TO LOOK SOMETHING UP OR STORE SOMETHING IN A DATABASE 4HE DIFFERENCE BETWEEN ENTITY BEANS AND THE OTHER TWO BEAN TYPES IS THAT ENTITY BEANS EXIST ONLY BECAUSE THERES SOMETHING IN A DATA BASE 3OMETHING THE BEAN REPRESENTS 3O ENTITY BEANS ARE BY DEFINITION TIED TO SOMETHING IN A PERSISTENT STORE Q: )T SEEMS LIKE PERFORMANCE WOULD BE BETTERNOT WORSEWITH "-0 $OESNT "-0 GIVE YOU MORE CONTROL A: 9OU HAVE MORE FLEXIBILITY WITH "-0 BUT NOT NECESSARILY MORE CONTROL !ND ACCORDING TO NEARLY ALL BENCHMARKS AGAINST THE MAJOR *%% SERVERS NOT BETTER PERFORMANCE 4HAT MIGHT SOUND COUNTERINTUITIVE BUT REMEMBER THAT THE #ONTAINER CAN DO THINGS THAT YOU CANT DO FROM WITHIN BEAN CODE 4HINGS LIKE GANGING MULTIPLE CALLS TO THE DATABASE USING NATIVE CODE TO GET IN AN OUT OF THE DATABASE FASTER THAN YOU COULD LAZY LOADING DIRTY DETECTION NEITHER OF WHICH ARE GUARANTEED BY THE SPEC OF COURSE AND DOZENS OF OTHER TRICKS 7E KNOW IT GOES AGAINST THE CONVENTIONAL WISDOM THAT SAYS IF YOU WANT IT DONE RIGHT DO IT YOURSELF BUT KEEP IN MIND THAT THE SERVER VENDORS ARE COMPETING FOR YOUR BUSINESS 4HEY KNOW PERFORMANCE MATTERS TO YOU 4HE SPEC ADDEDAND CHANGEDA LOT OF THE #-0 SPECIFICATION FOR THE SOLE PURPOSE OF GIVING THE VENDORS MORE ROOM FOR OPTIMIZING #-0 -ESSAGE DRIVEN AND SESSION BEANS DO NOT REPRESENT SOMETHING IN A DATABASE ALTHOUGH THEY MAY NEED TO USE SOME THING IN A DATABASE AS PART OF THEIR BUSI NESS LOGIC &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Page 11 Return to Table of Contents HQWLW\EHDQV\QFKURQL]DWLRQ )JZQMNPQ[\WZaWV\PM M^WT]\QWVWN+58 "EGINNING WITH VERSION OF THE %*" SPEC YOU COULD USE BOTH CONTAINER MANAGED PERSISTENCE #-0 AND BEAN MANAGED PERSISTENCE"-0 !T THE HIGHEST LEVEL THE DIFFERENCE BETWEEN #-0 AND "-0 IS ABOUT WHO WRITES THE DATABASE ACCESS CODE 7ITH %*" YOU CAN STILL USE BOTH BUT THERES NOW VERY LITTLE REASON TO EVER USE "-0 4HE ORIGINAL %*" SPECIlCATION FOR #-0 ENTITY BEANS WAS UM WEAK #LUNKY )NEFlCIENT ,IMITED .OT 4HAT 'OOD !ND ALTHOUGH MANY VENDORS WERE ABLE TO OVERCOME MANY OF THE PROBLEMS WITH #-0 THE SOLUTIONS WERE OUTSIDE THE SPECIlCATION SO YOUR CHOICE AS BEAN DEVELOPER WAS TO USE STANDARD #-0 BUT KEEP YOUR PORTABILITY OR STEP OUTSIDE THE SPEC REDUCING YOUR PORTABILITY BUT GET BETTER PERFORMANCE AND FEATURES 7HEN THE %*" SPEC WAS CREATED THE TEAM SPENT A GREAT DEAL OF TIME AND EFFORT TALKING TO BOTH END USER CUSTOMERS BEAN DEVELOPERS AND CONTAINER VENDORS ALL OF WHOM WERE VERY HAPPY TO DESCRIBE THE PROBLEMS WITH #-0 )N GRAPHIC DETAIL COMPLETE WITH SUGGESTIONS FOR 3UN ON h)LL TELL YOU WHAT YOU CAN $/ WITH YOUR #-0 BEAN SPECv 4HE %*" SPEC TEAM LISTENED !ND DESIGNED !ND LISTENED !ND DESIGNED !ND IN THE END THEY CAME UP WITH SOMETHING AWFUL ! SOLUTION THAT WAS AN EQUAL OPPORTUNITY PISSER OFFER 6ENDORS HATED IT $EVELOPERS HATED IT 0EOPLE LIKE US WHO HAD TO EXPLAIN THE NEW TECHNOLOGY TO CUSTOMERS REALLY HATED IT 3O WITH THE DEADLINE UPON THEM THE TEAM SCRAPPED MUCH OF WHAT THEYD DONE WITH #-0 AND CAME UP WITH A MUCH CLEANER SOLUTION 7ITH THE NEW %*" SPEC #-0 IS LIGHTYEARS AHEAD OF WHERE IT WAS IN %*" OR IN THE lRST TERRIBLE VERSION OF THE PRE RELEASE %*" SPEC !ND NOW MOST DEVELOPERS USING ENTITY BEANS WILL USE #-0 )N FACT THE EXAM DOESNT COVER "-0 AT ALL SINCE ITS THERE MORE FOR LEGACY SUPPORT OR VERY SPECIAL CASES MORE THAN ANYTHING ELSE "ECAUSE OF THE HEAVY SHIFT TO #-0 FOR REASONS WELL EXPLORE WE WONT COVER "-0 IN THIS BOOK 3O FROM THIS POINT FORWARD WERE GOING TO ASSUME THAT WERE TALKING ABOUT #-0 4HE DIFFERENCES BETWEEN THE LIFECYCLE AND THE DEVELOPERS RESPONSIBILITY FOR #-0 VS "-0 ARE DRAMATIC SO DONT FORGET THAT EVERYTHING WE TALK ABOUT NOW WILL BE FROM THE PERSPECTIVE OF A #-0 BEAN EVEN IF WE DONT EXPLICITLY SAY THAT WERE REFERRING TO #-0 7KHH[DP GRHVQ·WFRYHU WKHKLVWRU\JRVVLSRUVFDQGDOV RIWKH(-%VSHFLILFDWLRQ $OWKRXJKLWZRXOGKDYHPDGH WKHERRNPXFKVSLFLHULILWGLG $KZHOOZH¶OOMXVWKDYHWRPDNH GRZLWKWKHWHFKQLFDOFRQWHQW 6WLOOZHWKLQNWKHZD\D VSHF DQ\VSHF HYROYHVLV LQWHUHVWLQJ6XQZRUNVKDUG WRZDONWKH¿QHOLQHEHWZHHQ KDYLQJDPHDQLQJIXOVSHF ZLWKVWURQJJXDUDQWHHVIRU WKHSURJUDPPHUDQGRQH WKDW¶VYDJXHHQRXJKRQ LPSOHPHQWDWLRQGHWDLOVWR OHWWKHYHQGRUVFRPSHWH RQWKHSHUIRUPDQFHRIWKHLU LPSOHPHQWDWLRQV 5HJDUGOHVVWKHUH¶VQRWKLQJRQ WKHH[DPDERXWWKHGLIIHUHQW YHUVLRQVRIWKHVSHF$VORQJDV \RXNQRZZKDW¶VLQWKHVSHF \RX¶UHVDIH+RZHYHULQWKH UHDOZRUOG\RX¶UHOLNHO\WRFRPH DFURVV(-%DSSOLFDWLRQV VREHSUHSDUHGWROHDUQWKH GLIIHUHQFHV(VSHFLDOO\LI\RX¶UH WKHOXFN\RQHLQFKDUJHRI PLJUDWLQJWKHDSSWRD FRPSOLDQWVHUYHU \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 12 HQWLW\EHDQV +RZDEHDQDFWRUEHFRPHVDEHDQHQWLW\ 6FHQDULRFOLHQWZDQWVWRJHWWKHDGGUHVVRID VSHFL¿F&03&XVWRPHUHQWLW\SULPDU\NH\ :KHUHWKH #LV QXPEHU"7KHFOLHQWLVFDOOLQJ DQG,GRQ·WVHHDQ\ERG\ SOD\LQJQXPEHU JHW$GGUHVV VV GUH $G JHW VWXE FRQWH[W (-% REMHFW EHDQ FOLHQW EHDQ FRQWH[W %HDQ3RRO '% &OLHQWFDOOVJHW$GGUHVV RQWKHVWXEIRUHQWLW\EHDQ7KHFOLHQWJRWWKHVWXEIURPD SUHYLRXVFDOORQWKHKRPHVWXE \RX·OOVHHH[DFWO\KRZWKHFOLHQWJRWWKHVWXELQWKHILUVW SODFHLQDPLQXWH 7KHFDOOLVSDVVHGWRWKH(-%REMHFW 7KH(-%REMHFWJHWVWKHFDOODQGSDQLFVEHFDXVHWKHUH,6QRHQWLW\EHDQIRU 7KH(-%REMHFWLVWKHDJHQWERG\JXDUGIRUEXWKHLVQ·WDWWDFKHGWRDVSHFLILF EHDQWKDW·VSOD\LQJ &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 13 HQWLW\EHDQV\QFKURQL]DWLRQ +RZDEHDQDFWRUEHFRPHVDEHDQHQWLW\ 7KH&03EHDQFRPHVRXWRIWKHSRRODQGSUHSDUHVWRSOD\ ,·OOMXVWZDLW ZKLOHKHOHDUQVKRZ WREH ,NQRZ,·PJRLQJ WREHSOD\LQJ EXWXPZKDW·VP\ PRWLYDWLRQ" FRQWH[W (-% REMHFW EHDQ HME$FWLYDWH %HDQ3RRO G /RD HME '% FW VHOH 7KH&RQWDLQHU¶DFWLYDWHV·DEHDQE\SXOOLQJLWRXWRIWKHSRRODQGFDOOLQJHME$FWLYDWH 7KH&RQWDLQHUGRHVDVHOHFWRQSULPDU\NH\LQWKHGDWDEDVHWRJHWWKHUHDO HQWLW\GDWDWRSXWLQWRWKHEHDQ WKHGDWDWKDWZLOOEHFRPHWKHEHDQ·VVWDWH 7KH&RQWDLQHUORDGVWKHEHDQZLWKWKHHQWLW\GDWDIURPWKHGDWDEDVHDQGWKHQFDOOV HME/RDG WRWHOOWKHEHDQ´+H\EHDQ\RX·YHMXVWEHHQORDGHGµ \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 14 HQWLW\SDVVLYDWLRQDFWLYDWLRQ 2.WLPHRXW 'RHVWKDWSLFWXUHVD\WKDWHME3DVVLYDWH LVWKHEHDQ·VFRQWDLQHUFDOOEDFNIRUJRLQJEDFNWR WKHSRRO",VQ·WWKDWWKHRSSRVLWHRIKRZVHVVLRQ EHDQVZRUN":LWKVHVVLRQEHDQVHME$FWLYDWH KDVQRWKLQJWRGRZLWKWKHSRROVRZKDW·VWKH GHDO"$UHWKH\MXVWWU\LQJWRPDNHLWDV FRQIXVLQJDVSRVVLEOH""" 3DVVLYDWLRQDQG$FWLYDWLRQKDYHD 727$//<GLIIHUHQWPHDQLQJIRUHQWLW\ EHDQV 6WDWHOHVVVHVVLRQEHDQVJREDFNWRWKHSRRO ZLWKRXWSDVVLYDWLRQ LQRWKHUZRUGVZLWKRXWJHWWLQJ DQHME3DVVLYDWH FDOO 6WDWHIXOVHVVLRQEHDQVDUHSDVVLYDWHGZKHQ WKH&RQWDLQHUSXWVWKHPWRVOHHS SRVVLEO\ VHULDOL]DWLRQ WRFRQVHUYHUHVRXUFHVEHWZHHQFOLHQW PHWKRGLQYRFDWLRQV (QWLW\EHDQVDUHQ¶WSDVVLYDWHGLQWKHZD\WKDW VWDWHIXOVHVVLRQEHDQVDUHEXWHQWLW\EHDQV'2 JHWDQHME3DVVLYDWH FDOOZKHQWKH\¶UHDERXWWRJR EDFNWRWKHSRRO DQGDQHME$FWLYDWH ZKHQWKH\ FRPHRXWRIWKHSRRO 7KHPRVWLPSRUWDQWSRLQWLVWKDWXQOLNHVHVVLRQ EHDQVSDVVLYDWHGHQWLW\EHDQVDUHVWLOOOLYH REMHFWVRQWKHKHDS 7KHUH¶VQRVXFKWKLQJDVDEHDQVOHHSLQJLQD SRRO6WDWHOHVVEHDQVHQWLW\EHDQVDQGPHVVDJH GULYHQEHDQVDOOXVHSRROV$QGWKRVHSRROVDUHIRU OLYLQJ5$0XVLQJRQWKHKHDSREMHFWV 2QO\VWDWHIXOEHDQVDUHSXWWRVOHHS FDOOHG FRQIXVLQJO\SDVVLYDWLRQ EXWWKLVKDVQRWKLQJWR GRZLWKDSRRO 6R\HVWKH\¶YHRYHUORDGHGWKHZRUG³SDVVLYDWLRQ´ MXVWWRPDNHWKLQJVUHDOO\FRQIXVLQJIRU\RXDQGWR KHOSGULYHWKHQHHGIRUPRUH(-%ERRNV )RUZKLFK ZHWKDQNWKHPHYHU\VLQJOHGD\ &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 15 HQWLW\EHDQV\QFKURQL]DWLRQ HME3DVVLYDWH WKHGLIIHUHQFH EHWZHHQVHVVLRQDQGHQWLW\EHDQV 6WDWHIXOVHVVLRQEHDQ (QWLW\EHDQ 6WDWHOHVVVHVVLRQEHDQ alive on th e heap === (QWLW\%HDQ 3RRO 4&DOOHGZKHQWKHEHDQKDV ILQLVKHGDEXVLQHVVPHWKRG RWKHU WKDQUHPRYH DQGLVDERXW WRJREDFNWRWKHSRRO,QWKH SDVVLYDWHGVWDWHWKHEHDQKDVQR LGHQWLW\³LW·VQRWUHSUHVHQWLQJDQ\ HQWLW\IURPWKHGDWDEDVH 48VHLWWRUHOHDVHUHVRXUFHVWKDW \RXGRQ·WZDQWWRZDVWHZKLOH WKHEHDQLVVLWWLQJLQWKHSRRO QRWUXQQLQJDEXVLQHVVPHWKRG 7\SLFDOO\WKHPHWKRGLVHPSW\ 47KHEHDQLV127SDVVLYDWHG LQWKHVHVVLRQEHDQVHQVH,Q RWKHUZRUGVWKHEHDQLVQRW VHULDOL]HGRUVDYHGVRWKHUHLV 12UHTXLUHPHQWWKDW\RXQXOORXW UHIHUHQFHVWRQRQ6HULDOL]DEOH REMHFWV 47KH&RQWDLQHUFDOOVWKHEHDQ·V === p... he hea t n o d alive NOT bly serialize i s s po 4&DOOHGZKHQWKH&RQWDLQHU GHFLGHVWRFRQVHUYHUHVRXUFHV EHWZHHQEXVLQHVVPHWKRG LQYRFDWLRQVIURPWKHFOLHQW 48VHLWWRUHOHDVHUHVRXUFHVDQG doesnÕt apply... st beans are never paatssieless vated 4'RHVQ·WDSSO\6WDWHOHVVEHDQV KDYHDSRROEXWDFWLYDWLRQDQG SDVVLYDWLRQGRQ·WSOD\DQ\SDUW LQLW5HPHPEHUVWDWHOHVV VHVVLRQEHDQVZLOO1(9(5JHWDQ HME$FWLYDWH RUHME3DVVLYDWH FDOO WRSUHSDUHWKHEHDQ·VVWDWHIRU ZKDWPLJKWEHVHULDOL]DWLRQ QXOO RXWQRQ6HULDOL]DEOHUHIHUHQFHV HWF 47KHEHDQLV´SXWWRVOHHSµDQGLV QRORQJHUWDNLQJXSVSDFHRQWKH KHDS 47KH&RQWDLQHUFDOOVWKHEHDQ·V HME$FWLYDWH PHWKRGZKHQD FOLHQWFDOOVDEXVLQHVVPHWKRG RQWKH(-%REMHFW7KHEHDQ UXQVHME$FWLYDWH EHIRUHLW UXQVWKHEXVLQHVVPHWKRGWKDW WULJJHUHGWKHDFWLYDWLRQ HME$FWLYDWH PHWKRGZKHQD EHDQLVQHHGHGWRVHUYLFHD EXVLQHVVPHWKRGIURPDFOLHQW 7KHEHDQUXQVHME$FWLYDWH EHIRUHLWUXQVWKHEXVLQHVV PHWKRGWKDWWULJJHUHGWKH DFWLYDWLRQ 7KLVPHWKRGLVXVXDOO\ HPSW\DVZHOO \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 16 HQWLW\EHDQFDOOEDFNV <PM-V\Q\a*MIVQV\MZNIKMILL[\PZMMVM_K WV\IQVMZ KITTJIKS[QVKT]LQVO\ _WR][\NWZ[aVKPZWVQbI\QWV LQWHUIDFH!! 6HVVLRQ%HDQ LQWHUIDFH!! (QWLW\%HDQ VHW6HVVLRQ&RQWH[W 6HVVLRQ&RQWH[WVF VHW (QWLW\&RQWH[W (QWLW\&RQWH[WHF HME3DVVLYDWH HME3DVVLYDWH HME$FWLYDWH HME$FWLYDWH HME5HPRYH HME5HPRYH EntityConte of SessionConxt instead text XQVHW(QWLW\&RQWH[W New ! HME/RDG HME6WRUH 4HE %NTITY"EAN INTERFACE ADDS THREE NEW METHODS AND THE CONTEXT SETTER CHANGES TO GIVE THE BEAN AN %NTITY#ONTEXT INSTEAD OF A 3ESSION#ONTEXT "UT AND THIS IS AN EXTREMELY LARGE hBUTv EVEN THE METHODS WHICH ARE THE SAME IN BOTH INTERFACES DONT BEHAVE THE SAME /F THE FOUR METHODS IN 3ESSION"EAN ONLY THE CONTEXT SETTER BEHAVES THE SAME AS ITS COUNTERPART IN %NTITY"EAN 4HE OTHER THREE EJB0ASSIVATE EJB!CTIVATE AND EJB2EMOVE HAVE DRASTICALLY DIFFERENT MEANINGS 9OULL SEE &OR NOW JUST BE READY TO LET GO OF YOUR ATTACHMENTS TO THE MEANING OF ACTIVATION PASSIVATION AND REMOVAL 7HAT THOSE MEAN TO AN ENTITY BEAN IS NOTHING LIKE WHAT THEY MEAN TO A SESSION BEAN &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 17 HQWLW\EHDQV\QFKURQL]DWLRQ -^MV\PMUM\PWL[\PI\IZM\PM[IUM LWV¼\JMPI^M\PM[IUM 6HVVLRQ%HDQLQWHUIDFH (QWLW\%HDQLQWHUIDFH VHW6HVVLRQ&RQWH[W 6HVVLRQ&RQWH[WVF &RQWDLQHUJLYHVWKHEHDQDUHIHUHQFHWRLWVFRQWH[W VHW(QWLW\&RQWH[W (QWLW\&RQWH[WHF &RQWDLQHUJLYHVWKHEHDQDUHIHUHQFHWRLWVFRQWH[W HME3DVVLYDWH &DOOHGRQDVWDWHIXOVHVVLRQEHDQZKHQWKHEHDQ HME3DVVLYDWH &DOOHGZKHQWKHEHDQLVDERXWWRUHWXUQWRWKH LVDERXWWREHVHULDOL]HG RUVRPHWKLQJOLNHLW SRROIROORZLQJDWUDQVDFWLRQ HME$FWLYDWH &DOOHGRQDVWDWHIXOVHVVLRQEHDQZKHQWKHEHDQ HME$FWLYDWH &DOOHGZKHQWKHEHDQLVWDNHQRXWRIWKHSRROWR LVUHDFWLYDWHGIROORZLQJSDVVLYDWLRQ PLJKWEH GHVHULDOL]DWLRQ HME5HPRYH HME5HPRYH VHUYLFHDFOLHQW·VEXVLQHVVPHWKRGFDOO &DOOHGRQDVWDWHIXOEHDQZKHQWKHFOLHQWFDOOV UHPRYH &DOOHGRQDVWDWHOHVVEHDQZKHQWKH &RQWDLQHUZDQWVWRUHGXFHWKHVL]HRIWKHSRRO ean and th SessionBbehaves o b in e r a d s that setter metho ivate, t ur method Of the feoan, only the contexbean, activate, passsession bean y B it y Entit way ! In an ent different from a the same ve are completely and remo &DOOHGZKHQWKHFOLHQWFDOOVUHPRYH DQGZDQWV WRGHOHWHWKLVHQWLW\IURPWKHGDWDEDVH XQVHW(QWLW\&RQWH[W &DOOHGZKHQWKH&RQWDLQHUZDQWVWRUHGXFHWKH VL]HRIWKHSRRO HME/RDG &DOOHGZKHQWKHEHDQKDVEHHQUHIUHVKHGZLWK GDWDIURPWKHXQGHUO\LQJSHUVLVWHQWVWRUH E6WRUH (QWLW\&RQWH[WDGGV JHW3ULPDU\.H\ &DOOHGZKHQWKH&RQWDLQHULVDERXWWRXSGDWHWKH GDWDEDVHWRUHIOHFWWKHVWDWHRIWKHEHDQ QWH[W (QWLW\&RQWH[WH[WHQG(-%&R %RWK6HVVLRQ&RQWH[WDQG HVVLRQ&RQWH[W DW6 GWK HWKR QHP O\R VRQ (QWLW\&RQWH[WKD WKHUH¶V \ 7KLVLVWULFN\EHFDXVH GRHVQ¶WKDYH²JHW3ULPDU\.H (-%/RFDO2EMHFW DQG HFW 2EM (-% WKH RWK DJHW3ULPDU\.H\ LQE HDQ R%27+VHVVLRQDQGHQWLW\E LQWHUIDFHVWKDW¶VH[SRVHGW HSWLRQ H[F DQ JHW ZLOO LHQW QFO EHD FOLHQWV DOWKRXJKDVHVVLRQ \ LV WH[WYHUVLRQRIJHW3ULPDU\.H IRUFDOOLQJLW %XWWKHFRQ V HDQ LW\E HQW O\WR H[SRVHGRQ \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 18 HQWLW\EHDQFDOOEDFNV *]\_IQ\\PMZM¼[UWZM -V\Q\aJMIV[PI^MVM_ PWUMKWV\IQVMZKITTJIKS[\WW <RXUKRPHLQWHUIDFH (QWLW\%HDQLQWHUIDFH MDYD[HME(QWLW\%HDQ LQWHUIDFH!! &XVWRPHU+RPH &XVWRPHU%HDQ FUHDWH 6WULQJODVW6WULQJILUVW6WULQJDGGU HME&UHDWH 6WULQJODVW6WULQJILUVW6WULQJDGGU ILQG%\3ULPDU\.H\ 6WULQJNH\ HME3RVW&UHDWH 6WULQJODVW6WULQJILUVW6WULQJDGGU ILQG%\&LW\ 6WULQJFLW\ HME)LQG%\3ULPDU\.H\ 6WULQJNH\ XSGDWH$OO 6WULQJFPG HME)LQG%\&LW\ 6WULQJFLW\ HME+RPH8SGDWH$OO 6WULQJFPG ery create() in You must match evt one but TWO no th wi the home e(), like with methods: ejbCreat a new thingÑ d an s, an be session ). e( at ejbPostCre ery home You must match evwith an od th business me me>. ejbHome<methodNa s ARE part The Þnder methss,odbut we wonÕt of the bean cla them in our bean actually deÞne get there in a (long story, weÕll few minutes). LQWHUIDFH!! (QWLW\%HDQ VHW(QWLW\&RQWH[W (QWLW\&RQWH[WHF HME$FWLYDWH VHW(QWLW\&RQWH[W (QWLW\&RQWH[WHF HME$FWLYDWH HME3DVVLYDWH HME5HPRYH HME3DVVLYDWH XQVHW(QWLW\&RQWH[W HME5HPRYH HME/RDG XQVHW(QWLW\&RQWH[W HME/RDG HME6WRUH HME6WRUH EXVLQHVVPHWKRGVIURPWKH FRPSRQHQWLQWHUIDFH you must always implement the se SEVEN container callback met in your bean class. EntityBea hods adds three new methods thatn SessionBean interface didnÕt have. &OR ANY ENTITY BEAN USING CONTAINER MANAGED PERSISTENCE #-0 YOU WILL ALWAYS HAVE AT LEAST SEVEN CONTAINER CALLBACKSALL FROM THE %NTITY"EAN INTERFACE IMPLEMENTATION 9OU DONT HAVE TO HAVE A CREATE METHOD IN YOUR HOME BUT IF YOU DO HAVE CREATE METHODS YOU MUST MATCH EACH CREATE WITH NOT ONE BUT TWO METHODS EJB#REATE AND EJB0OST#REATE )F YOU HAVE HOME BUSINESS METHODS YOU MUST WRITE A MATCHING EJB(OMEMETHOD.AME METHOD IN THE BEAN CLASS )F YOU USE BEAN MANAGED PERSISTENCE "-0 WHERE YOU WRITE YOUR OWN DATABASE ACCESS CODE YOU ALSO MUST MATCH EACH lNDER METHOD WITH AN EJB&INDWHATEVER METHOD 2EMEMBER THE ONLY METHOD REQUIRED IN A HOME INTERFACE IS lND"Y0RIMARY+EY "UT IN A #-0 BEAN YOU WONT PUT ANY lNDERS IN YOUR BEAN CLASS EVEN THOUGH THEYRE IN YOUR HOME INTERFACE &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 19 HQWLW\EHDQV\QFKURQL]DWLRQ ?ZQ\QVOI+58MV\Q\aJMIV" UISMQ\IJ[\ZIK\ <(6,ORYH&037KH&RQWDLQHU LPSOHPHQWVP\ÀQGHUVP\JHWWHUV P\VHWWHUVP\VHOHFWPHWKRGVDQGLWZULWHV DOOWKHGDWDEDVHDFFHVVFRGHIRUWKHFUHDWH UHPRYHORDGDQGVWRUHPHWKRGVMXVWWKLQN KRZPDQ\ÁDVKPREV,FDQDWWHQGQRZ WKDW,GRQ·WKDYHWRZULWHDOOWKLVEHDQ FRGH 2KVXUH\RXJHWWRJRKDYHIXQZKLOH , GRDOOWKHUHDOZRUN+RZFRPHZLWKHDFKUHY RIWKH(-%VSHF , HQGXSGRLQJPRUHZRUN DQG\RXHQGXSGRLQJOHVV"7KDWEORZV:LOO\RX DWOHDVWWDNHVRPHSLFWXUHVRIWKHÁDVKPREQH[W ZHHNDW%DUQHVDQG1REOH"$WH[DFWO\30HYHU\RQH VKRZVXS$WHYHU\RQHUXQVLQDQGVWDUWVEUHDWKLQJ KDUGDQGFURXFKLQJEHKLQGERRNVKHOYHVOLNHWKH\·UH EHLQJFKDVHGE\EDGJX\V$WWKH\DOOUXQWRWKH FRPSXWHUVHFWLRQJUDEWKHQHDUHVW+HDG)LUVWERRN DQGVKRXW´,JRWLWµWKHQWKH\GLVSHUVH%\ HYHU\RQHLVJRQH -AKE YOUR #-0 ENTITY BEAN CLASS ABSTRACT 9OU STILL HAVE TO IMPLEMENT THE CONTAINER CALLBACKS FROM JAVAXEJB%NTITY"EAN AND IF YOU STILL HAVE TO WRITE ALL YOUR BUSINESS METHODS INCLUDING THOSE FROM THE HOME AND IF YOU HAVE ANY CREATE METHODS YOU HAVE TO MATCH THOSE IN THE BEAN CLASS AS WELL "UT THAT STILL LEAVES A PILE OF WORK FOR THE #ONTAINER TO DO INCLUDING THE ACCESSOR METHODS FOR YOUR PERSISTENT lELDS &OR EXAMPLE IF YOUR #USTOMER TABLE HAS A COLUMN FOR ADDRESS AND YOU MAP THAT TO A lELD IN YOUR BEAN CLASS SO THAT CLIENTS CAN FOR INSTANCE CALL GET!DDRESS YOU DONT WRITE THE GETTERS AND SETTERS FOR THAT lELD )N FACT YOU DONT EVEN DECLARE THE lELD 7ITH #-0 YOU CREATE A VIRTUAL lELD BY DElNING GETTERS AND SETTERS )N OTHER WORDS THE PERSISTENT lELD FOR ADDRESS EXISTS IN YOUR BEAN SIMPLY BECAUSE THERES AN ABSTRACT GETTER AND SETTER FOR IT 7ELL THATS NOT ENTIRELY TRUE 4HERE ALSO HAS TO BE AN ENTRY IN THE DEPLOYMENT DESCRIPTOR BUT WELL LOOK AT THAT IN THE NEXT CHAPTER \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 20 HQWLW\EHDQFODVV AW]X]\\PZMMSQVL[WN\PQVO[QVaW]ZJMIVKTI[[" 7KLQJVIURPWKHKRPHLQWHUIDFH )RUHDFKFUHDWH PHWKRGLQWKHKRPH\RXPXVWKDYH DPDWFKLQJHME&UHDWH DQGHME3RVW&UHDWH PHWKRG LQWKHEHDQFODVV )RUHDFKEXVLQHVVPHWKRGLQWKHKRPH\RXPXVW KDYHDPDWFKLQJHME+RPHPHWKRG!LQWKHEHDQ FODVV LQWHUIDFH!! &XVWRPHU+RPH FUHDWH 6WULQJODVW6WULQJILUVW6WULQJDGGUHVV ILQG%\3ULPDU\.H\ 6WULQJNH\ ILQG%\&LW\ 6WULQJFLW\ XSGDWH$OO 6WULQJFPG )RU&03EHDQV\RXZLOO127ZULWHPDWFKLQJILQGHU PHWKRGV7KH&RQWDLQHUZLOOZULWHWKHPIRU\RXEDVHG RQLQIR\RXSXWLQWKHGHSOR\PHQWGHVFULSWRU :H·OOJR RYHUDOOWKDWLQWKHQH[WFKDSWHU 7KLQJVIURPWKHFRPSRQHQWLQWHUIDFH LQWHUIDFH!! &XVWRPHU )RUHDFKPHWKRGLQWKHFRPSRQHQWLQWHUIDFH\RX PXVWKDYHDPDWFKLQJFRQFUHWHLPSOHPHQWDWLRQLQWKH EHDQFODVV JHW)LUVW1DPH VHW)LUVW1DPH 6WULQJQDPH JHW/DVW1DPH VHW/DVW1DPH 6WULQJQDPH JHW$GGUHVV VHW$GGUHVV 6WULQJDGGU 7KLQJVIURPWKH(QWLW\%HDQLQWHUIDFH LQWHUIDFH!! (QWLW\%HDQ <RXPXVWLPSOHPHQWWKH(QWLW\%HDQLQWHUIDFHHLWKHU GLUHFWO\RULQGLUHFWO\6RXQOHVV\RXKDYHDVXSHUFODVV WKDWLPSOHPHQWHGWKHPHWKRGV\RX·UHUHVSRQVLEOHIRU ZULWLQJFRQFUHWHLPSOHPHQWDWLRQVLQ\RXUEHDQFODVV VHW(QWLW\&RQWH[W (QWLW\&RQWH[WHF HME$FWLYDWH HME3DVVLYDWH HME5HPRYH XQVHW(QWLW\&RQWH[W HME/RDG HME6WRUH &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 21 HQWLW\EHDQV\QFKURQL]DWLRQ 84=;WS\PI\¼[NW]Z\PQVO[ 9LUWXDOSHUVLVWHQW¿HOGV )RUHDFKSHUVLVWHQWILHOGSURYLGHDQDEVWUDFWJHWWHUDQGVHWWHU :KDWLVD¶YLUWXDO SHUVLVWHQWÀHOG·",I\RX PHDQDQLQVWDQFHYDULDEOH WKHQZK\DUH\RXFDOOLQJ LW¶YLUWXDO·" ¶9LUWXDOSHUVLVWHQWÀHOGV·DUHIRUWKH YDOXHVWKDWPDSWRFROXPQVLQWKH GDWDEDVH7KH\UHSUHVHQWWKHHQWLW\·V SHUVLVWHQWVWDWH,Q\RXUEHDQFODVV FRGHWKH\H[LVWRQO\DVDEVWUDFW JHWWHUVDQGVHWWHUV This bean has FOUR persistent Þelds, fo Last, PK (primary ker First, y), and Address )LUVW-RKQ /DVW0D\HU 3. $GGUHVV'HUODQG6W )LUVW /DVW 3. $GGUHVV -RKQ )UDQ %HOD 0D\HU +HDO\ )OHFN 'HUODQG6W %DU6W 3LFN/DQH \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 22 YLUWXDO¿HOGV >QZ\]ITNQMTL[IZM67<QV[\IVKM^IZQIJTM[ SXEOLFDEVWUDFWFODVV&XVWRPHU%HDQ&03LPSOHPHQWV(QWLW\%HDQ^ SULYDWH(QWLW\&RQWH[WFRQWH[W note: the only insta nce variable is for th SXEOLF6WULQJHME&UHDWH 6WULQJODVW6WULQJ¿UVW6WULQJDGGU ^ WKLVVHW/DVW ODVW WKLVVHW)LUVW ¿UVW WKLVVHW3. PDNH3. WKLVVHW$GGUHVV DGGU UHWXUQQXOO ` SXEOLFDEVWUDFW6WULQJJHW/DVW SXEOLFDEVWUDFWYRLGVHW/DVW 6WULQJODVW SXEOLFDEVWUDFW6WULQJJHW)LUVW SXEOLFDEVWUDFWYRLGVHW)LUVW 6WULQJ¿UVW SXEOLFDEVWUDFW6WULQJJHW&XVW$GGUHVV SXEOLFDEVWUDFWYRLGVHW&XVW$GGUHVV 6WULQJDGGU SXEOLFDEVWUDFW6WULQJJHW3. SXEOLFDEVWUDFWYRLGVHW3. 6WULQJSN SXEOLF6WULQJJHW/DVW1DPH ^ UHWXUQWKLVJHW/DVW ` SXEOLFYRLGVHW/DVW1DPH 6WULQJQDPH ^ WKLVVHW/DVW QDPH ` SXEOLF6WULQJJHW)LUVW1DPH ^ UHWXUQWKLVJHW)LUVW ` SXEOLFYRLGVHW)LUVW1DPH 6WULQJQDPH ^ WKLVVHW)LUVW QDPH ` SXEOLF6WULQJJHW$GGUHVV ^ UHWXUQWKLVJHW&XVW$GGUHVV ` SXEOLFYRLGVHW$GGUHVV 6WULQJDGGU ^ WKLVVHW&XVW$GGUHVV DGGU ` PRUHPHWKRGVIURP(QWLW\%HDQ DQGWKHKRPHLQWHUIDFH ` e EntityContext! al persistent Þelds. These are the virtu than abstract re TheyÕre nothing mo that map to the s er tt se d an s er gett base, representing ta da columns in the ent state. The the entityÕs persist with the entity! ved things that get sa These are methods fr om interface, that are ex the component posed to the client. We know what youÕre thinking... why do you ha a SECOND set of ge tters and setters, wh ve en you can just expose th e abstract ones in your component interface? Well, you COULD expo se good idea. Think abou them, but itÕs not a t it.... hereÕs a hint: imagine that versions had more code the non-abstract than what you see here. bigger hint: imagine th at methods had VALIDA the non-abstract TION code... ridiculously big hint: th ink about encapsulation . &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 23 HQWLW\EHDQV\QFKURQL]DWLRQ 6KDUSHQ \RXU SHQFLO 8VLQJWKHLQWHUIDFHVEHORZZULWHDOHJDOEHDQFODVV<RXGRQ·WKDYH WRZULWHWKHDFWXDOEXVLQHVVORJLFEXWDWOHDVWOLVWDOOWKHPHWKRGV WKDW\RXKDYHWRZULWHLQWKHFODVVZLWKWKHLUFRUUHFWGHFODUDWLRQV LQWHUIDFH!! (QWLW\%HDQ LQWHUIDFH!! 3URGXFW LQWHUIDFH!! 3URGXFW+RPH FUHDWH 6WULQJGHVFULSWLRQ6WULQJFDWGRXEOHSULFH6WULQJ,' JHW&DWHJRU\ VHW(QWLW\&RQWH[W (QWLW\&RQWH[WHF ILQG%\3ULPDU\.H\ 6WULQJNH\ JHW,' HME$FWLYDWH ILQG%\&DWHJRU\ 6WULQJFDWHJRU\ JHW'HVFULSWLRQ HME3DVVLYDWH JHW/RZ6WRFN,WHPV VHW'HVFULSWLRQ HME5HPRYH JHW3ULFH XQVHW(QWLW\&RQWH[W VHW3ULFH HME/RDG :ULWHWKHFODVVKHUHLQWKHVSDFHEHORZGRQ·W ZRUU\DERXWLPSRUWVWDWHPHQWV HME6WRUH \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 24 &XVWRPHU%HDQ&03FRGH +WUXTM\MKWLMNWZ\PM+][\WUMZ*MIV+58KTI[[ QRWHLW·VQRWDQQRWDWHGEHFDXVHWKDW·V<285MRELQWKH6KDUSHQH[HUFLVHRQWKHQH[WSDJH SDFNDJHKHDG¿UVW LPSRUWMDYD[HME SXEOLFDEVWUDFWFODVV&XVWRPHU%HDQ&03LPSOHPHQWV(QWLW\%HDQ^ SULYDWH(QWLW\&RQWH[WFRQWH[W SXEOLF6WULQJHME&UHDWH 6WULQJODVW6WULQJ¿UVW6WULQJDGGU ^ WKLVVHW/DVW ODVW WKLVVHW)LUVW ¿UVW WKLVVHW3. PDNH3. WKLVVHW$GGUHVV DGGU UHWXUQQXOO ` SXEOLF6WULQJJHW/DVW1DPH UHWXUQWKLVJHW/DVW ` ^ SXEOLFYRLGVHW/DVW1DPH 6WULQJQDPH ^ WKLVVHW/DVW QDPH ` SXEOLF6WULQJJHW)LUVW1DPH UHWXUQWKLVJHW)LUVW ` ^ SXEOLFYRLGVHW)LUVW1DPH 6WULQJQDPH ^ WKLVVHW)LUVW QDPH ` SXEOLF6WULQJJHW$GGUHVV ^ UHWXUQWKLVJHW&XVW$GGUHVV ` SXEOLFYRLGVHW$GGUHVV 6WULQJDGGU ^ WKLVVHW&XVW$GGUHVV DGGU ` SXEOLFYRLGVHW(QWLW\&RQWH[W (QWLW\&RQWH[WFW[ ^ FRQWH[W FW[ ` &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 25 HQWLW\EHDQV\QFKURQL]DWLRQ SXEOLFDEVWUDFW6WULQJJHW/DVW SXEOLFDEVWUDFWYRLGVHW/DVW 6WULQJODVW SXEOLFDEVWUDFW6WULQJJHW)LUVW SXEOLFDEVWUDFWYRLGVHW)LUVW 6WULQJ¿UVW SXEOLFDEVWUDFW6WULQJJHW&XVW$GGUHVV SXEOLFDEVWUDFWYRLGVHW&XVW$GGUHVV 6WULQJDGGU SXEOLFDEVWUDFW6WULQJJHW3. SXEOLFDEVWUDFWYRLGVHW3. 6WULQJSN SXEOLFYRLGXQVHW(QWLW\&RQWH[W SXEOLFYRLGHME/RDG ^` SXEOLFYRLGHME6WRUH ^` SXEOLFYRLGHME$FWLYDWH ^` SXEOLFYRLGHME3DVVLYDWH ^` SXEOLFYRLGHME5HPRYH ^` SULYDWH6WULQJPDNH3. ^ LQWUDQG LQW 0DWKUDQGRP UHWXUQ³´UDQG ` ` ^` idest primary the worldÕs steupone that always is is th r: be em os rem (Well, we supp etty lame, key generatoer.same number would be prpr imary keys returned th ldnÕt work anyway, since JV M will but that wouique or... guess what... the But as we . un on ti be xcep MUST DuplicateKeyE er use a give you a nicein the Real World, youÕd eithor a REAL were saying, d arg as the primary key, sort, or client-suppliegeneration engine of some base autoprimary key iner to give you the data that.) ask the Contay (if your vendor supports generated ke 6KDUSHQ \RXU SHQFLO 0DUNHDFKPHWKRGLQWKH&XVWRPHU%HDQ&03FODVVZLWKRQHRIWKHIROORZLQJIRXU V\PEROV H C EB VF EDVHGRQWKHUHDVRQIRUWKDWPHWKRG·VH[LVWHQFHLQWKHFODVV)RUH[DPSOHWKH HME&UHDWH PHWKRGLVUHTXLUHGEHFDXVHWKHUH·VDPDWFKLQJFUHDWH LQWKHKRPH VRPDUNDQHQH[WWRWKHHME&UHDWH PHWKRG 3XWDFKHFNPDUNQH[WWRWKRVHPHWKRGVWKDWWKHFRPSLOHUFDUHVDERXW,QRWKHU ZRUGVLI\RXOHIWDPHWKRGRXWDQGWKHFRPSLOHUZRXOGFRPSODLQZLWKDQHUURUWKHQ PDUNWKDWPHWKRGZLWKD $QQRWDWHWKHFRGH\RXUVHOIZLWKDQ\RWKHUGHWDLOV\RXFDQWKLQNRI)RUWKLVH[HUFLVH EXWQRWWKHSUHYLRXVWZR GRDVPXFKDV\RXFDQRQ\RXURZQWKHQWXUQEDFNWR HDUOLHUSDJHVLQWKLVFKDSWHUDQGVHHLI\RXFDQDGGRUFKDQJHDQ\WKLQJ \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 26 HQWLW\EHDQOLIHF\FOH (QWLW\EHDQLQVWDQFHOLIHF\FOH GRHVQRWH[LVW FRQVWUXFWRU VHW(QWLW\&RQWH[W HME6WRUH HME)LQGPHWKRG! SRROHG HME6HOHFWPHWKRG! FRYHUHG LQWKHQH[WFKDSWHU HME$FWLYDWH HME&UHDWHPHWKRG! HME3RVW&UHDWHPHWKRG! XQVHW(QWLW\&RQWH[W HME3DVVLYDWH HME+RPHPHWKRG! KRPHEXVLQHVVPHWKRGV LQVWDQFHWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW HME5HPRYH PHWKRGUHDG\ EXVLQHVVPHWKRG FRPSRQHQWLQWHUIDFH HME/RDG HME6HOHFWPHWKRG! FDOOHGIURPDEXVLQHVVPHWKRG /HW·VVHHKHUHE\P\ FDOFXODWLRQVLWVKRXOGWDNHPH DSSUR[LPDWHO\\HDUVWRPHPRUL]H WKLVOLIHF\FOHGLDJUDPVRWKDWZRXOG PHDQ,FDQVFKHGXOHWKHH[DPIRU VRPHWLPHLQWKH\HDU e whole when you see th of d It looks bad only en e th y ..b lax. thing at once. asResuming youÕve regained this chapter, youÕll have it 90% down, and consciousness, chapter 7, youÕll have it all. by the end of &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 27 HQWLW\EHDQV\QFKURQL]DWLRQ (QWLW\EHDQLQVWDQFHWUDQVLWLRQV 0RYLQJIURPGRHVQRWH[LVW WRSRROHG GRHVQRWH[LVW FRQVWUXFWRU VHW(QWLW\&RQWH[W VHW(QWLW\&RQWH[W HME&UHDWHPHWKRG! HME3RVW&UHDWHPHWKRG! HME$FWLYDWH FRQVWUXFWRU QRWHQRFUHDWH HME6WRUH PHWKRGUHDG\ EXVLQHVVPHWKRG FRPSRQHQWLQWHUIDFH 0RYLQJIURPSRROHGWR PHWKRGUHDG\ HME&UHDWH HME3RVW&UHDWH 25 HME)LQGPHWKRG! HME6HOHFWPHWKRG! SRROHG HME+RPHPHWKRG! KRPHEXVLQHVVPHWKRGV LQVWDQFHWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW XQVHW(QWLW\&RQWH[W HME3DVVLYDWH l memorize this alof DonÕt try to eÕl st mo d spen right now ! W isl chapter looking the rest of th at this stuff. HME/RDG HME6HOHFWPHWKRG! FDOOHGIURPDEXVLQHVVPHWKRG GRHVQRWH[LVW FRQVWUXFWRU VHW(QWLW\&RQWH[W HME5HPRYH LQVWDQFHWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW XQVHW(QWLW\&RQWH[W HME$FWLYDWH HME3DVVLYDWH HME&UHDWHPHWKRG! HME3RVW&UHDWHPHWKRG! HME6WRUH HME)LQGPHWKRG! HME6HOHFWPHWKRG! SRROHG HME$FWLYDWH 0RYLQJIURPPHWKRGUHDG\ WRSRROHG HME+RPHPHWKRG! KRPHEXVLQHVVPHWKRGV HME3DVVLYDWH 7KH&RQWDLQHUGRHV127FDOO HME$FWLYDWH LILWFDOOVHME&UHDWH PHWKRGUHDG\ HME5HPRYH HME/RDG 25 HME5HPRYH 1HYHUERWK$EHDQGRLQJDQ HME5HPRYH ZLOO127EHSDVVLYDWHG EHIRUHJRLQJEDFNWRWKHSRRO EXVLQHVVPHWKRG FRPSRQHQWLQWHUIDFH HME6HOHFWPHWKRG! FDOOHGIURPDEXVLQHVVPHWKRG \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 28 HQWLW\VWDWHWUDQVLWLRQV (QWLW\EHDQLQVWDQFHWUDQVLWLRQV GRHVQRWH[LVW FRQVWUXFWRU VHW(QWLW\&RQWH[W LQVWDQFHWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW XQVHW(QWLW\&RQWH[W 0RYLQJWRGRHVQRWH[LVW HME&UHDWHPHWKRG! HME3RVW&UHDWHPHWKRG! HME$FWLYDWH 25 LQVWDQFHWKURZVDV\VWHPH[FHSWLRQ HME)LQGPHWKRG! HME6HOHFWPHWKRG! SRROHG HME3DVVLYDWH XQVHW(QWLW\&RQWH[W HME+RPHPHWKRG! KRPHEXVLQHVVPHWKRGV PHWKRGUHDG\ HME6WRUH EXVLQHVVPHWKRG FRPSRQHQWLQWHUIDFH HME5HPRYH HME/RDG HME6HOHFWPHWKRG! FDOOHGIURPDEXVLQHVVPHWKRG WKHUH DUH QR 'XPE 4XHVWLRQV Q: (OW COME THERE IS A LABEL FOR EJB3ELECTMETHOD WHATEVER 4(!4 IS IN BOTH THE POOLED STATE AND THE METHOD READY STATE (OW CAN YOU RUN THOSE METHODS IN BOTH A: 7ELL GET TO EJB3ELECT METHODS A BIT LATER BUT FOR NOW THINK OF THEM AS PRIVATE METHODS IN THE BEAN IN OTHER WORDS CALLED NOT BY THE CLIENT BUT ONLY BY THE BEANS OWN METHODS THAT DO SELECTS ON THE DATABASE 4HEYRE USED ONLY FOR #-0 BEANS AND CAN BE A HUGE CONVENIENCE SINCE THEYRE IMPLEMENTED FOR YOU BY THE #ONTAINER 4HE REASON THEYRE IN BOTH PLACES IS BECAUSE IT DEPENDS ON WHICH INTERFACE THE CLIENT USES TO CALL THE METHOD THAT IN TURN CALLS A SELECT METHOD 3O IF A CLIENT CALLS A HOME BUSINESS METHOD AND THE HOME METHOD CALLS AN EJB3ELECTMETHOD THE BEAN STAYS IN THE POOL TO RUN THE METHOD "UT IF THE SELECT METHOD IS CALLED FROM A METHOD IN THE BEANS COMPONENT INTERFACE THAT MEANS THE METHOD IS RUNNING ON A SPECIFIC ENTITY SAY &RANK &OOF SO THE BEAN IS IN THE METHOD READY STATE &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Page 29 Return to Table of Contents HQWLW\EHDQV\QFKURQL]DWLRQ WKHUH DUH QR 'XPE 4XHVWLRQV Q: )T LOOKS LIKE THERE ARE 47/ WAYS TO MOVE TO THE METHOD READY STATE EITHER THE CLIENT CALLS A CREATE METHOD OR THE #ONTAINER CALLS EJB!CTIVATE 3O DOES THIS MEAN THAT YOU CANT COUNT ON EJB!CTIVATE BEING CALLED EACH TIME YOU LEAVE THE POOL A: 4HATS RIGHT ! BEAN CAN MOVE TO THE METHOD READY STATE BY /.,9 THOSE TWO PATHS CREATION OR ACTIVATION BUT NEVER BOTH AT THE SAME TIME 3O IF YOU HAVE A DESIGN THAT ACQUIRES RESOURCES IN EJB!CTIVATE SO THAT THEYLL ALWAYS BE AVAILABLE WHILE THE BEAN IS SERVICING A BUSINESS METHOD YOU BETTER GRAB THEM IN EJB#REATE OR EJB0OST#REATE YOULL SEE THE DIFFERENCE IN A FEW MINUTES )N THE REAL WORLD ITS MUCH LESS COMMON IN %*" TO USE EJB!CTIVATE FOR MUCH OF ANYTHING 7ELL TALK MORE ABOUT THIS BOTH IN THIS CHAPTER AND THE LAST CHAPTER PATTERNS AND PERFORMANCE BUT THE SHORT VERSION IS THIS ITS USUALLY MORE EFFICIENT TO ACQUIRE AND RELEASE SCARCE RESOURCES JUST WITHIN THE BUSINESS METHODS THAT NEED THEM 4HAT WAY YOURE NOT HANGING ON TO THEM PREVENTING OTHER BEANS FROM HAVING ACCESS WHILE YOUR BEAN IS ACTIVE IE NOT IN THE POOL BUT NOT ACTIVELY RUNNING A METHOD 9ES THAT MEANS YOU HAVE SOME ADDITIONAL OVERHEAD IN EACH BUSINESS METHOD AS OPPOSED TO GRABBING THE THING ONCE IN EJB!CTIVATE BUT IN MANY CASES THE OVERHEAD OF GRABBING THE RESOURCE IS MINOR COMPARED TO THE SCALABILITY COST OF HOLDING RESOURCES WERE THINKING DATABASE CONNECTIONS FROM THE POOL OPEN LONGER THAN YOU NEED TO ACCESS THOSE RESOURCES "OTTOM LINE 9OULL PROBABLY FIND YOURSELF LEAVING EJB!CTIVATE EMPTY IN SO YOU WONT HAVE TO WORRY ABOUT MISSING IT WHEN YOU COME OUT OF THE POOL VIA AN EJB#REATE CALL 6KDUSHQ \RXU SHQFLO )RUWKHH[DP\RXKDYHWRNQRZH[DFWO\ ZKLFKFRQWDLQHUFDOOEDFNPHWKRGVDUHLQWKH (QWLW\%HDQLQWHUIDFHVR\RXQHHGWRPHPRUL]H WKHVH7KHWULFN\SDUWLVWKDWVRPHRIWKHP KDYHWKHVDPHQDPHVEXWFRPSOHWHO\GLIIHUHQW EHKDYLRUWKDQWKHLUVHVVLRQEHDQFRXQWHUSDUWVLQ WKH6HVVLRQ%HDQLQWHUIDFH'2127/22.21 7+(23326,7(3$*( 7KHFOLHQWFDOOVWKLVPHWKRGWRWHOOWKH &RQWDLQHUWKDWKH WKHFOLHQW LVGRQHXVLQJWKH EHDQ·V(-%REMHFWUHIHUHQFH BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB 7KLVPHWKRGLVFDOOHGZKHQWKHEHDQJRHV EDFNWRWKHSRRODIWHUDQHQWLW\LVGHOHWHGIURP WKHGDWDEDVH BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB 7KLVPHWKRGLVFDOOHGLPPHGLDWHO\DIWHUWKH EHDQ·VFRQVWUXFWRUUXQV BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB 7KLVPHWKRGLVFDOOHGRQWKHEHDQZKHQWKH EHDQLVLQWKHSRRODQGWKHFOLHQWLQYRNHVD EXVLQHVVPHWKRGRQWKHFRPSRQHQWLQWHUIDFH :H·UHORRNLQJIRUWKHILUVWPHWKRGFDOOHGLQWKDW VFHQDULR BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 30 HQWLW\OLIHF\FOHDFWLYLW\ 6KDUSHQ \RXU SHQFLO )LOOLQWKHPLVVLQJPHWKRGV'RQ·WZRUU\LI\RXGRQ·WJHWWKH QDPHH[DFWO\ULJKWMXVWWU\WRZRUNRXWZKDWKDSSHQVDW WKHWUDQVLWLRQVLQWKHHQWLW\EHDQVWDWHGLDJUDP GRHVQRWH[LVW LQVWDQFHWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW FRQVWUXFWRU SRROHG HME+RPHPHWKRG! KRPHEXVLQHVVPHWKRGV HME6WRUH HME3DVVLYDWH HME$FWLYDWH HME&UHDWHPHWKRG! HME6HOHFWPHWKRG! FRYHUHGLQWKHQH[WFKDSWHU PHWKRGUHDG\ HME6HOHFWPHWKRG! FRPSRQHQWLQWHUIDFH FDOOHGIURPDEXVLQHVVPHWKRG F\FOH HU\GHWDLORIWKLVOLIH <RXKDYHWRNQRZHY HVDLG OLIH7KHWULFNLHVWSDUWVDVZ QRZHYHU\GHWDLORIDEHDQ¶V O\GLIIHUHQW QWLUH XWH HVE QDP 7KHH[DPH[SHFWV\RXWRN PH HVD GVLQDQHQWLW\EHDQKDYHWK HDUOLHUDUHZKHUHWKHPHWKR LDOFDUHZLWK VLRQEHDQ7DNHH[WUDVSHF VHV LQD RVH PWK VIUR PHDQLQJ DEDVH GDW WKH LW\LQ PDNHDQHZHQW FUHDWH PDNHDQHZEHDQYV DQHQWLW\LQWKHGDWDEDVH HWH GHO YV RQH ¶UHG \RX UHPRYH WHOOWKH&RQWDLQHU QEDFN ZKROH WRWKHSRRO WHIXOEHDQYVVHQGWKHEHD HWKRG SDVVLYDWH VHULDOL]HWKHVWD WKHSRROWRUXQDEXVLQHVVP WRI WRX QJL EUL VWDWHIXOEHDQYV URXWRIWKHSRRO LQR RHV DFWLYDWH GHVHULDOL]HWKH DQJ QEH VVLR VVH FDOOHGZKHQDVWDWHOHV UHPHPEHU12PHWKRGLV &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 31 HQWLW\EHDQV\QFKURQL]DWLRQ ;WPW_,1,\PMKTQMV\OM\IZMNMZMVKM\W\PM -2*WJRMK\NWZ ' 7HEN A CLIENT WANTS TO CALL A BUSINESS METHOD ON A SPECIlC ENTITY IN OTHER WORDS AN ENTITY WITH A UNIQUE PRIMARY KEY LIKE ,EE ,OO THE CLIENT lRST NEEDS A REFERENCE TO THAT ENTITYS %*" OBJECT &OR AN ENTITY BEAN REMEMBER THERE ARE THREE WAYS THAT CAN HAPPEN &OLHQWFDOOVD¿QGHURQWKHKRPH FXVWRPHU+RPH¿QG%\3ULPDU\.H\ ³´ &OLHQWFDOOVFUHDWHRQWKHKRPH WRLQVHUWIRUWKH¿UVWWLPH FXVWRPHU+RPHFUHDWH ³/RR´³/HH´³´³%DU&LUFOH´ &OLHQWFDOOVDKRPHEXVLQHVVPHWKRGWKDWUHWXUQVD UHIHUHQFHWRWKHEHDQ¶VFRPSRQHQWLQWHUIDFH FXVWRPHU+RPHJHW&XVWRPHU%\6WUHHW ³%DU&LUFOH´ 7ELL TALK ABOUT EACH STAGE OF THE BEANS LIFECYCLE FOR lNDERS CREATES AND HOME BUSINESS METHODS "UT lRSTEACH OF THESE ASSUMES THAT THE BEAN INSTANCE ALREADY EXISTS IN THE POOL "UT HOW DOES THE BEAN GET THERE IN THE lRST PLACE GRHVQRWH[LVW FRQVWUXFWRU VHW(QWLW\&RQWH[W SRROHG Before the client can use a bean for ANYTHING -- creation, Þnders, business methods, etc., the Container has to make a new bean instance for the pool. Bean CONSTRUCTION isnÕt tied to entity bean CREATION. Construction means a new bean instance. But creation, for an entity bean, means a new entity is inserted into the database. \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 32 &03EHDQFRQVWUXFWLRQ &03(QWLW\EHDQFRQVWUXFWLRQ 6FHQDULR&RQWDLQHUZDQWVWRPDNHDQHZEHDQLQVWDQFHIRUWKHSRRO FRQWH[W FRQWH[W &RQWDLQHUPDNHVDQHZ(QWLW\&RQWH[W &RQWDLQHUPDNHVDQHZLQVWDQFHRIWKH QHZ EHDQ EHDQ REMHFW QHZ EHDQFODVV EHDQ·VFRQVWUXFWRUUXQV %HDQ3RRO &RQWDLQHUFDOOVVHW(QWLW\&RQWH[W RQWKH EHDQ7KLVLVWKH21/<WLPHLQDEHDQ LQVWDQFH·VOLIHWKDWLWZLOOJHWWKLVFDOO &RQWDLQHUSXWVWKHEHDQ ZKLFKQRZKDVD FRQWH[W LQWKHSRRO 1RWLFHFUHDWH ZDVQHYHUFDOOHG$FUHDWH PHWKRGLVRQO\IRULQVHUWLQJDQHZHQWLW\LQWR WKHGDWDEDVHDQGKDVQRWKLQJWRGRZLWKWKH EHDQLQVWDQFH·VFUHDWLRQ FRQWH[W RQWH[ WLW\& VHW(Q W EHDQ EHDQ FRQWH[W EHDQ FRQWH[W %HDQ3RRO %HDQ3RRO &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 33 HQWLW\EHDQV\QFKURQL]DWLRQ *MIV\PQVO[aW]KIVLWL]ZQVOMV\Q\aK WV[\Z]K\QWV" WLPHOLQH FRQVWUXFWRU VHW(QWLW\&RQWH[W 8VH\RXU(QWLW\&RQWH[WWR 8VH\RXU(QWLW\&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN G &07 JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 THIN o ANY d Õt n a You c ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHDQV EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQV PHWKRGVRQLW %07EHDQV $FFHVV $FFHVV \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH :KDWWRSXWLQWKHFRQVWUXFWRU :HNQRZLW·VSDLQIXOO\REYLRXVE\QRZ127+,1* 8QOHVV\RX·UHIRUFHGWRGRQ·WHYHQSXWDFRQVWUXFWRULQ\RXUFRGHDWDOODQGMXVWXVH WKHFRPSLOHUJHQHUDWHGGHIDXOWFRQVWUXFWRU :KDWHYHU\RXGREH685(\RXKDYHDSXEOLFQRDUJFRQVWUXFWRULQ\RXUFODVV SXEOLF&XVWRPHU ^` :KDWWRSXWLQWKHVHW(QWLW\&RQWH[W PHWKRG $VVLJQWKHFRQWH[WWRDQLQVWDQFHYDULDEOH5HPHPEHU\RXJHWRQO\21(FKDQFH WRVDYHLW<RXPLJKWQRWDOZD\VQHHGWRXVHDVHVVLRQFRQWH[WEXW\RX·OOSUREDEO\ QHHGDQHQWLW\FRQWH[W%HVLGHV\RXUFRQWH[WLVJRQQDVWD\DOLYHZKHWKHU\RXNHHS DUHIHUHQFHWRLWRUQRWVRWKHRQO\PHPRU\\RXVDYHLI\RXGRQ·WNHHSLWLVIRUWKH UHIHUHQFHYDULDEOHQRWWKHREMHFWLWVHOI SXEOLFYRLGVHW(QWLW\&RQWH[W (QWLW\&RQWH[WFW[ ^ FRQWH[W FW[ ` \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 34 &03EHDQFUHDWLRQ &03(QWLW\EHDQFUHDWLRQ 6FHQDULRFOLHQWZDQWVWRFUHDWHDQHZHQWLW\LQWKHGDWDEDVH UHPHPEHUWKH &RQWDLQHUPDGHWKHEHDQLQVWDQFHDQGWKHFRQWH[WHDUOLHUDQGSXWWKHPLQWKHSRRO FRQWH[W EHDQ %REµ H ´%LOO\ HME&UHDW EHDQ FOLHQW FU HD WH FRQWH[W +RPH REMHFW ´% LOO\ % RE µ VWXE RRO RPS QIU EHD W H J Eµ O\%R H ´%LO FUHDW %HDQ3RRO '% &OLHQWFDOOVFUHDWH ´%LOO\%REµ RQWKHKRPHVWXE 7KHFUHDWH ´%LOO\%REµ PHWKRGLQYRFDWLRQLVSDVVHGWRWKHKRPHREMHFW $EHDQLVSXOOHGRXWRIWKHSRROWRGRWKHFUHDWLRQ &RQWDLQHUFDOOVHME&UHDWH ´%LOO\%REµ RQWKHEHDQLQVWDQFH (-% REMHFW $ VWXE +RPH REMHFW EHDQ µ RE % OO\ L ´% FRQWH[W H DW % UH W& V R 3 E HM ,QVHUWDQHZ URZLQWKHGDW DEDVH ZLWKDQHZSU %HDQ3RRO LPDU\NH\ '% &RQWDLQHULQVHUWVDQHZHQWLW\ URZ LQWKHGDWDEDVHIRUSULPDU\NH\ &RQWDLQHUJLYHVWKH(-%REMHFWDQG(QWLW\&RQWH[WWKHSULPDU\NH\YDOXH &RQWDLQHUFDOOVHME3RVW&UHDWH ´%LOO\%REµ RQWKHEHDQWRJLYHWKHEHDQDFKDQFHWRILQLVKLQLWLDOL]LQJLWVHOI 7KHKRPHUHWXUQVWKH(-%REMHFWVWXEWRWKHFOLHQW &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 35 HQWLW\EHDQV\QFKURQL]DWLRQ &UHDWLQJDQHZHQWLW\ &03HQWLW\EHDQ note: this is not creation, but entitbeyan instance The Container ins creation. the bean object eatantiated rlier FOLHQW +RPH REMHFW (-% REMHFW '% EHDQ , on the bean instance see.. thereÕs no ÔneywÕhappened when the ead because it alr put the bean in the pool... Container Þrst FUHDWH HME&UHDWH GDWDEDVHLQVHUW QHZ HME3RVW&UHDWH EXVLQHVVPHWKRG EXVLQHVVPHWKRG 7HOA 4HIS IS QUITE DIFFERENT FROM SESSION BEAN CREATION 4HE #ONTAINER CALLS EJB#REATE ON A SESSION BEAN INSTANCE ONLY ONCE AT THE BEGINNING OF THE BEANS LIFE 4HATS ONE BIG DIFFERENCE BETWEEN SESSION AND ENTITY BEANSENTITY BEANS CAN RUN EJB#REATE OVER AND OVER LIKE A BUSINESS METHOD )N FACT THATS THE BEST WAY TO THINK OF IT AS JUST ANOTHER HOME BUSINESS METHOD AS THOUGH IT WERE NAMED EJB)NSERT WHICH IN OUR HUMBLE OPINION IT SHOULD HAVE BEEN NAMED BUT ONCE AGAIN THEY FORGOT TO ASK US "UT THE SECOND DIFFERENCE BETWEEN ENTITY AND SESSION BEAN EJB#REATE METHODS IS THE ORDER IN WHICH THE %*" OBJECT IS CREATED 7HEN AN ENTITY BEAN RUNS EJB#REATE THERES NO %*" OBJECT 9IKES 4HAT MEANS THE BEAN HAS NO WAY TO GET A REFERENCE TO ITS BODYGUARD )N OTHER WORDS AN ENTITY BEAN CANNOT USE THE EJB#REATE METHOD TO SAY GET A REFERENCE TO ITS OWN %*" OBJECT AND PASS THAT REFERENCE TO SOMEONE ELSE 2EMEMBER A BEAN CANT PASS A REFERENCE TO ITSELF EVER 7HEN A BEAN WANTS TO PASS A REFERENCE TO ITSELF IT MUST PASS A REFERENCE TO ITS OWN %*" OBJECT "UT IT CANT DO THAT IN EJB#REATE BECAUSE ITS TOO EARLY \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 36 &03EHDQFUHDWLRQ &03(QWLW\EHDQFUHDWLRQ +H\EHDQUXQ \RXUHME&UHDWH PHWKRGDQGKHUHDUH WKHFOLHQWDUJV 2.LQP\HME&UHDWH PHWKRG,·OOXVHWKRVHDUJV IURPWKHFOLHQWWRVHWP\RZQ SHUVLVWHQWVWDWH ODVWQDPHÀUVW QDPHSULPDU\NH\HWF *UHDW1RZ,FDQ ORRNDW\RXUSHUVLVWHQWÀHOG VWDWHDQGXVHWKHGDWDWR FUHDWHWKHQHZFXVWRPHULQ WKHGDWDEDVH 1RZWKDW,KDYHD SULPDU\NH\,FDQPDNH WKH(-%REMHFW DQGWHOO WKH(QWLW\&RQWH[W LQVHUW '% %HIRUH /DVW )LUVW 3.H\ /DVW )UDQNLH )RRI )UDQNLH )RRI %U\DQ %U\DQ %RRI %LOO\ %RE %RRI (-% REMHFW $IWHU )LUVW 2.EHDQLI\RXQHHG WRÀQLVKXS\RXUFUHDWLRQ ,·PGRQHLQVHUWLQJWKHURZDQG PDNLQJWKH(-%REMHFWVRUXQ\RXU HME3RVW&UHDWH DQGKHUH·VWKH RULJLQDOFUHDWHDUJVMXVWLQ FDVH\RXIRUJRW 3.H\ 7KDQNV1RZWKDW ,·PLQHME3RVW&UHDWH ,NQRZ WKDWP\(-%REMHFWLVUHDG\DQG ,FDQJHWDUHIHUHQFHWRLWIURPP\ FRQWH[W,QHHGWRSDVVDUHIHUHQFHWR P\VHOI ZKLFKPHDQVDUHIHUHQFHWR P\ERG\JXDUG RXWWRDQRWKHUEHDQ VR,5($//<QHHGHGDVHFRQG FKDQFHWRÀQLVKP\FUHDWLRQ &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 37 HQWLW\EHDQV\QFKURQL]DWLRQ ,PSOHPHQW\RXUHME&UHDWH PHWKRGV :KDWWRSXWLQHME&UHDWH 3XW\RXUHQWLW\LQLWLDOL]DWLRQFRGHKHUH7KLVPHDQVVHWWLQJYDOXHVIRU\RXUSHUVLVWHQW ILHOGVWKHILHOGVWKDWPDSWRFROXPQVLQWKHGDWDEDVHURZWKDW·VDERXWWREHLQVHUWHG 1RWLFHZHDUHQ·WVD\LQJ´SXW\RXUREMHFWLQLWLDOL]DWLRQFRGHKHUHµ:LWKVHVVLRQEHDQVZHWROG \RXWRWUHDWWKHHME&UHDWH NLQGRIOLNHDFRQVWUXFWRUIRUWKHEHDQ%XWZLWKHQWLW\EHDQVLW·VGLIIHU HQWEHFDXVHWKHLQVWDQFHZDVLQLWLDOL]HGDORQJWLPHDJR ZKHQLWVVHW(QWLW\&RQWH[W PHWKRGZDV FDOOHG 6RHME&UHDWH LVDERXWLQLWLDOL]LQJWKHEHDQDVWKLVQHZHQWLW\WKHEHDQZLOOQRZUHSUHVHQW)RU D&03EHDQWKLVPHDQVWKHFOLHQWKDVKDQGHG\RXDUJXPHQWVUHSUHVHQWLQJWKHGDWDWRLQVHUW LQWRWKHGDWDEDVH7KH&RQWDLQHUZLOOORRNDWWKHVWDWHRI\RXUSHUVLVWHQWILHOGVRQFHHME&UHDWH UHWXUQVDQGXVHWKRVHILHOGVWRPDNHWKHQHZURZLQWKHGDWDEDVH ,I\RXGRQRWKLQJHOVHLQHME&UHDWH \RXPXVWPDNHDSULPDU\NH\IRUWKLVHQWLW\7KDW·V\RXU PRVWLPSRUWDQWMRE<RXPLJKWPDNHWKHNH\EDVHGRQDUJXPHQWVWRWKHFUHDWHPHWKRG<RX PLJKWPDNHWKHNH\EDVHGRQDNH\JHQHUDWLQJDOJRULWKP<RXPLJKWKDYHDNH\VHUYHUWKDW\RX JRWRIRUWKHQH[WNH\:KDWHYHULWLV\RXPXVWFRPHXSZLWKWKHNH\DQGDVVLJQLWWR\RXUSULPDU\ NH\ILHOG:KHQHME&UHDWH WKH&RQWDLQHUORRNVDWWKHVWDWHRI\RXUSHUVLVWHQWILHOGVDQGXVHV WKDWGDWDWRPDNHWKHQHZURZ :DUQLQJ5HPHPEHUWKH&RQWDLQHUFDOOV\RXUHME&UHDWH EHIRUHLWFDQPDNHWKH(-%REMHFWVR \RXFDQ·WXVHHME&UHDWH WRJHWDUHIHUHQFHWR\RXUVHOI LHDUHIHUHQFHWR\RXUERG\JXDUG(-% REMHFW <RX·OOKDYHWRZDLWXQWLOHME3RVW&UHDWH EHIRUH\RXFDQDVN\RXUFRQWH[WIRUDUHIHUHQFH WR\RXURZQFRPSRQHQWLQWHUIDFH<RXDOVRFDQ·WXVHHME&UHDWH WRDFFHVV\RXURZQSHUVLVWHQW UHODWLRQVKLSV\RXKDYHWRZDLWXQWLOHME3RVW&UHDWH EXWZH·OOFRYHUDOOWKDWLQWKHQH[WFKDSWHU ary key! type of your prÕreim using is a e th be st mu s hi T primary key we In this case, the must be the return type (and at th String, so in the DD) weÕll declare this SXEOLF6WULQJHME&UHDWH 6WULQJODVW6WULQJ¿UVW ^ DVVLJQDUJVWRSHUVLVWHQW¿HOGV Once ejbCreate() complet WKLVVHW/DVW1DPH ODVW WKLVVHW)LUVW1DPH ¿UVW Container will use the es, VHWDSULPDU\NH\ WKLVVHW3ULPDU\.H\ WKLVPDNH.H\ UHWXUQQXOO ` ret valu set to make the new row. es you urn null ?? WhatÕs up with TH AT? \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 38 HQWLW\SULPDU\NH\ 7JRMK\QLMV\Q\a"\PMXZQUIZaSMa %VERY ENTITY -534 HAVE A UNIQUE IDENTITY 4HE #ONTAINER WILL NEVER LET YOU GET AWAY WITH HAVING TWO OR MORE ENTITIES WITH THE SAME PRIMARY KEY !ND AS OF %*" COMING UP WITH THAT PRIMARY KEY IS STILL YOUR JOB 4HE #ONTAINER WONT DO IT AT LEAST NOT ACCORDING TO THE SPECIlCATION ,I,·PMXVW VXSSRVHGWRUHWXUQQXOO ZK\GR,KDYHWRFRPH XSZLWKWKHNH\" ,·PQRWJRQQDGRLW<28 KDYHWRFRPHXSZLWKWKH SULPDU\NH\,GRQ·WNQRZZKDW \RXUEXVLQHVVORJLFLVDQG,·P QRWJRQQDEHDSULPDU\NH\ JHQHUDWRUIRU\RX $QGGRQ·WH[SHFWPHWR GRLWHLWKHULI\RX·UHXVLQJ &03<RXKDYHWRFRPHXSZLWK WKHSULPDU\NH\%()25(WKH &RQWDLQHUWULHVWRLQVHUWWKHQHZ URZ '% 7ITH #-0 YOURE STILL RESPONSIBLE FOR THE PRIMARY KEY 4HAT DOESNT MEAN YOU WONT USE A PRIMARY KEY SERVICE OF SOME KIND -AYBE YOU HAVE A SERVICE THAT AUTOMATICALLY ALLOCATES A BIG BLOCK OF PRIMARY KEYS FROM THE DATABASE AND THEN HANDS THEM OUT AS NEEDED /R YOU MIGHT HAVE SOME TYPE OF UNIQUE IDENTIlER ALGORITHM THAT MAKES UNIQUE KEYS FOR YOU /R MAYBE YOURE USING THE CUSTOMERS SOCIAL SECURITY NUMBER OR ACCOUNT NUMBER OR THATS UP TO YOU TO DECIDE !ND THE WAY YOU TELL THE #ONTAINER WHAT VALUE THE ABOUT TO BE CREATED ENTITY SHOULD HAVE IS THROUGH THE VALUE OF ONE OR MORE OF YOUR PERSISTENT lELDS )F YOU HAVE JUST ONE lELD AS YOUR PRIMARY KEY AND IT MAPS DIRECTLY TO A COLUMN IN THE DATABASE YOURE SET "UT IF YOU NEED MORE THAN ONE VALUE TO UNIQUELY IDENTIFY AN ENTITY LIKE MAYBE IT TAKES A COMBINATION OF NAME AND DATE YOU CAN USE A COMPOUND KEY THAT USES TWO OR MORE OF YOUR CONTAINER MANAGED PERSISTENT lELDS &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Page 39 Return to Table of Contents HQWLW\EHDQV\QFKURQL]DWLRQ 5XOHVIRUHME&UHDWH <RXPXVWLPSOHPHQWWKHHME&UHDWHPHWKRG!WRPDWFKHDFK FUHDWHPHWKRG!LQWKHKRPHLQWHUIDFH 7KHPHWKRGQDPHPXVWEHJLQZLWKWKHSUHIL[´HME&UHDWHµ 7KHPHWKRGPXVWEHGHFODUHGSXEOLFDQGPXVWQRWEHGHFODUHGVWDWLF RUILQDO 7KHGHFODUHGUHWXUQW\SHPXVWEHWKHHQWLW\EHDQ·VSULPDU\NH\W\SH HYHQWKRXJK\RXZLOOUHWXUQQXOOIURPWKHPHWKRG 7KHPHWKRGDUJXPHQWVPXVWEHWKHVDPHDVWKHDUJXPHQWVRIWKH PDWFKLQJFUHDWHPHWKRG! <RXPD\GHFODUHDWKURZVFODXVHZLWK&UHDWH([FHSWLRQRUDQ\ DUELWUDU\DSSOLFDWLRQ FKHFNHG H[FHSWLRQWKDW\RXOLNHDVORQJDVLW ZDVDOVRGHFODUHGLQWKHKRPHLQWHUIDFHEXW\RXPXVW127GHFODUHD 5HPRWH([FHSWLRQ 5XOHVIRU3ULPDU\.H\V $SULPDU\NH\FODVVPXVWEH6HULDOL]DEOHDQGSXEOLF <RXFDQXVHDVLQJOHSHUVLVWHQWILHOGIURP\RXUEHDQFODVVDV\RXU SULPDU\NH\E\LGHQWLI\LQJERWKWKHILHOGQDPHDQGWKHFODVVW\SHLQ WKH'' ,I\RXQHHGWZRRUPRUHSHUVLVWHQWILHOGVWRXQLTXHO\LGHQWLI\\RXU HQWLW\PDNHDFXVWRPFRPSRXQGSULPDU\NH\FODVV %\WKHHQGRIHME&UHDWH \RX0867KDYHDYDOLG SULPDU\NH\DVVLJQHGWRWKH &03ILHOGWKDW\RX·YHWKH WROGWKH&RQWDLQHULV\RXU SULPDU\NH\ILHOG ,I\RXXVHDFRPSRXQGNH\ WKHQ$//WKHILHOGVWKDW PDNHXSWKDWNH\PXVWKDYH YDOLGYDOXHV ,IDWWKHHQGRIHME&UHDWH \RXUSULPDU\NH\LVQXOO WKH&RQWDLQHUZRQ·WGRWKH FUHDWH $FRPSRXQGNH\FODVVPXVWEHPDGHXSRIILHOGVWKDWDUHGHILQHGDV SHUVLVWHQWILHOGVLQWKHEHDQFODVV7KHILHOGVLQWKHEHDQFODVVPXVW KDYHSXEOLFDFFHVVRUPHWKRGV Q: (OW COME ) CANT HAVE THE DATABASE COME UP WITH THE PRIMARY KEY 4HATS HOW WE USUALLY DO IT !RE YOU TELLING ME THE #ONTAINER CANT DO THAT A: 9ES THATS WHAT WERE TELLING YOU 4HE CONTRACT YOU HAVE WITH THE #ONTAINER SAYS THAT BY THE TIME EJB#REATE IS DONE YOUVE DONE WHATEVER YOU HAD TO DO TO MAKE A VALID PRIMARY KEY 9OU MIGHT HAVE A SERVER THAT LETS YOU SAY h,ET THE DATABASE COME UP WITH THE KEY WHEN YOU DO THE INSERTv BUT THERES NO GUARANTEE IN THE SPEC AND YOU CANT ASSUME THAT ALL %*" VENDORS SUPPORT THIS -AYBE SOME DAY IN THE FUTURE THERE WILL BE A DEPLOYMENT INDEPENDENT WAY TO SAY h5SE AUTO GENERATED KEYv IN THE DE PLOYMENT DESCRIPTOR "UT TODAY IS NOT THAT DAY !ND %*" IS NOT THAT SPEC \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Page 40 Return to Table of Contents HQWLW\EHDQFUHDWLRQ ,PSOHPHQW\RXUHME3RVW&UHDWH PHWKRGV :KDWWRSXWLQHME3RVW&UHDWH )LQLVK\RXULQLWLDOL]DWLRQFRGHKHUH1RZ\RXFDQVHHZK\HQWLW\EHDQVQHHG DQHME3RVW&UHDWH ³EHFDXVHHME&UHDWH LVWRRHDUO\IRUVRPHWKLQJV7KH WZRPRVWLPSRUWDQWUHDVRQVIRUHME3RVW&UHDWH DUHWKDW\RXFDQXVHLWWRJHW DUHIHUHQFHWR\RXURZQ(-%REMHFWDQG\RXPXVWXVHLWIRUDFFHVVLQJ\RXU FRQWDLQHUPDQDJHGUHODWLRQVKLSV &05 :H·OOORRNDW&05LQWKHQH[WFKDSWHU 7KLQNRIHME3RVW&UHDWH DVWKHRWKHUKDOIRI\RXUHME&UHDWH ZLWKVRPH5HDOO\ ,PSRUWDQW7KLQJVKDSSHQLQJLQWKHPLGGOH,W·V\RXUVHFRQG DQGODVW FKDQFHWR ILQLVK\RXULQLWLDOL]DWLRQLI\RXQHHGWRGRWKLQJVDVSDUWRIFUHDWLQJWKHQHZHQWLW\ WKDWGHSHQGRQKDYLQJDFFHVVWR\RXU(-%REMHFW RUVRPHWKLQJHOVHWKDWFDQ KDSSHQRQO\LQHME3RVW&UHDWH DQGQRWLQHME&UHDWH :H·OOUHYLVLWHME3RVW&UHDWH LQWKHQH[WFKDSWHUZKHQZHORRNDW&05 5XOHVIRUHME3RVW&UHDWH <RXPXVWLPSOHPHQWWKHHME3RVW&UHDWHPHWKRG!WRPDWFKHDFKFUHDWHPHWKRG! LQWKHKRPHLQWHUIDFH 7KHPHWKRGQDPHPXVWEHJLQZLWKWKHSUHIL[´HME3RVW&UHDWHµ 7KHPHWKRGPXVWEHGHFODUHGSXEOLFDQGPXVWQRWEHGHFODUHGVWDWLFRUILQDO 7KHGHFODUHGUHWXUQW\SHPXVWEHYRLG 7KHPHWKRGDUJXPHQWVPXVWEHWKHVDPHDVWKHPDWFKLQJHME&UHDWHPHWKRG! <RXPD\GHFODUHDWKURZVFODXVHZLWK&UHDWH([FHSWLRQRUDQ\DUELWUDU\DSSOLFD WLRQ FKHFNHG H[FHSWLRQWKDW\RXOLNHDVORQJDVLWZDVDOVRGHFODUHGLQWKHKRPH LQWHUIDFHEXW\RXPXVW127GHFODUHD5HPRWH([FHSWLRQ &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 41 HQWLW\EHDQV\QFKURQL]DWLRQ $OOWKUHHKDYHWKHVDPHDUJV EXWGLIIHUHQWUHWXUQW\SHV +RPHLQWHUIDFH FUHDWHPHWKRG! SXEOLF&XVWRPHUFUHDWH 6WULQJODVW6WULQJ¿UVW WKURZV &UHDWH([FHSWLRQ5HPRWH([FHSWLRQ %HDQFODVV HME&UHDWHPHWKRG! notice the name differences SXEOLF6WULQJHME&UHDWH 6WULQJODVW6WULQJ¿UVW ^` %HDQFODVV HME3RVW&UHDWHPHWKRG! another differen ce SXEOLFYRLGHME3RVW&UHDWH 6WULQJODVW6WULQJ¿UVW ^` clare the same esnÕt have to de the bean class ledoss it throws them. (And you e bean. exceptions, un a RemoteException from th ion. NEVER throwainer throws a RemoteExcept Only the Cont YiX`e gfn\i 7KLQNDERXWWKHWKUHHPHWKRGVDERYHDQGKRZWKH\¶UHUHODWHGWRRQHDQRWKHU7KLQNDERXW ZKDWWKH\¶UHHDFKUHVSRQVLEOHIRU1RZDQVZHUWKHVHWZRTXHVWLRQV :K\DUHWKHUHWXUQW\SHVGHFODUHGOLNHWKLV",QRWKHUZRUGVH[SODLQZK\HDFKRIWKHWKUHH GLIIHUHQWUHWXUQW\SHVLVZKDWLWLV :K\GRERWKHME&UHDWHPHWKRG!DQGHME3RVW&UHDWHPHWKRG!KDYHWKHVDPHDUJXPHQWV" \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 42 HQWLW\EHDQFUHDWLRQ ,·PKDYLQJDKDUG WLPHFRPLQJXSZLWKD UHDVRQZK\DEHDQZRXOG QHHGDUHIHUHQFHWRLWV RZQ(-%REMHFW ,PDJLQH\RXLQSXWDQHZ&XVWRPHU LQWKHGDWDEDVH0D\EH\RXU EXVLQHVVORJLFVD\V´:KHQ\RX PDNHDQHZ&XVWRPHUWHOOWKHPDUNHWLQJ GHSDUWPHQWE\SDVVLQJDUHIHUHQFHWRWKH QHZ&XVWRPHUHQWLW\EHDQRXWWRWKH &XVWRPHU5HJLVWUDWLRQEHDQ´ ,VWLOOGRQ·WVHHZK\ ,QHHGWKH(-%REMHFW FDQ·W,MXVWSDVV´WKLVµDV WKHDUJXPHQWWRWKHRWKHU EHDQ" 7KLQNDERXWLWFROOHJHER\ )LUVWRIILW·VDJDLQVW%HDQ/DZ7KH (-%VSHFIRUELGVSDVVLQJ¶WKLV·DV DQDUJXPHQWIURPEHDQFRGH12%2'< LVVXSSRVHGWRJHWDGLUHFWUHIHUHQFHWR WKHEHDQH[FHSWWKHFRQWDLQHU*HH]LI VRPHERG\','JHWDUHIHUHQFHWRWKH EHDQWKH\FRXOGMXVWFDOOPHWKRGVRQ LWDQGVNLSDOOP\VHUYLFHV &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 43 HQWLW\EHDQV\QFKURQL]DWLRQ ,I\RXZDQWWKHEHDQ\RX JRWWDJRWKURXJKPH$QG\RX GR127ZDQWWRPHVVZLWKPH,·YH ZDWFKHGWKH0DWUL[OLNHWLPHV DQG,·YHEHHQWDNLQJ3LODWHVIRU OLNH\HDUV 2K,JHWLWLI,ZDQW WRSDVVDUHIHUHQFHWRWKH EHDQ,SDVVDUHIHUHQFHWRWKH EHDQ·VERG\JXDUGLQVWHDG/LNH SDVVLQJWKH(-%REMHFW·V EXVLQHVVFDUGLQVWHDGRIWKH EHDQ·V5LJKW" ,W·VOLNHWKLVLI,·PDEHDQ, VD\WRDPHWKRGRIVRPHRQH HOVH´+HUH·VDFDUG\RXFDQXVH WRUHDFKPH%XWGRQ·WFDOOPHFDOO P\ERG\JXDUGDQGKHUH·VKLV FRQWDFWLQIRUPDWLRQµ ,QVWHDGRI EHDQ GR6WXII WKLV (-% REMHFW 8VH GR6WXII P\&RQWH[WJHW(-%2EMHFW \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 44 HQWLW\EHDQFUHDWLRQ *MIV\PQVO[aW]KIVLWL]ZQVOM V\Q\aKZMI\QWV" WLPHOLQH HME&UHDWH HME3RVW&UHDWH 8VH\RXU(QWLW\&RQWH[WWR 8VH\RXU(QWLW\&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHW\RXUSULPDU\NH\ JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHW\RXUSULPDU\NH\ JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQVRQO\VR HQWLWLHVFDQ·WXVHWKLV PHWKRGVRQLW %07EHDQVRQO\VR HQWLWLHVFDQ·WXVHWKLV $FFHVV $FFHVV \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH itÕs too early get a reference object or primto to your still waiting forary key, because the contaiEJB yo Once you return u to Þnish your ejbCre ner is will look in yo from ejbCreate(), the ate(). youÕve made asur bean and see what statecontainer did based on tha result of the initializatiochanges can the cont e client-supplied args. Onl n you y THEN key is (by lookaiinner Þgure out what your pr it needs that tog in your primary key Þeld!imary ), and make an EJB ob ject. reate(), uÕre in ejbPostC By the time ryoknows your primary key the containe e (or found) the EJB and has madthis key. object for &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 45 HQWLW\EHDQV\QFKURQL]DWLRQ &03(QWLW\ILQGHU 6FHQDULRFOLHQWZDQWVWRJHWDUHIHUHQFHWRDQH[LVWLQJHQWLW\EHDQ FRQWH[W EHDQ FOLHQW ÀQ G% \3 ULP DU \. H\ +RPH REMHFW VWXE ´ µ EHDQ H\ ´µ \3ULPDU\. HME)LQG% FRQWH[W %HDQ3RRO ´µ U\.H\ 3ULPD \ % G ÀQ '% &OLHQWFDOOVILQG%\3ULPDU\.H\ ´µ RQWKHKRPHUHIHUHQFH 7KHILQGHUPHWKRGLVSDVVHGWRWKHKRPHREMHFW $EHDQLVVHOHFWHGIURPWKHSRROWRUXQWKHHME)LQG%\3ULPDU\.H\ ´µ PHWKRG 7KHEHDQGRHVDVHOHFWRQWKHGDWDEDVHWRYHULI\WKDWDQHQWLW\ZLWKSULPDU\NH\H[LVWV FRQWH[W (-% REMHFW EHDQ VWXE UHWXU QVWXE +RPH REMHFW EHDQ µ UHWXUQ´ FRQWH[W %HDQ3RRO '% 7KHEHDQUHWXUQVWKHSULPDU\NH\ WRWKHKRPHZKLFKPHDQVWKHHQWLW\H[LVWVLQWKHGDWDEDVH 7KH&RQWDLQHUPDNHV RUILQGV DQ(-%REMHFWIRU 7KH&RQWDLQHUUHWXUQVWKH(-%REMHFWVWXEIRU 1RWHLIWKHEHDQKDGQRWIRXQGDPDWFKLQJHQWLW\IRUSULPDU\NH\WKHFOLHQWZRXOGJHWDQ 2EMHFW1RW)RXQG([FHSWLRQ VXEFODVVRI)LQGHU([FHSWLRQ \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 46 HQWLW\EHDQ¿QGHUV )LQGLQJDQH[LVWLQJHQWLW\ &03HQWLW\EHDQ FOLHQW +RPH REMHFW (-% REMHFW EHDQ '% ves the The bean neverr lea ods! pool for Þnde meth ÀQGPHWKRG! DUJV HME)LQGPHWKRG! DUJV GDWDEDVHVHDUFK QHZ EXVLQHVVPHWKRG EXVLQHVVPHWKRG &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 47 HQWLW\EHDQV\QFKURQL]DWLRQ &03(QWLW\EHDQILQGHUV +H\EHDQ UXQWKLVHME)LQG PHWKRGDQGKHUHLV WKHFOLHQWDUJ´µ 5HOD[$QG\RX·UHULJKW,·P WKHRQHWKDWLPSOHPHQWHGWKH&03 ÀQGHUPHWKRGVVRZH·OOVKRZLWWKLVZD\ HYHQWKRXJKLWLV\RXWKDWFRQFHSWXDOO\ GRHVWKHZRUN,JXHVVLQWKHHQG LWGRHVQ·WPDWWHUDVORQJDVLW KDSSHQV 8PDUHQ·W\RXIRUJHWWLQJWKDW,·PD &03EHDQ",GRQ·WKDYHWKHVHPHWKRGV LQP\EHDQFODVV$OWKRXJKFRPHWRWKLQN RILW,·PDFRQFUHWHLPSOHPHQWDWLRQWKDW WKH&RQWDLQHU³,PHDQ<28³PDGHIURP P\DEVWUDFWFODVVVRPD\EH,'2KDYHWKH PHWKRGVDIWHUDOOEXW,MXVWGLGQ·W UHDOL]HLWDQG 1RZWKDW,KDYH YHULÀHGWKDWWKLVHQWLW\ H[LVWV,FDQPDNHWKH (-%REMHFWLILWGRHVQ·W DOUHDG\H[LVW VHDUFKIRUSN '% /DVW )LUVW 3.H\ 3RO\ 0RUSKLVP 'HZH\ &KHDWHP (-% REMHFW 2.FOLHQWKHUH·V \RXU(-%REMHFW UHIHUHQFHIRU <RXFDQXVHLWWRFDOO EXVLQHVVPHWKRGVRQ3RO\ 0RUSKLVP 7KDQNV,UHDOO\ QHHGWRFKDQJH 3RO\·VODVWQDPHWR (VWKHU ,JXHVV,MXVW JHWWRVWD\LQWKH SRRODQGIRUJHW WKHZKROHWKLQJ \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 48 HQWLW\EHDQ¿QGHUV <28GRQ·WLPSOHPHQWWKH)LQGHUPHWKRGV LQWHUIDFH!! &XVWRPHU+RPH <RXLPSOHPHQW FUHDWHDQGKRPHEXVLQHVVPHWKRGV &RQWDLQHULPSOHPHQWV ILQGHUPHWKRGV FUHDWH 6WULQJODVW6WULQJILUVW XSGDWH$OO 6WULQJFPG ILQG%\3ULPDU\.H\ 6WULQJNH\ ILQG%\&LW\ 6WULQJFLW\ *UST DONT WRITE @EM &OR A #-0 BEAN DONT PUT ANYTHING IN YOUR CLASS ABOUT THE lNDER METHODS 9OU WILL IMPLEMENT YOUR CREATE AND HOME BUSINESS METHODS BUT AS FAR AS YOUR OWN BEAN CLASSTHE ONE THAT YOU WRITEGOES THERE ARE NO lNDER METHODS 9OU DO DECLARE THEM IN THE HOME INTERFACE OF COURSE INCLUDING THE MANDATORY lND"Y0RIMARY+EY BUT YOU DONT WRITE ANY OTHER *AVA CODE RELATED TO YOUR lNDERS 4HE #ONTAINER LOOKS IN YOUR HOME INTERFACE AND YOUR DEPLOYMENT DESCRIPTOR TO lGURE OUT WHAT TO DO WITH THE lNDER METHODS AND THE #ONTAINER WRITES ALL THE CODE FOR THEM 9OULL NEVER SEE IT 'RQ·WSXWDQ\WKLQJLQ \RXUEHDQFODVVDERXWWKH ILQGHUV7KH&RQWDLQHU LPSOHPHQWV\RXUILQGHU PHWKRGVXVLQJWKHKRPH LQWHUIDFHDQG\RXU GHSOR\PHQWGHVFULSWRUWR ILJXUHRXWZKDWWRGR <RXUEHDQFODVVPXVW QRWHYHQ0(17,21WKH ILQGHUPHWKRGV &XVWRPHU%HDQ HME&UHDWH 6WULQJODVW6WULQJILUVW HME3RVW&UHDWH 6WULQJODVW6WULQJILUVW HME+RPH8SGDWH$OO 6WULQJFPG HME)LQG%\3ULPDU\.H\ 6WULQJNH\ HME)LQG%\&LW\ 6WULQJFLW\ VHW(QWLW\&RQWH[W (QWLW\&RQWH[WHF HME$FWLYDWH HME3DVVLYDWH HME5HPRYH XQVHW(QWLW\&RQWH[W HME/RDG HME6WRUH EXVLQHVVPHWKRGVIURPWKH FRPSRQHQWLQWHUIDFH &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Page 49 Return to Table of Contents HQWLW\EHDQV\QFKURQL]DWLRQ +PPPP,QRWLFHGWKDWWKHEHDQ GLGQ·WFRPHRXWRIWKHSRRO%XWZKDW·V 5($//<ZHLUGLVWKDWQRZWKHUH·VDQ(-%REMHFW IRUEXWWKHUH·VQRHQWLW\EHDQSOD\LQJWKH SDUWRI7KHEHDQQHYHUJRWORDGHGXSZLWK WKHHQWLW\GDWDIURPWKHGDWDEDVHVRZKDW KDSSHQVLIWKHFOLHQWFDOOVDEXVLQHVVPHWKRG OLNHJHW$GGUHVV """ ,KHDUZKDW\RX·UHVD\LQJEXW FRPHRQDUHQ·WWKHFKDQFHV SUHWW\JRRGWKDWWKHFOLHQW,6 JRQQDFDOODEXVLQHVVPHWKRG RQWKHEHDQ"7KH\ZHQWWRWKH WURXEOHRIÀQGLQJLW :K\VKRXOG,ZDVWHDEHDQ·V WLPHE\ORDGLQJLWXSZLWKWKH GDWDZKHQWKHFOLHQWPLJKWQRW HYHUFDOODEXVLQHVVPHWKRGRQ WKDW(-%REMHFWUHIHUHQFH" <RXMXVWDOZD\VKDYHWR NQRZWKHWUXWK2.\RX JRWPHRQWKLVRQH<HVLW,6OLNHO\ WKHFOLHQWZLOOFDOODEXVLQHVVPHWKRGRQWKH HQWLW\VRLWPLJKWVHHPPRUHHIÀFLHQWWRKDYH WKHEHDQUHDG\%XWHYHQLI,','ORDGWKH GDWDLQWRWKHEHDQGXULQJWKHÀQGHUPHWKRG ,·GVWLOOKDYHWRUHORDGLWDQ\ZD\:K\" %HFDXVHE\WKHWLPHWKHFOLHQWJHWV DURXQGWRFDOOLQJWKHEXVLQHVVPHWKRG WKHEHDQ·VVWDWHPLJKWEHVWDOH \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Page 50 Return to Table of Contents HQWLW\EHDQ¿QGHUV ,·PQRWIROORZLQJ ZKDWGR\RXPHDQE\ VWDOH"+RZFDQWKHEHDQEHFRPH VWDOHLIQRERG\HOVHFDQJHW WRWKHEHDQ·VGDWD"+RZ FRXOGDQ\ERG\FKDQJHWKH HQWLW\" 2NQRZ,JHWLWLIWKHEHDQZHUH WREHORDGHGXSZLWKGDWDGXULQJWKH ÀQGHUPHWKRGWKHQE\WKHWLPHWKH FOLHQWFDOOVDEXVLQHVVPHWKRGRQLWWKH XQGHUO\LQJHQWLW\GDWDLQWKHGDWDEDVH PLJKWKDYHFKDQJHG 7KLQNDERXWLW7KHHQWLW\3RO\0RUSKLVP OLYHVLQWKHGDWDEDVH DURZLQDWDEOH 6XUH ZHFDQKDQGRXWUHIHUHQFHVWRWKH(-%REMHFW IRUWKLVHQWLW\%87WKHHQWLW\%($1LVQ·WWKH RQO\ZD\WRJHWWRWKHGDWDEDVH6RZKLOHWKHFOLHQWLV ZDLWLQJWRFDOODPHWKRGIRUDOOZHNQRZVRPHRQHHOVH FRXOGKDYHXSGDWHG3RO\·VUHFRUGLQWKHGDWDEDVH ,IWKDWZHUHWRKDSSHQWKHQZKHQWKHFOLHQWFDOOV DPHWKRGRQWKH3RO\HQWLW\EHDQWKHEHDQ UHWXUQVROGZURQJLQIRUPDWLRQ <HV,·PJRQQDKDYHWRORDG WKHGDWDLQDJDLQDVVRRQDVWKH FOLHQWFDOOVDEXVLQHVVPHWKRGMXVWWR PDNHVXUHWKDWWKHEHDQLVUHIUHVKHG ZLWKWKHPRVWFXUUHQWGDWDVRWKHUH·V QRSRLQWLQORDGLQJLWLQGXULQJWKH ÀQGHU,WZRXOGMXVWEHDZDVWH RIUHVRXUFHV &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 51 HQWLW\EHDQV\QFKURQL]DWLRQ &03+RPH%XVLQHVV0HWKRGV 6FHQDULRFOLHQWZDQWVWRFDOODEXVLQHVVPHWKRGLQWKHKRPH FRQWH[W EHDQ FOLHQW +RPH REMHFW XS GD WH $O O VWXE EHDQ H$OO HME8SGDW FRQWH[W %HDQ3RRO H$OO XSGDW '% &OLHQWFDOOVXSGDWH$OO RQKRPHUHIHUHQFH 7KHXSGDWH$OO PHWKRGLVSDVVHGWRWKHKRPHREMHFW $EHDQLVVHOHFWHGIURPWKHSRROWRUXQWKHHME8SGDWH$OO PHWKRG FRQWH[W EHDQ FOLHQW UHWXUQ GDWD +RPH REMHFW EHDQ DWD UHWXUQG FRQWH[W %HDQ3RRO '% :LWKRXWOHDYLQJWKHSRROWKHEHDQUXQVWKHKRPHPHWKRGSUREDEO\FDOOLQJRQD&RQWDLQHU LPSOHPHQWHGVHOHFWPHWKRG!WRDFFHVVWKHGDWDEDVH 7KHEHDQUHWXUQVIURPWKHPHWKRG SRVVLEO\UHWXUQLQJGDWD 7KH&RQWDLQHUSDVVHVWKHUHWXUQYDOXHEDFNWRWKHFOLHQW \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 52 KRPHEXVLQHVVPHWKRGV &OLHQWLQYRNHVD+RPH%XVLQHVV0HWKRG &03HQWLW\EHDQ FOLHQW +RPH REMHFW EHDQ '% ves the pool The bean neveressleame thods for home busin KRPHEXVLQHVVPHWKRG KRPHEXVLQHVVPHWKRG No EJB obje ct is involved ! GDWDEDVHDFFHVVYLDD &RQWDLQHULPSOHPHQWHG VHOHFWPHWKRG :KDWWRSXWLQDKRPHEXVLQHVVPHWKRG $V\RXZULWH\RXUFRGHUHPHPEHUWKDWDQHQWLW\EHDQVWD\VLQWKHSRROZKHQLWUXQVD KRPHEXVLQHVVPHWKRG3XWFRGHLQ\RXUKRPHEXVLQHVVPHWKRGVWKDWDSSO\WRWKHXQGHUO\LQJ GDWDEDVH³IRUDJURXSRIHQWLWLHVUDWKHUWKDQRQHVSHFLILFHQWLW\³DQGWKDWGRQ·WUHWXUQWKHEHDQ·V FRPSRQHQWLQWHUIDFH WKHZD\ILQGHUVDQGFUHDWHPHWKRGVPXVW SXEOLF&ROOHFWLRQHME+RPH'LVSOD\$OO ^ GRDVHOHFWRQWKHGDWDEDVHXVLQJDFRQWDLQHULPSOHPHQWHG VHOHFWPHWKRG QH[WFKDSWHU WKHQUHWXUQDQ$UUD\/LVWRI6WULQJV ` 5XOHVIRUKRPHEXVLQHVVPHWKRGV <RXPXVWKDYHDQHME+RPHPHWKRG!PHWKRGIRUHYHU\KRPHPHWKRG!LQWKHKRPHLQWHUIDFH 7KHQDPHPXVWEHJLQZLWKWKHSUHIL[´HME+RPHµIROORZHGE\WKHQDPHRIWKHKRPHPHWKRG!EXW ZLWKWKHILUVWOHWWHURIWKHPHWKRGFDSLWDOL]HG)RUH[DPSOHXSGDWH$OO LQWKHKRPHLQWHUIDFHZLOOEH HME+RPH8SGDWH$OO LQWKHEHDQFODVV 7KHPHWKRGPXVWEHGHFODUHGSXEOLFDQG127VWDWLF ,IWKHKRPHLQWHUIDFHLV5HPRWHWKHDUJXPHQWVDQGUHWXUQYDOXHVPXVWEHOHJDOW\SHVIRU50,,,23 <RXPD\GHFODUHDWKURZVFODXVHZLWK\RXURZQDSSOLFDWLRQ FKHFNHG H[FHSWLRQVDVORQJDVWKH H[FHSWLRQVZHUHDOVRGHFODUHGLQWKHKRPHLQWHUIDFH (YHQLI\RXUOLIHGHSHQGVRQLW\RXPXVW127GHFODUHD5HPRWH([FHSWLRQ &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Page 53 Return to Table of Contents HQWLW\EHDQV\QFKURQL]DWLRQ *MIV\PQVO[aW]KIVLWQVPWUMJ][QVM[[UM\PWL[ 8VH\RXU(QWLW\&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHW\RXUSULPDU\NH\ JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ e the canÕt do becaus two things youING an entity! The bean isnÕt BE pool acting on behalf bean is in the entities of this type, of ALL the e particular entity. So but not any onno EJB object and of the bean has imary key. course, no pr EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQVRQO\VR HQWLWLHVFDQ·WXVHWKLV $FFHVV \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 54 HQWLW\EXVLQHVVPHWKRGV 6WDUWLQJDEXVLQHVVPHWKRG LQDWUDQVDFWLRQ 6FHQDULRFOLHQWZDQWVWRJHWWKHDGGUHVVRIDVSHFL¿F&03&XVWRPHUHQWLW\ JHW$GGUHVV VV GUH $G JHW VWXE FOLHQW FRQWH[W (-% REMHFW +(/3 EHDQ EHDQ FRQWH[W HME$FWLYDWH '% &OLHQWFDOOVJHW$GGUHVV RQWKHVWXEIRUHQWLW\EHDQ 7KHFDOOLVSDVVHGWRWKH(-%REMHFW 7KH(-%REMHFWJHWVWKHFDOODQGSDQLFVEHFDXVHWKHUH,6QRHQWLW\EHDQIRU 7KH&RQWDLQHUVHHVWKDWWKLVPHWKRGQHHGVDWUDQVDFWLRQVRWKH&RQWDLQHUVWDUWVRQH WKHQSXOOVDEHDQIURPWKHSRRODQGFDOOVHME$FWLYDWH RQWKHEHDQ EHDQ FRQWH[W HME/RDG 6(/(&7 7KH&RQWDLQHUWHOOVWKHGDWDEDVHWRORFNHQWLW\LQWKHGDWDEDVH7KH 7HOOGDWDEDVH WRORFNURZ '% &RQWDLQHUWKHQGRHVD6(/(&7RQWKHHQWLW\·VGDWD 7KH&RQWDLQHUSRSXODWHVWKHHQWLW\EHDQ·VSHUVLVWHQWILHOGVZLWKWKHUHDOHQWLW\GDWD 7KH&RQWDLQHUFDOOVHME/RDG RQWKHEHDQWRWHOOWKHEHDQ´<RX·YHMXVWEHHQORDGHGµ &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 55 HQWLW\EHDQV\QFKURQL]DWLRQ &RPSOHWLQJDEXVLQHVVPHWKRG DQGDWUDQVDFWLRQ 6FHQDULREHDQFRPSOHWHVDEXVLQHVVPHWKRGWKDWHQGVDWUDQVDFWLRQ UHWXUQ XUQ UHW VWXE FRQWH[W (-% REMHFW EHDQ JHW$GGUHVV FOLHQW UHWXUQ EHDQ FRQWH[W '% 7KHJHW$GGUHVV PHWKRGLVSDVVHGWRWKHEHDQ ILQDOO\ 7KHEHDQUHWXUQVIURPWKHPHWKRG 7KH(-%REMHFWVHQGVWKHUHWXUQEDFNWRWKHFOLHQW EHDQ FRQWH[W HME6WRUH 7KH&RQWDLQHUFDOOVHME6WRUH WRWHOOWKHEHDQ´,·PDERXWWRWDNH\RXUSHUVLVWHQW ILHOGVWDWHDQGXSGDWHWKHGDWDEDVHVREHVXUH\RXUVWDWHLVUHDG\µ 83'$7( 7UDQVDFWLRQ(QGV XQORFNURZ '% 7KHUHDOHQWLW\LVXSGDWHGLQWKHGDWDEDVH 7KH&RQWDLQHUFRPPLWVWKHWUDQVDFWLRQDQGWHOOVWKHGDWDEDVHWRXQORFNWKHHQWLW\ \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 56 EHLQJDQHQWLW\EHDQ %HLQJ-RKQ(QWLW\%HDQ ,WDOOVWDUWVZKHQWKH FOLHQWFDOOVDEXVLQHVV PHWKRGRQDQ(-%REMHFW IRU-RKQ0DOFRP7KH(-% REMHFWIUHDNVRXWEHFDXVH WKHUH·VDFDOOEXWQRHQWLW\ EHDQWRUXQWKHPHWKRG ,PLJKWQRWEH FRPSOHWHO\UHDG\ZKHQ,FRPH RXWRIWKHSRROVRWKH&RQWDLQHU FDOOVP\HME$FWLYDWH PHWKRG,W·V DFWXDOO\DSUHWW\XVHOHVVPHWKRGVR ,DOPRVWQHYHUGRDQ\WKLQJWKHUH%XW, VXSSRVH,PLJKWUHDFTXLUHD6RFNHWRU VRPHWKLQJ,GRQ·WZDQWWRKDQJRQWR ZKLOH¶EHWZHHQSURMHFWV·%XWLW·VKDUG IRUPHWRLPDJLQHDQ\WKLQJ,ZRXOGGR LQHME$FWLYDWH WKDW,ZRXOGQ·W UDWKHUGRLQVRPHRWKHU PHWKRG 6RWKH &RQWDLQHUSKRQHV EDFNWRWKHSRRO DQGVD\V´:HQHHG DEHDQRXWKHUH WRVHUYLFHDFOLHQW PHWKRGµ %XWDIWHUWKDW,VWLOOQHHG WRDVN´:KRDP,VXSSRVHG WREH"µDQGWKDW·VZKHQ,JHWWKH ´%HLQJ-RKQ0DOFRPµVFULSWZLWKDOORIP\ FKDUDFWHU·VLPSRUWDQWGDWD7KH&RQWDLQHU FDOOVP\HME/RDG PHWKRGWRWHOOPHWKDW ,KDYHHYHU\WKLQJ,QHHGWRSOD\WKH UROHRI-RKQ0DOFRP$IWHUWKDW,·P LQFKDUDFWHUDQGUHDG\IRUDFWLRQ &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 57 HQWLW\EHDQV\QFKURQL]DWLRQ 6KRZWLPH7KHFOLHQW·VEXVLQHVV PHWKRGFDOOLVÀQDOO\SDVVHGWRPH +H\ZLOOVRPHERG\WHOOP\SV\FKR ERG\JXDUGWRUHOD[LW·VQRWHDV\ EHLQJEULOOLDQW\RXNQRZ $IWHUP\VWXQQLQJSHUIRUPDQFHDV -RKQ0DOFRPWKH&RQWDLQHU ZKR WDNHVWKLVMREZD\WRRVHULRXVO\ FDOOVP\HME6WRUH PHWKRGWRVHHLI,·YH FKDQJHGDQ\RI-RKQ·VLQWHUQDOVWDWHYDOXHV :HOORI&2856(,KDYHWKLVFKDUDFWHU WKURXJKPXFKWUDJHG\VXIIHULQJDQG EXVLQHVVORJLFKDVJURZQDVDQHQWLW\ 2I&2856(KH·VDGLIIHUHQWSHUVRQ 6R,VD\´'RQ·WJHW\RXU NQLFNHUVLQDWZLVWKHUH·VWKH GDWDµ WKH\DOODFWOLNHLW·VOLWHUDOO\ WKHHQGRIWKHZRUOGRYHUKHUHLIWKH GDWDEDVHGRHVQ·WNQRZHYHU\OLWWOH WKLQJWKDWKDSSHQVWRP\ FKDUDFWHU 7KHQLW·VRYHUDQG ,·PEDFNWRWKHSRRO %XWÀUVWWKH&RQWDLQHUFDOOV P\HME3DVVLYDWH PHWKRGWRWHOOPH WKDW,·PGRQHEHLQJ-RKQ0DOFRP DQGWKDWLW·VVDIHWRUHOHDVHDQ\ UHVRXUFHV,ZDVNHHSLQJRQKLV EHKDOI,DOPRVWQHYHUXVHWKLV PHWKRGEHFDXVHLW·VQRWDQ HIÀFLHQWZD\WRPDQDJH WKLQJV \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 58 DFWLYDWLRQSDVVLYDWLRQ $FWLYDWLRQSDVVLYDWLRQRID&03HQWLW\EHDQ FOLHQW &RQWDLQHU (-% REMHFW EHDQ '% EXVLQHVVPHWKRG HME$FWLYDWH 6(/(&7HQWLW\GDWD SRSXODWHEHDQVWDWH HME/RDG EXVLQHVVPHWKRG HME6WRUH 83'$7(HQWLW\GDWD HME3DVVLYDWH 4HE #ONTAINER ALWAYS CALLS EJB,OAD AFTER EJB!CTIVATE 4HE #ONTAINER ALWAYS CALLS EJB3TORE BEFORE EJB0ASSIVATE 4HE #ONTAINER CAN CALL EJB3TORE AND EJB3TORE AT OTHER TIMES &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 59 HQWLW\EHDQV\QFKURQL]DWLRQ 6KDUSHQ \RXU SHQFLO ,I\RX¶UHWDNLQJWKHH[DPWUXVWXVKHUH\RX5($//<QHHGWRVWRSULJKWQRZ DQGGRWKLVH[HUFLVHEHIRUHWXUQLQJWKHSDJH7KLQNDERXWHYHU\WKLQJ\RX¶YH OHDUQHGLQWKLVFKDSWHUDERXWWKHOLIHF\FOHRIDQHQWLW\EHDQHVSHFLDOO\KRZ DQGZKHQLWFRPHVRXWRIWKHSRROWREHFRPHDQHQWLW\ 7DNH\RXUEHVWJXHVVDERXWZKLFKDVSHFWVRIEHDQQHVVDUHDYDLODEOHGXULQJ HDFKRIWKHIRXUFRQWDLQHUFDOOEDFNVIRUHME$FWLYDWH HME3DVVLYDWH 8VH\RXU(QWLW\&RQWH[WWR HME$FWLYDWH HME3DVVLYDWH HME/RDG HME6WRUH T T T T JHWDUHIHUHQFHWR\RXUKRPH T T T T JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW T T T T IRUFHWKHWUDQVDFWLRQWRUROOEDFN T T T T ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\EHHQVHWWRUROOEDFN T T T T JHWDUHIHUHQFHWR\RXU(-%REMHFW T T T T JHW\RXUSULPDU\NH\ $FFHVV HME$FWLYDWH HME3DVVLYDWH HME/RDG HME6WRUH T T T T PHWKRGVRIDQRWKHUEHDQ T T T T \RXUVSHFLDO-1',FRQWH[W T T T T DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 60 DFWLYDWLRQSDVVLYDWLRQ *MIV\PQVO[aW]KIVLWL]ZQVOIK\Q^I\QWVIVLTWILQVO WLPHOLQH HME$FWLYDWH HME/RDG 8VH\RXU(QWLW\&RQWH[WWR 8VH\RXU(QWLW\&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHW\RXUSULPDU\NH\ JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHW\RXUSULPDU\NH\ JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQVRQO\VR HQWLWLHVFDQ·WXVHWKLV PHWKRGVRQLW %07EHDQVRQO\VR HQWLWLHVFDQ·WXVHWKLV $FFHVV $FFHVV \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH that efers (no, make The Container prif you access a bean INSISTS) thatmanager, you must be in or a resource transaction contextÕ, so a Ômeaningful is too early. But once you ejbActivate() d(), youÕre IN a transaction get to ejbLoatold the Container NOT (unless youÕve saction... which is for very to use a tran ly). special cases on Now we can do entity bean CANeverything that an do... the same th we can do in ings think of this aasbusiness method. You can th business method. e beginning of the &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 61 HQWLW\EHDQV\QFKURQL]DWLRQ *MIV\PQVO[aW]KIVLWL]ZQVOXI[[Q^I\QWVIVL[\WZQVO WLPHOLQH HME3DVVLYDWH HME6WRUH 8VH\RXU(QWLW\&RQWH[WWR 8VH\RXU(QWLW\&RQWH[WWR JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHW\RXUSULPDU\NH\ JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 JHWDUHIHUHQFHWR\RXUKRPH JHWDUHIHUHQFHWR\RXU(-%REMHFW JHW\RXUSULPDU\NH\ JHWVHFXULW\LQIRUPDWLRQDERXWWKHFOLHQW IRUFHDWUDQVDFWLRQWRUROOEDFN &07 EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHDQV ILQGRXWLIWKHWUDQVDFWLRQKDVDOUHDG\ EHHQVHWWRUROOEDFN &07EHDQV EHHQVHWWRUROOEDFN &07EHDQV JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO JHWDWUDQVDFWLRQUHIHUHQFHDQGFDOO PHWKRGVRQLW %07EHDQVRQO\VR HQWLWLHVFDQ·WXVHWKLV PHWKRGVRQLW %07EHDQVRQO\VR HQWLWLHVFDQ·WXVHWKLV $FFHVV $FFHVV \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH \RXUVSHFLDO-1',HQYLURQPHQW DQRWKHUEHDQ·VPHWKRGV DUHVRXUFHPDQDJHU OLNHDGDWDEDVH By the time the Container starts to passivate you, youÕre no longer associated with a client or a transaction , the Container th ejbActivatefu()l transaction wi s wa it as st Ju u in a Ômeaning no longer has yoher words, you canÕt be in at a ot (in u canÕt ge he contextÕ is point), so yoce th at n io ct manager. T sa an ur tr or a reso to an be a to do this e nc refere Õs too dangerous (see Container says niitngful transaction contextÕ. ea Ôm a t ou th wi chapter) the transactions FYI -- this page is EXAC Y the same as the previous one onTL ivation and loading. (except for theact notes we made). The rules for whatÕs allo wed during activation are the same for pass and the rules for loading are idenivation, tical to the rules for storing. \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 62 FRPPLWRSWLRQV &RPPLWRSWLRQVZKDW5($//<KDSSHQV WRDEHDQDIWHUDWUDQVDFWLRQFRPPLWV" (the previous pictures are just ONE way it could work, but there are two other ways) &RPPLWRSWLRQ$ 7KHEHDQVWD\VUHDG\DWWDFKHGWRWKH(-%REMHFWORDGHG ZLWKGDWDEXW127LQDWUDQVDFWLRQ7KH&RQWDLQHUNHHSVWKH HQWLW\ORFNHGVRWKDWQRERG\FDQFKDQJHWKHEHDQ·VVWDWH :KHQDFOLHQWFDOOVDEXVLQHVVPHWKRGRQWKLVHQWLW\EHDQ·V (-%REMHFWWKH&RQWDLQHUGRHV127GRDQHME$FWLYDWH RU HME/RDG DQGDVVXPHVWKHEHDQ·VGDWDLVVWLOOLQV\QFZLWK WKHXQGHUO\LQJSHUVLVWHQWVWRUH $ %HDQVWD\VFRQQHFWHGDQGLQV\QF :KHQWKHQH[WEXVLQHVVPHWKRGFDOO FRPHVLQWRWKHEHDQLWMXVWUXQV1R HME$FWLYDWH QRHME/RDG (-% REMHFW 'DUNR EHDQ (-% REMHFW &RPPLWRSWLRQ& 7KHEHDQLVSDVVLYDWHGDQGSXWEDFNLQWRWKHSRRO7KH QH[WWLPHDFOLHQWFDOOVDEXVLQHVVPHWKRGRQWKLVHQWLW\ HYHU\WKLQJZH·YHVHHQLQWKLVFKDSWHUKDSSHQV³DEHDQ FRPHVRXWRIWKHSRROLVDFWLYDWHGWKHORDGHGDQG ILQDOO\WKHEXVLQHVVPHWKRGLWSDVVHGWRWKHEHDQ 'DUNR 'RQQ\ bean lock is released, butthe pool to ck ba doesnÕt go & (-% REMHFW 322/ EHDQ ys locked %HDQVWD\VFRQQHFWHGEXWQRWLQV\QF :KHQWKHQH[WEXVLQHVVPHWKRGFDOO FRPHVLQWRWKHEHDQHME/RDG LV FDOOHG %XWQRHME$FWLYDWH EHDQ % &RPPLWRSWLRQ% 7KHEHDQVWD\VDWWDFKHGWRWKH(-%REMHFWORDGHGZLWK GDWDEXWWKHEHDQ·VVWDWHLVPDUNHGDV¶LQYDOLG·,QRWKHU ZRUGVWKH&RQWDLQHUNQRZVWKDWWKHEHDQPLJKWEHFRPH VWDOHEHWZHHQQRZDQGWKHQH[WWLPHDFOLHQWLQYRNHV DPHWKRGRQWKLVEHDQVRWKH&RQWDLQHUZLOOGRDQ HME/RDG ZKHQDQHZEXVLQHVVPHWKRGFRPHVLQIRUWKLV HQWLW\ %XWQRHME$FWLYDWH entity sta 'RQQ\ %HDQLVGLVFRQQHFWHGIURPWKH(-% REMHFWDQGJRHVEDFNWRWKHSRRO:LWK WKHQH[WEXVLQHVVPHWKRGFDOOWRWKLV EHDQWKH&RQWDLQHUSXOOVDEHDQRXWRI WKHSRROWKHQFDOOVHME$FWLYDWH DQG HME/RDG 'DUNR 'RQQ\ goes lock is released, bean ol po e th to ck ba &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 63 HQWLW\EHDQV\QFKURQL]DWLRQ there are no Dumb Questions Q: (OW DOES THESE COMMIT OPTIONS AFFECT ME $O ) NEED TO KNOW WHICH ONE MY #ONTAINER IS CHOOSING $O ) GET A CHOICE CREATED THAT THEN STAYS STUCK TO THE %*" OBJECT !RENT POOLS MORE EFlCIENT BECAUSE YOU NEED ONLY ENOUGH BEANS TO SERVICE THE ACTUAL IN PROGRESS METHODS A: A: !CCORDING TO THE SPECIFICATION YOUR VENDOR IS FREE TO USE ANY OF THE THREE OPTIONS 9OUR CONTAINER MIGHT LET YOU CHOOSE OR TUNE THE OPTION BUT DONT ALWAYS COUNT ON IT "UT DO YOU NEED TO KNOW WHICH IS BEING USED 9OU CAN SEE WHERE THE TRADE OFFS ARE OF COURSEIF YOU KEEP THE ENTITY LOCKED IN THE DATABASE NO OTHER APPLICATIONS CAN USE THAT ENTITYS DATA 4HATS FINE IF THE %*" IS THE ONLY APPLICATION THAT NEEDS THAT DATABASE !ND THAT OPTION COMMIT OPTION ! GIVES YOU THE BEST PERFORMANCE WHEN A BUSINESS METHOD CALL COMES IN FOR THE ENTITY BECAUSE THERES NO EJB!CTIVATE THE BEAN NEVER WENT BACK TO THE POOL AND MORE SIGNIFICANTLYTHERES NO TRIP TO THE DATABASE OR AN EJB,OAD Q: "UT COULDNT OPTION ! REALLY KILL ME IF ) WANT TO KEEP THE ENTITIES UNLOCKED IN THE DATABASE WHEN THEYRE NOT BEING USED BY THE BEAN A: &OR THIS REASON MOST VENDORS WONT USE OPTION ! AS A DEFAULT THEY ALLOW IT ONLY IF THE DEPLOYER OR ADMIN CHOOSES IT ASSUMING ITS EVEN SUPPORTED AT ALL Q: ) SEE ANOTHER POSSIBLY EVEN 7/23% PROBLEM WITH OPTION ! DOESNT THIS MEAN A CONTINUALLY GROWING HEAP FULL OF OBJECTS ,IKE EACH TIME SOMEONE USES A PARTICULAR ENTITY AN ENTITY BEAN INSTANCE IS .O CONTAINER WORTH CONFIGURING WOULD LEAVE YOU STUCK WITH A CONSTANTLY ENDLESSLY GROWING ACCUMULATION OF OBJECTS %VEN A CONTAINER THAT DOES USE OPTION ! CAN USE SOMETHING LIKE A ,25 ,EAST 2ECENTLY 5SED ALGORITHM TO SAY TO ITSELF h(MMM NOBODY HAS CALLED A METHOD ON ENTITY FOR A LONG TIME SO )LL PASSIVATE THAT BEAN AND PUT HIM BACK IN THE POOL AND UNLOCK THE ROW v "UT WITH AT LEAST THE OPTION OF USING OPTION ! A CONTAINER CAN SAY TO ITSELF h4HIS ENTITY IS REALLY POPULAR )TS A BIG PAIN TO KEEP GOING TO THE DATABASE AND DOING THE WHOLE LOAD STOREACTIVATEPASSIVATE CYCLE EACH TIME SO )LL JUST KEEP HIM LOADED AND READY EVEN BETWEEN TRANSACTIONSv Q: /H MAN ) JUST THOUGHT OF SOMETHING 2%!,,9 BAD WITH THIS ARCHITECTUREEVEN IF YOU DO ./4 KEEP ACCUMULATING ENTITY BEANS WHAT ABOUT THE %*" OBJECTS $ONT THEY JUST KEEP GROWING AND GROWING SO THAT EACH TIME A CLIENT DOES A lND AND GETS BACK A STUB AN %*" OBJECT IS CREATED FOR THAT ENTITY AND IT JUST STAYS AROUND UNTIL THE ENTITY ITSELF IS REMOVED !T ANY GIVEN TIME ) WILL HAVE ALL THE %*" OBJECTS ON THE HEAP THAT REPRESENT EVERY ENTITY ANYONE HAS EVER ACCESSED UP TO THAT POINT A: !H NOW WE COME TO THE DIFFERENCE BETWEEN THE hCONCEPTUALv VIEW OF THE ARCHITECTURE AND THE hACTUALv IMPLEMENTATION !CCORDING TO THE SPEC YOURE SUPPOSED TO ASSUME THAT THIS IS INDEED HOW IT WORKS WHEN A CLIENT WANTS AN ENTITY BEAN EITHER THROUGH A CREATE OR FIND THE #ONTAINER MAKES AN %*" OBJECT FOR THAT ENTITY hHERES THE %*" OBJECT FOR HERES THE %*" OBJECT FOR v ETC !ND AS A "EAN 0ROVIDER AS OPPOSED TO SAY A CONTAINERSERVER PROVIDER YOU ARE TO PROGRAM AS IF THATS REALLY THE WAY IT WORKS "UT INTERNALLY THE #ONTAINER MIGHT BE DOING SOMETHING QUITE DIFFERENT FROM THE CONCEPTUAL ARCHITECTURAL VIEW )MAGINE THAT 9/5 WERE A CONTAINER DEVELOPERHOW WOULD YOU IMPLEMENT THINGS /NE IDEA MIGHT BE TO PUT THE BURDEN ON THE CLIENTS BY KEEPING ALL THE INFORMATION IN THE STUBS &OR EXAMPLE WHEN THE CLIENT GETS A STUB TO AN %*" OBJECT YOU CAN MAKE SURE THAT THE STUB KNOWS THE UNIQUE )$ FOR THE ENTITY IE THE PRIMARY KEY "UT THE %*" OBJECTS MIGHT BE GENERIC IN SUCH A WAY THAT WHEN THE CLIENT CALLS A METHOD ON A STUB THAT STUB IS CONNECTED NOT TO AN %*" OBJECT JUST FOR THAT ENTITY BUT RATHER A ONE %*" OBJECT FITS ALL THAT KNOWS HOW TO MAKE SURE THE METHOD ENDS UP AT THE RIGHT ENTITY 4HAT WAY WHEN A CLIENT ISNT USING AN %*" OBJECT THAT %*" OBJECT ISNT SITTING AROUND WASTING SPACE 4HATS JUST ONE IDEA Q: 4HAT MIGHT BE lNE FOR STUBS BUT WHAT IF THE CLIENT IS LOCAL 4HEN THEY HAVE A REAL REFERENCE TO THE %*" OBJECT SO THAT %*" OBJECT WOULD (!6% TO BE ENTITY SPECIlC A: 9ES THATS TRUE "UT WITH LOCAL CLIENTS NORMAL *AVA GARBAGE COLLECTION WORKS 7HEN THERE ARE NO CLIENTS WITH REFERENCES TO A PARTICULAR %*" OBJECT THE INSTANCE WILL DIE \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 64 FRPPLWRSWLRQV there are no Dumb Questions Q: 7HICH OF THE THREE COMMIT OPTIONS DO YOU THINK IS MOST USED A: 7ELL WE CANT REALLY ANSWER THAT BUT IF WE WERE IN A BETTING MOOD OUR MONEY WOULD BE ON OPTION " ! @SMART OPTION " THAT KNOWS WHEN IT MAKES SENSE TO HANG ON TO A BEAN AND WHEN IT DOESNT "UT AVOIDING THE EJB!CTIVATE WHICH IS USUALLY USELESS ANYWAY IS A 'OOD 4HING Q: "UT ISNT THE HIT TO THE DATABASETHE LOADA MUCH BIGGER HIT THAN JUST ONE MORE METHOD CALL ON THE STACK A: &IRST OF ALL ACTIVATION IS PROBABLY A LITTLE MORE THAN JUST ONE MORE METHOD CALL ON THE STACK BECAUSE THERES OVERHEAD JUST IN MAINTAINING THE ORGANIZATION OF THE POOL ITSELF "UT YES YOURE RIGHT ABOUT THE DATABASE HIT BEING A BIGGER DEAL "UT THERES ALL SORTS OF WAYS THE #ONTAINER CAN OPTIMIZE AND WHATS THE ALTERNATIVE 4HE ONLY ALTERNATIVE OPTION ! KEEPS THE DATABASE LOCKED WHICH IS TYPICALLY NOT WHAT YOU WANT Q: "UT WHAT IF MY %*" APP )3 THE ONLY THING THAT USES THAT DATABASE A: ) THINK WE MENTIONED THIS EARLIERIF YOU KNOW ABSOLUTELY POSITIVELY NO QUESTION THAT YOUR %*" APP IS THE ONLY THING TOUCHING THAT DATABASE THEN YES IF YOUR VENDOR SUPPORTS IT YOULL HAVE BETTER PERFORMANCE WITH OPTION ! /F COURSE CACHING THINGS IN MEMORY IS STILL DANGEROUS BECAUSE ENTITIES ARE PERSISTENT 3O THE VENDOR STILL MUST IMPLEMENT SOMETHING THAT SAVES THE STATE OF THE ENTITIES TO THE DATABASE IF THEY CHANGE 3O THERE WILL STILL BE STORING IF NOT LOADING 4HERE ARE STILL OTHER POTENTIAL PROB LEMS WITH OPTION ! THOUGH &OR EXAMPLE YOU MIGHT HAVE A SERVER THAT SUPPORTS CLUSTERING AND THE SERVER WILL HAVE TO BE CERTAIN THAT ALL INSTANCES OF THAT ENTITY BEAN IN SYNC NOT JUST WITH THE DATABASE BUT WITH ONE ANOTHER "UT THATS ANOTHER ISSUE 9OULL LEARN MORE IN THE PATTERNS AND PERFORMANCE CHAPTER Q: 3O IS THE REAL POINT OF ALL THIS THE FACT THAT AS LONG AS YOURE IN A TRANSACTION YOU WONT HAVE ALL THESE LOADSTORE ACTIVATEPASSIVATE CALLS A: 9ES A BIG PART OF THE POINT ANYWAY Q: YOURE DOING MORE LOADSSTORES THAN YOU NEED TO SO THE KEY IS TO HAVE YOUR TRANSACTIONS BE AS LONG AS YOU NEED AND NO LONGER Q: /NE MORE THING DOESNT THIS MEAN THAT EJB0ASSIVATE AND EJB!CTIVATE MIGHT NOT EVER BE CALLED ON A BEAN IF THE CONTAINER USES OPTION ! OR " !ND DOESNT THIS MEAN THAT ) SHOULDNT RELY ON EJB!CTIVATE OR EJB0ASSIVATE IN MY DESIGN A: 9%3 4HATS %8!#4,9 WHAT IT MEANS )TS USUALLY A LOUSY STRATEGY "UT YOU CAN IMAGINE HOW IT MIGHT SOUND LIKE A GOOD IDEA 8PUHPHPEHUKRZ, WROG\RXLWZRXOGEHPRUH HIÀFLHQWWRJHWUHVRXUFHV LQHME$FWLYDWHDQGUHOHDVHWKHP LQHME3DVVLYDWH"&DQZHMXVW SUHWHQGWKDW,QHYHUVDLG WKDW" 4HEN SHOULDNT YOUR GOAL BE TO HAVE THE LONGEST TRANSACTIONS A: 9OU ARENT BEING SERIOUS RIGHT *UST FOR FUN WELL PRETEND YOU ARE AND SAY ./ ./ ./ ./ 4HINK ABOUT YOUR CONCURRENCY ! GOOD RULE OF THUMB WITH A ZILLION EXCEPTIONS OF COURSE SO ALL RULE OF THUMB DISCLAIMERS APPLY IS THAT TRANSACTIONS SHOULD BE NO LONGER THAN ABSOLUTELY NECESSARY 4HE LONGER YOUR BEAN IS IN A TRANSACTION THE MORE LIKELY IT IS THAT OTHERS ARE WAITING IN LINE TO GET TO THAT ENTITY /F COURSE ITS ALSO POSSIBLE TO HAVE TRANSACTIONS WHICH ARE TOO SHORT WHICH MEANS &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 65 HQWLW\EHDQV\QFKURQL]DWLRQ ([HUFLVH &03HQWLW\EHDQ&UHDWLRQ )LOOLQWKHILYHPLVVLQJDUURZODEHOVIRUWKHREMHFWLQWHUDFWLRQ GLDJUDP5HPHPEHUWKH\DUHQ·WQHFHVVDULO\PHWKRGFDOOV7KLV LVH[DFWO\WKHVDPHGLDJUDPWKDW\RXVDZHDUOLHULQWKHFKDSWHU ([FHSW\RXUVZLOOORRNQLFHUEHFDXVH\RX·OOSUREDEO\PDNHLWDOO IDQF\ZLWKH[RWLFV\PEROVDQGPD\EHFRORUFRGHGPDUNHUVDQG FOLHQW +RPH REMHFW (-% REMHFW EHDQ '% FUHDWH \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 66 HQWLW\EHDQH[HUFLVH (QWLW\%HDQ%XVLQHVV0HWKRG ZLWKDFWLYDWLRQ SDVVLYDWLRQ ([HUFLVH 7KLVWLPH\RX·UHJRQQDGUDZWKHDUURZV\RXUVHOI$VVXPHWKDWWKHEHDQLVLQWKHSRRODWWKH WLPHWKHFOLHQWFDOOVDEXVLQHVVPHWKRG ZKLFKDOVRPHDQVWKHEHDQLVQRWLQDWUDQVDFWLRQ 'UDZDUURZVDQGODEHOWKHPZLWKWKHDFWLRQVWKDWRFFXUWKURXJKWKHFRPSOHWLRQRIWKH EXVLQHVVPHWKRG6KRZZKDWKDSSHQVZKHQWKHEHDQUHWXUQVWRWKHSRRO :KHQZHGLGLWWKHUHZHUHHLJKWPRUHDUURZVEXW\RXPLJKWKDYHPRUHRUOHVVGHSHQGLQJ RQKRZ\RXZDQWWRGRLW'R127IOLSEDFNWKURXJKWKHPRVWUHFHQWSDJHV<RXFDQGRWKLV FOLHQW &RQWDLQHU (-% REMHFW EHDQ '% EXVLQHVVPHWKRG &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 67 HQWLW\EHDQV\QFKURQL]DWLRQ ([HUFLVH )LOOLQWKH3URGXFW%HDQ80/LVKER[ZLWKWKHPHWKRGVWKDW<28PXVWZULWH LQ\RXUEHDQFODVVJLYHQWKHFRPSRQHQWDQGKRPHLQWHUIDFHV'RQ·WIRUJHW WKHFRQWDLQHUFDOOEDFNVIURP(QWLW\%HDQDOWKRXJKZH·YHVKRZQ\RXRQO\ WKUHHRIWKHVHYHQ7KHUHVW\RX·OOKDYHWRUHPHPEHUDQGILOOLQ LQWHUIDFH!! 3URGXFW 3URGXFW%HDQ JHW3URGXFW'HVFULSWLRQ JHW4XDQWLW\ JHW3ULFH LQWHUIDFH!! (QWLW\%HDQ HME$FWLYDWH HME5HPRYH XQVHW(QWLW\&RQWH[W LQWHUIDFH!! 3URGXFW+RPH FUHDWH 6WULQJ,'6WULQJSULFH6WULQJGHVFULSWLRQ ILQG%\3ULPDU\.H\ 6WULQJNH\ JHW/RZ,QYHQWRU\ LQWOLPLW \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 68 FRIIHHFUDPPRFNH[DP 0RFN([DP 1 7HATS TRUE FOR A BEAN PROVIDER WHEN CREATING AN ENTITY BEAN USING CONTAINER MANAGED PERSISTENCE #HOOSE ALL THAT APPLY R! #ONTAINER MANAGED PERSISTENT lELDS MUST BE DElNED IN THE ENTITY BEAN CLASS R" #ONTAINER MANAGED RELATIONSHIP lELDS MUST BE DElNED IN THE ENTITY BEAN CLASS R# 7HEN IMPLEMENTING A ONE TO MANY RELATIONSHIP THE JAVAUTIL,IST INTERFACE MUST NOT BE USED R$ !CCESSOR METHODS FOR CONTAINER MANAGED RELATIONSHIP lELDS MUST BE EXPOSED IN THE BEANS REMOTE COMPONENT INTERFACE 2 7HICH OF THE FOLLOWING IS A LEGAL ACCESSOR METHOD FOR A PERSISTENT lELD IN AN ENTITY BEAN WITH CONTAINER MANAGED PERSISTENCE #HOOSE ALL THAT APPLY R! R" R# R$ 3 SXEOLFYRLGJHW&XVWRPHU1XP DEVWUDFWYRLGJHW&XVWRPHU1XP SXEOLFDEVWUDFWLQWJHW&XVWRPHU1XP 7HICH OF THE FOLLOWING ARE LEGAL ACCESSOR METHODS IN AN ENTITY BEAN WITH CONTAINER MANAGED PERSISTENCE #HOOSE ALL THAT APPLY R! R" R# R$ SXEOLFJHW&XVWRPHU1XP SXEOLFDEVWUDFWLQW*HW&XVWRPHU1XP SXEOLFDEVWUDFWLQWJHWFXVWRPHU1XP SXEOLFDEVWUDFWLQWJHW&XVWRPHU1XP SXEOLFDEVWUDFWLQWJHW&XVWRPHU1XP ^` &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 69 HQWLW\EHDQV\QFKURQL]DWLRQ 4 7HICH ARE REQUIREMENTS FOR A #-0 ENTITY BEAN CLASS #HOOSE ALL THAT APPLY R! R" R# R$ 4HE CLASS MUST DElNE A ¿QDOL]H METHOD 4HE SOURCE lLE MUST DElNE AT LEAST ONE CONSTRUCTOR 4HE CLASS MUST BE DECLARED PUBLIC AND ABSTRACT 4HE CLASS MUST IMPLEMENT DIRECTLY OR INDIRECTLY THE MDYD[HME(QWHUSULVH%HDQ INTERFACE R% !LL GETTER AND SETTER METHODS FOR THE BEANS ABSTRACT PERSISTENCE SCHEMA MUST BE ABSTRACT 5 7HICH ARE LEGAL DECLARATIONS FOR A #-0 BEANS EJB#REATE METHODS #HOOSE ALL THAT APPLY R! SXEOLFYRLGHME&UHDWH%LJ&XVWRPHU WKURZV MDYD[HME&UHDWH([FHSWLRQ R" SXEOLF6WULQJHME&UHDWH$FFRXQW WKURZV MDYD[HME&UHDWH([FHSWLRQ R# VWDWLF6WULQJHME&UHDWH WKURZV MDYD[HME&UHDWH([FHSWLRQ R$ SXEOLFLQWHME&UHDWH WKURZV MDYD[HME&UHDWH([FHSWLRQ R% SXEOLF¿QDO6WULQJHME&UHDWH WKURZV MDYD[HME&UHDWH([FHSWLRQ 6 7HICH ARE LEGAL DECLARATIONS FOR A METHOD IN A #-0 BEAN #HOOSE ALL THAT APPLY R! SXEOLF$FFRXQWHME6HOHFW$FFW ORQJ[ WKURZV MDYD[HME)LQGHU([FHSWLRQ R" SXEOLFDEVWUDFW$FFWHME6HOHFW$FFW ORQJ[ WKURZV MDYD[HME)LQGHU([FHSWLRQ R# SXEOLF$FFRXQWHME3RVW&UHDWH $FFWNH\ WKURZV MDYD[HME&UHDWH([FHSWLRQ R$ SXEOLFYRLGHME3RVW&UHDWH $FFWNH\ WKURZV MDYD[HME&UHDWH([FHSWLRQ R% SXEOLFVWDWLFYRLGHME3RVW&UHDWH $FFWNH\ WKURZV MDYD[HME&UHDWH([FHSWLRQ \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 70 FRIIHHFUDPPRFNH[DP 7 7HICH METHODS FROM THE %NTITY#ONTEXT INTERFACE CAN BE INVOKED FROM WITHIN THE SET%NTITY#ONTEXT METHOD #HOOSE ALL THAT APPLY R! R" R# R$ R% 8 JHW&DOOHU,GHQWLW\ JHW&DOOHU3ULQFLSDO VHW5ROOEDFN2QO\ HME6WRUH HME&UHDWH HME6HOHFW HME5HPRYH HME3DVVLYDWH 7HICH METHODS FROM THE %NTITY#ONTEXT INTERFACE CAN BE INVOKED FROM WITHIN THE EJB#REATE METHOD #HOOSE ALL THAT APPLY R! R" R# R$ R% 10 JHW(-%/RFDO+RPH 7HICH CAN BE CALLED ON A #-0 BEAN TO TRANSITION IT FROM THE READY STATE TO THE POOLED STATE #HOOSE ALL THAT APPLY R! R" R# R$ R% 9 JHW(-%2EMHFW JHW(-%+RPH JHW(-%2EMHFW JHW&DOOHU3ULQFLSDO JHW8VHU7UDQVDFWLRQ VHW5ROOEDFN2QO\ 7HAT IS TRUE FOR A #-0 BEAN IN THE READY STATE R! R" R# R$ )TS HME/RDG )TS HME6WRUH CAN BE CALLED DIRECTLY AFTER EJB3TORE CAN BE CALLED DIRECTLY AFTER A BUSINESS METHOD /NE OF ITS BUSINESS METHODS CAN BE CALLED DIRECTLY AFTER EJB3TORE .ONE OF THE ABOVE &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 71 HQWLW\EHDQV\QFKURQL]DWLRQ 11 7HICH METHODS FROM THE %NTITY#ONTEXT INTERFACE MUST ./4 BE INVOKED FROM WITHIN THE EJB,OAD METHOD #HOOSE ALL THAT APPLY R! R" R# R$ R% 12 JHW(-%2EMHFW JHW&DOOHU3ULQFLSDO JHW8VHU7UDQVDFWLRQ VHW5ROOEDFN2QO\ 7HICH METHOD CALLED ON A #-0 BEAN IS !,7!93 ASSOCIATED WITH A STATE CHANGE IN THE BEAN #HOOSE ALL THAT APPLY R! R" R# R$ R% 13 JHW(-%+RPH HME/RDG HME)LQG HME5HPRYH HME$FWLYDWH XQVHW(QWLW\&RQWH[W 7HATS TRUE ABOUT AN #-0 ENTITY BEANS PRIMARY KEY #HOOSE ALL THAT APPLY R! 4HE BEANS PRIMARY KEY CLASS MUST PROVIDE A SUITABLE IMPLEMENTATION OF THE HASH#ODE AND EQUALS METHODS R" 7HEN SPECIFYING THE PRIMARY KEY IN THE DEPLOYMENT DESCRIPTOR ONLY THE lELD NAME MUST BE DECLARED R# !LL lELDS IN THE PRIMARY KEY CLASS MUST BE DECLARED PUBLIC R$ !LL lELDS USED IN THE PRIMARY KEY MUST BE CONTAINER MANAGED lELDS 14 (OW MANY EJB#REATE METHODS CAN A #-0 ENTITY BEAN HAVE R! R" R# R$ R% OR TO MANY TO MANY \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Page 72 Return to Table of Contents FRIIHHFUDPPRFNH[DP 15 7HICH METHODS ARE ALWAYS INVOKED IN DIRECT RESPONSE TO A CLIENT OPERATION #HOOSE ALL THAT APPLY R! HME/RDG R" HME&UHDWH R# HME5HPRYH R$HME$FWLYDWH R% HME3DVVLYDWH R& VHW(QWLW\&RQWH[W 16 7HICH ADDITIONAL METHODS MIGHT THE CONTAINER CALL WHEN INVOKING EJB2EMOVE #HOOSE ALL THAT APPLY R! R" R# R$ R% 17 HME)LQG HME/RDG HME6WRUH HME$FWLYDWH HME3DVVLYDWH !T WHAT POINTS MUST THE CONTAINER ESTABLISH A #-0 BEANS PRIMARY KEY #HOOSE ALL THAT APPLY R! BEFORE CALLINGQHZ,QVWDQFH R"BEFORE CALLING VHW(QWLW\&RQWH[W R# BEFORE CALLINGHME&UHDWH R$ BEFORE CALLINGHME3RVW&UHDWH 18 7HICH METHODS RUN IN THE TRANSACTION CONTEXT OF THE METHOD THAT CAUSES THEIR INVOCATION #HOOSE ALL THAT APPLY R! R" R# R$ R% R& HME/RDG HME5HPRYH HME6HOHFW HME$FWLYDWH HME3DVVLYDWH VHW(QWLW\&RQWH[W &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 73 HQWLW\EHDQV\QFKURQL]DWLRQ 0RFN([DP$QVZHUV 1 7HATS TRUE FOR A BEAN PROVIDER WHEN CREATING AN ENTITY BEAN USING CONTAINER MANAGED PERSISTENCE #HOOSE ALL THAT APPLY R! #ONTAINER MANAGED PERSISTENT lELDS MUST BE DElNED IN THE ENTITY BEAN CLASS R" #ONTAINER MANAGED RELATIONSHIP lELDS MUST BE DElNED IN THE ENTITY BEAN CLASS Collection R# 7HEN IMPLEMENTING A ONE TO MANY RELATIONSHIP THE JAVAUTIL,IST - OnlySet can be used or INTERFACE MUST NOT BE USED R$ !CCESSOR METHODS FOR CONTAINER MANAGED RELATIONSHIP lELDS MUST BE EXPOSED IN THE BEANS REMOTE COMPONENT INTERFACE 2 7HICH OF THE FOLLOWING IS A LEGAL ACCESSOR METHOD FOR A PERSISTENT lELD IN AN ENTITY BEAN WITH CONTAINER MANAGED PERSISTENCE #HOOSE ALL THAT APPLY R! R" R# R$ 3 SXEOLFJHW&XVWRPHU1XP lic abstractÓ - It must be thÒpeubvalue of whatrn and retu eld is... it IS after ever the ÞTER. all, a GET SXEOLFYRLGJHW&XVWRPHU1XP DEVWUDFWYRLGJHW&XVWRPHU1XP SXEOLFDEVWUDFWLQWJHW&XVWRPHU1XP 7HICH OF THE FOLLOWING ARE LEGAL ACCESSOR METHODS IN AN ENTITY BEAN WITH CONTAINER MANAGED PERSISTENCE #HOOSE ALL THAT APPLY R! R" R# R$ SXEOLFDEVWUDFWLQW*HW&XVWRPHU1XP SXEOLFDEVWUDFWLQWJHWFXVWRPHU1XP SXEOLFDEVWUDFWLQWJHW&XVWRPHU1XP SXEOLFDEVWUDFWLQWJHW&XVWRPHU1XP ^` - you have to follow the Java naming convention - not legal Jav a \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 74 PRFNH[DPDQVZHUV 4 7HICH ARE REQUIREMENTS FOR A #-0 ENTITY BEAN CLASS #HOOSE ALL THAT APPLY R! R" R# R$ 4HE CLASS MUST DElNE A ¿QDOL]H (spec: 190) METHOD - No, you want the default constructor 4HE SOURCE lLE MUST DElNE AT LEAST ONE CONSTRUCTOR 4HE CLASS MUST BE DECLARED PUBLIC AND ABSTRACT 4HE CLASS MUST IMPLEMENT DIRECTLY OR INDIRECTLY THE MDYD[HME(QWHUSULVH%HDQ INTERFACE - EntityBean extends EnterpriseBean R% !LL GETTER AND SETTER METHODS FOR THE BEANS ABSTRACT PERSISTENCE SCHEMA MUST BE ABSTRACT 5 7HICH ARE LEGAL DECLARATIONS FOR A #-0 BEANS EJB#REATE METHODS #HOOSE ALL THAT APPLY (spec: 192) R! SXEOLFYRLGHME&UHDWH%LJ&XVWRPHU WKURZV MDYD[HME&UHDWH([FHSWLRQ R" SXEOLF6WULQJHME&UHDWH$FFRXQW WKURZV MDYD[HME&UHDWH([FHSWLRQ R# VWDWLF6WULQJHME&UHDWH WKURZV MDYD[HME&UHDWH([FHSWLRQ R$ SXEOLFLQWHME&UHDWH WKURZV - it canÕt be: - static - void - Þnal e must returne th ary im pr th of type key MDYD[HME&UHDWH([FHSWLRQ R% SXEOLF¿QDO6WULQJHME&UHDWH MDYD[HME&UHDWH([FHSWLRQ 6 7HICH ARE LEGAL DECLARATIONS FOR A METHOD IN A #-0 BEAN #HOOSE ALL THAT APPLY R! SXEOLF$FFRXQWHME6HOHFW$FFW ORQJ[ WKURZV MDYD[HME)LQGHU([FHSWLRQ R" SXEOLFDEVWUDFW$FFWHME6HOHFW$FFW ORQJ[ WKURZV MDYD[HME)LQGHU([FHSWLRQ R# SXEOLF$FFRXQWHME3RVW&UHDWH $FFWNH\ WKURZV (spec: 192-193) - must be: abstract void - canÕt be static MDYD[HME&UHDWH([FHSWLRQ R$ SXEOLFYRLGHME3RVW&UHDWH $FFWNH\ WKURZV MDYD[HME&UHDWH([FHSWLRQ R% SXEOLFVWDWLFYRLGHME3RVW&UHDWH $FFWNH\ WKURZV MDYD[HME&UHDWH([FHSWLRQ &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 75 HQWLW\EHDQV\QFKURQL]DWLRQ 7 7HICH METHODS FROM THE %NTITY#ONTEXT INTERFACE CAN BE INVOKED FROM WITHIN THE SET%NTITY#ONTEXT METHOD #HOOSE ALL THAT APPLY R! R" R# R$ R% 8 JHW(-%/RFDO+RPH JHW&DOOHU,GHQWLW\ JHW&DOOHU3ULQFLSDO VHW5ROOEDFN2QO\ for these last - just too heearerlÕsy not a caller tx. three. T ontext just means a setEntityC g in the pool bean is goin HME6WRUH HME&UHDWH HME6HOHFW - with ejbRemove(), the bean wonÕt get an ejbPassivate() HME5HPRYH HME3DVVLYDWH (spec: 179) JHW(-%+RPH JHW(-%2EMHFW JHW&DOOHU3ULQFLSDO JHW8VHU7UDQVDFWLRQ VHW5ROOEDFN2QO\ VER ans can NEti - Entity be ty en e us ca be invoke this, use CMT beans must 7HAT IS TRUE FOR A #-0 BEAN IN THE READY STATE R! R" R# R$ (spec: 168-169) - store can be called anytime and is not called to transition the bean 7HICH METHODS FROM THE %NTITY#ONTEXT INTERFACE CAN BE INVOKED FROM WITHIN THE EJB#REATE METHOD #HOOSE ALL THAT APPLY R! R" R# R$ R% 10 JHW(-%2EMHFW 7HICH CAN BE CALLED ON A #-0 BEAN TO TRANSITION IT FROM THE READY STATE TO THE POOLED STATE #HOOSE ALL THAT APPLY R! R" R# R$ R% 9 (spec: 179) )TS HME/RDG )TS HME6WRUH CAN BE CALLED DIRECTLY AFTER EJB3TORE CAN BE CALLED DIRECTLY AFTER A BUSINESS METHOD /NE OF ITS BUSINESS METHODS CAN BE CALLED DIRECTLY AFTER EJB3TORE .ONE OF THE ABOVE (spec: 169) - The poinet anis:d load, stor hods business metd by the can be callein any Container order \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 76 PRFNH[DPDQVZHUV 11 7HICH METHODS FROM THE %NTITY#ONTEXT INTERFACE MUST ./4 BE INVOKED FROM WITHIN THE EJB,OAD METHOD #HOOSE ALL THAT APPLY R! R" R# R$ R% 12 JHW(-%+RPH JHW(-%2EMHFW JHW&DOOHU3ULQFLSDO JHW8VHU7UDQVDFWLRQ - for BMT only VHW5ROOEDFN2QO\ 7HICH METHOD CALLED ON A #-0 BEAN IS !,7!93 ASSOCIATED WITH A STATE CHANGE IN THE BEAN #HOOSE ALL THAT APPLY R! R" R# R$ R% 13 (spec: 180) HME/RDG (spec: 168-169) - not always! HME)LQG to the pool - bean goes back HME$FWLYDWH - bean comes out of the pool XQVHW(QWLW\&RQWH[W - bean goes from the pool to death HME5HPRYH 7HATS TRUE ABOUT AN #-0 ENTITY BEANS PRIMARY KEY #HOOSE ALL THAT APPLY ) (spec: 203, 275 R! 4HE BEANS PRIMARY KEY CLASS MUST PROVIDE A SUITABLE IMPLEMENTATION OF THE HASH#ODE AND EQUALS METHODS R" 7HEN SPECIFYING THE PRIMARY KEY IN THE DEPLOYMENT DESCRIPTOR ONLY THE lELD NAME MUST BE DECLARED - no, you need the type R# !LL lELDS IN THE PRIMARY KEY CLASS MUST BE DECLARED PUBLIC R$ !LL lELDS USED IN THE PRIMARY KEY MUST BE CONTAINER MANAGED lELDS 14 (OW MANY EJB#REATE METHODS CAN A #-0 ENTITY BEAN HAVE R! R" R# R$ R% (spec: 171) OR let You donÕt hateve betoans ea cr s nt ie cl TO MANY TO MANY &KDSWHU Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 6. Being an Entity Bean Return to Table of Contents Page 77 HQWLW\EHDQV\QFKURQL]DWLRQ 15 7HICH METHODS ARE ALWAYS INVOKED IN DIRECT RESPONSE TO A CLIENT OPERATION #HOOSE ALL THAT APPLY R! HME/RDG R" HME&UHDWH R# HME5HPRYH R$HME$FWLYDWH R% HME3DVVLYDWH R& VHW(QWLW\&RQWH[W 16 17 and Load, Passivaontete, xt setEntityC d by the can be callewhen it Container wants to 7HICH ADDITIONAL METHODS MIGHT THE CONTAINER CALL WHEN INVOKING EJB2EMOVE #HOOSE ALL THAT APPLY R! R" R# R$ R% HME)LQG HME/RDG HME6WRUH HME$FWLYDWH (spec: 171-172) (spec: 176) a bean has to be ready and loaded before it can do a remove - it may have cascading deletes to take care of HME3DVVLYDWH !T WHAT POINTS MUST THE CONTAINER ESTABLISH A #-0 BEANS PRIMARY KEY #HOOSE ALL THAT APPLY (spec: 175) R! BEFORE CALLINGQHZ,QVWDQFH R"BEFORE CALLING VHW(QWLW\&RQWH[W set in es the values you R# BEFORE CALLINGHME&UHDWH the Containsoerthusat in ejbPostCreate the ejbCreate y to use its key R$ BEFORE CALLINGHME3RVW&UHDWH bean is read 18 7HICH METHODS RUN IN THE TRANSACTION CONTEXT OF THE METHOD THAT CAUSES THEIR INVOCATION #HOOSE ALL THAT APPLY R! R" R# R$ R% R& (spec: 174-178) HME/RDG HME5HPRYH HME6HOHFW HME$FWLYDWH HME3DVVLYDWH VHW(QWLW\&RQWH[W \RXDUHKHUH Chapter 6. Being an Entity Bean Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Table of Contents Chapter 7. When Beans Relate.................................................................... 2 Section 7.1. OBJECTIVES: Entity Relationships: CMP and CMR................................................................................................. 3 Section 7.2. Beanifying your movie database................................................................................................................................. 4 Section 7.3. But we don't want to think in TABLES We want to think in CLASSES..................................................................... 5 Section 7.4. We need relationships between the Movie bean and the Director............................................................................. 7 Section 7.5. Why should the Director be a bean? Why can't it just be data?................................................................................. 8 Section 7.6. Relationships and multiplicity.................................................................................................................................... 9 Section 7.7. Multiplicity in Bean Classes...................................................................................................................................... 10 Section 7.8. Multiplicity affects return type !................................................................................................................................ 11 Section 7.9. You need a pair of abstract getters and setters for each CMP field (column values) and each CMR field (relationship with another entity)....................................................................................................................................................................... 12 Section 7.10. Defining your "abstract persistence schema" (virtual fields aren't enough).......................................................... 13 Section 7.11. The Container needs these TWO things to know you have a CMP field "title"....................................................... 13 Section 7.12. Persistent CMP fields in the DD.............................................................................................................................. 14 Section 7.13. there are no Dumb Questions.................................................................................................................................. 14 Section 7.14. Using relationships in your code............................................................................................................................. 15 Section 7.15. Defining relationships in your abstract persistence schema (in the DD)............................................................... 16 Section 7.16. Relationship definition for the Director-to-Movie relationship............................................................................. 17 Section 7.17. Sharpen your pencil................................................................................................................................................. 19 Section 7.18. Director-to-Movie relationship............................................................................................................................... 19 Section 7.19. Relationships can be one-way ( unidirectional)..................................................................................................... 20 Section 7.20. The dark side of a unidirectional (one-way) relationship...................................................................................... 21 Section 7.21. Cascade delete can propagate.................................................................................................................................. 22 Section 7.22. Director-to-Movie relationship............................................................................................................................... 22 Section 7.23. Movie-to-Trailer relationship................................................................................................................................. 22 Section 7.24. The abstract schema> element is in the <entity> element and the <ejb-relation> is in the <relationships..... 23 Section 7.25. MovieBean with a CMR code field.......................................................................................................................... 25 Section 7.26. The MovieBean's home interface............................................................................................................................ 27 Section 7.27. Mapping from abstract schema to a real database................................................................................................. 29 Section 7.28. Writing your portable queries, for select and finder methods............................................................................... 30 Section 7.29. EJB-QL for the MovieBean..................................................................................................................................... 31 Section 7.30. Using an optional WHERE clause.......................................................................................................................... 32 Section 7.31. Navigating to another related bean......................................................................................................................... 33 Section 7.32. Selecting a value rather than the whole bean......................................................................................................... 34 Section 7.33. EJB-QL SELECT..................................................................................................................................................... 35 Section 7.34. EJB-QL SELECT—when to use Object ()............................................................................................................... 36 Section 7.35. Sharpen your pencil................................................................................................................................................ 36 Section 7.36. What does it MEAN to return an abstract schema type?....................................................................................... 37 Section 7.37. SELECT and FROM are mandatory !..................................................................................................................... 38 Section 7.38. Identifiers................................................................................................................................................................ 39 Section 7.39. The WHERE clause................................................................................................................................................. 40 Section 7.40. The WHERE clause................................................................................................................................................. 41 Section 7.41. The problem with using Collection types................................................................................................................ 42 Section 7.42. Collections don't bark() !........................................................................................................................................ 43 Section 7.43. The IN operator lets you say "For an individual element IN the Collection..."..................................................... 44 Section 7.44. The BETWEEN expression..................................................................................................................................... 45 Section 7.45. The other "IN"......................................................................................................................................................... 46 Section 7.46. The IS EMPTY comparison expression.................................................................................................................. 47 Section 7.47. The IS NOT EMPTY comparison expression.......................................................................................................... 47 Section 7.48. The LIKE expression............................................................................................................................................... 48 Section 7.49. The NOT LIKE expression...................................................................................................................................... 49 Section 7.50. Relationship assignments........................................................................................................................................ 51 Section 7.51. If the multiplicity of the relationship field is ONE, it's a MOVE If the multiplicity is MANY, it's a COPY........... 52 Section 7.52. Exercise: Multiplicity and Assignments................................................................................................................. 52 Section 7.53. Solution: Multiplicity and Assignments................................................................................................................. 53 Section 7.54. COFFEE CRAM....................................................................................................................................................... 54 Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Section 7.55. COFFEE CRAM....................................................................................................................................................... 60 Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Page 1 Return to Table of Contents Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 2 entity relationships 5 4 4 :KHQ%HDQV5HODWH +DYH\RXKHDUG DERXW2QHWR0DQ\ UHODWLRQVKLSV",WKRXJKW PD\EHZHFRXOGWU\ WKDW 2KUHDOO\"7RREDG\RXGRQ·W KDYHD&RQWDLQHUWRPDQDJH\RXU UHIHUHQWLDOLQWHJULW\%HFDXVH ,ZRXOGMXVWORYHWRFDVFDGH GHOHWH\RXULJKWQRZ Copyright Safari Books Online #896963 (QWLW\EHDQVQHHGUHODWLRQVKLSV$Q2UGHUQHHGVD&XVWRPHU$/LQH,WHP QHHGVDQ2UGHU$Q2UGHUQHHGV/LQH,WHPV$0RYLHQHHGVD'LUHFWRU$'LUHFWRUQHHGV 0RYLHV$0RYLHQHHGV$FWRUV$Q$FWRUQHHGVWDOHQW(QWLW\EHDQVFDQKDYHFRQWDLQHU PDQDJHGUHODWLRQVKLSV &05 DQGWKH&RQWDLQHUWDNHVFDUHRIYLUWXDOO\HYHU\WKLQJ0DNH DQHZ0RYLHDQGJLYHLWD'LUHFWRU"7KDW'LUHFWRUDXWRPDWLFDOO\KDVRQHPRUH0RYLHLQKLV 0RYLHFROOHFWLRQ0DNHDQHZ/LQH,WHPWKDW¶VUHODWHGWRDQ2UGHU",I\RXDVNWKH&XVWRPHU WRVKRZ\RXKLV2UGHUVKLV2UGHUVZLOOVKRZWKHQHZ/LQH,WHP%HVWRIDOO\RXFDQXVH (-%4/WRZULWHSRUWDEOH WKLQNYHQGRUGDWDEVHLQGHSHQGHQW TXHULHV WKLVLVDQHZFKDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 3 H[DPREMHFWLYHV (QWLW\5HODWLRQVKLSV&03DQG&05 2IILFLDO ,GHQWLI\FRUUHFWDQGLQFRUUHFW VWDWHPHQWVRUH[DPSOHVDERXW SHUVLVWHQWUHODWLRQVKLSVUHPRYH SURWRFROVDQGDERXWWKHDEVWUDFW VFKHPDW\SHRID&03HQWLW\EHDQ ,GHQWLI\FRUUHFWDQGLQFRUUHFW VWDWHPHQWVRUH[DPSOHVDERXW UXOHVDQGVHPDQWLFVIRUUHODWLRQVKLS DVVLJQPHQWXSGDWLQJLQD&03EHDQ 0DWFKWKHQDPHZLWKDGHVFULSWLRQRI SXUSRVHRUIXQFWLRQDOLW\IRUHDFKRI WKHIROORZLQJHMEQDPH!DEVWUDFW VFKHPDQDPH!HMEUHODWLRQVKLSUROH! FPU¿HOG!FPU¿HOGW\SH!DQG UHODWLRQVKLSUROHVRXUFH! ,GHQWLI\FRUUHFWO\LPSOHPHQWHG GHSOR\PHQWGHVFULSWRUHOHPHQWVIRU D&03EHDQ LQFOXGLQJFRQWDLQHU PDQDJHGUHODWLRQVKLSV ,GHQWLI\FRUUHFWDQGLQFRUUHFWV\QWD[ IRUDQ(-%4/TXHU\LQFOXGLQJWKH 6(/(&7)520DQG:+(5(FODXVH ,GHQWLI\FRUUHFWDQGLQFRUUHFW VWDWHPHQWVRUH[DPSOHVDERXWWKH SXUSRVHDQGXVHRI(-%4/ ,GHQWLI\FRUUHFWDQGLQFRUUHFW FRQGLWLRQDOH[SUHVVLRQVEHWZHHQ H[SUHVVLRQVOLNHH[SUHVVLRQVDQG FRPSDULVRQH[SUHVVLRQV :KDWLWUHDOO\PHDQV <RXKDYHWRNQRZWKDWWKHFRQWDLQHUPDQDJHV UHODWLRQVKLSVEHWZHHQWZRHQWLW\EHDQVWKURXJK FRQWDLQHUPDQDJHGSHUVLVWHQW &05 ¿HOGVDQGWKDW &05¿HOGVPXVWEHGHVFULEHGLQWKHGHSOR\PHQW GHVFULSWRUXVLQJWKHUHODWLRQVKLSV!VHFWLRQ <RXKDYHWRXQGHUVWDQGWKHLPSOLFDWLRQVRIPXOWLSOLFLW\ LQDUHODWLRQVKLSDQGWKDWWKH&RQWDLQHUFDUHVZKHWKHU WKHUHODWLRQVKLSLVRQHWRRQHRQHWRPDQ\RUPDQ\ WRPDQ\<RXPXVWNQRZWKDWWKH&RQWDLQHUPDLQWDLQV WKHUHIHUHQWLDOLQWHJULW\RIWKHGDWDEDVHE\XVLQJWKH PXOWLSOLFLW\ZKHQGRLQJDVVLJQPHQWV)RUH[DPSOHLI WKHUHFDQEHRQO\RQH&XVWRPHUSHU2UGHULI\RXDVVLJQ DQ2UGHUWRD&XVWRPHUWKDW2UGHUFDQQRWH[LVWLQDQ\ RWKHU&XVWRPHU¶V2UGHUFROOHFWLRQ$QGLI\RXUHDVVLJQ WKDW2UGHUWRDGLIIHUHQW&XVWRPHUWKH2UGHULVPRYHG IURPRQH&XVWRPHU¶VFROOHFWLRQWRWKHRWKHU%XWLI\RX DVVLJQD&XVWRPHUZLWKWKUHHH[LVWLQJ2UGHUVWRDQRWKHU 2UGHUWKHUHIHUHQFHWRWKH&XVWRPHULVFRSLHGQRW PRYHGVRWKDWWKHUHIHUHQFHWRD&XVWRPHUWKDWWKH RWKHUWKUHHH[LVWLQJ2UGHUVKDYHLVQRWDIIHFWHG <RXKDYHWRNQRZWKDW(-%4/TXHULHVDUHGH¿QHGLQ WKHGHSOR\PHQWGHVFULSWRUDQGDUHIRU)LQGHUDQG6HOHFW PHWKRGVRQO\<RXKDYHWRNQRZWKHEDVLFV\QWD[RI(-% 4/DQGWKDW)520DQG6(/(&7DUHPDQGDWRU\EXW :+(5(LVRSWLRQDO <RXKDYHWRNQRZWKDWLI\RXXVHSDWKQDYLJDWLRQWRVD\ JHWWKHWLWOHRID0RYLH\RXFDQVD\PWLWOH DVVXPLQJ³P´ LVGHFODUHGDVWKHDEVWUDFWVFKHPDW\SHRI0RYLHDQG ³WLWOH´LVD&03¿HOGRIWKH0RYLH%HDQ %XWWKDW\RXFDQ¶W VD\GPRYLHVWLWOHZKHUH³G´LVD'LUHFWRUDQG³PRYLHV´LV D&05¿HOGWKDWLVD&ROOHFWLRQRIPRYLHV)RUWKDW\RX PXVWXVHWKH,1RSHUDWRUZLWKLQD)520FODXVH<RX¶OO VHHLW¶VDOOLQKHUH$QGPXFKVLPSOHUWKDQLWVRXQGV &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 4 HQWLW\EHDQUHODWLRQVKLSV *MIVQNaQVOaW]ZUW^QMLI\IJI[M )MAGINE YOU HAVE A MOVIE APPLICATION IT ISNT MAKING THE FOLKS AT IMDBCOM NERVOUS BUT IT WORKS FOR YOU ,ZDQWWRVHHDOOWKH PRYLHVZLWKGLUHFWRUVZKR DUHQRPRUHWKDQGHJUHHV DZD\IURP.HYLQ%DFRQDQGWKH JHQUHLV´URPDQWLFKRUURUµ 9OU CAN USE IT TO LOOK UP A MOVIE /NCE YOU HAVE A MOVIE YOU CAN USE IT TO LAUNCH THE MOVIE TRAILER 9OU CAN DO ALL KINDS OF SEARCHES TO SAY lND ALL SCI l MOVIES OR lND ALL ACTION MOVIES BY A SPECIlC DIRECTOR 7E CAN MAKE BEANS FOR -OVIE 4RAILER AND $IRECTOR "UT HOW DO THEY MAP TO THE DATABASE !ND HOW DO THEY RELATE TO ONE ANOTHER $IWHU a foreign key Director tabl into the e 0RYLH7DEOH 0RYLH,' 7LWOH *HQUH 'LUHFWRU,' <HDU &URXFKLQJ3L[HOV+LGGHQ0RXVH $FWLRQ 7KH)LIWK$UUD\(OHPHQW $FWLRQ 7KH5HWXUQRIWKH%HDQ4XHHQ )DQWDV\ /RUGRIWKH/RRSV 6FL¿ 'LUHFWRU7DEOH 7UDLOHU7DEOH 'LUHFWRU,' 2VFDU:LQQHU 'HJUHHV 1DPH 7UDLOHU,' )LOH1DPH 758( -LP<LQJVW &URXFKLQJ3L[HOV+LGGHQ0RXVH7UDLOHU )$/6( -HVVLFD6DQW 7KH)LIWK$UUD\(OHPHQW7UDLOHU )$/6( 6N\OHU6DIIRUG 7KH5HWXUQRIWKH%HDQ4XHHQ7UDLOHU )$/6( -RKQ'DZVRQ /RUGRIWKH/RRSV7UDLOHU Director doesnÕt have a reference to his movies. The trailerÕs prim ary key always matches th e MovieID \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 5 HQWLW\EHDQUHODWLRQVKLSV *]\_MLWV¼\_IV\\W\PQVSQV<)*4-; ?M_IV\\W\PQVSQV+4);;-; 7E KNOW WE CAN MAP TABLES TO BEAN CLASSES NO PROBLEM !LL THE COLUMNS BECOME PERSISTENT lELDS IN THE BEAN CLASS REPRESENTED BY ABSTRACT GETTERS AND SETTERS AND WERE GOOD TO GO %XCEPT IF THIS WERE A CLASS AND NOT A TABLE WE WOULDNT HAVE DESIGNED IT THAT WAY 7ED PROBABLY MAKE THE MOVIE CLASS A LOT MORE FRIENDLY AND USEFUL FOR EXAMPLE SO THAT A CLIENT COULD WORK WITH JUST THE MOVIE BEAN RATHER THAN HAVING TO GET REFERENCES TO ALL THREE BEANS 7LWOH *HQUH 'LUHFWRU,' <HDU &URXFKLQJ3L[HOV+LGGHQ0RXVH $FWLRQ 0RYLH,' 7KH)LIWK$UUD\(OHPHQW $FWLRQ 7KH5HWXUQRIWKH%HDQ4XHHQ )DQWDV\ /RUGRIWKH/RRSV 6FL¿ 'LUHFWRU,' 2VFDU:LQQHU 'HJUHHV 1DPH But this does Look what thenÕt look very client-friendly reference to al client would have to do... . a reference l three tables ! If the cl get a directorID frtooma movie, they have to geietnt has get a referenc the movie, then use that the e to a Directo to r. 0RYLH 7UDLOHU,' )LOH1DPH &URXFKLQJ3L[HOV+LGGHQ0RXVH7UDLOHU 758( -LP<LQJVW 7KH)LIWK$UUD\(OHPHQW7UDLOHU )$/6( -HVVLFD6DQW 7KH5HWXUQRIWKH%HDQ4XHHQ7UDLOHU )$/6( 6N\OHU6DIIRUG /RUGRIWKH/RRSV7UDLOHU )$/6( -RKQ'DZVRQ 7UDLOHU JHW7UDLOHU,' JHW)LOH1DPH JHW0RYLH,' JHW7LWOH JHW*HQUH JHW'LUHFWRU,' JHW<HDU 'LUHFWRU JHW'LUHFWRU,' JHW2VFDU:LQQHU JHW'HJUHHV JHW1DPH a straight mapping from tables to classes &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 6 HQWLW\EHDQUHODWLRQVKLSV :RXOGQ·WLWEHGUHDP\LIWKHUH ZHUHDZD\WRXVHFRQWDLQHUPDQDJHG SHUVLVWHQFHEXWKDYHFODVVHVWKDWGLGQ·W KDYHWRORRNH[DFWO\OLNHWKHWDEOHV"6RWKDW DFOLHQWFRXOGJHWDPRYLHDQGDVNWKHPRYLH IRUDUHDO'LUHFWRUUHIHUHQFHQRWMXVWWKH GLUHFWRU·VIRUHLJQNH\\HDKWKDWZRXOG EHJUHDWEXWLW·VSUREDEO\MXVWD IDQWDV\ \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 7 HQWLW\EHDQUHODWLRQVKLSV ?MVMMLZMTI\QWV[PQX[JM\ _MMV\PM5W^QMJMIV IVL\PM, QZMK\WZJMIV 7E WANT THE -OVIE BEAN TO HAVE A REFERENCE TO ITS MATCHING $IRECTOR BEAN AND WE WANT TO DO ALL SORTS OF SEARCHES AGAINST THE -OVIE BEAN AND HAVE THE QUERIES USE THE $IRECTOR BEANS DATA AS WELL )N OTHER WORDS WE WANT TO MAKE IT EASY ON THE CLIENT AND ON THE DEVELOPER TO THINK IN A MORE NATURAL WAY RATHER THAN IN THE DATABASE EFlCIENT WAY THAT WAS USED TO DESIGN THE SCHEMA OF THE DATABASE 7HO WANTS THAT 2EMEMBER THIS IS THE // WORLD AND RELATIONAL DATABASES WHILE CRUCIAL TO YOUR BUSINESS ARE SO 7E WANT TO USE DATABASES WE JUST DONT WANT TO THINK LIKE DATABASES )F YOURE ONE OF THE LUCKY ONES WHO GETS TO USE AN // DATABASE AND ASSUMING THAT DATABASE STILL SOMEHOW MANAGES TO PERFORM WELL ENOUGH FOR YOUR NEEDS THEN YOU CAN JUST SMILE SMUGLY DURING THIS lRST SECTION LQVWHDGRIWKLV 0RYLH JHW0RYLH,' JHW7LWOH JHW*HQUH JHW'LUHFWRU,' JHW<HDU ZHZDQWWKLV we donÕt want to keep just the DirectorÕs foreign key 0RYLH JHW0RYLH,' JHW7LWOH JHW*HQUH We want reference at Director en o a tity BEAN JHW'LUHFWRU 'LUHFWRU JHW<HDU JHW'LUHFWRU,' JHW2VFDU:LQQHU JHW'HJUHHV JHW1DPH 0RYLH+RPH FUHDWH ILQG%\3ULPDU\.H\ ILQG%\*HQUH OLVW$OO0RYLHV OLVW0RYLHV%\'LUHFWRU$QG*HQUH ILQG0RYLHV%\.HYLQ'HJUHHV JHW0RYLHV or have WeÕll let the Direct ie beans. ov M of a Collection 7ERE NOT GOING TO USE THE 4RAILER TABLE AND BEAN AFTER THIS ALTHOUGH YOULL SEE IT IN CODE TO KEEP THE EXAMPLE CLEANER !DDING THE 4RAILER BEAN WOULDNT ADD ANY NEW COMPLEXITY TO THE APPLICATION THOUGH )F YOU CAN SET UP ONE BEAN TO BEAN RELATIONSHIP YOU CAN SET UP OTHERS &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Page 8 Return to Table of Contents HQWLW\EHDQUHODWLRQVKLSV ?Pa[PW]TL\PM,QZMK\WZJMIJMIV' ?PaKIV¼\Q\R][\JMLI\I' 7HY NOT HAVE THE -OVIE BEAN SIMPLY GO TO THE DATABASE USING THE $IRECTORS FOREIGN KEY STORED AS A PERSISTENT lELD IN THE -OVIE BEAN AND GET THE $IRECTORS DATA "UT IF YOU MAKE THE DIRECTOR A BEAN AS WELL YOU GET TO THINK /.,9 IN OBJECTS EXCEPT DURING DEPLOYMENT WHEN YOU DO HAVE TO MAP FROM YOUR BEANS TO YOUR TABLES !ND YOU GET ALL THE BENElTS OF CONTAINER MANAGED PERSISTENCE AND SYNCHRONIZATION )MAGINE IF SOMEONE CALLS A SETTER METHOD TO CHANGE THE DIRECTORS OSCAR WINNING STATUS )F YOURE MANAGING THIS YOUD HAVE TO GET A *$"# CONNECTION AND SYNCHRONIZE THE DATABASE YOURSELF !ND OF COURSE YOUD HAVE TO KNOW WHEN IS THE RIGHT TIME ETC "UT IF ITS DIRECTOR IS ALSO A BEAN YOU CALL A SET7INNER METHOD AND YOURE DONE /H AND THERES ANOTHER COOL THING 9OU MIGHT DECIDE FOR EXAMPLE TO BANISH A SPECIlC DIRECTOR FROM YOUR DATABASE BECAUSE YOU THINK HIS MOVIES SUCK IN AN UNRECOVERABLE WAY .O PROBLEM DELETING THAT DIRECTOR OBVIOUSLY "UT THEN WHAT HAPPENS TO THE MOVIES (OW CAN YOU HAVE MOVIES IN THE DATABASE THAT DONT HAVE A DIRECTOR 7HAT IS THE DATABASE IS ALREADY SET UP IN SUCH A WAY THAT YOU ARENT ALLOWED TO HAVE A NULL VALUE FOR THE DIRECTOR COLUMN )N OTHER WORDS WHAT IF A MOVIE CANNOT EXIST IN THE DATABASE WITHOUT A DIRECTOR 9OU CAN HANDLE THIS BRAINLESSLY BY SETTING UP THE -OVIE TO $IRECTOR BEAN RELATIONSHIP IN SUCH A WAY THAT WHEN YOU DELETE A DIRECTOR FROM THE DATABASE ALL OF HIS MOVIES ARE AUTOMATICALLY DELETED 9OU DO THIS WITH ONE SIMPLE TAG IN THE DEPLOYMENT DESCRIPTOR AND 0//& 3OMEBODY CALLS REMOVE ON A DIRECTOR BEAN AND REMOVE WILL AUTOMATICALLY BE CALLED ON ALL OF THE MOVIE ENTITY BEANS THAT HAVE A REFERENCE TO THIS DIRECTOR AS THE VALUE OF THEIR DIRECTOR PERSISTENT lELD ,IERWKWKH'LUHFWRUDQG WKH0RYLHDUHHQWLW\EHDQV \RXQHYHUKDYHWRZRUU\ DERXWV\QFKURQL]LQJDQ\ RIWKH0RYLH·VUHODWHG GDWDLIVRPHERG\XVHV WKH0RYLHEHDQWRFKDQJH VRPHWKLQJDERXWWKH'LUHFWRU HYHU\WKLQJ·VWDNHQFDUHRIE\ WKH&RQWDLQHU <RXFDQHYHQVHWWKLQJVXS IRUDFDVFDGHGHOHWHVRLI \RXUHPRYHD'LUHFWRUDOO WKH'LUHFWRU·VPRYLHVZLOOEH UHPRYHGDXWRPDWLFDOO\ \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 9 UHODWLRQVKLSPXOWLSOLFLW\ :MTI\QWV[PQX[IVLU]T\QXTQKQ\a $ 7VM\W7VM (DFK0RYLH 2QH0RYLH % PI[ 2QH7UDLOHU (DFK7UDLOHU 7VM\W5IVa 2QH'LUHFWRU 2QH'LUHFWRU PI[ PI[ PI[ 0DQ\0RYLHV (DFK0RYLH 5IVa\W5IVa $Q$FWRU 0DQ\$FWRUV PI[UILM PI[ 0DQ\0RYLHV $0RYLH &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 10 HQWLW\EHDQUHODWLRQVKLSV 5]T\QXTQKQ\aQV*MIV+TI[[M[ (ERES HOW IT WORKS WITH OUR BEAN CODE 4HIS MULTIPLICITY NOTATION IS THE ONLY 5-, LIKE THING YOU HAVE TO KNOW ON THE EXAM 7UDLOHU JHW7UDLOHU,' JHW)LOH1DPH JHW0RYLH (DFK0RYLH KDVH[DFWO\ RQH7UDLOHU (DFK7UDLOHU KDVH[DFWO\ RQH0RYLH 0RYLH JHW0RYLH,' JHW7LWOH JHW*HQUH (DFK$FWRU KDV]HURRU PRUH0RYLHV JHW'LUHFWRU JHW7UDLOHU (DFK'LUHFWRU KDV]HURRU PRUH0RYLHV $FWRU (DFK0RYLH KDVKDVRQHRU JHW$FWRU,' PRUH$FWRUV JHW1DPH JHW$JHQW JHW7DOHQW JHW0RYLHV 'LUHFWRU (DFK0RYLH KDVH[DFWO\ RQH'LUHFWRU JHW'LUHFWRU,' JHW2VFDU:LQQHU JHW'HJUHHV JHW1DPH JHW0RYLHV 4O READ THIS YOU HAVE TO FOLLOW THE ARROW TO ITS DESTINATION )N THE 0ERSON TO 0ETS RELATIONSHIP 0ERSON HAS A MULTIPLICITY OF /.% BUT 0ETS HAS A MULTIPLICITY OF -!.9 WHICH COULD BE ZERO 4O lND OUT HOW MANY 0ETS A 0ERSON CAN HAVE YOU HAVE TO FOLLOW THE ARROW OUT OF 0ERSON AND INTO 0ETS 4HE NUMBER CLOSEST TO THE CLASS IS THE MULTIPLICITY OF THAT CLASS TO OTHERS )N OTHER WORDS HOW MANY OF THAT TYPE DOES THE OTHER TYPE HAVE 3HUVRQ JHW3HWV this number says how many PERSON objects does each PET have this numberany says how m each 3HWV PETS does ve ha N O S R PE JHW3HUVRQ \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 11 UHODWLRQVKLSPXOWLSOLFLW\ 5]T\QXTQKQ\aINNMK\[ZM\]ZV\aXM ! -OVIE HAS ONE $IRECTOR 7HEN YOU CALL GET$IRECTOR YOU GET BACK ONE $IRECTOR 3O THE RETURN TYPE OF GET$IRECTOR IS A $IRECTOR "UT A $IRECTOR HAS MANY MOVIES SO WHEN YOU CALL GET-OVIES YOU GET BACK A #OLLECTION OF -OVIES 0XOWLSOLFLW\ PDQ\ 0XOWLSOLFLW\ RQH 'LUHFWRU 0RYLH &ROOHFWLRQJHW0RYLHV 'LUHFWRUJHW'LUHFWRU )N THE -OVIE TO $IRECTOR RELATIONSHIP THE MULTIPLICITY OF -OVIE IS MANY AND THE MULTIPLICITY OF $IRECTOR IS ONE Movie has a multiplicity of ÒmanyÓ in its relationship with Director. That does NOT mean that Movie has ÒmanyÓ Directors... it means that Director has ÒmanyÓ Movies! So Director has a getMovies() that returns a Collection of Movies. And Director has a multiplicity of ÒoneÓ, which means that Movie will return just one Director. ! MULTIPLICITY OF ONE MEANS THE OBJECT YOURE RELATED TO HOLDS JUST ONE OF YOU $IRECTOR HAS A MULTIPLICITY OF ONE SO THE -OVIE OBJECT RELATED TO $IRECTOR RETURNS JUST A SINGLE $IRECTOR BEAN /+ TECHNICALLY THE COMPONENT INTERFACE OF $IRECTOR BUT YOU KNOW WHAT WE MEAN BY NOW ! MULTIPLICITY OF MANY MEANS THE OBJECT YOURE RELATED TO HOLDS A #OLLECTION OF YOU -OVIE HAS A MULTIPLICITY OF MANY SO THE $IRECTOR OBJECT RELATED TO -OVIE RETURNS A #OLLECTION OF -OVIE BEANS &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 12 HQWLW\EHDQUHODWLRQVKLSV ,MNQVQVO^QZ\]ITNQMTL[NWZXMZ[Q[\MV\ LI\INQMTL[IVLZMTI\QWV[PQXNQMTL[ )N THE PREVIOUS CHAPTER WE LOOKED AT DElNING CONTAINER MANAGED lELDSYOU PUT IN A PAIR OF ABSTRACT GETTERS AND SETTERS 7E SAID A CONTAINER MANAGED lELD EXISTS SIMPLY BECAUSE YOU HAVE A GETTER AND SETTER FOR IT )T WORKS THE SAME WAY WITH CONTAINER MANAGED RELATIONSHIP #-2 lELDS 9OU DElNE A PAIR OF ABSTRACT GETTERS AND SETTERS BUT RATHER THAN SETTING AND RETURNING A VALUE THAT MAPS TO A COLUMN IN A TABLE YOU SET AND RETURN A REFERENCE TO ANOTHER ENTITY BEAN OR A #OLLECTION THAT WILL HOLD REFERENCES TO THE ENTITY BEAN 4HE RESTRICTIONS FOR #-2 lELDS ARE THAT THEY CAN REFER ONLY TO THE LOCAL COMPONENT INTERFACE OF THE ENTITY BEAN AND THAT IF THE METHOD RETURNS A #OLLECTION THE DECLARED TYPE CAN BE ONLY #OLLECTION OR 3ET NOT -AP ,IST ETC 4HE TERMINOLOGY IS A LITTLE CONFUSING BECAUSE BOTH #-0 AND #-2 lELDS ARE CONTAINER MANAGED PERSISTENT lELDS 4HEY MIGHT HAVE CALLED IT #-0#$ CONTAINER MANAGED PERSISTENT COLUMN DATA lELDS AND #-02 CONTAINER MANAGED PERSISTENT RELATIONSHIP lELDS /F COURSE PERSISTENCE ISNT RESTRICTED TO JUST RELATIONAL DATABASE SO WERE USING THE TERM hCOLUMNv A LITTLE LOOSELY "UT IN %*" ALL CONTAINER MANAGED PERSISTENT lELDS WERE #-0 lELDS BECAUSE THERE WAS NO CONCEPT OF PERSISTENT RELATIONSHIPS IN %*" #-0 AW]VMMLIXIQZWNIJ[\ZIK\OM\\MZ[IVL[M\\MZ[NWZ MIKP+58NQMTLKWT]UV^IT]M[IVLMIKP+5:NQMTL ZMTI\QWV[PQX_Q\PIVW\PMZMV\Q\a $&05ILHOGLVDOZD\V DQRWKHUHQWLW\EHDQ·V ORFDOLQWHUIDFHW\SHRUD &ROOHFWLRQRIWKHP ,ILW·VD&ROOHFWLRQ LWPXVWEHHLWKHU MDYDXWLO&ROOHFWLRQRU MDYDXWLO6HW <P SV &03ÀHOG SXEOLFDEVWUDFW6WULQJJHW7LWOH SXEOLFDEVWUDFWYRLGVHW7LWOH 6WULQJJ 7KHRQO\GLIIHUHQFH EHWZHHQD&03ILHOGDQG D&05ILHOGLVWKH7<3( make sure the and return typeargument match &05 UHODWLRQVKLS ÀHOG SXEOLFDEVWUDFW'LUHFWRUJHW'LUHFWRU SXEOLFDEVWUDFWYRLGVHW'LUHFWRU 'LUHFWRUG e of a CMR The argument and return typ r Entity the ano ays Òvirtual ÞeldÓ is alw ion. lect Col a or e, typ l loca beanÕs Director must be the local component interface for the Director bean. \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 13 WKHDEVWUDFWSHUVLVWHQFHVFKHPD ,MNQVQVOaW]Z¹IJ[\ZIK\XMZ[Q[\MVKM [KPMUIº^QZ\]ITNQMTL[IZMV¼\MVW]OP 4O DElNE PERSISTENT lELDS AND RELATIONSHIPS YOU NEED TO CREATE AN ABSTRACT PERSISTENCE SCHEMA $ElNING THE VIRTUAL lELDS ISNT ENOUGH 9OUR ABSTRACT PERSISTENCE SCHEMA IS A COMBINATION OF YOUR VIRTUAL lELDS IN YOUR BEAN CLASS PLUS SOME THINGS YOU WRITE IN THE DEPLOYMENT DESCRIPTOR 4HE WAY YOU DESCRIBE #-0 lELDS IN THE $$ IS SIMPLE AND STRAIGHTFORWARD 7ITH #-2 YOULL HAVE TO DO A LITTLE MORE TO SET THINGS UP INCLUDING DESCRIBING THE MULTIPLICITY FOR EACH OF THE TWO PARTICIPANTS IN A RELATIONSHIP "UT WELL GET TO #-2 lELDS IN JUST A MINUTE &OR NOW WELL START WITH JUST #-0 lELDS <PM+WV\IQVMZVMML[\PM[M<?7\PQVO[\W SVW_aW]PI^MI+58NQMTL¹\Q\TMº ,QWKH'' $QDEVWUDFWSHUVLVWHQWVFKHPD LVDFRPELQDWLRQRIVRPHVWXII \RXSXWLQWKHGHSOR\PHQW GHVFULSWRUSOXV\RXUDEVWUDFW JHWWHUVDQGVHWWHUV 7RJHWKHUWKH\WHOOWKH &RQWDLQHUKRZWRPDQDJH\RXU EHDQ·VSHUVLVWHQFHLQFOXGLQJ ERWKSHUVLVWHQW),(/'6 FDOOHG&03ILHOGV DQG SHUVLVWHQW5(/$7,216+,36 FDOOHG&05ILHOGV HQWLW\! HMEQDPH!0RYLH%HDQHMEQDPH! ORFDOKRPH!KHDG¿UVW0RYLH+RPHORFDOKRPH! ORFDO!KHDG¿UVW0RYLHORFDO! HMEFODVV!KHDG¿UVW0RYLH%HDQHMEFODVV! DEVWUDFWVFKHPDQDPH!0RYLH6FKHPDDEVWUDFWVFKHPDQDPH! FPS¿HOG! ¿HOGQDPH!WLWOH¿HOGQDPH! FPS¿HOG! HQWLW\! ,QWKHEHDQFODVV SXEOLFDEVWUDFW6WULQJJHW7LWOH SXEOLFDEVWUDFWYRLGVHW7LWOH 6WULQJJ &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 14 HQWLW\EHDQUHODWLRQVKLSV 8MZ[Q[\MV\+58NQMTL[QV\PM,, HQWLW\! GLVSOD\QDPH!0RYLH%HDQGLVSOD\QDPH! iner that IT HMEQDPH!0RYLH%HDQHMEQDPH! tell the Contaur persistence yo g ORFDOKRPH!KHDG¿UVW0RYLH+RPHORFDOKRPH! is managin eanÓ) ÒB ORFDO!KHDG¿UVW0RYLHORFDO! (as opposed to HMEFODVV!KHDG¿UVW0RYLH%HDQHMEFODVV! SHUVLVWHQFHW\SH!&RQWDLQHUSHUVLVWHQFHW\SH! fully-qualiÞed class SULPNH\FODVV!MDYDODQJ6WULQJSULPNH\FODVV! na me for the Þeld UHHQWUDQW!)DOVHUHHQWUDQW! that represents the FPSYHUVLRQ![FPSYHUVLRQ! primary key DEVWUDFWVFKHPDQDPH!0RYLH6FKHPDDEVWUDFWVFKHPDQDPH! FPS¿HOG! that ¿HOGQDPH!JHQUH¿HOGQDPH! schema soity type it r u o y e Nam w which ent FPS¿HOG! st be you kno nts. This name mu FPS¿HOG! represe the DD!! ¿HOGQDPH!PRYLH,'¿HOGQDPH! unique in FPS¿HOG! FPS¿HOG! ¿HOGQDPH!\HDU¿HOGQDPH! iner told the Conta FPS¿HOG! which it ll Now that youÕve te e, ag man FPS¿HOG! WHICH Þeldsdstois the Special One... ¿HOGQDPH!WLWOH¿HOGQDPH! of those Þel y Þeld. FPS¿HOG! the primary ke SULPNH\¿HOG!PRYLH,'SULPNH\¿HOG! HQWLW\! WKHUH DUH QR 7KHQDPLQJ FRQYHQWLRQLV 5(48,5(' DLURIDEVWUDFW DS DYH <RXPXVWK YHWKH JHWWHUVDQGVHWWHUVWKDWKD ´DQG ¿UVWFKDUDFWHUDIWHUWKH³JHW &03 ³VHW´FDSLWDOL]HGEXWLQWKH XVWDUW ¿HOGGH¿QLWLRQLQWKH''\R YLQJRII ZLWKDORZHUFDVHOHWWHUOHD ZRUGV WKH³JHW´DQG³VHW´,QRWKHU KH LQW HOG 3¿ &0 WKH QH \RXGH¿ FH ''DVLILWZHUHDUHDOLQVWDQ UH(-% YDULDEOHIROORZLQJWKHROGS WLRQ -DYD%HDQVQDPLQJFRQYHQ 'XPE 4XHVWLRQV Q: (OW DOES THE #ONTAINER KNOW THE TYPE OF THE PERSISTENT lELD ) SEE A DElNITION FOR THE PRIMARY KEY TYPE PRIM KEY CLASS BUT ) DONT SEE ANYTHING FOR THE OTHER #-0 lELDS A: 4HE #ONTAINER FIGURES IT OUT BASED ON THE RETURN TYPE AND ARGUMENT OF THE ABSTRACT GETTERS AND SETTERS IN THE BEAN CLASS !ND THEY HAD BETTER MATCH )F YOU HAVE A GET,IMIT.UM THAT RETURNS AN INT YOU BETTER HAVE A SET,IMIT.UMINT I THAT TAKES AN INT \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 15 HQWLW\EHDQUHODWLRQVKLSV =[QVOZMTI\QWV[PQX[QVaW]ZKWLM )N YOUR BEAN CODE YOU USE YOUR VIRTUAL RELATIONSHIP lELDS JUST AS YOU WOULD ANY OTHER GETTER AND SETTER 4HE ONLY DIFFERENCE BETWEEN USING A REAL lELD IE AN INSTANCE VARIABLE DECLARED IN YOUR CLASS AND A VIRTUAL lELD IS THAT YOU CAN ACCESS YOUR VIRTUAL lELDS ONLY BY CALLING THE ABSTRACT GETTERS AND SETTERS WHICH THE #ONTAINER IMPLEMENTS AT DEPLOY TIME <RXFDQXVH\RXUYLUWXDO UHODWLRQVKLSILHOGVMXVW DV\RXZRXOGDQ\RWKHU YLUWXDOILHOGE\FDOOLQJ WKHDEVWUDFWJHWWHUVWKDW <28GHILQHGEXWZKLFK WKH&RQWDLQHULPSOHPHQWV 4HE ONLY NEW RESTRICTION IS THAT A RELATIONSHIP lELD CAN BE ONLY A LOCAL COMPONENT INTERFACE TYPE 9OU CANT HAVE A #-2 RELATIONSHIP lELD THAT USES A BEANS 2EMOTE INTERFACE 9LUWXDOÀHOGLQWKHEHDQFODVV SXEOLFDEVWUDFW'LUHFWRUJHW'LUHFWRU SXEOLFDEVWUDFWYRLGVHW'LUHFWRU 'LUHFWRUG type of a The argument and return ays another alw is CMR Òvirtual ÞeldÓ be a ST MU it and e, typ n bea Entity virtual a e hav Õt can You !! local interface ace. erf int ote Þeld to the entityÕs Rem 7KHW\SHRIDUHODWLRQVKLS ILHOG0867EHWKHORFDO FRPSRQHQWLQWHUIDFHRI WKHHQWLW\ ([SRVHGEXVLQHVVPHWKRGLQWKHEHDQFODVV SXEOLF'LUHFWRUJHW0RYLH'LUHFWRU ^ UHWXUQJHW'LUHFWRU ` $&05ILHOGZLOO $/:$<6EHDORFDO FRPSRQHQWLQWHUIDFHW\SH ector to a Now we can return the Dir ct getter. tra abs the ing call client, by SXEOLF6WULQJJHW0RYLH'LUHFWRU1DPH ^ UHWXUQJHW'LUHFWRU JHW'LUHFWRU1DPH ` We can also do the wo client, if we donÕt wantrk on behalf of the Director reference to thto expose the real e client. In fact, thatÕs how weÕre going to Director is a local int do it, because the is going to have Remoteerface, but the Movie clients. &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 16 HQWLW\EHDQUHODWLRQVKLSV ,MNQVQVOZMTI\QWV[PQX[QVaW]ZIJ[\ZIK\ XMZ[Q[\MVKM[KPMUIQV\PM,, #-0 lELD DElNITIONS ARE SO EASY 9OU DECLARE A CMP lELD AND GIVE IT A NAME THAT MATCHES ONE OF YOUR GETTERSETTER PAIRS FOLLOWING THE NAMING RULE OF DROPPING THE hGETv AND hSETv AND BEGINNING THE #-0 lELD NAME WITH A LOWER CASE LETTER "UT #-2 IS MORE INVOLVED 9OU CANT BE IN A RELATIONSHIP WITH ONLY YOURSELF SO A RELATIONSHIP ALWAYS INCLUDES TWO BEANS ionship has Every relawto beans. exactly t 'HÀQHDQHMEUHODWLRQEHWZHHQWZREHDQV $ 'HÀQHDQHMEUHODWLRQVKLSUROHIRURQHEHDQ . how many of Multiplicity of this bean (i.e ticipant have) par PXOWLSOLFLW\IRUWKLVEHDQ this type will the OTHER about? VRXUFHIRUWKLVEHDQ Which bean are we really talking in the bean class FPUÀHOGIRUWKLVEHDQ Must match the Òvirtual ÞeldÓ FDVFDGHGHOHWHIRUWKLVEHDQ Only if THIS bean wa to be deleted when its partner is delnts eted. $ 'HÀQHDQHMEUHODWLRQVKLSUROHIRUWKHVHFRQGEHDQ PXOWLSOLFLW\IRUWKLVEHDQ VRXUFHIRUWKLVEHDQ FPUÀHOGIRUWKLVEHDQ FDVFDGHGHOHWHIRUWKLVEHDQ for the Same stuffrticipant in second paionship the relat \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 17 UHODWLRQVKLSVLQWKHGHSOR\PHQWGHVFULSWRU 5HODWLRQVKLSGHÀQLWLRQIRUWKH 'LUHFWRUWR0RYLHUHODWLRQVKLS 'LUHFWRU 0RYLH &ROOHFWLRQJHW0RYLHV 'LUHFWRUJHW'LUHFWRU 'HÀQHDQHMEUHODWLRQEHWZHHQWZREHDQV Each <ejb-relation> descri HMEUHODWLRQ! They REALLY should hav bes one pair of beans... one relationship. e named this <ejb-relations hip> $ 'HÀQHDQHMEUHODWLRQVKLSUROHIRURQHEHDQ HMEUHODWLRQVKLSUROH! HMEUHODWLRQVKLSUROHQDPH!'LUHFWRU%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\IRUWKLVEHDQ PXOWLSOLFLW\!2QHPXOWLSOLFLW\! VRXUFHIRUWKLVEHDQ This is a made-up name! ItÕs choose to call the ROLE. simply what you We could have called it ÒDirectorRoleÓ or any thi participant weÕre talking ng that tells us which about. Remember, this does NOT say how many participant the Director bean will have of the other ... it says how many of THIS bean the OTHER part Movie will have only ONE Director. ner will have. UHODWLRQVKLSUROHVRXUFH! HMEQDPH!'LUHFWRU%HDQHMEQDPH! UHODWLRQVKLSUROHVRXUFH! We can use any role-name we want, but sooner or later the Container must know EXACTLY which bean weÕre talking about. Put the beanÕs <ejb-name> which is just a label telling the Container where to Þnd this beanÕs real deÞnition in the <enterprisebeans> section of the DD. This MUST match an <ejb-name> in the DD. FPUÀHOGIRUWKLVEHDQ FPU¿HOG! FPU¿HOGQDPH!PRYLHVFPU¿HOGQDPH! This must match a Òvirtual ÞeldÓ in the bean class for getMovies() / setMovies () FPU¿HOGW\SH!MDYDXWLO&ROOHFWLRQFPU¿HOGW\SH! FPU¿HOG! FDVFDGHGHOHWHIRUWKLVEHDQ QRWVSHFL¿HGIRUWKLVEHDQ HMEUHODWLRQVKLSUROH! Use <cmr-Þeld-type> ONLY if other partner has a MANY relationshipthe to this bean, which means you will hold more one, so you need a Collection. You canthan say java.util.Set (your only two choices)also &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 18 HQWLW\EHDQUHODWLRQVKLSV $ 'HÀQHDQHMEUHODWLRQVKLSUROHIRUWKHVHFRQGEHDQ HMEUHODWLRQVKLSUROH! HMEUHODWLRQVKLSUROHQDPH!0RYLH%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\IRUWKLVEHDQ PXOWLSOLFLW\!0DQ\PXOWLSOLFLW\! VRXUFHIRUWKLVEHDQ UHODWLRQVKLSUROHVRXUFH! HMEQDPH!0RYLH%HDQHMEQDPH! UHODWLRQVKLSUROHVRXUFH! Remember, this is just a name for your own (or the App Assembler or a toolÕs) use. It means nothing in your Java code. This means Director will have MANY of type (Movie). Se we expect that Directorthis have a CMR Þeld with a Collection type will rather than a single Movie This MUST match the <ejb-name> for where you deÞned the Movie entity bean in the <enterprise-beans> section of the DD. FPUÀHOGIRUWKLVEHDQ FPU¿HOG! FPU¿HOGQDPH!GLUHFWRUFPU¿HOGQDPH! FPU¿HOG! FDVFDGHGHOHWHIRUWKLVEHDQ FDVFDGHGHOHWH! HMEUHODWLRQVKLSUROH! nÕt have a Look... no type! Webedocause it isnÕt a e> yp <cmr-Þeld-t , the only reason Collection. In facteld -type> tag is -Þ mr <c thereÕs a pe is a Colty ld Þe for when the to distinguish lection, and you havellection. <cmrCo or t between Se Þeld-type> Put this in if you want THIS bean to be deleted whenever its partner is deleted. HMEUHODWLRQ! \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 19 HQWLW\EHDQUHODWLRQVKLSV 6KDUSHQ \RXU SHQFLO (ERES THE RELATIONSHIP $$ FOR $IRECTOR TO -OVIE BUT WEVE LEFT A FEW THINGS OUT 3EE IF YOU CAN lLL THEM IN CORRECTLY WITHOUT LOOKING ON THE PREVIOUS PAGES 'LUHFWRU 0RYLH &ROOHFWLRQJHW0RYLHV 'LUHFWRUJHW'LUHFWRU , QZMK\WZ\W5W^QMZMTI\QWV[PQX UHODWLRQVKLSV! HMEUHODWLRQ! HMEUHODWLRQVKLSUROH! HMEUHODWLRQVKLSUROHQDPH!HMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!PXOWLSOLFLW\! UHODWLRQVKLSUROHVRXUFH! HMEQDPH!'LUHFWRU%HDQHMEQDPH! UHODWLRQVKLSUROHVRXUFH! FPU¿HOG! FPU¿HOGQDPH!FPU¿HOGQDPH! FPU¿HOG! HMEUHODWLRQVKLSUROH! HMEUHODWLRQVKLSUROH! HMEUHODWLRQVKLSUROHQDPH!0RYLH%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!PXOWLSOLFLW\! FDVFDGHGHOHWH! UHODWLRQVKLSUROHVRXUFH! HMEQDPH!HMEQDPH! UHODWLRQVKLSUROHVRXUFH! HMEUHODWLRQVKLSUROH! HMEUHODWLRQ! UHODWLRQVKLSV! &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 20 HQWLW\EHDQUHODWLRQVKLSV 'RHVWKLVPHDQWKDWZKHQ DEHDQLVLQDUHODWLRQVKLSLW 0867KDYHDÀHOGIRUWKHRWKHU EHDQ":KDWLI,ZDQW0RYLHWRKDYH D7UDLOHUEXW,GRQ·WZDQWDQ\ERG\ WRXVH7UDLOHUWRJHWWRD0RYLH" 7UDLOHU 0RYLH QRUHIHUHQFHWR0RYLH 7UDLOHUJHW7UDLOHU :MTI\QWV[PQX[KIVJMWVM_Ia ] VQLQZMK\QWVIT 9OU CAN HAVE A RELATIONSHIP BETWEEN TWO BEANS BUT HAVE A #-2 lELD IN ONLY ONE OF THE TWO BEANS &OR EXAMPLE IF YOU SET UP A RELATIONSHIP BETWEEN A -OVIE AND ITS 4RAILER A ONE TO ONE RELATIONSHIP AND YOU DONT WANT CLIENTS TO USE A 4RAILER TO GET TO A -OVIE JUST LEAVE THE #-2 lELD FOR -OVIE OUT OF THE 4RAILER BEAN 3IMPLE AS THAT )N THAT CASE THE 4RAILER"EAN WONT KNOW ANYTHING ABOUT THE -OVIE"EAN EVEN THOUGH THEYRE BOTH PARTNERS IN A RELATIONSHIP HMEUHODWLRQVKLSUROH! HMEUHODWLRQVKLSUROHQDPH!7UDLOHU%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!2QHPXOWLSOLFLW\! FDVFDGHGHOHWH! UHODWLRQVKLSUROHVRXUFH! HMEQDPH!7UDLOHU%HDQHMEQDPH! UHODWLRQVKLSUROHVRXUFH! FPU¿HOG! Leave the cmr-Þeld out of a FPU¿HOGQDPH!FPU¿HOGQDPH! FPU¿HOG! relationship role if you donÕt want HMEUHODWLRQVKLSUROH! this bean to have partner bean. a reference to its \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 21 GLUHFWLRQDOLW\LQUHODWLRQVKLSV <PMLIZS[QLMWNI]VQLQZMK\QWVITWVM_IaZMTI\QWV[PQX +H\ZH·UH LQDUHODWLRQVKLS WRJHWKHU <HV ZHDUH 1RZH·UHQRW 1RZH·UHQRW 7UDLOHU 0RYLH $UH 'RQ·W\RXWKLQN,·G NQRZLI,ZHUHLQD UHODWLRQVKLS",GRQ·W HYHQOLNH\RX $UH 1RW 0RYLH 7UDLOHU 0RYLH 7UDLOHU 7UDLOHU 0RYLH <RX·UHLQWKH''ZLWK PHEDE\$QG,KDYHDYLUWXDO ÀHOGIRU\RXHYHQWKRXJK\RX GRQ·WKDYHRQHIRUPH 2KJUHDW,·PLQD UHODWLRQVKLSDJDLQVW P\ZLOOZLWKQR NQRZOHGJH 7KDW·VQRWHYHQWKH %(67SDUW\RX·UHVHW WRFDVFDGHGHOHWHVRWKDW ZKHQ,GLH\RXGLH6R\RX EHWWHUWDNHJRRGFDUH RIPH 0RYLH 7UDLOHU 0RYLH 7UDLOHU &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 22 HQWLW\EHDQUHODWLRQVKLSV +I[KILMLMTM\MKIVXZWXIOI\M 0RYLH 'LUHFWRU 7UDLOHU ,QZMK\WZ\W5W^QM ZMTI\QWV[PQX 5W^QM\W<ZIQTMZ ZMTI\QWV[PQX 'LUHFWRU FDQ·WFDVFDGHGHOHWH 0RYLH FRXOGFDVFDGHGHOHWHEXWGRHVQ·W ZDQWWR 0RYLHFDVFDGHGHOHWH! 7UDLOHUFDVFDGHGHOHWH! <RXFDQ·WVHWFDVFDGHGHOHWHIRUWKH'LUHFWRU EHDQLQWKH'LUHFWRUWR0RYLHUHODWLRQVKLS EHFDXVH0RYLHKDVDPXOWLSOLFLW\RI0DQ\7KLQN DERXWLWLIVRPHRQHGHOHWHVD0RYLHVKRXOG WKDW'LUHFWRUEHGHOHWHG"2IFRXUVHQRW7KH 'LUHFWRUFRXOGVWLOOEHWKH'LUHFWRURIRWKHU PRYLHVWKDWVWLOOH[LVW %XWWKH'LUHFWRUKDVDPXOWLSOLFLW\RI2QH ZLWKWKH0RYLHVRWKH0RYLHEHDQFDQVD\´, FDQKDYHRQO\RQH'LUHFWRUVRLIKHJRHV,JRµ %HFDXVHWKLVLVDRQHWRRQHUHODWLRQVKLSERWK SDUWQHUVFRXOGVHWDFDVFDGHGHOHWH%XWZH ZDQWLWWRJRLQRQO\RQHGLUHFWLRQZKHUH GHOHWLQJWKH0RYLHGHOHWHVWKH7UDLOHUEXW QRWWKHUHYHUVH-XVWEHFDXVHWKH7UDLOHU LVGHOHWHGGRHVQRWPHDQZHZDQWWKH0RYLH GHOHWHGEXWWKDW·VGHWHUPLQHGE\RXUEXVLQHVV ORJLFDQGQRW(-%UXOHV D'LUHFWRUUHPRYH 'LUHFWRU 7UDLOHU 0RYLH D0RYLHUHPRYH 'LUHFWRU 0RYLH 7UDLOHU D7UDLOHUUHPRYH 'LUHFWRU 0RYLH 7UDLOHU \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Page 23 Return to Table of Contents UHODWLRQVKLSVLQWKHGHSOR\PHQWGHVFULSWRU <PMIJ[\ZIK\[KPMUIQ[QV\PM$MV\Q\a&MTMUMV\IVL\PM $MRJZMTI\QWV&Q[QV\PM$ZMTI\QWV[PQX[&MTMUMV\ HMEMDU! GLVSOD\QDPH!0RYLH-DUGLVSOD\QDPH! HQWHUSULVHEHDQV! HQWLW\! GLVSOD\QDPH!0RYLH%HDQGLVSOD\QDPH! HMEQDPH!0RYLH%HDQHMEQDPH! ORFDOKRPH!KHDG¿UVW0RYLH+RPHORFDOKRPH! ORFDO!KHDG¿UVW0RYLHORFDO! HMEFODVV!KHDG¿UVW0RYLH%HDQHMEFODVV! SHUVLVWHQFHW\SH!&RQWDLQHUSHUVLVWHQFHW\SH! SULPNH\FODVV!MDYDODQJ6WULQJSULPNH\FODVV! UHHQWUDQW!)DOVHUHHQWUDQW! FPSYHUVLRQ![FPSYHUVLRQ! DEVWUDFWVFKHPDQDPH!0RYLH6FKHPDDEVWUDFWVFKHPDQDPH! FPS¿HOG! The abstract schema for persisten ¿HOGQDPH!JHQUH¿HOGQDPH! FPS¿HOG! Þelds is deÞned HERE in the <enttity> FPS¿HOG! element under <enterprise-beans>, wher e ¿HOGQDPH!PRYLH,'¿HOGQDPH! all beans are described. FPS¿HOG! FPS¿HOG! missing!! There ¿HOGQDPH!\HDU¿HOGQDPH! WHOA! Notice whatfoÕs r Director. You FPS¿HOG! is NO <cmp-Þeld>eld> elements inside FPS¿HOG! donÕt list <cmr-Þ n. TheyÕre deÞned ¿HOGQDPH!WLWOH¿HOGQDPH! the <entity> sectiotionships> section FPS¿HOG! ONLY in the <rela SULPNH\¿HOG!PRYLH,'SULPNH\¿HOG! (next page) VHFXULW\LGHQWLW\! XVHFDOOHULGHQWLW\!XVHFDOOHULGHQWLW\! VHFXULW\LGHQWLW\! HQWLW\! HQWLW\! GLVSOD\QDPH!'LUHFWRU%HDQGLVSOD\QDPH! HMEQDPH!'LUHFWRU%HDQHMEQDPH! ORFDOKRPH!KHDG¿UVW'LUHFWRU+RPHORFDOKRPH! ORFDO!KHDG¿UVW'LUHFWRUORFDO! HMEFODVV!KHDG¿UVW'LUHFWRU%HDQHMEFODVV! SHUVLVWHQFHW\SH!&RQWDLQHUSHUVLVWHQFHW\SH! SULPNH\FODVV!MDYDODQJ6WULQJSULPNH\FODVV! UHHQWUDQW!)DOVHUHHQWUDQW! FPSYHUVLRQ![FPSYHUVLRQ! DEVWUDFWVFKHPDQDPH!'LUHFWRU6FKHPDDEVWUDFWVFKHPDQDPH! FPS¿HOG! ¿HOGQDPH!QDPH¿HOGQDPH! FPS¿HOG! &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 24 HQWLW\EHDQUHODWLRQVKLSV FPS¿HOG! ¿HOGQDPH!ZLQQHU¿HOGQDPH! FPS¿HOG! FPS¿HOG! ¿HOGQDPH!GLUHFWRU,'¿HOGQDPH! FPS¿HOG! FPS¿HOG! ¿HOGQDPH!GHJUHHV¿HOGQDPH! FPS¿HOG! SULPNH\¿HOG!GLUHFWRU,'SULPNH\¿HOG! VHFXULW\LGHQWLW\! XVHFDOOHULGHQWLW\!XVHFDOOHULGHQWLW\! VHFXULW\LGHQWLW\! HQWLW\! HQWHUSULVHEHDQV! UHODWLRQVKLSV! HMEUHODWLRQ! ise-beans> section AFTER the <enterhaprve been described), we (and all the beans ips> section that uses e have the <relationsh to refer BACK to th <ejb-name> elementssection <enterprise-beans> HMEUHODWLRQVKLSUROH! HMEUHODWLRQVKLSUROHQDPH!'LUHFWRU%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!2QHPXOWLSOLFLW\! UHODWLRQVKLSUROHVRXUFH! This name MUST match someth ing in this HMEQDPH!'LUHFWRU%HDQHMEQDPH! DD, described in an <entity> elem UHODWLRQVKLSUROHVRXUFH! ent in the <enterprise-beans> section FPU¿HOG! FPU¿HOGQDPH!PRYLHVFPU¿HOGQDPH! FPU¿HOGW\SH!MDYDXWLO&ROOHFWLRQFPU¿HOGW\SH! FPU¿HOG! <cmr-Þeld-type> is only when your partner (not HMEUHODWLRQVKLSUROH! necessa rily YOU) has a multiplicity of Many. This can be ONLY Map, etc.) Collection or Set (NOT List, HMEUHODWLRQVKLSUROH! HMEUHODWLRQVKLSUROHQDPH!0RYLH%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!0DQ\PXOWLSOLFLW\! FDVFDGHGHOHWH! MovieBean says, ÒYou can te UHODWLRQVKLSUROHVRXUFH! me when you delete my Directdele HMEQDPH!0RYLH%HDQHMEQDPH! orÓ UHODWLRQVKLSUROHVRXUFH! FPU¿HOG! FPU¿HOGQDPH!GLUHFWRUFPU¿HOGQDPH! FPU¿HOG! HMEUHODWLRQVKLSUROH! use the Director has a HMEUHODWLRQ! UHODWLRQVKLSV! No <cmr-Þeld-type> beca tionship, so we return multiplicity of One in this relaection of Directors. only one Director, not a Coll \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 25 WKH0RYLH%HDQFRGH 5W^QM*MIVKWLM_Q\PI+5:NQMTL SDFNDJHKHDG¿UVW LPSRUWMDYD[HME LPSRUWMDYD[QDPLQJ LPSRUWMDYDXWLO SXEOLFDEVWUDFWFODVV0RYLH%HDQLPSOHPHQWV(QWLW\%HDQ^ SULYDWH(QWLW\&RQWH[WFRQWH[W SXEOLF6WULQJHME&UHDWH 6WULQJPRYLH,'6WULQJWLWOHLQW\HDU6WULQJJHQUH 6WULQJGLUHFWRU,' WKURZV&UHDWH([FHSWLRQ^ VHW0RYLH,' PRYLH,' VHW7LWOH WLWOH VHW<HDU \HDU VHW*HQUH JHQUH UHWXUQQXOO ` our tent Þelds, by calling Here we set our perWesis must NOT try to reference abstract setters. RELATIONSHIPS. That has to our own persistent been inserted, and that happens wait until WE have eate() completes. only AFTER ejbCr SXEOLFYRLGHME3RVW&UHDWH 6WULQJPRYLH,'6WULQJWLWOHLQW\HDU6WULQJJHQUH 6WULQJGLUHFWRU,' WKURZV&UHDWH([FHSWLRQ^ WU\^ ,QLWLDO&RQWH[WFW[ QHZ,QLWLDO&RQWH[W 'LUHFWRU+RPHGLU+RPH 'LUHFWRU+RPH FW[ORRNXS ³MDYDFRPSHQYHME'LUHFWRU+RPH´ 'LUHFWRUGLU GLU+RPH¿QG%\3ULPDU\.H\ GLUHFWRU,' VHW'LUHFWRU GLU `FDWFK ([FHSWLRQH[ Now itÕs safe to assign our persistent relationship ^KDQGOHH[FHSWLRQ` Þeld for Director. We lookup the Director (using ` the directorID parameter from create) and call our abstract setter to assign this Director. SXEOLF6WULQJHME+RPH/LVW0RYLHV%\'LUHFWRU$QG*HQUH 6WULQJGLU,'6WULQJJHQUH ^ 6WULQJOLVW QXOO WU\^ &ROOHFWLRQF HME6HOHFW*HW0RYLHV%\'LUHFWRU$QG*HQUH GLU,'JHQUH ,WHUDWRULW FLWHUDWRU ZKLOH LWKDV1H[W ^ r the client to ss method fo the OLVW LWQH[W A home busine all the movies that Þtthat d of ho t ` lis et m a get s. an ejbSelect `FDWFK ([FHSWLRQH[ ^H[SULQW6WDFN7UDFH ` query. We usole lection of String objecte UHWXUQOLVW th C t a le s e rn w tu , d re etho ` ase code! ing a select m ab By us te all the dat Container wri &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 26 HQWLW\EHDQUHODWLRQVKLSV SXEOLF6WULQJHME+RPH/LVW$OO0RYLHV ^ 6WULQJOLVW QXOO WU\^ ess method &ROOHFWLRQF HME6HOHFW*HW$OO0RYLHV Another home businme thod to Þnd ,WHUDWRULWD FLWHUDWRU t ec sel a s that use ZKLOH LWDKDV1H[W ^ returns a t ec sel is all movies. Th 0RYLHPRYLH 0RYLH LWDQH[W ovie objects, M Collection of actuhal it and pull out OLVW ³³PRYLHJHW0RYLH7LWOH ug ro th lk so we wa ` can return a the titles, so thatcliweent. `FDWFK ([FHSWLRQH[ ^KDQGOHH[FHSWLRQ` big String to the UHWXUQOLVW ` SXEOLF6WULQJJHW0RYLH7LWOH ^ Two exposed business methods from UHWXUQJHW7LWOH the component interface... ` SXEOLF6WULQJJHW0RYLH'LUHFWRU1DPH ^ UHWXUQJHW'LUHFWRU JHW'LUHFWRU1DPH ` SXEOLFDEVWUDFW6WULQJJHW0RYLH,' SXEOLFDEVWUDFWYRLGVHW0RYLH,' 6WULQJL SXEOLFDEVWUDFW6WULQJJHW7LWOH SXEOLFDEVWUDFWYRLGVHW7LWOH 6WULQJW SXEOLFDEVWUDFWLQWJHW<HDU SXEOLFDEVWUDFWYRLGVHW<HDU LQW\ SXEOLFDEVWUDFW6WULQJJHW*HQUH SXEOLFDEVWUDFWYRLGVHW*HQUH 6WULQJJ SXEOLFDEVWUDFW'LUHFWRUJHW'LUHFWRU SXEOLFDEVWUDFWYRLGVHW'LUHFWRU 'LUHFWRUG We have FIVE virtual Þelds: four CMP Þelds and one CMR Þeld for Director. SXEOLFDEVWUDFW&ROOHFWLRQHME6HOHFW*HW$OO0RYLHV WKURZV)LQGHU([FHSWLRQ SXEOLFDEVWUDFW&ROOHFWLRQHME6HOHFW*HW0RYLHV%\'LUHFWRU$QG*HQUH 6WULQJGLU6WULQJ JHQUH WKURZV)LQGHU([FHSWLRQ SXEOLFYRLGXQVHW(QWLW\&RQWH[W SXEOLFYRLGHME/RDG ^` SXEOLFYRLGHME6WRUH ^` SXEOLFYRLGHME$FWLYDWH ^` SXEOLFYRLGHME3DVVLYDWH ^` SXEOLFYRLGHME5HPRYH ^` ^` The two select methods are declared as abstract methods. The Collection doesnÕt say WHAT the methods will return... but weÕll tell the Container that one will return a Collection of Movies and one a Collection of Strings. SXEOLFYRLGVHW(QWLW\&RQWH[W (QWLW\&RQWH[WFW[ ^ FRQWH[W FW[ ` ` The rest are regular old callback methods. \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Page 27 Return to Table of Contents WKH0RYLH+RPH5HPRWHLQWHUIDFH <PM5W^QM*MIV¼[PWUMQV\MZNIKM LPSRUWMDYD[HME LPSRUWMDYDXWLO LPSRUWMDYDUPL SXEOLFLQWHUIDFH0RYLH+RPH5HPRWHH[WHQGV(-%+RPH^ SXEOLF0RYLH5HPRWHFUHDWH 6WULQJPRYLH,'6WULQJWLWOHLQW\HDU6WULQJJHQUH 6WULQJGLUHFWRU,' WKURZV&UHDWH([FHSWLRQ5HPRWH([FHSWLRQ SXEOLF0RYLH5HPRWH¿QG%\3ULPDU\.H\ 6WULQJNH\ WKURZV)LQGHU([FHSWLRQ 5HPRWH([FHSWLRQ SXEOLF&ROOHFWLRQ¿QG%\*HQUH 6WULQJJHQUH WKURZV)LQGHU([FHSWLRQ5HPRWH([FHSWLRQ SXEOLF6WULQJOLVW$OO0RYLHV WKURZV)LQGHU([FHSWLRQ5HPRWH([FHSWLRQ SXEOLF6WULQJOLVW0RYLHV%\'LUHFWRU$QG*HQUH 6WULQJGLUHFWRU,'6WULQJJHQUH WKURZV)LQGHU([FHSWLRQ5HPRWH([FHSWLRQ SXEOLF&ROOHFWLRQ¿QG0RYLHV%\.HYLQ'HJUHHV LQWGHJUHHV WKURZV)LQGHU([FHSWLRQ 5HPRWH([FHSWLRQ ` Whoa! There are three Þn werenÕt in the bean cla der methods that ss... remember, the Cont ainer implements your Þnder me ANYTHING about Þnde thods--you donÕt put rs You put them ONLY in in your bean class code. the home. (well, thatÕs not complet ely have to tell the Contain true...somewhere you er HOW to implement th Þnders, which brings us e to the next page...) art with methods all st in the ss ne si bu e m eyÕre The ho HomeÓ when th the preÞx Òejb e home, they donÕt have bean class. In th that preÞx. &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 28 HQWLW\EHDQUHODWLRQVKLSV +RZWKHKHFN GRHVWKH&RQWDLQHU NQRZKRZWRLPSOHPHQW WKHVHOHFWPHWKRGV" $QG ZKDW DERXWWKH)LQGHU PHWKRGV",FDQXQGHUVWDQG KRZLWFDQGRÀQG%\3ULPDU\.H\ EHFDXVHLWDOUHDG\NQRZVWKH NH\EXWZKDWDERXWWKH RWKHUV" $QGZKLOH,·P DWLW+2:'2(6,7.12: :+$7<285),(/'60$372 ,17+('$7$%$6("+RZGR\RX OLQN\RXUDEVWUDFWVFKHPDWRUHDO GDWDEDVHWDEOHVDQGFROXPQV" \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 29 GDWDEDVHWRHQWLW\PDSSLQJ 5IXXQVONZWUIJ[\ZIK\[KPMUI\WIZMITLI\IJI[M 3OONER OR LATER YOU HAVE TO TELL THE #ONTAINER HOW AND WHERE TO MANAGE YOUR PERSISTENT DATA 4HIS HAPPENS OUTSIDE THE DEPLOYMENT DESCRIPTOR IN A VENDOR SPECIlC WAY 4HAT MEANS OF COURSE THAT YOURE NOT TESTED ON IT IN THE EXAM %VERY VENDOR HAS THEIR OWN MECHANISM BUT YOULL USUALLY GET SOME SORT OF A '5) WHERE IF YOURE LUCKY YOU CAN TELL THE #ONTAINER TO CONNECT TO A SPECIlC DATABASE AND THEN YOULL DRAG AND DROP OR DRAW CONNECTIONS FROM #-0 lELDS TO COLUMNS IN ONE OR MORE TABLES "ECAUSE THIS MAPPING HAPPENS OUTSIDE OF %*" IN THE SERVER THERES NOT MUCH WE CAN SAY ABOUT IT 4HE PART WERE MOST INTERESTED IN IS MAPPING FROM ABSTRACT QUERIES TO 2%!, QUERIES AND THAT HAPPENS IN A VERY COOL WAY USING A VENDOR INDEPENDENT QUERY LANGUAGE JUST FOR YOUR ENTITY BEAN QUERIES %*" 1, 7ELL LOOK AT THAT ON THE NEXT PAGE DEVWUDFWVFKHPDQDPH!0RYLH6FKHPDDEVWUDFWVFKHPDQDPH! FPS¿HOG! ¿HOGQDPH!JHQUH¿HOGQDPH! FPS¿HOG! FPS¿HOG! ¿HOGQDPH!PRYLH,'¿HOGQDPH! FPS¿HOG! FPS¿HOG! ¿HOGQDPH!\HDU¿HOGQDPH! FPS¿HOG! FPS¿HOG! ¿HOGQDPH!WLWOH¿HOGQDPH! FPS¿HOG! SULPNH\¿HOG!PRYLH,'SULPNH\¿HOG! Notice that weÕre not ma column (the one that ke pping the Director ID ep the Director table) be s the foreign key into cause we donÕt HAVE a persistent CMP Þeld fo r that, when we can have the Director ID. Why do the real Director, as an entity bean, instead? Th at comes in, but we donÕt Õs where our CMR Þeld map that to a database. Relationships happen by ma the <relationships> part tching up two BEANS in of the DD. 0RYLH,' 7LWOH *HQUH 'LUHFWRU,' <HDU &URXFKLQJ3L[HOV+LGGHQ0RXVH $FWLRQ 7KH)LIWK$UUD\(OHPHQW $FWLRQ 7KH5HWXUQRIWKH%HDQ4XHHQ )DQWDV\ /RUGRIWKH/RRSV 6FL¿ &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 30 HQWLW\EHDQUHODWLRQVKLSV ?ZQ\QVOaW]ZXWZ\IJTMY]MZQM[NWZ[MTMK\IVLNQVLMZUM\PWL[ 5HPHPEHU,·PD SURJUDPPHUVRWKHOHVV, NQRZWKHKDSSLHU,DP:LWK (-%4/,GRQ·WKDYHWRNQRZ DQ\WKLQJDERXWWKHUHDOGDWDEDVH ZKHUHP\EHDQZLOOHQGXS UXQQLQJ %*" 1, IS A PORTABLE QUERY LANGUAGE THAT LETS YOU WRITE 31, LIKE STATEMENTS WRITE INTO THE DEPLOYMENT DESCRIPTOR 9OU PUT THEM IN THE 8-, AND WITHOUT HAVING TO KNOW ANYTHING ABOUT THE REAL DATABASE !S A "EAN 0ROVIDER YOU CAN HAPPILY WRITE ALL THE QUERIES YOU WANT TRUSTING THAT THE $EPLOYER WILL IN THE END MAP YOUR #-0 lELDS TO REAL TABLES AND DATA !LL WE CARE ABOUT HERE IS WRITING OUR QUERIES TO MATCH THE METHODS WEVE CHOSEN TO EXPOSE TO THE CLIENT IN THE HOME AND COMPONENT INTERFACES 3ELECT METHODS ARE HERE SIMPLY TO MAKE YOUR LIFE EASIER BY LETTING THE #ONTAINER BUILD THE REAL DATABASE ACCESS CODE FROM YOUR QUERIES 3O THE PROCESS IS USUALLY SOMETHING LIKE 7RITE A HOME BUSINESS METHOD )MPLEMENT THE HOME BUSINESS METHOD TO CALL THE EJB3ELECT METHOD THAT DOES THE REAL DATA ACCESS $ECLARE THE ABSTRACT SELECT METHOD IN YOUR BEAN CLASS 7RITE THE %*" 1, FOR THE SELECT QUERY \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 31 (-%4/TXHULHV -2*94NWZ\PM5W^QM*MIV ,ETS LOOK AT THE %*" 1, FOR ALL THE QUERIES THE -OVIE"EAN NEEDS $ONT WORRY IF YOURE NOT UNDERSTANDING ALL OF IT AFTER WE WALK THROUGH THE EXAMPLES WELL EXPLORE THE DIFFERENT PIECES IN DETAIL AND WORK OUT THE SYNTAX )F YOU KNOW 31, MOST OF THIS WILL BE FAMILIAR ALTHOUGH THERE ARE FEW DIFFERENCES YOULL SEE )F YOU DONT KNOW 31, THATS /+ 7ELL GET YOU STARTED WRITING QUERIES AND YOU CAN USE THE SPEC TO LEARN MORE ABOUT THE %*" 1, LANGUAGE 4HE EXAM EXPECTS YOU TO KNOW JUST THE BASICS OF %*" 1, ,QWKHEHDQFODVV turns a The method re ovie objects M Collection of SXEOLFDEVWUDFW&ROOHFWLRQHME6HOHFW*HW$OO0RYLHV WKURZV)LQGHU([FHSWLRQ ,QWKHWKHGHSOR\PHQWGHVFULSWRU 6(/(&72%-(&7 P )5200RYLH6FKHPDP This says ÒReturn what eve representing.Ó In this ex r ÔmÕ is am to represent MovieSch ple, ÔmÕ is said ema which really means the Movie entit y be Þrst part says, ÒGive me an type. So this Movie beansÓ. ... ÒLet ÔmÕ ring a variable the Movie la ec d e lik st a from This is ju abstract schem e, ÒLet represent the n REALLY think of it lik type. ca e bean. But you e Movie component interfac means, Y th L t L A en E es R ÔmÕ repr ie beans. So it ov M s, d or w r an. In othe ent a Movie be ÒLet ÔmÕ repres :KDWLWUHDOO\VD\V ³*LYHPHDOOWKH0RYLHEHDQV´ &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 32 HQWLW\EHDQUHODWLRQVKLSV =[QVOIVWX\QWVIT?0-:-KTI][M 4HIS EXAMPLE CONSTRAINS THE OBJECTS RETURNED FROM THE SEARCH BY ADDING A 7(%2% CLAUSE THAT SAYS WHAT EXTRA CRITERIA THE OBJECTS HAVE TO MEET BEFORE THEY QUALIFY FOR hTHINGS THAT CAN BE RETURNED FROM THE QUERYv 4HE 7(%2% CLAUSE IS YOUR WAY OF SAYING h$ONT JUST GIVE ME !.9 OLD -OVIE ) WANT /.,9 THE MOVIES THATv ,QWKHEHDQFODVV turns a The method reMovie objects of n Collectio SXEOLFDEVWUDFW&ROOHFWLRQHME6HOHFW*HW$OO0RYLHV ,QWKHWKHGHSOR\PHQWGHVFULSWRU WKURZV)LQGHU([FHSWLRQ the CMP Þeld in ÒgenreÓ must behich means you can Movie bean, w re() / setGenre() Þnd a getGen the bean class method pair in this means the Þrst parameter to the method the This says we want the t en es pr re Õ ly Ôm to on Let type of whatever ÔmÕ tubean pe ovies returned rns Movie bean ty restrict the M CMP Þeld ÒgenreÓ out to be... e those where th t parameter of the rs Þ matches the he Ò1Ó means the Þrst T d select metho Òm.genreÓ uses the dot he T parameter. a ÒpathÓ ator to make iented er op on ti ga vi na in an object-or that navigates the left hand operand :KDWLWUHDOO\VD\V eÑ way. ItÕs simpl e right hand is a th d ³*LYHPHDOOWKH0RYLHEHDQVWKDWDUHLQWKH an almost is a type, JHQUHPDWFKLQJWKH¿UVWSDUDPHWHUWRWKH that type. ItÕs ntax to of d el Þ r be sy em m a HME6HOHFW*HW$OO0RYLHVPHWKRG´ ing regular Jav exactly like us enreÓ, where genre is an .g say, ÒmyMovie (in this case, a virtual le ab ri va instance .) d el Þ nt persiste 6(/(&72%-(&7 P )5200RYLH6FKHPDP:+(5(PJHQUH " \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 33 SDWKQDYLJDWLRQ 6I^QOI\QVO\WIVW\PMZZMTI\MLJMIV 4HIS EXAMPLE IS FOR A lNDER METHOD SO YOU WONT lND IT IN THE BEAN CLASS REMEMBER YOU DECLARE ABSTRACT METHODS FOR ONLY THE SELECT METHODS NOT THE lNDERS 4HE #ONTAINER USES YOUR HOME INTERFACE TO lGURE OUT WHAT THE lNDERS ARE AND THEN ITS UP TO YOU TO WRITE THE %*" 1, FOR THE lNDERS 9OU CAN USE THE DOT OPERATOR TO NAVIGATE NOT JUST TO A #-0 lELD IN THE BEAN BUT TO ANOTHER lELD WITHIN A BEAN REFERENCED IN A #-2 lELD )N OTHER WORDS )F -OVIE HAS A REFERENCE TO A $IRECTOR AND THEYRE IN A RELATIONSHIP YOU CAN USE THE -OVIE TO GET TO THE $IRECTOR TO ASK FOR A #-0 VALUE FROM THE $IRECTOR ,QWKHKRPHLQWHUIDFH SXEOLF&ROOHFWLRQ¿QG0RYLHV%\.HYLQ'HJUHHV LQWGHJUHHV WKURZV)LQGHU([FHSWLRQ 5HPRWH([FHSWLRQ ,QWKHWKHGHSOR\PHQWGHVFULSWRU ns Òthe m.director mea ectorÕ in CMR Þeld Ôdir Ó the Movie bean ÒdegreesÓ is persistent Þa the Directoreld in bean. 6(/(&72%-(&7 P )5200RYLH6FKHPDP:+(5(PGLUHFWRUGHJUHHV " This says that we want objects returned wh type is whatever ÔmÕ turnsose ou t to be representing... o you know? ... and what dthat ÔmÕ t It turns ou ovie bean means the M restrict the Movies returned to only those where the MovieÕs Director has a CMP ÒdegreesÓ Þeld with a value that matches the Þrst parameter of the Þnder. :KDWLWUHDOO\VD\V ³*LYHPHDOOWKH0RYLHEHDQVWKDWKDYHD'LUHFWRU ZKRVHGHJUHHVPDWFKZKDWWKHFOLHQWVHQWLQWR WKH¿QGHUPHWKRG´ 25 ³,ZDQWWRJHW0RYLHVEDFNEXWRQO\WKRVH0RYLHV GLUHFWHGE\VRPHRQHZKRLVDVSHFL¿FQXPEHU RIGHJUHHVDZD\IURP.HYLQ%DFRQ´ 6L[GHJUHHVIURP.HYLQ%DFRQLVDSRSXODUJDPH ZKHUH\RXWU\WRWDNHD+ROO\ZRRGW\SHDQGVHH KRZPDQ\FRQQHFWLRQVLWWDNHVWRJHWWKDWSHUVRQ OLQNHGWR.HYLQ%DFRQµ &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 34 HQWLW\EHDQUHODWLRQVKLSV ;MTMK\QVOI^IT]MZI\PMZ\PIV\PM_PWTMJMIV 4HIS EXAMPLE IS FOR A HOME BUSINESS METHOD THAT TAKES TWO PARAMETERS AND RETURNS NOT -OVIES BUT 3TRINGS ,QWKHKRPH LQWHUIDFH ng, with all rns a single Stri query. tu re d ho et m s Thi that match the the Movie titles SXEOLF6WULQJHME+RPH/LVW0RYLHV%\'LUHFWRU$QG*HQUH 6WULQJGLU,'6WULQJJHQUH ,QWKHWKHGHSOR\PHQWGHVFULSWRU this is different! WeÕre returning just Movie titles, not Movie objects 6(/(&7PWLWOH)5200RYLH6FKHPDP the ÒgenreÓ m.genre means the Movie bean in CMP Þeld :+(5(PGLUHFWRUGLUHFWRU,' "$1'PJHQUH " This restricts the matc Movies with a Director hes to only those equal to the Þrst para who has an ID meter to the method AND whose Movie genre value equals the second parameter to the method :KDWLWUHDOO\VD\V ³*LYHPHEDFN0RYLHWLWOHV 6WULQJV WKDWKDYH WKHGLUHFWRUDQGJHQUH,VSHFL¿HGLQWKHPHWKRG DUJXPHQWV´ \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 35 (-%4/6(/(&7 -2*94;-4-+< .OW THAT YOUVE SEEN SOME QUERIES WELL LOOK AT SOME OF THE DETAILS AND RULES 4XHU\'RPDLQV 4HE DOMAIN OF A QUERY IS A SINGLE DEPLOYMENT DESCRIPTOR 3O THAT MEANS A SINGLE EJB JAR lLE (OWEVER MANY BEANS YOU CAN SQUISH IN THE EJB JAR THATS HOW MANY BEANS YOU CAN SEARCH ACROSS 6(/(&7 4HE 3%,%#4 CLAUSE SAYS WHAT KIND OF THING THIS QUERY WILL RETURN )T CAN BE ONLY ONE OF THESE TWO DQDEVWUDFWVFKHPDW\SHIRUDQHQWLW\EHDQ DEVWUDFWVFKHPDQDPH!0RYLH6FKHPDDEVWUDFWVFKHPDQDPH! terms (and in In formal EJB abstract schema the spec) the ECT variable type as a SELNGE variable, 25 is called a RA es over the because it rangbean type. entire entity DFPSÀHOG!VLQJOHYDOXHW\SHIRUDQHQWLW\EHDQ 6(/(&72%-(&7 P )5200RYLH6FKHPDP FPS¿HOG! ¿HOGQDPH!WLWOH¿HOGQDPH! FPS¿HOG! 6(/(&7PWLWOH)5200RYLH6FKHPDP WDWLRQWR <RXFDQ·WXVHGRWQR UHWXUQD&05ÀHOG H2. $VD6(/(&7W\SHWKHVHDU PWLWOHRUPJHQUH %XWWKLVLV1272. PGLUHFWRU WQRWD&05¿HOGZLWK <RXFDQXVHD&03¿HOGEX 7W\SH /(& 6( WKH WKHGRWQRWDWLRQDV When you use a <cmp-Þeld> instead of a range variable, we call this a single-valued path expression. NOTE: weÕre deviating from standard spec naming conventions in our abstract schema name, because we want it to be OBVIOUS that weÕre using the schema name and not, say, the bean name. In the spec, the abstract schema name is the same as the component interface name. &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 36 HQWLW\EHDQUHODWLRQVKLSV -2*94;-4-+<¸_PMV\W][M7JRMK\ 7HATS WITH THE /"*%#4 M THING )TS A STRUCTURE THE *%% DESIGNERS PUT IN TO BE CONSISTENT WITH A VERSION OF 31, )TS ANNOYING "UT YOU HAVE TO DO IT WHENEVER YOU RETURN A BEAN TYPE INSTEAD OF A #-0 lELD TYPE !ND YOU MUST ./4 DO IT WHENEVER YOU RETURN A #-0 lELD TYPE ,I\RXUHWXUQWKHEHDQ·VDEVWUDFW VFKHPDW\SHXVH2%-(&7 DEVWUDFWVFKHPDQDPH!0RYLH6FKHPDDEVWUDFWVFKHPDQDPH! 6(/(&72%-(&7 P ,I\RXUHWXUQDFPSILHOG! GRQ·WXVH2%-(&7 FPS¿HOG! ¿HOGQDPH!WLWOH¿HOGQDPH! FPS¿HOG! 6(/(&7PWLWOH vigate a the dot, to na If youÕre using t use the OBJECT ( ) path, you donÕ 6KDUSHQ \RXU SHQFLO &LUFOHWKH(-%4/VWDWHPHQWLILWKDVDOHJDO6(/(&7FODXVH 6(/(&72%-(&7 P )5200RYLH6FKHPDP 6(/(&7PWLWOH)5200RYLH6FKHPDP 6(/(&7P)5200RYLH6FKHPDP 6(/(&72%-(&7 PWLWOH )5200RYLH6FKHPDP \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 37 (-%4/6(/(&7 ?PI\LWM[Q\5-)6\WZM\]ZVIV I J[\ZIK\[KPMUI\aXM' 7KHDEVWUDFWVFKHPDLVMXVWDVWXSLG ODEHOLQ;0/VRREYLRXVO\7+$7LVQRW ZKDW\RX·UHUHWXUQLQJ<RX·UHUHWXUQLQJWKH EHDQW\SH%XWWKDWFDQ·WEHULJKWHLWKHU\RX·UH REYLRXVO\UHWXUQLQJWKHFRPSRQHQWLQWHUIDFHW\SH EXW7+$7FDQ·WEHULJKWEHFDXVHKRZZRXOGLW NQRZLILW·VVXSSRVHGWRJLYH\RXWKHORFDO RU5HPRWHW\SH 7KH0RYLH6FKHPDDEVWUDFWVFKHPDQDPH UHSUHVHQWVWKHEHDQW\SH $VLIE\PDJLFWKH&RQWDLQHUNQRZVZKLFK LQWHUIDFHYLHZWRUHWXUQORFDORU5HPRWH GHSHQGLQJRQZKHWKHUWKHLQYRFDWLRQRIWKH TXHU\FDPHIURPDKRPHRU5HPRWHLQWHUIDFH 0RYLH/RFDO You never havet to worry abou pe whether the tyote is local or Rem JHW0RYLH,' JHW7LWOH JHW*HQUH JHW'LUHFWRU JHW7UDLOHU 0RYLH5HPRWH JHW0RYLH,' JHW7LWOH JHW*HQUH JHW'LUHFWRU JHW7UDLOHU :KHQ\RXVHHWKLV 0RYLH6FKHPDP 7KLQNWKLV P WKHFRPSRQHQWLQWHUIDFH DQGZH¶OOOHWWKH&RQWDLQHUVRUWRXWWKH 5HPRWHYVORFDOLVVXH &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 38 HQWLW\EHDQUHODWLRQVKLSV ;-4-+<IVL.:75IZMUIVLI\WZa 9OU GOTTA HAVE 3%,%#4 AND &2/- NO MATTER WHAT BUT THE 7(%2% CLAUSE IS OPTIONAL 7ITH 3%,%#4 YOU CAN ALSO USE THE KEYWORD $)34).#4 TO SAY THAT YOU DONT WANT ANY DUPLICATES RETURNED IN YOUR #OLLECTION use it declare it 6(/(&7',67,1&72%-(&7 P )5200RYLH6FKHPDP ns DISTINCT mea Ó es at ic pl Òno du in the FROM, we declare identiÞcation variable Òm the thatÕs used in the SELE Ó CT. 4HE &2/- CLAUSE DECLARES THE IDENTIlCATION VARIABLE LIKE THE hMv IN OUR EXAMPLE QUERY 4HIS ALSO DElNES THE DOMAIN OF THE QUERY BY SAYING TO THE #ONTAINER h(ERES WHERE ) WANT YOU TO BE LOOKINGv 4HE ORDER IN WHICH THE &2/- AND 3%,%#4 MIGHT FEEL A LITTLE STRANGE IF YOURE NOT USED TO 31, "UT THINK OF IT LIKE SOMEONE SAYING h) WANT YOU TO GO GET MY THINGS AND THE THINGS )M TALKING ABOUT ARE &2/- MY TOP DRAWERv )F YOU NEED TO DECLARE MORE IDENTIlCATION VARIABLES TO USE LATER IN THE QUERY YOU CAN SEPARATE THEM WITH A COMMA 6(/(&72%-(&7 P )5200RYLH6FKHPDP'LUHFWRU6FKHPDG We declared two different and deÞned the domain identiÞers, of the query to those two abstract sch emas. 9OU CAN ALSO USE THE !3 KEYWORD )TS OPTIONAL BUT YOU MIGHT SEE IT ON THE EXAM OR IN SOMEONE ELSES CODE OR YOUR COMPANY STYLE GUIDE MIGHT INSIST ON USING IT 7ERE TOO LAZY TO KEEP TYPING IT 6(/(&72%-(&7 P )5200RYLH6FKHPD$6P \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 39 (-%4/LGHQWL¿HUV 1LMV\QNQMZ[ 7HEN YOU MAKE AN IDENTIlER YOU HAVE TO FOLLOW THE *AVA PROGRAMMING LANGUAGE GUIDELINES FOR WHAT YOU CAN NAME THINGS 9OU CANT USE THE OTHER *AVA IDENTIlERS ,GHQWLÀHUV 0XVWEHYDOLG-DYDLGHQWLILHUV 0XVW127EHVDPHDVDQDEVWUDFWVFKHPDQDPH! RUHMEQDPH!DQ\ZKHUHLQWKH'' 0XVW127EHRQHRIWKH(-%4/UHVHUYHGZRUGV ? (-%4/UHVHUYHGZRUGV 6(/(&7 25 )520 %(7:((1 :+(5( /,.( ',67,1&7 ,1 2%-(&7 $6 18// 81.12:1 758( (037< )$/6( 0(0%(5 127 2) $1' ,6 :DWFKRXWIRU TXHVWLRQVZLWK QDPHFRQÁLFWV QHMEQDPH!PXVW DWD 5HPHPEHUWK KHHMEMDU EHXQLTXHIRUHDFKEHDQLQW FRPSOHWH ZKLFKPHDQVXQLTXHIRURQH WKHDEVWUDFW ''7KHVDPHLVWUXHIRU QLTXHLQ VFKHPDQDPH!LWPXVWEHX WKH'' DQ¶WXVHDQ <RXDOUHDG\NQRZWKDW\RXF RXUTXHU\WKDW LGHQWL¿FDWLRQYDULDEOHLQ\ DEVWUDFW PDWFKHVDQHMEQDPH!RU XWNHHSLQ VFKHPDQDPH!LQWKH''E HVHQVLWLYH PLQGWKDW(-%4/LVQRWFDV 6R\RXPXVW127GRWKLV 6(/(&72%-(&7 PRYLH H RYL HP RYL 00 )52 In EJB-QL, ÒMovieÓ is the same as ÒmovieÓ, and you canÕt have conßicting names. QRWXVHGLQWKHFXUUHQWYHUVLRQLW¶VIXWXUHXVHLVXQNQRZQ &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 40 HQWLW\EHDQUHODWLRQVKLSV <PM?0-:-KTI][M ! 7(%2% CLAUSE IS OPTIONAL AND LETS YOU PUT EXTRA CONDITIONS ON WHATS RETURNED 2EMEMBER THAT WITHOUT ONE WE GOT EVERYTHING OF THE TYPE USED IN THE 3%,%#4 127XVLQJ:+(5( 6(/(&72%-(&7 P )5200RYLH6FKHPDP ([LVWLQJ(QWLWLHV (QWLWLHVUHWXUQHG IURPWKHTXHU\ 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH ng Enclose a siStngrile quotes literal in 8VLQJ:+(5( 6(/(&72%-(&7 P )5200RYLH6FKHPDP:+(5(PJHQUH µ+RUURU¶ ([LVWLQJ(QWLWLHV 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH 0RYLH (QWLWLHVUHWXUQHG IURPWKHTXHU\ 0RYLH 0RYLH 0RYLH the movies , we get only Using WHERE value ÒHorrorÓ in their that have the reÓ. (In other words, CMP Þeld Ògenhose getGenre() method those movies wÒHorrorÓ.) would return 0RYLH \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 41 WKH:+(5(FODXVH <PM?0-:-KTI][M <RXFDQXVHOLWHUDOV :+(5(PGLUHFWRUGHJUHHV! :+(5(PJHQUH µ+RUURU¶ <RXFDQXVHLQSXWSDUDPHWHUV :+(5(PGLUHFWRUGHJUHHV!" $QGWKHSDUDPHWHUVGRQ·WKDYHWREHLQRUGHU WKDW·VZK\WKH\KDYHQXPEHUV :+(5(PJHQUH "$1'PGLUHFWRUGHJUHHV" SXEOLF6WULQJHME+RPH/LVW0RYLHV%\'LUHFWRU$QG*HQUH 6WULQJGLU,'6WULQJJHQUH <RXFDQGRFRPSDULVRQV :+(5(PGLUHFWRUGHJUHHV %XW\RXEHWWHUFRPSDUHDSSOHVWRDSSOHV :+(5(PGLUHFWRU r pare a Directo You canÕt conummeric type! bean to a 6W &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 42 HQWLW\EHDQUHODWLRQVKLSV <PMXZWJTMU_Q\P][QVO+ WTTMK\QWV\aXM[ )MAGINE YOU WANT TO USE THE $IRECTOR3CHEMA TO RETURN ALL THE $IRECTORS THAT HAVE MADE HORROR MOVIES 3OUNDS PRETTY SIMPLE $IRECTOR HAS A #-2 lELD FOR MOVIES AND EACH MOVIE HAS A #-0 lELD FOR GENRE 3O WE COME UP WITH SOMETHING LIKE THIS glance, Þrst 6(/(&72%-(&7 G Looks good on UT! This is O )520'LUHFWRU6FKHPDG but WATCH :+(5(GPRYLHVJHQUH µ+RUURU¶ NOT LEGAL. 'RJ 7HY CANT YOU DO THIS ,ETS BACK UP AND IMAGINE THIS IS *AVA CODE (OW DO YOU NORMALLY USE THE DOT OPERATOR IN *AVA 9OU MIGHT DO SOMETHING LIKE 2ZQHUR QHZ2ZQHU RJHW'RJ EDUN method of Ow ner method of Dog YRLGEDUN e can go This is Þne...e Owwner, get its through thcall bark() on the Dog, and g. OwnerÕs do 2ZQHU 'RJD'RJ 'RJJHW'RJ 4HE /WNER HAS A GET$OG METHOD AND THE $OG HAS A BARK .O PROBLEM "UT WHAT IF WE CHANGED THE /WNER TO ALLOW ONE /WNER TO HAVE MULTIPLE $OG OBJECTS 4HEN WHAT HAPPENS 2ZQHUR QHZ2ZQHU RJHW'RJV EDUN method of Owner we reallyg? w who are o YIKES!!thNiso method on? A D calling llection... Or a Co 2ZQHU &ROOHFWLRQGRJV &ROOHFWLRQJHW'RJV \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 43 XVLQJFROOHFWLRQVLQDSDWK +WTTMK\QWV[LWV¼\JIZS GXK 9OU CANT USE THE DOT NAVIGATION WHEN SOMETHING YOURE USING IN THE PATH IS A #OLLECTION &OR EXAMPLE YOU CANT DO THIS 2ZQHUR QHZ2ZQHU RJHW'RJV EDUN 2ZQHU &ROOHFWLRQGRJV &ROOHFWLRQJHW'RJV nd you Collection. leAct a s rn tu ion! re s hi ol T () on a C canÕt call bark )TS THE $OG OBJECTS ). THE COLLECTION THAT CAN BARK 3O IN *AVA YOUD HAVE TO lRST ACCESS THE INDIVIDUAL $OG ELEMENTS IN THE COLLECTION AND ONE AT A TIME ASK EACH $OG TO BARK 4HE $OG CODE ABOVE IS ALMOST EXACTLY LIKE THIS 7(%2% CLAUSE WE HAD BEFORE :+(5(GPRYLHVJHQUH µ+RUURU¶ 6 W !ND SINCE hMOVIESv AND hGENREv ARE VIRTUAL lELDS WITH ACCESSORS YOU CAN THINK OF IT JUST LIKE THIS GJHW0RYLHV 'LUHFWRU JHW'LUHFWRU,' JHW2VFDU:LQQHU JHW'HJUHHV JHW1DPH JHW0RYLHV JHW*HQUH µ+RUURU¶ 6W 7KLQNDERXWLWZKDWZRXOG\RX EHWU\LQJWRVD\KHUH"7KDWWKH ZKROHFROOHFWLRQKDVDJHQUH"1R DFROOHFWLRQGRHVQ·WKDYHDJHW*HQUH ,W·VWKHPRYLHVLQWKHFROOHFWLRQWKDW KDYHDJHW*HQUH 6RWKLVV\QWD[ GRHVQ·WPDNHVHQVH 0RYLH JHW0RYLH,' JHW7LWOH JHW*HQUH JHW'LUHFWRU JHW7UDLOHU &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 44 HQWLW\EHDQUHODWLRQVKLSV <PM16WXMZI\WZTM\[aW][Ia ¹.WZIVQVLQ^QL]ITMTMUMV\16\PM+WTTMK\QWVº 4HE ). OPERATOR FOR THE &2/- CLAUSE LETS YOU REFER TO A #OLLECTION BUT NAME AN IDENTIlER AS REPRESENTING /.% MEMBER OF THE #OLLECTION 6(/(&7',67,1&72%-(&7 G )520'LUHFWRU6FKHPDG,1 GPRYLHV P :+(5(PJHQUH µ+RUURU¶ Now ÒmÓ represents an individual Movie IN the movies Collection (a CMR Þeld of Director) This query looks through and looks through ea all of the Directors, Director, and looks fochr of the Movies of each that matches ÔHorror a Movie with a genre The query returns a set (no duplicates becaÕ. use we said ÒDISTINCTÓ of Directors an individual Let ÒmÓ represent . So, ÒmÓ ct lle Movie IN the Co nt ion Collection e does NOT represethethMovie er th type, but ra the Collection is type (the type holding.) 7KH,1RSHUDWRULV RYHUORDGHGLQ(-%4/ 0FODXVHLVGLIIHU 7KH,1RSHUDWRULQWKH)52 RSHUDWRUXVHGLQD VRQ SDUL HQWIURPWKH,1FRP :+(5(FODXVH DQVD\ ,QWKH:+(5(FODXVH\RXF V\¶ GJHQUH,1 µ+RUURU¶µ)DQWD D\LQJ DQGLWLVMXVWDVKRUWFXWIRUV HQUH µ)DQWDV\¶ GJHQUH µ+RUURU¶25GJ WKH,1RSHUDWRU %XWLWKDVQRWKLQJWRGRZLWK HVLJQDWHLQGLYLGXDO XVHGLQD)520FODXVHWRG RMXVWUHPHPEHU PHPEHUV,1D&ROOHFWLRQ6 RRNDWWKHLQGLYLGXDO )520FODXVH,1VD\V³/ QGUDWKHUWKDQXVLQJ QD FWLR ROOH H& REMHFWVLQWK XVHWKHW\SHRIWKH SH HW\ VWK WKH&ROOHFWLRQD ´ LRQ LWHPV,1WKH&ROOHFW HHLIWKH¿HOG :+(5(FODXVH,1VD\V³6 OLWHUDOVLQWKLVVHW´ YDOXHPDWFKHVRQHRIWKH \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Page 45 Return to Table of Contents (-%4/%(7:((1 <PM*-<?--6M`XZM[[QWV ,ETS SAY YOU WANT TO lND MOVIES IN THE DATABASE MADE IN THE S AND S BUT YOU WANT TO EXCLUDE ANY MOVIES MADE IN OR REALLY BAD MOVIE YEARS 9OU COULD SAY 6(/(&7',67,1&72%-(&7 P )5200RYLH6FKHPDP :+(5( P\HDU!$1'P\HDU 25 P\HDU!$1'P\HDU /R YOU COULD DO IT THE COOLER WAY 6(/(&7',67,1&72%-(&7 P )5200RYLH6FKHPDP :+(5( P\HDU%(7:((1$1' $1' P\HDU127%(7:((1$1' N is INclusive tice that BETWEE In this example no included), and 1979 WILL be 60 19 s ar ye e th (i.e. e TWEEN is EXclusiv and that NOT BE ,I,·P%(7:((1 DURFNDQGDKDUGSODFH ERWKWKHURFNDQGWKHKDUG SODFHDUH,1&/8'(', EHW\RXNQRZWKHIHHOLQJ &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Page 46 Return to Table of Contents HQWLW\EHDQUHODWLRQVKLSV <PMW\PMZ¹16º 7EVE USED ). WITH A &2/- CLAUSE TO HELP NAVIGATE THROUGH COLLECTIONS BUT ). HAS ANOTHER PERSONALITY YOU MIGHT SAY ). HAS BEEN OVERLOADED FOR USE IN THE 7(%2% CLAUSE ,ETS SAY THAT YOU WANT TO SELECT MOVIES FROM A SUBSET OF ALL THE MOVIE GENRES IN YOUR DATABASE 6(/(&7',67,1&72%-(&7 P )5200RYLH6FKHPDP :+(5(PJHQUH,1 µKRUURU¶µP\VWHU\¶ ose genre rn only those movies wh This query would retu ÒmysteryÓ. This ßavor of IN lets is either ÒhorrorÓ or g akin to an enumerated list. you created somethin !ND THERES ALWAYS ./4 ). FOR THOSE TIMES WHEN YOU NEED SOME ACTION 6(/(&7',67,1&72%-(&7 P )5200RYLH6FKHPDP :+(5(PJHQUH127,1 µURPDQFH¶µFRPHG\¶ This query would return only those movies whose genre is NOT ÒromanceÓ or ÒcomedyÓ ´ µLV127WKH VDPHDV´!µ :KHQFRPSDULQJYDOXHVLQD V³QRW :+(5(FODXVH³!´PHDQ DGRI HTXDOV´<RXXVH7+$7LQVWH DW³ ´ DWK -DY URP QJI RPL ³ ´%XWF LW VHH \RX KHQ WZ ULJK FDQORRNVR SWHG RQDTXHVWLRQ'RQ¶WEHWHP 6WULQJO LWH DUHLQ6 UDOV ,1*/( TXRWHV :KLOHZH ¶UHRQWKH WRSLFRI VLVWHQFLH LQFRQ VEHWZHH Q-DYDV DQG(-% \QWD[ 4/GRQ ¶WEHWULFN D6WULQJLQ HGE\ G :521* RXEOHTXRWHV7K LQ LVLV :+(5( (-%4/ PIRR,1 ³EDU´³E ,WPXVWE D]´ H µEDU¶µE D]¶ \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 47 ,6(037< <PM1;-58<AKWUXIZQ[WVM`XZM[[QWV 4HIS BABY WILL LET YOU KNOW WHETHER THE COLLECTION IN QUESTION IS EMPTY -XVWVKRZ PHWKHORVHUVZKR KDYHQ·WPDGHDQ\ PRYLHV 6(/(&7',67,1&72%-(&7 G )520'LUHFWRU6FKHPDG :+(5(GPRYLHV,6(037< ctors who dire turns only ieths. ose This querymre ov m y have not ade an <PM1;67<-58<AKWUXIZQ[WVM`XZM[[QWV )3 ./4 %-049 LETS YOU SAY h$ONT GIVE ME ANYTHING WHERE THIS lELD IS EMPTYv 6(/(&7',67,1&72%-(&7 G )520'LUHFWRU6FKHPDG :+(5(GPRYLHV,6127(037< ose returns onlyleth t one movie This querywh as at ve directors ovieo shacollection. in their m &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 48 HQWLW\EHDQUHODWLRQVKLSV <PM413-M`XZM[[QWV ,IKE EXPRESSIONS ARE USED TO COMPARE SINGLE VALUE PATH EXPRESSIONS WITH A 3TRING LITERAL 4HE POWER OF ,)+% IS THAT THE 3TRING LITERAL CAN HAVE WILDCARD ELEMENTS GIVING YOU A SIMPLE PATTERN MATCHER 4HE hv WILDCARD MATCHES AGAINST TO MANY CHARACTERS THE h?v MATCHES AGAINST A SINGLE CHARACTER ONLY 6(/(&7',67,1&72%-(&7 G )520'LUHFWRU6FKHPDG :+(5(GSKRQH/,.(µ¶ is a wildcard The Ò%Ó symmanboyl characters for 0 to This query uld re turn only those di who have phwo rect digits Ô719Õ. on(He eynu,mbers beginning with theors ph on e nu mber Strings, they have those dashes...)s can to be 6(/(&7',67,1&72%-(&7 P )5200RYLH6FKHPDP :+(5(P¿OPFRGH/,.(µBPP¶ This query es for movies whos Þlm codes arseearinchth e 7Xmm family. e l is a wildcard The Ò_Ó syembo acter for a singl char <PM67<413-M`XZM[[QWV 6(/(&7',67,1&72%-(&7 P )5200RYLH6FKHPDP :+(5(P¿OPFRGH127/,.(µPP¶ e guess what this on You can probrnabs lyall movies whose Þlm codes does... retu in ÒmmÓ. DONÕT end \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Page 49 Return to Table of Contents HQWLW\EHDQUHODWLRQVKLSV :MDD=LHGAFLK (QWLW\EHDQVFDQKDYHSHUVLVWHQWUHODWLRQVKLSVZLWKRWKHU HQWLW\EHDQV $FRQWDLQHUPDQDJHGUHODWLRQVKLS &05 ILHOGLVGHILQHG LQWKHEHDQFODVVMXVWDVD&03ILHOGLV³ZLWKDSDLURI DEVWUDFWJHWWHUVDQGVHWWHUVWKDWUHWXUQHLWKHUWKHORFDO FRPSRQHQWLQWHUIDFHRIWKHEHDQRUD&ROOHFWLRQ ,IWKHYLUWXDOILHOGLVD&ROOHFWLRQLWPXVWEHGHFODUHGDV HLWKHUMDYDXWLO&ROOHFWLRQRUMDYDXWLO6HW1RRWKHU&ROOHF WLRQW\SHLVDOORZHGDVWKHGHFODUHGUHWXUQW\SHRID&05 ILHOG 5HODWLRQVKLSVKDYHPXOWLSOLFLWLHV³WKH\FDQEHRQHWR RQHRQHWRPDQ\RUPDQ\WRPDQ\ 0DQ\WRRQHZRUNV LQWKHVDPHZD\WKDWRQHWRPDQ\ZRUNVLWMXVWGHSHQGV RQZKRVHSRLQWRIYLHZ\RX·UHXVLQJ )RUH[DPSOHD0RYLHEHDQKDVDPDQ\WRRQHUHODWLRQ VKLSZLWKD'LUHFWRUEHDQ(DFK0RYLHKDVRQO\RQH 'LUHFWRUEXWD'LUHFWRUFDQKDYHPDQ\0RYLHV 0XOWLSOLFLW\DIIHFWVWKHUHWXUQW\SHRIWKHYLUWXDOILHOG 0RYLHKDVRQO\RQH'LUHFWRUVRWKH&05ILHOGLQWKH EHDQLVJHW'LUHFWRU WKDWUHWXUQVDUHIHUHQFHWRD 'LUHFWRU·VORFDOFRPSRQHQWLQWHUIDFH%XWD'LUHFWRUKDV PDQ\PRYLHVVRD'LUHFWRU·VYLUWXDOILHOGLVJHW0RYLHV ZKLFKUHWXUQVD&ROOHFWLRQ $&03EHDQPXVWGHILQHDQ´DEVWUDFWSHUVLVWHQFHVFKH PDLQWKH''WKDWOLVWVHDFKRIWKHEHDQ·V&03ILHOGV DQGDOVRLGHQWLILHVZKLFKRIWKHILHOGVLVWKHSULPDU\NH\ XQOHVVLW·VDFRPSRXQGSULPDU\NH\ 7KH''PXVW DOZD\VGHILQHWKHW\SHRIWKHSULPDU\NH\HYHQLIWKH SULPDU\NH\LVQRWDILHOGRIWKHEHDQ 5HPHPEHULIWKH SULPDU\NH\LVQRWDILHOGRIWKHEHDQLWPXVWEHDSUL PDU\NH\FODVVFRPSRVHGRI&03ILHOGVIURPWKHEHDQ 5HODWLRQVKLS &05 ILHOGVDUHQRWGHILQHGLQWKHHQWHU SULVHEHDQ!SRUWLRQRIWKH'' ZKHUH\RXGHILQH\RXU &03ILHOGV EXWDUHLQVWHDGGHILQHGLQWKHUHODWLRQ VKLSV!VHFWLRQRIWKH'' (DFKUHODWLRQVKLSPXVWKDYHWZRSDUWQHUVZLWKHDFK SDUWQHUGHVFULEHGLQDQHMEUHODWLRQVKLSUROH!HOHPHQW WKDWLQFOXGHVWKH&05ILHOGQDPHWKHVRXUFH HME QDPH! IRUWKHSDUWQHUWKHPXOWLSOLFLW\ LHKRZPDQ\RI WKLVEHDQZLOOWKHRWKHUSDUWQHUKDYH DQGDQRSWLRQDO FDVFDGHGHOHWH! ZKLFKVD\V´'HOHWHPHLIP\SDUWQHU LVGHOHWHGµ 5HODWLRQVKLSVFDQEHRQHZD\ XQLGLUHFWLRQDO RU WZRZD\ ELGLUHFWLRQDO $XQLGLUHFWLRQDOUHODWLRQVKLSLV GHVFULEHGLQWKH''LQWKHVDPHZD\DVDELGLUHFWLRQDO UHODWLRQVKLSH[FHSWWKDWRQO\RQHRIWKHWZRSDUWQHUV KDVD&05ILHOG7KLVPHDQVEHDQ$KDVDUHIHUHQFHWR EHDQ% WKH\·UHLQDUHODWLRQVKLS EXWEHDQ%GRHVQRW KDYHDUHIHUHQFHEDFNWR$'LUHFWLRQDOLW\GRHVQRWDIIHFW PXOWLSOLFLW\RUFDVFDGHGHOHWHLWMXVWPHDQVWKDWRQO\RQH RIWKHWZRSDUWQHUVKDVDUHIHUHQFHWRWKHRWKHU7RWKH &RQWDLQHUWKH\·UHVWLOOLQDUHODWLRQVKLS &DVFDGHGHOHWHVWHOOWKH&RQWDLQHUWRGHOHWHWKHEHDQ ZLWKWKHFDVFDGHGHOHWH!WDJZKHQWKHEHDQ·VSDUWQHU LVGHOHWHG7KLVZRUNVRQO\LIWKHEHDQKDVDPXOWLSOLF LW\RIRQH <RXZRXOGQ·WZDQWWRGHOHWHD'LUHFWRUMXVW EHFDXVHRQHRIWKLV0RYLHVZDVGHOHWHGEXW\RXPLJKW ZDQWWRGHOHWHD0RYLHLILWVVROH'LUHFWRUZDVGHOHWHG $EHDQFDQQRWDFFHVVLWV&05ILHOGVLQHME&UHDWH LW PXVWZDLWXQWLOHME3RVW&UHDWH EHIRUHVD\DVVLJQLQJ WKH&05YLUWXDOILHOGDQDFWXDOYDOXH,QRWKHUZRUGV\RX FDQ·WXVH\RXUDEVWUDFWJHWWHUVDQGVHWWHUVIRU\RXU&05 ILHOGVXQWLODIWHUHME&UHDWH FRPSOHWHV (-%4/LVDSRUWDEOHTXHU\ODQJXDJH\RXFDQXVHWR VSHFLI\\RXUTXHULHVIRUERWKILQGHUDQGVHOHFWPHWKRGV ,QDQ(-%4/TXHU\WKH6(/(&7DQG)520FODXVHV DUHUHTXLUHGWKH:+(5(FODXVHLVRSWLRQDO $6(/(&7FDQUHWXUQHLWKHUDQDEVWUDFWVFKHPDW\SH! ZKLFKUHDOO\PHDQVWKHEHDQ·VORFDOFRPSRQHQWLQWHU IDFHW\SH RUDVLQJOHYDOXHILHOG ,I\RXXVHWKHGRWQRWDWLRQIRUQDYLJDWLQJDSDWK IRUH[ DPSOHPJHQUH \RXFDQ·WXVHD&ROOHFWLRQW\SHDVSDUW RIWKHSDWK7RXVHD&ROOHFWLRQW\SHDVSDUWRID)520 FODXVH\RXPXVWHQFORVHLWXVLQJWKH,1 GPRYLHV RSHUDWRU &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 50 HQWLW\EHDQUHODWLRQVKLSV : MTI\QWV[PQXI[[QOVUMV\[ ! -OVIE CAN HAVE ONLY ONE $IRECTOR )F YOU SET A -OVIES $IRECTOR lELD TO $IRECTOR THE -OVIE WILL BE IN $IRECTOR S COLLECTION OF -OVIES BUT IN NO OTHER $IRECTORS COLLECTION )F YOU LATER REASSIGN THE -OVIES $IRECTOR lELD SO THAT THE -OVIE HAS A DIFFERENT $IRECTOR$IRECTOR THE PREVIOUS $IRECTOR $IRECTOR WILL NO LONGER HAVE THAT -OVIE IN ITS #OLLECTION 1RWKDWPRYLH VKRXOGEH0,1(,I D0RYLHFDQKDYHRQO\ RQH'LUHFWRUWKHQ ,·PLW ,GLUHFWHGWKDW PRYLH7KDWPRYLH LV0,1(,WEHORQJVLQ 0<&ROOHFWLRQ 4HIS ALL HAPPENS AUTOMATICALLY 4O MAINTAIN THE INTEGRITY OF THE DATABASE THE #ONTAINER MANAGES BOTH SIDES OF THE RELATIONSHIP AT ALL TIMES 'LUHFWRU 'LUHFWRU %HIRUH 0RYLH% 0RYLH' 0RYLH$ 0RYLH& 'LUHFWRU 7KUHH0RYLHVLQ'LUHFWRU ·V0RYLH&ROOHFWLRQ,I\RX FDOOJHW'LUHFWRU RQDQ\RI WKHVH0RYLHVWKH\ZLOOUHWXUQ 'LUHFWRU 7KHUHLVRQO\RQH0RYLH LQ'LUHFWRU·V&ROOHFWLRQ ,W·VWKHRQO\0RYLHWKDW ZLOOUHWXUQ'LUHFWRULI 'LUHFWRU \RXFDOOJHW'LUHFWRU RQ WKH0RYLH PRYLH$VHW'LUHFWRU PRYLH'JHW'LUHFWRU ovie AÕs this says, ÒsetbeMwh atever the to or ct Dire ovie D.Ó M r fo is Director $IWHU 0RYLH% 0RYLH$ 0RYLH' 0RYLH& 'LUHFWRU 0RYLH$FDQKDYHRQO\RQH'LUHFWRUVRZKHQ ZHUHDVVLJQHGWKH'LUHFWRUIRU0RYLH$WR EH'LUHFWRU0RYLH$ZDVDXWRPDWLFDOO\ 5(029('IURP'LUHFWRU·V&ROOHFWLRQRI 0RYLHV5HPHPEHUWKH&RQWDLQHUPDQDJHV ERWKVLGHVRIWKHUHODWLRQVKLS :KHQZHUHDVVLJQHGWKH 'LUHFWRURI0RYLH$WKDW0RYLH DXWRPDWLFDOO\PRYHGLQWR'LUHFWRU 'LUHFWRU ·V&ROOHFWLRQRI0RYLHV \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 51 DVVLJQPHQWVLQUHODWLRQVKLSV 1N\PMU]T\QXTQKQ\aWN\PMZMTI\QWV[PQXNQMTLQ[76-Q\¼[I57>1N\PMU]T\QXTQKQ\aQ[5)6AQ\¼[I+78A HMEUHODWLRQVKLSUROHQDPH!0RYLH%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!0DQ\PXOWLSOLFLW\! HMEUHODWLRQVKLSUROHQDPH!'LUHFWRU%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!2QHPXOWLSOLFLW\! 7HEN WE ASSIGNED $IRECTOR TO -OVIE ! THE -OVIE MOVED FROM $IRECTOR S #OLLECTION TO $IRECTOR S #OLLECTION 4HE $IRECTOR #-2 lELD HAS A MULTIPLICITY OF /NE IN ITS RELATIONSHIP WITH -OVIE SO A -OVIE CANT EXIST IN THE #OLLECTION OF TWO DIFFERENT $IRECTORS BECAUSE A -OVIE CAN HAVE ONLY /NE $IRECTOR "UT JUST BECAUSE WE MOVE THE -OVIE DOESNT MEAN THE $IRECTOR ALSO MOVES 4HE MULTIPLICITY OF -OVIE IS -ANY SO WHEN YOU ASSIGN ANOTHER -OVIES $IRECTOR TO A DIFFERENT -OVIE THAT $IRECTOR REFERENCE IS COPIED AND BOTH -OVIES WILL NOW HAVE A REFERENCE 7KH029,(UHIHUHQFHZDV029('IURPRQH'LUHFWRUWRDQRWKHU PRYLH$VHW'LUHFWRU PRYLH'JHW'LUHFWRU 0RYLH$ 0RYLH' 0RYLH& 'LUHFWRU %HFDXVHWKH'LUHFWRUWR0RYLHUHODWLRQVKLSLV2QHWR0DQ\D 0RYLHFDQ1(9(5H[LVWLQPRUHWKDQRQH'LUHFWRU·V&ROOHFWLRQ,I \RXFKDQJHD0RYLH·V'LUHFWRUWKH0RYLHLVQRWFRSLHG,WLVPRYHG WRWKHQHZ'LUHFWRU·V&ROOHFWLRQ 'LUHFWRU 7KH',5(&725UHIHUHQFHZDV&23,('IURPRQH0RYLHWRDQRWKHU 0RYLH$ 0RYLH' 'LUHFWRU 'LUHFWRU 0RYLH' Movie DÕs s reference wa ved. copied, not mo &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 52 HQWLW\EHDQUHODWLRQVKLSV 0XOWLSOLFLW\DQG$VVLJQPHQWV ([HUFLVH *LYHQWKLVUHODWLRQVKLS HMEUHODWLRQVKLSUROHQDPH!0RYLH%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!2QHPXOWLSOLFLW\! HMEUHODWLRQVKLSUROHQDPH!7UDLOHU%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!2QHPXOWLSOLFLW\! $QGWKLVFXUUHQWVFHQDULR 7UDLOHU% 0RYLH 7UDLOHU$ 0RYLH 'UDZZKDWWKHSLFWXUHZLOOORRNOLNHDIWHU\RXUXQ PRYLHVHW7UDLOHU PRYLHJHW7UDLOHU Draw your picture here... $QVZHUWKHVHTXHVWLRQV :KDWLVWKHUHVXOWRIPRYLHJHW7UDLOHU :KDWLVWKHUHVXOWRIPRYLHJHW7UDLOHU :KDWLVWKHUHVXOWRIWUDLOHU$JHW0RYLH :KDWLVWKHUHVXOWRIWUDLOHU%JHW0RYLH \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 53 H[HUFLVHVROXWLRQ 0XOWLSOLFLW\DQG$VVLJQPHQWV 6ROXWLRQ *LYHQWKLVELGLUHFWLRQDOUHODWLRQVKLS HMEUHODWLRQVKLSUROHQDPH!0RYLH%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!2QHPXOWLSOLFLW\! HMEUHODWLRQVKLSUROHQDPH!7UDLOHU%HDQHMEUHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!2QHPXOWLSOLFLW\! $QGWKLVFXUUHQWVFHQDULR 7UDLOHU% 0RYLH 7UDLOHU$ 0RYLH 'UDZZKDWWKHSLFWXUHZLOOORRNOLNHDIWHU\RXUXQ PRYLHVHW7UDLOHU PRYLHJHW7UDLOHU null 7UDLOHU% 7UDLOHU$ 0RYLH 0RYLH $QVZHUWKHVHTXHVWLRQV PRYLHJHW7UDLOHU PRYLHJHW7UDLOHU WUDLOHU$JHW0RYLH WUDLOHU%JHW0RYLH null returns trailerB so a Trailer can have returns null! Movie has a multiplicity of One, Movie 1 and Movie 2 both with be canÕt r only ONE Movie. A Traile One, so there can also returns null... Trailer has a multiplicityrBofwent to Movie 1, Traile be only one Trailer per Movie. When onship. relati the from d necte discon out, kicked was TrailerA returns Movie1, not that it has been assigned to Movie1, the Container updates the Þeld returned from the TrailerÕs getMovie(). &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 54 HQWLW\EHDQUHODWLRQVKLSV 0RFN([DP 1 7HATS TRUE FOR AN ENTITY BEAN PROVIDER USING CONTAINER MANAGED PERSISTENCE TO CREATE PERSISTENT RELATIONSHIPS #HOOSE ALL THAT APPLY R! ! LOCAL INTERFACE IS REQUIRED FOR SUCH A BEAN TO HAVE A BIDIRECTIONAL RELATIONSHIP WITH ANOTHER ENTITY BEAN R" 3UCH A BEAN CAN HAVE RELATIONSHIPS WITH ONLY SESSION BEANS R# 2ELATIONSHIPS CAN BE ONLY ONE TO ONE OR ONE TO MANY R$ ! GETTER METHOD RETURN TYPE CAN BE JAVAUTIL,IST 2 7HATS TRUE FOR AN ENTITY BEAN PROVIDER USING CONTAINER MANAGED PERSISTENCE TO CREATE PERSISTENT RELATIONSHIPS #HOOSE ALL THAT APPLY R! ! REMOTE INTERFACE IS REQUIRED FOR SUCH A BEAN TO HAVE A BIDIRECTIONAL RELATIONSHIP WITH ANOTHER ENTITY BEAN R" 3UCH A BEAN CAN HAVE RELATIONSHIPS WITH ONLY MESSAGE DRIVEN BEANS R# ! GET METHOD RETURN TYPE CAN USE JAVAUTIL-AP R$ 2ELATIONSHIPS CAN BE ONE TO ONE ONE TO MANY OR MANY TO MANY 3 7HEN A REMOTE CLIENT INVOKES A METHOD ON AN ENTITY BEAN USING CONTAINER MANAGED PERSISTENCE AND THAT BEAN HAS ALREADY BEEN REMOVED WHAT EXCEPTION WILL BE THROWN R! R" R# R$ R% MDYD[HME$FFHVV/RFDO([FHSWLRQ MDYD[HME2EMHFW1RW)RXQG([FHSWLRQ MDYDUPL1R6XFK2EMHFW([FHSWLRQ MDYDUPL6WXE1RW)RXQG([FHSWLRQ MDYD[HME1R6XFK(QWLW\([FHSWLRQ \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 55 FRIIHHFUDPPRFNH[DP 4 'IVEN THE CONTAINER MANAGED UNIDIRECTIONAL RELATIONSHIP &OO "AR !ND THE OBJECT RELATIONS F F B B 7HAT WILL BE TRUE AFTER THE FOLLOWING CODE RUNS #HOOSE ALL THAT APPLY I VHW%DU IJHW%DU R! R" R# R% 5 QXOO EJHW)RR QXOO EJHW%DU QXOO NONE OF THE ABOVE ONE TO ONE MANY TO ONE MANY TO MANY ALL OF THE ABOVE NONE OF THE ABOVE 7HICH DEPLOYMENT DESCRIPTOR ELEMENTS VALUES MUST BE A TYPE OF COLLECTION #HOOSE ALL THAT APPLY R! R" R# R$ IJHW%DU )F AN ENTITY BEAN ! HAS BEEN REMOVED FROM A RELATIONSHIP WITH BEAN " IN WHICH CASES WILL BEAN !S ACCESSOR METHOD FOR BEAN " RETURN A NON NULL VALUE #HOOSE ALL THAT APPLY R! R" R# R$ R% 6 HMEQDPH! FPU¿HOG! FPU¿HOGW\SH! HMEUHODWLRQ! &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 56 HQWLW\EHDQUHODWLRQVKLSV 7 7HICH DEPLOYMENT DESCRIPTOR ELEMENTS MUST HAVE EXACTLY TWO DECLARATIONS OF ANOTHER DEPLOYMENT DESCRIPTOR ELEMENT #HOOSE ALL THAT APPLY R! R" R# R$ R% 8 10 FPU¿HOG! FPU¿HOGW\SH! HMEUHODWLRQ! HMEUHODWLRQVKLSUROH! 7HICH SETS OF ELEMENTS ARE MANDATORY WITHIN AN EJB RELATIONSHIP ROLE ELEMENT #HOOSE ALL THAT APPLY R! R" R# R$ R% 9 HMEQDPH! FPU¿HOG!PXOWLSOLFLW\! FPU¿HOG!UHODWLRQVKLSUROHVRXUFH! PXOWLSOLFLW\!UHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!UHODWLRQVKLSUROHVRXUFH! UHODWLRQVKLSUROHQDPH!UHODWLRQVKLSUROHVRXUFH! 7HICH ARE VALID VALUES FOR A CMR lELD TYPE ELEMENT #HOOSE ALL THAT APPLY R! R" R# R$ R% 3TRING )NTEGER MDYDXWLO6HW MDYDXWLO/LVW MDYDXWLO&ROOHFWLRQ 7HICH SETS OF ELEMENTS ARE MANDATORY WITHIN AN EJB RELATION ELEMENT R! HMEUHODWLRQQDPH!HMEUHODWLRQVKLSUROH! R" HMEUHODWLRQVKLSUROH!HMEUHODWLRQVKLSUROH! R# GHVFULSWLRQ!HMEUHODWLRQQDPH! HMEUHODWLRQVKLSUROH! R$ HMEUHODWLRQQDPH!HMEUHODWLRQVKLSUROH! HMEUHODWLRQVKLSUROH! \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Page 57 Return to Table of Contents FRIIHHFUDPPRFNH[DP 11 'IVEN #-0 BEANS #USTOMER"EAN /RDER"EAN AND ,INE)TEMS"EAN WITH THE FOLLOWING RELATIONSHIPS #USTOMER"EAN /RDER"EAN N /RDER"EAN ,INE)TEMS"EAN N AND THE FOLLOWING %*" 1, QUERY 3%,%#4 $)34).#4 /"*%#4 C &2/- #USTOMER C ). C/RDER O ). OLINE)TEMS LI 7(%2% OLSURGXFWBW\SH µUHIULJHUDWRU¶ 7HICH OF THE FOLLOWING PROPERLY DESCRIBES THE RESULT OF THE QUERY #HOOSE ALL THAT APPLY R! R" R# R$ 12 4HE QUERY IS INVALID !LL ORDERS THAT INCLUDE A LINE ITEM THAT REFERS TO A REFRIGERATOR !LL LINE ITEMS THAT REFER TO A REFRIGERATOR !LL CUSTOMERS THAT HAVE ORDERS THAT REFER TO A REFRIGERATOR 'IVEN #-0 BEANS #USTOMER"EAN /RDER"EAN AND ,INE)TEMS"EAN WITH THE FOLLOWING RELATIONSHIPS #USTOMER"EAN /RDER"EAN N /RDER"EAN ,INE)TEMS"EAN N 7HICH WILL RETURN ALL ORDERS THAT HAVE LINE ITEMS #HOOSE ALL THAT APPLY R! 3%,%#4 $)34).#4 O &2/- /RDER O ). OLINE)TEMS LI R" 3%,%#4 $)34).#4 /"*%#4O &2/- /RDER O ). OLINE)TEMS LI R# 3%,%#4 /"*%#4O &2/- /RDER O 7(%2% OLINE)TEMS R$ 3%,%#4 /"*%#4O &2/- /RDER O &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 58 HQWLW\EHDQUHODWLRQVKLSV 13 7HATS TRUE ABOUT %*" 1, PATH EXPRESSIONS #HOOSE ALL THAT APPLY R! )N A PATH EXPRESSION THE IS CONSIDERED THE NAVIGATION OPERATOR R" 0ATH EXPRESSIONS CAN TERMINATE WITH EITHER FPU¿HOG OR FPS¿HOG R# ! PATH EXPRESSION THAT ENDS IN A FPU¿HOG CANNOT BE FURTHER COMPOSED R$ ! PATH EXPRESSION CAN END WITH A SINGLE VALUE OR A COLLECTION VALUE 14 7HATS TRUE ABOUT %*" 1, QUERIES #HOOSE ALL THAT APPLY R! /F THE THREE CLAUSE TYPES 3%,%#4 &2/- AND 7(%2% ONLY THE 3%,%#4 CLAUSE IS REQUIRED R" 4HE 3%,%#4 CLAUSE DESIGNATES QUERY DOMAIN R# 4HE 7(%2% CLAUSE DETERMINES THE TYPES OF OBJECTS TO BE SELECTED R$ !N %*" 1, QUERY MAY HAVE PARAMETERS 15 7HATS TRUE ABOUT %*" 1, 7(%2% CLAUSES #HOOSE ALL THAT APPLY R! )DENTIlCATION VARIABLES USED IN A 7(%2% CLAUSE CAN BE DElNED ONLY IN THE &2/- CLAUSE R" )DENTIlCATION VARIABLES CAN REPRESENT A SINGLE VALUE OR A COLLECTION R# 4HE NUMBER OF INPUT PARAMETERS MUST EQUAL THE NUMBER OF PARAMETERS FOR THE lNDER OR SELECTOR METHOD R$ )NPUT CLAUSES CAN ONLY BE USED IN 7(%2% CLAUSES 16 'IVEN THE %*" 1, EXPRESSION S GLVFRXQW %(7:((1$1' 7HICH EXPRESSION IS EQUIVALENT R! R" R# R$ SGLVFRXQW!$1'SGLVFRXQW SGLVFRXQW! $1'SGLVFRXQW SGLVFRXQW!DQGSGLVFRXQW SGLVFRXQW! DQGSGLVFRXQW \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Page 59 Return to Table of Contents FRIIHHFUDPPRFNH[DP 17 18 7HATS TRUE ABOUT %*" 1, ). EXPRESSIONS #HOOSE ALL THAT APPLY R! R" R# R$ 4HE VLQJOHBYDOXHGBSDWKBH[SUHVVLRQ MUST HAVE A 3TRING VALUE 4HE ./4 LOGICAL OPERATOR CAN BE USED IN AN ). EXPRESSION 4HE STRING LITERAL LIST CAN BE EMPTY 4HE FOLLOWING EXPRESSION IS LEGAL RFRXQWU\ ). h5+v h53v 'IVEN THE %*" 1, EXPRESSION PRODUCTCODE ,)+% @&?S 7HICH VALUES WOULD RESULT IN A TRUE COMPARISON #HOOSE ALL THAT APPLY R! R" R# R$ R% &S &S &8S &S &ITS &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 60 HQWLW\EHDQUHODWLRQVKLSV 0RFN([DP$QVZHUV 1 7HATS TRUE FOR AN ENTITY BEAN PROVIDER USING CONTAINER MANAGED PERSISTENCE TO CREATE PERSISTENT RELATIONSHIPS #HOOSE ALL THAT APPLY R! ! LOCAL INTERFACE IS REQUIRED FOR SUCH A BEAN TO HAVE A BIDIRECTIONAL RELATIONSHIP WITH ANOTHER ENTITY BEAN R" 3UCH A BEAN CAN HAVE RELATIONSHIPS WITH ONLY SESSION BEANS R# 2ELATIONSHIPS CAN BE ONLY ONE TO ONE OR ONE TO MANY ion or Set R$ ! GETTER METHOD RETURN TYPE CAN BE JAVAUTIL,IST - must be Collect 2 7HATS TRUE FOR AN ENTITY BEAN PROVIDER USING CONTAINER MANAGED PERSISTENCE TO CREATE PERSISTENT RELATIONSHIPS #HOOSE ALL THAT APPLY R! ! REMOTE INTERFACE IS REQUIRED FOR SUCH A BEAN TO HAVE A BIDIRECTIONAL - must be local RELATIONSHIP WITH ANOTHER ENTITY BEAN R" 3UCH A BEAN CAN HAVE RELATIONSHIPS WITH ONLY MESSAGE DRIVEN BEANS R# ! GET METHOD RETURN TYPE CAN USE JAVAUTIL-AP - only Collection or Set R$ 2ELATIONSHIPS CAN BE ONE TO ONE ONE TO MANY OR MANY TO MANY 3 7HEN A REMOTE CLIENT INVOKES A METHOD ON AN ENTITY BEAN USING CONTAINER MANAGED PERSISTENCE AND THAT BEAN HAS ALREADY BEEN REMOVED WHAT EXCEPTION WILL BE THROWN R! R" R# R$ R% (spec: 132) MDYD[HME$FFHVV/RFDO([FHSWLRQ MDYD[HME2EMHFW1RW)RXQG([FHSWLRQ MDYDUPL1R6XFK2EMHFW([FHSWLRQ MDYDUPL6WXE1RW)RXQG([FHSWLRQ MDYD[HME1R6XFK(QWLW\([FHSWLRQ \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 61 PRFNH[DPDQVZHUV 4 'IVEN THE CONTAINER MANAGED UNIDIRECTIONAL RELATIONSHIP &OO (spec: 138) "AR !ND THE OBJECT RELATIONS F F B B 7HAT WILL BE TRUE AFTER THE FOLLOWING CODE RUNS #HOOSE ALL THAT APPLY I VHW%DU IJHW%DU R! R" R# R$ 5 QXOO EJHW)RR QXOO EJHW)RR QXOO NONE OF THE ABOVE (spec: 132) ONE TO ONE MANY TO ONE MANY TO MANY ALL OF THE ABOVE NONE OF THE ABOVE ) 7HICH DEPLOYMENT DESCRIPTOR ELEMENTS VALUES MUST BE A TYPE OF COLLECTION #HOOSE ALL THAT APPLY R! R" R# R$ IJHW%DU The setBar() method ÒbreaksÓ both of the existing object relationships and creates a single new one. )F AN ENTITY BEAN ! HAS BEEN REMOVED FROM A RELATIONSHIP WITH BEAN " IN WHICH CASES WILL BEAN !S ACCESSOR METHOD FOR BEAN " RETURN A NON NULL VALUE #HOOSE ALL THAT APPLY R! R" R# R$ R% 6 (spec: 463-464 HMEQDPH! FPU¿HOG! FPU¿HOGW\SH! - Collection or Set. HMEUHODWLRQ! &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 62 HQWLW\EHDQUHODWLRQVKLSV 7 7HICH DEPLOYMENT DESCRIPTOR ELEMENTS MUST HAVE EXACTLY TWO DECLARATIONS OF ANOTHER DEPLOYMENT DESCRIPTOR ELEMENT #HOOSE ALL THAT APPLY R! R" R# R$ R% 8 9 10 HMEQDPH! FPU¿HOG! FPU¿HOGW\SH! HMEUHODWLRQ! o its gotta have tw role> sub elements <ejb-relationship- HMEUHODWLRQVKLSUROH! 7HICH SETS OF ELEMENTS ARE MANDATORY WITHIN AN EJB RELATIONSHIP ROLE ELEMENT #HOOSE ALL THAT APPLY R! R" R# R$ R% (spec: 468) FPU¿HOG!PXOWLSOLFLW\! FPU¿HOG!UHODWLRQVKLSUROHVRXUFH! PXOWLSOLFLW\!UHODWLRQVKLSUROHQDPH! PXOWLSOLFLW\!UHODWLRQVKLSUROHVRXUFH! tionship, and whoÕs in theeyrerelalated th how are UHODWLRQVKLSUROHQDPH!UHODWLRQVKLSUROHVRXUFH! 7HICH ARE VALID VALUES FOR A CMR lELD TYPE ELEMENT #HOOSE ALL THAT APPLY R! R" R# R$ R% ) (spec: 467-468 3TRING )NTEGER MDYDXWLO6HW MDYDXWLO/LVW (spec: 464) terface Either a loCcaoll leinction or type, or a e local interSet of th face type MDYDXWLO&ROOHFWLRQ 7HICH SETS OF ELEMENTS ARE MANDATORY WITHIN AN EJB RELATION ELEMENT ) (spec: 467-468 R! HMEUHODWLRQQDPH!HMEUHODWLRQVKLSUROH! R" HMEUHODWLRQVKLSUROH!HMEUHODWLRQVKLSUROH! gotta have two partners R# GHVFULSWLRQ!HMEUHODWLRQQDPH! HMEUHODWLRQVKLSUROH! R$ HMEUHODWLRQQDPH!HMEUHODWLRQVKLSUROH! HMEUHODWLRQVKLSUROH! \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 63 PRFNH[DPDQVZHUV 11 'IVEN #-0 BEANS #USTOMER"EAN /RDER"EAN AND ,INE)TEMS"EAN WITH THE FOLLOWING RELATIONSHIPS (spec: 233) #USTOMER"EAN /RDER"EAN N /RDER"EAN ,INE)TEMS"EAN N AND THE FOLLOWING %*" 1, QUERY 3%,%#4 $)34).#4 /"*%#4 C &2/- #USTOMER C ). C/RDER O ). OLINE)TEMS LI 7(%2% OLSURGXFWBW\SH µUHIULJHUDWRU¶ 7HICH OF THE FOLLOWING PROPERLY DESCRIBES THE RESULT OF THE QUERY #HOOSE ALL THAT APPLY R! R" R# R$ 12 4HE QUERY IS INVALID !LL ORDERS THAT INCLUDE A LINE ITEM THAT REFERS TO A REFRIGERATOR !LL LINE ITEMS THAT REFER TO A REFRIGERATOR ause The SELECTthcle query at speciÞes th Customer must return objects. !LL CUSTOMERS THAT HAVE ORDERS THAT REFER TO A REFRIGERATOR 'IVEN #-0 BEANS #USTOMER"EAN /RDER"EAN AND ,INE)TEMS"EAN WITH THE FOLLOWING RELATIONSHIPS (spec: 236) #USTOMER"EAN /RDER"EAN N /RDER"EAN ,INE)TEMS"EAN N 7HICH WILL RETURN ALL ORDERS THAT HAVE LINE ITEMS #HOOSE ALL THAT APPLY R! 3%,%#4 $)34).#4 O &2/- /RDER O ). OLINE)TEMS LI R" 3%,%#4 $)34).#4 /"*%#4O &2/- /RDER O ). OLINE)TEMS LI R# 3%,%#4 /"*%#4O &2/- /RDER O 7(%2% OLINE)TEMS R$ 3%,%#4 /"*%#4O &2/- /RDER O &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 64 HQWLW\EHDQUHODWLRQVKLSV 13 7HATS TRUE ABOUT %*" 1, PATH EXPRESSIONS #HOOSE ALL THAT APPLY (spec: 225-226) R! )N A PATH EXPRESSION THE IS CONSIDERED THE NAVIGATION OPERATOR R" 0ATH EXPRESSIONS CAN TERMINATE WITH EITHER FPU¿HOG OR FPS¿HOG R# ! PATH EXPRESSION THAT ENDS IN A FPU¿HOG CANNOT BE FURTHER COMPOSED R$ ! PATH EXPRESSION CAN END WITH A SINGLE VALUE OR A COLLECTION VALUE 14 7HATS TRUE ABOUT %*" 1, QUERIES #HOOSE ALL THAT APPLY (spec: 218-219) R! /F THE THREE CLAUSE TYPES 3%,%#4 &2/- AND 7(%2% ONLY THE 3%,%#4 CLAUSE IS REQUIRED R" 4HE 3%,%#4 CLAUSE DESIGNATES QUERY DOMAIN R# 4HE 7(%2% CLAUSE DETERMINES THE TYPES OF OBJECTS TO BE SELECTED R$ !N %*" 1, QUERY MAY HAVE PARAMETERS 15 7HATS TRUE ABOUT %*" 1, 7(%2% CLAUSES #HOOSE ALL THAT APPLY (spec: 227-228) R! )DENTIlCATION VARIABLES USED IN A 7(%2% CLAUSE CAN BE DElNED ONLY IN THE &2/- CLAUSE R" )DENTIlCATION VARIABLES CAN REPRESENT A SINGLE VALUE OR A COLLECTION R# 4HE NUMBER OF INPUT PARAMETERS MUST EQUAL THE NUMBER OF PARAMETERS FOR THE lNDER OR SELECTOR METHOD R$ )NPUT CLAUSES CAN ONLY BE USED IN 7(%2% CLAUSES 16 'IVEN THE %*" 1, EXPRESSION (spec: 229) S GLVFRXQW %(7:((1$1' 7HICH EXPRESSION IS EQUIVALENT R! R" R# R$ SGLVFRXQW!$1'SGLVFRXQW SGLVFRXQW! $1'SGLVFRXQW SGLVFRXQW!DQGSGLVFRXQW SGLVFRXQW! DQGSGLVFRXQW \RXDUHKHUH Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 7. When Beans Relate Return to Table of Contents Page 65 PRFNH[DPDQVZHUV 17 18 (spec: 229-230 7HATS TRUE ABOUT %*" 1, ). EXPRESSIONS #HOOSE ALL THAT APPLY R! R" R# R$ ) 4HE VLQJOHBYDOXHGBSDWKBH[SUHVVLRQ MUST HAVE A 3TRING VALUE 4HE ./4 LOGICAL OPERATOR CAN BE USED IN AN ). EXPRESSION 4HE STRING LITERAL LIST CAN BE EMPTY 4HE FOLLOWING EXPRESSION IS LEGAL RFRXQWU\ ). h5+v h53v 'IVEN THE %*" 1, EXPRESSION watch out foesr! double quot (spec: 230) PRODUCTCODE ,)+% @&?S 7HICH VALUES WOULD RESULT IN A TRUE COMPARISON #HOOSE ALL THAT APPLY R! R" R# R$ R% &S &S &8S &S &ITS ter is a The % charac quence r fo rd wildca tersa. seThe _ is ac of char for a single a wildcard character &KDSWHU Chapter 7. When Beans Relate Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Table of Contents Chapter 8. Getting the Message................................................................... 1 Section 8.1. OBJECTIVES............................................................................................................................................................... 2 Section 8.2. Imagine this scenario.................................................................................................................................................. 3 Section 8.3. Too bad these guys aren't message-driven beans....................................................................................................... 5 Section 8.4. Message-driven bean class........................................................................................................................................ 10 Section 8.5. Writing a message-driven bean: your job as Bean Provider..................................................................................... 11 Section 8.6. Sharpen your pencil................................................................................................................................................... 11 Section 8.7. Notice something missing from the code?................................................................................................................ 13 Section 8.8. Complete DD for a message-driven bean................................................................................................................. 13 Section 8.9. Topics and Queues.................................................................................................................................................... 14 Section 8.10. There are no: Dumb Questions............................................................................................................................... 16 Section 8.11. Only ONE bean per pool gets a copy of a topic message......................................................................................... 17 Section 8.12. With a queue, only one bean gets the message. Period.......................................................................................... 18 Section 8.13. MessageDrivenContext............................................................................................................................................ 19 Section 8.14. MessageDrivenContext........................................................................................................................................... 20 Section 8.15. message acknowledgement..................................................................................................................................... 22 Section 8.16. That's all well and good, but let's go back and see how our earlier scenario ended.............................................. 23 Section 8.17. Off the path: Think about it..................................................................................................................................... 24 Section 8.18. COFFEE CRAM....................................................................................................................................................... 27 Section 8.19. COFFEE CRAM....................................................................................................................................................... 30 Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 1 message-driven beans 5 4 4 *HWWLQJWKH0HVVDJH :RZ0HVVDJHGULYHQ EHDQVDUHIDQWDVWLF,FDQVHQGD PHVVDJHDQGWKHQJREDFNWRZKDW, ZDVGRLQJEHIRUHZLWKRXWZDLWLQJ IRUDUHSO\7KDWPHDQV,KDYHPRUH WLPHWRUHDGWKLVQLIW\´+HDG )LUVW6OLGH5XOHµERRN ,W·VIXQWRUHFHLYHPHVVDJHV1RWDVPXFKIXQDVVD\JHWWLQJWKDW(%D\ SDFNDJHZLWKWKHJHQXLQH6PXUIODPSEXWIXQDQGHI¿FLHQWQRQHWKHOHVV,PDJLQH LI\RXVHQW\RXURUGHUWR(%D\DQG\RXFRXOGQ¶WOHDYH\RXUKRXVHXQWLOWKHSDFNDJH ZDVGHOLYHUHG7KDW¶VZKDWLW¶VOLNHZLWK6HVVLRQDQG(QWLW\EHDQVEHFDXVHWKHUH¶VDOO FDOOVWRDEHDQ¶VFOLHQWLQWHUIDFH KRPHFRPSRQHQWORFDO5HPRWHGRHVQ¶WPDWWHU DUH V\QFKURQRXV%XWZLWKPHVVDJHGULYHQEHDQVWKHFOLHQWPDNHVDPHVVDJHDQGVHQGVLW WRDPHVVDJLQJVHUYLFH7KHQWKHFOLHQWZDONVDZD\/DWHUWKHPHVVDJLQJVHUYLFHVHQGV WKH&RQWDLQHUWKHPHVVDJHDQGWKH&RQWDLQHUJLYHLWWRDPHVVDJHGULYHQEHDQ WKLVLVDQHZFKDSWHU Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 2 H[DPREMHFWLYHV 0HVVDJH'ULYHQ%HDQV 2IILFLDO ,GHQWLI\FRUUHFWDQGLQFRUUHFWVWDWHPHQWV RUH[DPSOHVDERXWWKHFOLHQWYLHZDQG OLIHF\FOHRIDPHVVDJHGULYHQEHDQ :KDWLWUHDOO\PHDQV <RXKDYHWRNQRZWKDWPHVVDJHGULYHQEHDQVGRQ¶W KDYHFOLHQWV7KDWPHDQVQRKRPHLQWHUIDFHQR FRPSRQHQWLQWHUIDFHDQGQRLVVXHVRIORFDOYV 5HPRWH2IFRXUVHPHVVDJHGULYHQEHDQVGRKDYHD FDOOHUEXWWKDW¶VWKH&RQWDLQHU$QGZHGRQ¶WFRQVLGHU WKH&RQWDLQHUDFOLHQW7KH&RQWDLQHULVWKHERVVQRW WKHFXVWRPHU 0HVVDJHGULYHQEHDQVKDYHDYHU\VLPSO\OLIHF\FOH²LI \RXNQRZKRZVWDWHOHVVVHVVLRQEHDQVZRUN\RX NQRZKRZPHVVDJHGULYHQEHDQVZRUN7KHRQO\ GLIIHUHQWLVWKDWLQVWHDGRIEULQJLQJDEHDQRXWRIWKH SRROWRVHUYLFHDFOLHQWPHWKRGFDOOWKH&RQWDLQHU EULQJVPHVVDJHGULYHQEHDQVRXWRIWKHSRROWR VHUYLFHDQLQFRPLQJ-06PHVVDJH Copyright Safari Books Online #896963 ,GHQWLI\WKHLQWHUIDFH V DQGPHWKRGVD PHVVDJHGULYHQEHDQPXVWLPSOHPHQW ,GHQWLI\WKHXVHDQGEHKDYLRURIWKH 0HVVDJH'ULYHQ&RQWH[WLQWHUIDFHPHWKRGV )URPDOLVWLGHQWLI\WKHUHVSRQVLELOLW\RIWKH %HDQ3URYLGHUDQGWKHUHVSRQVLELOLW\RIWKH &RQWDLQHUSURYLGHUIRUDPHVVDJHGULYHQ EHDQ <RXPXVWNQRZWKDWPHVVDJHGULYHQEHDQVLPSOHPHQW WZRLQWHUIDFHV²MDYD[HME0HVVDJH'ULYHQ%HDQWKH LQWHUIDFHZLWKWKH&RQWDLQHUFDOOEDFNV QRWPDQ\ DQG MDYD[MPV0HVVDJH/LVWHQHUWKHLQWHUIDFHZLWKDVLQJOH RQ0HVVDJH 0HVVDJHP PHWKRG)RUDPHVVDJH GULYHQEHDQRQ0HVVDJH LVWKHRQO\EXVLQHVV PHWKRG <RXKDYHWRNQRZWKDWPHVVDJHGULYHQEHDQVFDQ¶W FDOOPRVWRIWKHPHWKRGVLQ0HVVDJH'ULYHQ&RQWH[W HYHU7KLQNDERXWLW,IWKHUH¶VQRFOLHQWKRZFRXOG \RXJHWFOLHQWVHFXULW\LQIR",IWKHUH¶VQRFOLHQWZKLFK PHDQVQRFOLHQWYLHZKRZFRXOG\RXFDOOJHW(-%2E MHFW RUJHW(-%+RPH "<RXGRQ¶WKDYHDKRPH<RX GRQ¶WKDYHDQ(-%2EMHFW <RXDOVRKDYHWRUHFRJQL]HWKDWLIWKHUH¶VQRFOLHQW\RX FDQ¶WGHFODUHDQ\FKHFNHGH[FHSWLRQV:KRZRXOG FDWFKWKHP"$OVRMXVWDVZLWKVWDWHOHVVVHVVLRQEHDQV LI\RXVWDUWDWUDQVDFWLRQLQDPHWKRG ZKLFKFDQRQO\ PHDQD%07EHDQLQRQ0HVVDJH \RXPXVW¿QLVKLW EHIRUHWKHPHWKRGHQGV &KDSWHU Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 3 PHVVDJHGULYHQEHDQV 1UIOQVM\PQ[[KMVIZQW 9OU HAVE TO ASK SOMEONE TO DO A VERY IMPORTANT JOB 9OU HAVE NO IDEA HOW LONG ITS GOING TO TAKE THEM 9OU HAVE TO WAIT RIGHT WHERE YOU ARE UNTIL THEY lNISH 9OU CANT DO ANYTHING ELSE WHILE YOURE WAITING SOHDVHRKSOHDVHGRQ·W WDNHDOOGD\RQWKLV,KDYH 62PDQ\RWKHUWKLQJV,·G UDWKHUEHGRLQJWKDQZDLW KHUHIRU\RXWRÀQLVK +TQMV\ \RXDUHKHUH Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 4 PHVVDJHGULYHQEHDQV *HH]KRZ KDUGFDQLWEH":KDW LVWKHVHUYHUGRLQJ " &KDSWHU Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 5 PHVVDJHGULYHQEHDQV +H\ZH·UHDWWKH SRROGXGH,WKRXJKW, WROG\RXQHYHUWRFDOOPH KHUH 7HOOKLPWRFKLOO:H·OO JHWWRLWDQGLW·VQRW OLNHKH·VJRWDQ\WKLQJ EHWWHUWRGR ;MZ ^MZ +TQMV\ JMNWZMUM[[IOMLZQ^MVJMIV[ <WWJIL\PM[MO]a[IZMV¼\ UM[[IOMLZQ^MVJMIV[ -ETHOD CALLS LOCAL OR REMOTE ARE SYNCHRONOUS 4HE CALLER IS STUCK WAITING UNTIL THE SERVER RETURNS -ESSAGE DRIVEN BEANS ADDED IN %*" GIVE YOU ASYNCHRONOUS COMMUNICATION BETWEEN THE CLIENT MESSAGE SENDER AND THE SERVER MESSAGE RECEIVER )N MESSAGING TERMS THE SENDER IS CALLED THE MESSAGE 0RODUCER AND THE RECEIVER IS CALLED THE MESSAGE #ONSUMER IN\MZUM[[IOMLZQ^MVJMIV[ 7ITH MESSAGING THE 0RODUCER SENDS A MESSAGE AND THEN GOES ABOUT HIS BUSINESS (E DOESNT HAVE TO WAIT FOR THE #ONSUMER TO EVEN GET THE MESSAGE LET ALONE PROCESS IT 7HEN THE #ONSUMER GETS A MESSAGE HE PROCESSES IT )N THE MEANTIME THE CLIENT CAN STILL HAVE A LIFE \RXDUHKHUH Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 6 PHVVDJHGULYHQEHDQV 0HVVDJHGULYHQEHDQRYHUYLHZ &OLHQW 3URGXFHU VHQGDPHVVDJHWR WKHPHVVDJLQJVHUYLFH PHVVDJLQJVHUYLFH JH VVD PH EHDQ EHDQ EHDQ FOLHQW 0HVVDJHGULYHQ %HDQ3RRO 3URGXFHU 0HVVDJHLVGHOLYHUHGWRWKH &RQWDLQHU&OLHQWLVGRLQJRWKHU FOLHQWWKLQJV PHVVDJLQJVHUYLFH PHV VDJ H EHDQ EHDQ EHDQ WRVRP HRWK VHUYHU HU 0HVVDJHGULYHQ %HDQ3RRO FOLHQW 3URGXFHU &RQWDLQHUDFNQRZOHGJHVWKH PHVVDJHWRWKHVHUYLFHDQG UHPRYH SXOOVDEHDQRXWRIWKHSRROWR \RXFDQ 2.JRWLW WKHTXHXH P SURFHVVWKHPHVVDJH LWIUR PHVVDJLQJVHUYLFH EHDQ EHDQ FOLHQW EHDQ 0HVVDJHGULYHQ %HDQ3RRO 3URGXFHU &KDSWHU Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 7 PHVVDJHGULYHQEHDQV &RQWDLQHULQYRNHVWKHEHDQV RQ0HVVDJH PHWKRGZLWKWKH PHVVDJHDVWKHDUJXPHQW PHVVDJLQJVHUYLFH RQ0 HVV DJH FOLHQW EHDQ EHDQ PVJ EHDQ 0HVVDJHGULYHQ %HDQ3RRO 3URGXFHU 7KHEHDQ·VWUDQVDFWLRQFRPPLWV DQGWKH&RQWDLQHUVHQGVWKHEHDQ EDFNWRWKHSRRO PHVVDJLQJVHUYLFH ,IWKHEHDQ·VWUDQVDFWLRQLQ RQ0HVVDJHKDGUROOHGEDFN WKH&RQWDLQHUZRXOGKDYHWROGWKH PHVVDJLQJVHUYLFHWRSXWWKH PHVVDJHEDFNRQWKHTXHXH EHDQ EHDQ EHDQ 0HVVDJHGULYHQ %HDQ3RRO %HDQ3RRO ,WV¼\\PMaTWWSITW\TQSM[\I\MTM[[[M[[QWVJMIV[' ,IKE STATELESS SESSION BEANS MESSAGE DRIVEN BEANS HAVE NO UNIQUE IDENTITY TO CLIENTS ACTUALLY THEY DONT HAVE CLIENTS SINCE THE #ONTAINER ISNT REALLY CONSIDERED A CLIENT TO THE BEAN THEYRE POOLED AND THEY HAVE NO INDIVIDUAL STATE THAT AFFECTS THEIR BUSINESS LOGIC 4HAT MEANS ONE MESSAGE DRIVEN BEAN OF A PARTICULAR TYPE IS THE SAME AS ANY OTHER BEAN FROM THE SAME HOME *UST LIKE STATELESS BEANS \RXDUHKHUH Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 8 PHVVDJHGULYHQEHDQV 0XOWLSOHEHDQVRIWKHVDPH W\SHFDQSURFHVVPHVVDJHV FRQFXUUHQWO\ VJ JH P HVVD RQ0 PHVVDJLQJVHUYLFH RQ0 HVV DJH EHDQ EHDQ :HOFRPH%HDQ J PV JH VVD 0H RQ PVJ EHDQ :HOFRPH%HDQ :HOFRPH%HDQ 0'% %HDQ3RRO EHDQ :HOFRPH%HDQ %XWWKHFRQWDLQHUZLOOPDNHVXUH WKDWHDFKEHDQLVWKUHDGVDIH &DQ·W\RXVHH,·P %86<",·YHDOUHDG\JRWD PHVVDJHJRLQJ'RQ·WJHWPH VWDUWHGRQDQRWKHURQH :HOFRPH%HDQ 1RSUREOHP,·OO PDNHVXUHQRRWKHU WKUHDGVERWKHU\RXXQWLO \RX·UHGRQHZLWKWKLVRQH &RQWDLQHU &KDSWHU Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 9 PHVVDJHGULYHQEHDQV 7KHOLIHF\FOHRIPHVVDJHGULYHQEHDQV ORRNVMXVWOLNHVWDWHOHVVVHVVLRQEHDQV 0HVVDJHGULYHQ EHDQ GRHVQRWH[LVW FRQVWUXFWRU VHW0HVVDJH'ULYHQ&RQWH[W HME&UHDWH EHDQWKURZVV\VWHPH[FHSWLRQ XQFKHFNHGXQFDXJKW HME5HPRYH PHWKRGUHDG\ n passivatio note: noivation or act RQ0HVVDJH ly this is the onbetween difference ssion stateless se d MDB lifecycle an lifecycle. message-driven beans have only ONE business methodÑ onMessage(). SXEOLFYRLGRQ0HVVDJH 0HVVDJHPVJ ^ RSHQWKHPHVVDJHDQGGRVRPHWKLQJZLWKLW ` a mesbusiness logic thing ly on he T (other ! it do s n tÕ ca Tha needs to doÑor sage-driven beancontainer callbacks for creating, than the three tting its context) removing, and se \RXDUHKHUH Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 10 PHVVDJHGULYHQEHDQV 5M[[IOMLZQ^MVJMIVKTI[[ SDFNDJHKHDG¿UVW LPSRUWMDYD[HME LPSRUWMDYD[MPV rt: a new impo.jms package x the java t! es to implemen TWO interfac SXEOLFFODVV:HOFRPH%HDQLPSOHPHQWV0HVVDJH'ULYHQ%HDQ0HVVDJH/LVWHQHU^ javax.ejb.MessageDriv enBean (with the cont er callbacks) javax.jm ainthe onMessage() callb s.MessageListener (with ack method) SULYDWH0HVVDJH'ULYHQ&RQWH[WFRQWH[W extends EJBObject jus t like SessionContext EntityContext and SXEOLFYRLGHME&UHDWH ^` you MUST have a single, no-arg ejbCreate() SXEOLFYRLGHME5HPRYH ^` works just like stateless session beans... called when the Container wants to reduce the pool SXEOLFYRLGVHW0HVVDJH'ULYHQ&RQWH[W 0HVVDJH'ULYHQ&RQWH[WFW[ ^ FRQWH[W FW[ just do the same thing ` do... save your context you always SXEOLFYRLGRQ0HVVDJH 0HVVDJHPVJ ^ SURFHVVWKHPHVVDJH WU\^ FINALLY! A real bus ss method. This is the only methodinedeÞ MessageListener interface ned in the . LI PVJLQVWDQFHRI7H[W0HVVDJH ^ 7H[W0HVVDJHPHVVDJH 7H[W0HVVDJH PVJ ge, but it might TextMessage is-a Messa message like have been another kindessof , ObjectMesage pM Ma ge, BytesMessa we test it So . age sage or StreamMess the cast. ore bef eof anc with inst 6\VWHPRXWSULQWOQ PHVVDJHJHW7H[W ` `FDWFK -06([FHSWLRQH[ ^ H[SULQW6WDFN7UDFH ` Yikes. A checked ` from the getText(ex) ception ! ItÕs ` method. &KDSWHU Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 11 PHVVDJHGULYHQEHDQV ?ZQ\QVOIUM[[IOMLZQ^MVJMIV" aW]ZRWJI[*MIV8ZW^QLMZ 7KLVWLPHWKHUH·VQRFOLHQW YLHZVR,GRQ·WKDYHWR PDWFKDQ\WKLQJIURPWKHKRPH RUFRPSRQHQWLQWHUIDFHV 7KH\GRQ·WH[LVW-XVWOLNHPH <RXSXW7+5((NLQGVRIPHWKRGV LQWKHEHDQFODVV %HDQ/DZHME&UHDWH PHWKRG :ULWHDVLQJOHQRDUJXPHQWHME&UHDWH PHWKRGLQWKH EHDQ,WGRHVQ¶WPDWFKDQ\WKLQJRUFRPHIURPDQ\LQWHU IDFH,W¶VWKHUHEHFDXVHLW0867EH LQWHUIDFH!! 0HVVDJH/LVWHQHU RQ0HVVDJH 0HVVDJH/LVWHQHULPSOHPHQWDWLRQ RQ0HVVDJH 7KLVLV\RXUEXVLQHVVPHWKRG<RXURQO\EXVLQHVV PHWKRG LQWHUIDFH!! 0HVVDJH'ULYHQ%HDQ HME5HPRYH VHW0HVVDJH'ULYHQ&RQWH[W 0HVVDJH'ULYHQ%HDQLPSOHPHQWDWLRQ FRQWDLQHUFDOOEDFNV ,PSOHPHQWERWKRIWKHPHWKRGVIURPWKH 0HVVDJH'ULYHQ%HDQLQWHUIDFHZKLFK\RXUEHDQPXVW LPSOHPHQWLQWKHRI¿FLDO-DYDZD\ LHXVLQJWKH µLPSOHPHQWV0HVVDJH'ULYHQ%HDQ¶GHFODUDWLRQHLWKHULQ \RXUEHDQFODVVRURQHRILWVVXSHUFODVVHV 6KDUSHQ \RXU SHQFLO 2IWKHWKUHHW\SHVRIPHWKRGV\RX SXWLQ\RXUEHDQFKHFNRIIWKHRQHV WKHFRPSLOHUFDUHVDERXW &RPSLOHUFKHFNHG" HME&UHDWH RQ0HVVDJH HME5HPRYH DQGVHW0HVVDJH'ULYHQ&RQWH[W \RXDUHKHUH Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID: doannn16@yahoo.com Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. Chapter 8. Getting the Message Return to Table of Contents Page 12 PHVVDJHGULYHQEHDQV 5XOHVIRUWKHPHVVDJHGULYHQEHDQFODVV 7KHFODVVPXVWLPSOHPHQWMDYD[HME0HVVDJH'ULYHQ%HDQDQG MDYD[HME0HVVDJH/LVWHQHU SXEOLFFODVV:HOFRPH%HDQLPSOHPHQWV0HVVDJH'ULYHQ%HDQ0HVVDJH/LVWHQHU^ 7KHFODVVPXVWEHSXEOLFPXVWQRWEH¿QDODQGPXVWQRWEHDEVWUDFW 7KHFODVVPXVWKDYHDSXEOLFFRQVWUXFWRUWKDWWDNHVQRDUJXPHQWV -XVWOLNH WKHRWKHUEHDQVVRWKH&RQWDLQHUFDQPDNHDQHZLQVWDQFH SXEOLF:HOFRPH%HDQ ^` nstructor. t in the defaultdoconÕt put any pu r ile mp co e th If you can, justhaletve one, be SURE itÕs no-arg, and But if you do for ejbCreate() code in it. Wait 7KHFODVVPXVWKDYHDQRDUJHME&UHDWH PHWKRG,WPXVWEHSXEOLFQRW ¿QDOQRWVWDWLFZLWKDYRLGUHWXUQW\SH SXEOLFYRLGHME&UHDWH ^` code in here. By Put initialization thod is called, you the time this meur context. already have yo 7KHFODVVPXVWGH¿QHWKHRQ0HVVDJH PHWKRGIURPWKH 0HVVDJH/LVWHQHULQWHUIDFH,WPXVWEHSXEOLFQRW¿QDOQRWVWDWLF ZLWKDYRLGUHWXUQW\SHDQGLWWDNHVDVLQJOHDUJXPHQWRIW\SH MDYD[MPV0HVVDJH SXEOLFYRLGRQ0HVVDJH 0HVVDJHPVJ ^` The REAL business method. You r logic goes here... 0HVVDJHGULYHQ EHDQVFDQ·W GHFODUHFKHFNHG H[FHSWLRQV <RX¶OOOHDUQPRUHDERXW XWIRU H[FHSWLRQVLQ&KDSWHUE UH¶VQR 7KH WLW ERX QND WWKL MXV QRZ SHFWLQJ FOLHQW6RZKRDUH\RXH[ HVH GWK URXQ FKD FDW DWU\ WRZUDS JK FDOOV"7KH&RQWDLQHUZLOOODX 867 LI\RXWU\WRGRWKLV<RX0 FNHG FDWFKDQGKDQGOHDQ\FKH H[FHSWLRQVWKDW\RXJHW <RXPXVWKDYHWKHHME5HPRYH DQGVHW0HVVDJH'ULYHQ&RQWH[W PHWKRGVIURPWKH0HVVDJH'ULYHQ%HDQLQWHUIDFHH[DFWO\DVGHFODUHG LQWKHLQWHUIDFH SXEOLFYRLGHME5HPRYH ^` SXEOLFYRLGVHW0HVVDJH'ULYHQ&RQWH[W 0HVVDJH'ULYHQ&RQWH[WFW[ ^ FRQWH[W FW[ ` 1RPHWKRGVLQWKHFODVVDUHDOORZHGWRWKURZDSSOLFDWLRQH[FHSWLRQV $QGWKH\ VKRXOGQ¶WEHGHFODULQJUXQWLPHH[FHSWLRQVHLWKHUDOWKRXJKLW¶VWHFKQLFDOO\OHJDO &KDSWHU Chapter 8. Getting the Message Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safar