版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、大連交通大學2012屆本科生畢業(yè)設計 (論文) 外文翻譯CHAPTER 8 Writing Bean-Managed Persistent Entity BeansIn Chapter 7, we covered some basic entity bean concepts. We learned that there are two kinds of entity beansbean-managed persistent and container-managed persistent.In this chapter, well demonstrate how to program bea
2、n-managed persistent entity beans. When you code these types of entity beans, you must provide your own data access logic. You are responsible for providing the implementation to map your entity bean instances to and from storage. To do this, youd typically. use a database API such as JDBC or SQL/J.
3、 This is in stark contrast to container-managed persistent entity beans, which have their data access handled for them by the EJB container. This chapter will teach you the basics of bean-managed persistence and show you how to build a simple bean-managed entity bean using JDBC.Implementation Guidel
4、ines for Bean-Managed PersistenceIn Chapter 7, we saw that all entity bean classesboth bean-managed persistent and container-managed persistentmust implement the javax.ejb.EntityBean interface. This interface defines callback methods that the container invokes on your beans. What you should put in t
5、hese methods depends in part on whether you are using bean-managed persistence or container-managed persistence. Table 8.1 is a summary of what you should implement in each method, assuming your entity beans persistence is bean-managed. Take a quick glance at the.Table 8.1 Descriptions and Implement
6、ation Guidelines for Bean-Managed Persistent Entities.chart for now. You should refer back to the chart when reading through the code in this chapter or when programming your own entity bean classes. The order of methods listed very roughly models the flow of control of an entity bean instances life
7、 cycle that we saw at the end of Chapter 7.Bean-Managed Persistence Example: A Bank AccountOur first example will be a simple bank account entity bean. This bank account bean can be used to represent and manipulate real bank account data in an underlying relational database. The object model for our
8、 bank account is detailed in Figure 8.1.Lets take a look at each of the files that we must create for our entity bean component.Figure 8.1 The bank account object model.Account.javaAccount.java is our entity beans remote interfacewhat the client sees. Its shown in Source 8.1.Notice that the account
9、remote interface extends javax.ejb.EJBObject, which all remote interfaces must do. Our interface exposes a number of methods for manipulating entity beans, such as for making deposits and withdrawals. All of our methods throw remote exceptions to facilitate system-level catastrophic.Source 8.1 Accou
10、nt.java.failures. Notice that in our withdrawal method, we also throw our own custom application-level exception, Account Exception. Well define that exception bit later.AccountHome.javaOur home interface is specified by AccountHome.java, shown in Source 8.2.Source 8.2 AccountHome.java (continued).W
11、e provide one method to create a new account. This will create new database data representing a bank account. It returns an EJB object to the client so that the client can manipulate that newly created account. Notice that we throw the application-level javax.ejb.CreateException, which all create()
12、methods must throw.We also have two finder methods. findByPrimaryKey() searches the database for a bank account that already exists; it searches by the account ID, which we will define below in AccountPK.java. We also have a custom finder method, findByOwnerName(), which searches the database for al
13、l bank accounts that have the same owners name. Because were using bean-managed persistence. well need to implement both of these finder methods in our entity bean implementation (if we were using container-managed persistence, the container would search the database for us). As with our create meth
14、od, both finders return EJB objects so that the client can manipulate the newly found bank accounts. We throw the application-level javax.ejb.FinderException, which all finders must throw.AccountPK.javaOur entity beans primary key class is defined by AccountPK.java, detailed inSource 8.3.Our primary
15、 key is a simple stringthe account ID string. For example, an account ID string could be “ABC-123-0000.” This string must be unique to its bank accountwe rely on the client code that constructs our account ID to make sure its unique. The primary key is used to identify each bank account uniquely.Acc
16、ountBean.javaNext, we have our entity bean implementation class, AccountBean.java. Our bean implementation code is quite lengthy, and it is divided into several sections: Bean-managed state fields. These are the persistable fields of our entity bean class. Our bean instance will load and store the d
17、atabase data into these fields.Source 8.3 AccountPK.java.Business logic methods. These methods perform services for clients, such as withdrawing or depositing into an account. They are exposed by the remote interface, Account.EJB-required methods. These are EJB-required methods that the container wi
18、ll call to manage our bean. They also include our creator and finder methods defined in the home interface.The code is presented in Source 8.4. Notice how cumbersome the code isjust for a simple bank account. This is an unfortunate drawback of bean-managed persistence because you must provide all da
19、ta access code.Notice that most of the logic in our bean is JDBC code. Our withdraw and deposit methods simply modify the in-memory fields of the entity bean instance. If the client tries to withdraw from a negative account, we throw our custom application-level exception, AccountException. Whenever
20、 we perform persistent operations, we retrieve a JDBC connection via the getConnection() method.We acquire our environment information from the EntityContext by calling getEnvironment(). We then use that environment as a parameter to the JDBC DriverManagers getConnection() method. This environment s
21、pecifies the JDBC drivers to load, via the jdbc.drivers property. We specify this property in the Source 8.4 AccountBean.java (continues).application-specific environment properties that ship with our bean, as well see very shortly. We also specify the particular database to connect to via a propert
22、y we call JDBC_URL. This property is passed to the DriverManager as well, so it knows with which database to hook up.In our bank account example, we retrieve our JDBC connections via the JDBC call DriverManager.getConnection(). We also close each connection after every method call. This allows our E
23、JB container to pool JDBC connections. When the connection is not in use, another bean can use our connection.Although this works with BEA WebLogic server, it is not a standard, portable way for connection pooling. WebLogic performs pooling directly beneath the JDBC 1.0 driver shell, but other EJB v
24、endors may require different mechanisms for pooling database connections. Connection pooling is unfortunately not specified by JDBC 1.0, which means that any enterprise beans that use JDBC 1.0 are not very portable.There is a light at the end of the tunnel. The new JDBC 2.0 specification, which has
25、been finalized, supports a portable mechanism of database connection pooling. Already vendors are beginning to support JDBC 2.0, and by the time you read this, most every serious relational database vendor should have a JDBC 2.0 driver. The Java 2 Platform, Enterprise Edition (J2EE) specification ma
26、ndates support of JDBC 2.0 as well, which is good news for anyone writing to J2EE and the EJB 1.1 specification. Furthermore, EJB 1.1 specifies a portable way to retrieve a JDBC driver through the Java Naming and Directory Interface (JNDI), which we detail in Appendix D.Our finder methods use JDBC t
27、o perform SELECT statements on the relational database to query for bank account records. We create a new primary key class for the data we find, and we return the primary key to the container. The container will then instantiate EJB objects that match each primary key, so that the clients can start
28、 working with the data. To create a new bank account, the client calls create() on the home object, which calls our ejbCreate() and ejbPostCreate() methods. Notice that we insert some data into the database using JDBC in ejbCreate(). We also assign our member variables the data passed in from the cl
29、ient. We dont need to use ejbPostCreate() for anything. Our entity bean is now associated with some specific database data and is associated with a client-specific EJB object.Notice that we dont hold any bean-independent resources, so our setEntity- Context() and unsetEntityContext() methods are fai
30、rly bare-boned. We also dont hold any bean-specific resources, so our ejbPassivate() and ejbActivate() methods are empty.When the container synchronizes our bean with the database, the ejbLoad() and ejbStore() methods perform JDBC persistence, thus keeping everyones data in synch. Notice that ejbLoa
31、d() acquires the primary key via a getPrimaryKey() call to the Entity Context. This is how it figures out what data to load.JDBC can be very tough to debug due to incompatibilities between databases. Its much easier to debug JDBC if you log what JDBC is doing behind the scenes. To do this, see the c
32、ommented code in the getConnection() method of AccountBean.java. Simply uncomment those lines to enable logging.AccountException.javaOur custom exception class is AccountException.java, displayed in Source 8.5. It simply delegates to the parent java.lang.Exception class. Its still useful to define o
33、ur own custom exception class, however, so that we can distinguish between a problem with our bank account component, and a problem with another part of a deployed system.Source 8.5 AccountException.java.Client.javaOur last Java file is a simple test client to exercise our beans methods. Its shown i
34、n Source 8.6.The client code is fairly self-explanatory. We perform some bank account operations in the try block. We have a finally clause to make sure our bank account is properly deleted afterward, regardless of any exceptions that may have been thrown.The Deployment DescriptorNow, lets take a lo
35、ok at our deployment descriptor. The deployment descriptors for entity beans are slightly different from those for their sister session beans. Our deployment descriptor is shown in Table 8.2.Table 8.2 Deployment Descriptor Settings for AccountBean.Table 8.2 illustrates a typical deployment descripto
36、r for a bean-managed persistent entity bean. Notice that we have two new fields that we do not have for session beans:The primary key class nameIdentifies the Java class for our primary key. Session beans do not have primary keys because they are not persistent.The container-managed fields Entry spe
37、cifies what fields of your entity bean class are persistent fields. This applies only to container-managed persistent entity beans (described in Chapter 9), and it should be left blank when using bean-managed persistence.Environment PropertiesNext, we have our beans custom environment properties. Th
38、ese environment properties allow consumers of your bean to tune your beans functionality without touching your beans source code (shown originally in Chapter 6). Our bean class retrieves these properties via EntityContext.getEnvironment(). The properties are shown in Table 8.3.Notice that were using
39、 enterprise bean environment properties to specify JDBC initialization information. This enables a consumer of our bean to use the database of his or her choice without modifying our bean code. The particular JDBC settings you use will vary depending on your configuration. Consult your database docu
40、mentation or JDBC driver documentation for more details.The JDBC_URL setting is passed to the DriverManager to locate the proper database. The jdbc.drivers setting is passed to the DriverManager to locate the proper JDBC driver.Setting Up the DatabaseLastly, you need to create the appropriate databa
41、se table and columns for our bank accounts. You can do this through your databases GUI or command-line interface. The books included CD-ROM comes with a preconfigured sample database that you can use right away. If youre using a different database, you Table 8.3 Environment Properties for AccountBea
42、n should enter the following SQL Data Definition Language (DDL) statements in your databases SQL interface:This creates an empty table of bank accounts. The first column is the bank account id (the primary key), the second column is the bank account owners name, and the third column is the bank acco
43、unt balance.Running the Client ProgramTo run the client program, type a command similar to the following (depending on what your EJB containers Java Naming and Directory Interface (JNDI) connection parameters aresee your containers documentation):The initialization parameters are required by JNDI to
44、 find the home object, as we learned in Chapter 4.Server-Side OutputWhen you run the client, you should see something similar to the following on the server side. Note that your particular output may vary, due to variances in EJB container behavior.Notice whats happening here:When our client code ca
45、lled create() on the home object, the container created an entity bean instance. The container first called newInstance() and setEntityContext() to get the entity bean into the available pool of entity beans. The container then serviced our clients create() method by taking that bean out of the pool
46、. It called the bean instances ejbCreate() method, which created some new database data, and returned control back to the container. Finally, the container associated the bean instance with a new EJB object and returned that EJB object to the client.To service our finder method, the container instan
47、tiated another entity bean. The container called newInstance() and then setEntityContext() to get that new bean instance into the available pool of entity beans. It then used the bean in the pool to service our finder method. Note that the bean instance is still in the pool and could service any num
48、ber of finder methods.In addition to the methods that the client calls, our EJB container interleaved a few ejbStore() and ejbLoad() calls to keep the database in synch.Testing JDBC Database Work.Probably the most frustrating part of an application is doing the database work. Often you will have pun
49、ctuation errors or misspellings, which are tough to debug when performing JDBC. This is because your JDBC queries are not compiledthey are interpreted at runtime, so you dont get the nifty things like type checking that the Java language gives you. You are basically at the mercy of the JDBC driver.
50、It may or may not give you useful feedback.You might consider using SQL/J instead of JDBC. SQL/J precompiles your SQL code, and you dont have to write all the prepares and JDBC connection codeyou just write embedded SQL code. SQL/J is available with Oracle Corporations Oracle database and with IBMs
51、DB2 database.When performing any kind of database work, the best way to debug is to set up a simple test database. If your queries are not functioning properly, try duplicating and running them against your database using your databases direct interface. This should help you track down your database
52、 problems much more quickly.Client-Side OutputRunning the client program yields the following client-side output:We created an entity bean, deposited into it, and tried to withdraw more than we had. The entity bean correctly threw an application-level exception back to us indicating that our balance
53、 had insufficient funds.SummaryIn this chapter, youve seen how to write bean-managed persistent entity beans. Bean-managed persistent entity beans are useful if you need to control the underlying database operations yourself. EJBs real advantage comes from containermanaged persistent entity beans. C
54、ontainer-managed persistent entity beans can be developed much more rapidly because the container handles all data access logic for you. The next chapter covers container-managed persistence.- 7 -大連交通大學2012屆本科生畢業(yè)設計 (論文) 外文翻譯第八章 編寫B(tài)ean管理持久性的實體Bean在第七章里,我們已經(jīng)介紹了一些基本的實體Bean概念。我們已經(jīng)掌握了兩種實體BeanBean管理持久性和容器
55、管理持久性。這一章,我們將示范怎樣使用Bean管理持久性的實體Bean編寫程序。當你編寫這樣的實體Bean代碼時,你必須提供自己的數(shù)據(jù)存取邏輯。為了從存儲器中映射實體Bean實例,你需要負責提供數(shù)據(jù)來源。為此,你會代表性地使用一個數(shù)據(jù)庫應用程序接口,例如JDBC或SQL/J。這與容器管理持久性實體Bean組件形成明顯的對比,使用從它們的EJB容器提供的數(shù)據(jù)存儲句柄商業(yè)應用組件為其處理數(shù)據(jù)存取。這一章將講解一些Bean管理持久性的基礎知識,以及向你展示如何應用JDBC連接方式建立一個簡單的Bean管理持久化操作的實體Bean。8.1 Bean管理持久化組件處理的指導方針在第七章,我們已經(jīng)了解了包
56、括Bean管理持久化操作和容器管理持久化操作在內(nèi)的所有必須實現(xiàn)javax.ejb.EntityBean接口的實體Bean類。這一接口規(guī)定了容器調(diào)用你的實體Bean的所有返回方法。通過這些方法所能輸入的信息在某種程度上取決于是否正在使用持久性組件處理或持久性容器處理持久化操作。假設你的組件是持久性組件處理持久化操作,表格8.1是對在每種方法中所應執(zhí)行的摘要信息??焖贋g覽此圖表。你應該在通讀本章節(jié)的代碼或為自己的實體組件編碼時回過頭來參考這張圖表。我們在第七章結尾粗略的列出的方法指令代表了實體組件實例生命周期的控制流的控制方法。8.2 持久性組件處理實例:銀行賬戶我們的第一個實例將是一個簡單的銀行
57、賬戶實體組件。這個銀行賬戶組件能夠潛在的使用在關系數(shù)據(jù)庫里聲明和操作銀行真正的賬戶數(shù)據(jù)。我們銀行賬戶的對象模型詳見圖8.1。我們看一下實體組件必須創(chuàng)建的每個文件。8.2.1 賬戶賬戶是客戶看到的實體組件的遠程接口,詳見資料8.1。注意:賬戶遠程接口實現(xiàn)了商業(yè)應用組件javax.ejb.EJBObject接口,這是所有遠程接口必須要實現(xiàn)的。我們的接口提供了一系列的方法用來操作實體組件,如標識存款和取款。所有的方法都會拋出接口異常用來表示系統(tǒng)級的災難性失敗。注意我們的撤回方法,我們同樣拋出我們客戶的應用級異常:AccountException。我們將在稍后這個異常。8.2.2 賬戶原置我們的本地接
58、口用賬戶本地表示,見源代碼8.2。我們提供一種方法去建立一個新的賬戶。這就將建立一個新的表示銀行賬戶的數(shù)據(jù)庫。它將商業(yè)應用組件技術對象返回給顧客,使顧客能夠操作那個新建的賬戶。注意我們拋出應用級的javax.ejb.CreateException異常。所有的create方法必須能夠拋出這個異常。我們同樣有兩種定位方法。原始碼定位搜索已存在的銀行賬戶數(shù)據(jù)庫,它用賬戶身份識別,我們將在下面的原始碼定位語言中作以規(guī)定。我們還有自定義定位法物主姓名搜索,用來搜索所有同姓名的銀行賬戶數(shù)據(jù)。因為我們正在用持久性組件處理,所以需要在實體組件設備中同時使用這兩種定位方法(如果使用持久性容器處理,容器會為我們搜索數(shù)據(jù)庫)。根據(jù)我們的建立方法,兩種定位程序返回商業(yè)應用組件技術客體,這樣,客戶就能夠操作新建的銀行賬戶。8.2.3 賬戶原始密碼我們的實體組件原始碼由賬戶原始碼定義,詳見8.3。我們的原始碼是一個簡單的賬戶身份識別串。例如,一個賬戶識別串為“ABC-123-0000.”這個串是只有銀行賬戶才有的。我們依靠客戶密碼建立賬戶身份識別以確保它是唯一的。原始碼用于識別銀行賬戶的唯一性。8.2.4 賬戶組件接下來,我們看實體組件設備分類,賬戶組件。我們的組件設備代碼非常長,我們將其分為幾個部分:(1)組件處理狀態(tài)域我們的實體組件分類里有持續(xù)的場。實體組件實例將向這些
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國串串香行業(yè)營銷創(chuàng)新戰(zhàn)略制定與實施研究報告
- 2025-2030年中國智能公交行業(yè)開拓第二增長曲線戰(zhàn)略制定與實施研究報告
- 2025-2030年中國螢石行業(yè)資本規(guī)劃與股權融資戰(zhàn)略制定與實施研究報告
- 2025-2030年中國XRF儀器行業(yè)全國市場開拓戰(zhàn)略制定與實施研究報告
- 化學品 快速雄激素干擾活性報告試驗 征求意見稿
- 安徽省房屋建筑安徽省工程建筑信息模型(BIM)審查數(shù)據(jù)標準(2025版)
- 2025年鋁制桌椅項目可行性研究報告
- 燒烤排煙知識培訓課件
- 實驗學校上學期工作參考計劃
- 防詐騙安全知識培訓課件
- 2024年股東股權繼承轉讓協(xié)議3篇
- 2025年中央歌劇院畢業(yè)生公開招聘11人歷年高頻重點提升(共500題)附帶答案詳解
- 北京市高校課件 開天辟地的大事變 中國近代史綱要 教學課件
- 監(jiān)事會年度工作計劃
- 2024中國近海生態(tài)分區(qū)
- 山東省濟南市2023-2024學年高一上學期1月期末考試化學試題(解析版)
- 北師大版五年級數(shù)學下冊第3單元第1課時分數(shù)乘法(一)課件
- 2024-2030年中國汽車保險杠行業(yè)市場發(fā)展現(xiàn)狀及前景趨勢分析報告
- 智研咨詢發(fā)布:中國種豬行業(yè)市場現(xiàn)狀、發(fā)展概況、未來前景分析報告
- 六年級上冊分數(shù)四則混合運算100題及答案
- 2024年認證行業(yè)法律法規(guī)及認證基礎知識
評論
0/150
提交評論