版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、大连交通大学2012届本科生毕业设计 (论文) 外文翻译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届本科生毕业设计 (论文) 外文翻译第八章 编写Bean管理持久性的实体Bean在第七章里,我们已经介绍了一些基本的实体Bean概念。我们已经掌握了两种实体BeanBean管理持久性和容器
55、管理持久性。这一章,我们将示范怎样使用Bean管理持久性的实体Bean编写程序。当你编写这样的实体Bean代码时,你必须提供自己的数据存取逻辑。为了从存储器中映射实体Bean实例,你需要负责提供数据来源。为此,你会代表性地使用一个数据库应用程序接口,例如JDBC或SQL/J。这与容器管理持久性实体Bean组件形成明显的对比,使用从它们的EJB容器提供的数据存储句柄商业应用组件为其处理数据存取。这一章将讲解一些Bean管理持久性的基础知识,以及向你展示如何应用JDBC连接方式建立一个简单的Bean管理持久化操作的实体Bean。8.1 Bean管理持久化组件处理的指导方针在第七章,我们已经了解了包
56、括Bean管理持久化操作和容器管理持久化操作在内的所有必须实现javax.ejb.EntityBean接口的实体Bean类。这一接口规定了容器调用你的实体Bean的所有返回方法。通过这些方法所能输入的信息在某种程度上取决于是否正在使用持久性组件处理或持久性容器处理持久化操作。假设你的组件是持久性组件处理持久化操作,表格8.1是对在每种方法中所应执行的摘要信息。快速浏览此图表。你应该在通读本章节的代码或为自己的实体组件编码时回过头来参考这张图表。我们在第七章结尾粗略的列出的方法指令代表了实体组件实例生命周期的控制流的控制方法。8.2 持久性组件处理实例:银行账户我们的第一个实例将是一个简单的银行
57、账户实体组件。这个银行账户组件能够潜在的使用在关系数据库里声明和操作银行真正的账户数据。我们银行账户的对象模型详见图8.1。我们看一下实体组件必须创建的每个文件。8.2.1 账户账户是客户看到的实体组件的远程接口,详见资料8.1。注意:账户远程接口实现了商业应用组件javax.ejb.EJBObject接口,这是所有远程接口必须要实现的。我们的接口提供了一系列的方法用来操作实体组件,如标识存款和取款。所有的方法都会抛出接口异常用来表示系统级的灾难性失败。注意我们的撤回方法,我们同样抛出我们客户的应用级异常:AccountException。我们将在稍后这个异常。8.2.2 账户原置我们的本地接
58、口用账户本地表示,见源代码8.2。我们提供一种方法去建立一个新的账户。这就将建立一个新的表示银行账户的数据库。它将商业应用组件技术对象返回给顾客,使顾客能够操作那个新建的账户。注意我们抛出应用级的javax.ejb.CreateException异常。所有的create方法必须能够抛出这个异常。我们同样有两种定位方法。原始码定位搜索已存在的银行账户数据库,它用账户身份识别,我们将在下面的原始码定位语言中作以规定。我们还有自定义定位法物主姓名搜索,用来搜索所有同姓名的银行账户数据。因为我们正在用持久性组件处理,所以需要在实体组件设备中同时使用这两种定位方法(如果使用持久性容器处理,容器会为我们搜索数据库)。根据我们的建立方法,两种定位程序返回商业应用组件技术客体,这样,客户就能够操作新建的银行账户。8.2.3 账户原始密码我们的实体组件原始码由账户原始码定义,详见8.3。我们的原始码是一个简单的账户身份识别串。例如,一个账户识别串为“ABC-123-0000.”这个串是只有银行账户才有的。我们依靠客户密码建立账户身份识别以确保它是唯一的。原始码用于识别银行账户的唯一性。8.2.4 账户组件接下来,我们看实体组件设备分类,账户组件。我们的组件设备代码非常长,我们将其分为几个部分:(1)组件处理状态域我们的实体组件分类里有持续的场。实体组件实例将向这些
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030年中国串串香行业营销创新战略制定与实施研究报告
- 2025-2030年中国智能公交行业开拓第二增长曲线战略制定与实施研究报告
- 2025-2030年中国萤石行业资本规划与股权融资战略制定与实施研究报告
- 2025-2030年中国XRF仪器行业全国市场开拓战略制定与实施研究报告
- 化学品 快速雄激素干扰活性报告试验 征求意见稿
- 安徽省房屋建筑安徽省工程建筑信息模型(BIM)审查数据标准(2025版)
- 2025年铝制桌椅项目可行性研究报告
- 烧烤排烟知识培训课件
- 实验学校上学期工作参考计划
- 防诈骗安全知识培训课件
- 2024年股东股权继承转让协议3篇
- 2025年中央歌剧院毕业生公开招聘11人历年高频重点提升(共500题)附带答案详解
- 北京市高校课件 开天辟地的大事变 中国近代史纲要 教学课件
- 监事会年度工作计划
- 2024中国近海生态分区
- 山东省济南市2023-2024学年高一上学期1月期末考试化学试题(解析版)
- 北师大版五年级数学下册第3单元第1课时分数乘法(一)课件
- 2024-2030年中国汽车保险杠行业市场发展现状及前景趋势分析报告
- 智研咨询发布:中国种猪行业市场现状、发展概况、未来前景分析报告
- 六年级上册分数四则混合运算100题及答案
- 2024年认证行业法律法规及认证基础知识
评论
0/150
提交评论