Home > Articles > Programming > Java

This chapter is from the book

This chapter is from the book

Step 5: Packaging the CMP 2.0 Entity Beans as an ejb-jar File

To package the sample application, we first need to create the application archive file that will hold the ejb-jar and Web war files. Let's first create the application archive file CMP20App.ear.

  1. Create an ear file that will include the EJB jar and Web war files. Start the Sun J2SDKEE1.3 RI and the deployment tool. Using the deployment tool, select File|New|Application. When a new application window pops up, click the Browse button to go to the APPHOME\chapter11 subdirectory. Enter the name of the ear file (CMP20App.ear) as shown in Figure 11-1. Click New Application and then OK to create this file.

    Figure 11-1Figure 11-1 Creating an enterprise application archive file

  2. Package the EJB component by creating one ejb-jar file, then package all three EJBs into one jar file.

  3. Select File|New|Enterprise Bean. When the Enterprise Bean Wizard pops up, click Next. In the Wizard window, ensure that the Create New Jar radio button is selected and displays CMP20App. If not, use the pull-down menu to select it.

  4. In the JAR Display Name area, enter CMP20JAR and click Edit to bring up the Edit Contents window. Using the pull-down menu, select the APPHOME\chapter11 directory and note the subdirectories (including cmp and Web). Select the cmp directory icon; it will expand and display the contents of the directory. To add classes to the jar file, select AddressEJB.class, LocalAddressHome.class, and LocalAddress.class. Click the Add button, as shown in Figure 11-2, and then click OK. The Edit Content window will close. Click Next.

    Figure 11-2Figure 11-2 Creating an Enterprise JavaBean archive fileÑCMP20JAR

  5. In the Wizard window, enter the general information regarding the AddressEJB. First click the radio button to specify that this is an entity bean; then, using the pull-down menu under Enterprise Bean Class, select AddressEJB.class. Use the default AddressEJB for the Enterprise Bean Class Name. Under the Local Interfaces, select LocalAddressHome and LocalAddress for the local home and local interface (see Figure 11-3). Click Next.

    Figure 11-3Figure 11-3 Adding the AddressEJB bean to the CMP20JAR

  6. Now, specify the CMP fields, abstract persistence schema, and the primary key. In the Wizard, select the Container Managed Persistence (2.0) radio button. In the Fields To Be Persisted box, select all the fields. In the Abstract Schema Name area, enter Address; specify that primary key as a Java String type under the Primary Key Class text box by entering java.lang.String. Under Primary Key Field Name, use the pull-down menu to select addressID as the primary key field name (see Figure 11-4). Click the Next button twice.

    Figure 11-4Figure 11-4 Specifying CMP fields in the abstract persistence schema

  7. The AddressEJB bean references StudentEJB in the code—at runtime, AddressEJB needs to locate the StudentEJB instance. Under the Coded Name column, enter ejb/StudentRef; under Type and Interfaces, select Entity and Local, respectively. Under the Home Interface and Local/Remote Interface column, enter the full path of the LocalStudentHome and LocalStudent interfaces. Under Enterprise Bean Name, enter StudentEJB. In this case, don't specify the full path name (see Figure 11-5). Click Finish.

    Figure 11-5Figure 11-5 Specifying EJB references for the AddressEJB class

  8. The parameters for AddressEJB are specified; now, we'll repeat the same process for RosterEJB and StudentEJB.

  9. To add the RosterEJB bean to the existing CMP20JAR file, select it. Then, using the deployment tool, select File|New|Enterprise Bean. Click Next in the Wizard and verify that Add to the Existing Jar File is selected and that CMP20JAR is displayed. Click the Edit button. When the Edit Content window appears, go to the CMP subdirectory under chapter11. Add LocalRosterHome.class, LocalRoster.class, and RosterEJB.class to the jar file by clicking the Add button for each (see Figure 11-6). Click OK and then Next.

    Figure 11-6Figure 11-6 Adding the RosterEJB bean to the CMP20JAR file

  10. In the Wizard, select Entity as the Enterprise Bean Class, and select RosterEJB from the pull-down menu. Select LocalRosterHome and LocalRoster under the Local Interfaces section as home and local interfaces (see Figure 11-7). Click Next.

    Figure 11-7Figure 11-7 Specifying the RosterEJB bean class and local interfaces

  11. In the Wizard, select Container Managed Persistence (2.0). Select all the fields to be persisted, enter Roster as the Abstract Schema Name, and from the pull-down menu, select java.lang.String as the primary key class and rosterID as the primary key field (see Figure 11-8). Click Next twice.

    Figure 11-8Figure 11-8 Specifying CMP 2.0 type and CMP fields in the abstract persistence schema

  12. Because the RosterEJB class references StudentEJB, specify the JNDI name and the local references. Enter ejb/StudentRef as the Coded Name; select Local as the Type and Local as the Interface. Use the fully qualified LocalStudentHome and LocalStudent names for the home and local interfaces. Enter StudentEJB as the Enterprise Bean Class name (see Figure 11-9). Click Finish.

    Figure 11-9Figure 11-9 Specifying EJB references

  13. Add the StudentEJB bean to the jar file. Using the deployment tool, select File|New|Enterprise Bean. When the Wizard pops up, click Next and then Edit. When the Edit Content window pops up, go to the APPHOME\chapter11 \cmp directory and add LocalStudentHome.class, Local- Student.class, and StudentEJB.class to the CMP20JAR file by clicking the Add button. Click OK and then Next (see Figure 11-10).

    Figure 11-10Figure 11-10 Adding the StudentEJB bean to the CMP20JAR file

  14. Select Entity. Using the pull-down menu, select StudentEJB, LocalStudentHome, and LocalStudent as the Bean Class types and Local Home and Local Interface respectively (see Figure 11-11). Click Next.

    Figure 11-11Figure 11-11 Specifying the StudentEJB bean class and local interfaces

  15. In the Wizard, select the Container Managed Persistence (2.0) radio button; select studentID, firstName, and lastName as the fields to be persisted. Enter Student as the abstract schema name; enter java.lang.String as the primary key class; and enter studentID as the primary field (see Figure 11-12).

    Figure 11-12Figure 11-12 Specifying CMP fields in the abstract persistence schema

  16. Because the LocalStudentHome interface declares two finder methods, specify the EJB QL query. Select Finder/Select Methods and enter the SELECT DISTINCT OBJECT(s) FROM Student AS s WHERE s.firstName = ?1 statement for the findByFirstName finder method.

  17. Repeat the same for the EJB QL statement. Change the s.firstName to s.lastName (see Figure 11-13). Click OK in the Finder/Select Method window.

    Figure 11-13Figure 11-13 Specifying the EJB QL query statement

  18. Because the StudentEJB code doesn't reference any objects, there's no need to specify a JNDI lookup. Click Finish to package all three EJBs into one jar file.

  19. Now, specify the relationships between the EJBs and generate the SQL statements. Select the CMP20JAR icon and select the Relationships tab. Click Add to display the Add Relationship window, in which you use the pull-down menu to specify the relationship between the StudentEJB and the AddressEJB (a one-to-many relationship). Under Multiplicity, select One to Many. Click the Description button. In the window that pops up, enter Student-has-many-Address. Click Apply, and then click OK.

  20. Specify the relationship between these two beans. Under Enterprise Bean A, use the pull-down menu to select StudentEJB as the Enterprise Bean Name, addresses for Field Referencing Bean B, and the java.uti.Collection as the Field Type (see Figure 11-14). Remember, there are two CMR abstract methods declared in the StudentEJB class that returned a collection. The address field corresponds to that CMR abstract method. Under Enterprise Bean Name B, use the pull-down menu and select AddressEJB as the Enterprise Bean Name. Because this relationship is unidirectional, use the "Field Referencing Bean B" pull-down menu to select CMR field "addresses" and leave its corresponding field to none for Enterprise Bean B. The AddressEJB entity is dependent on StudentEJB entity, and it must be deleted if the StudentEJB entity is deleted. To enable cascade delete, check the "Delete When Bean A is Deleted" option."

    Figure 11-14Figure 11-14 Specifying the StudentEJB-AddressEJB relationship

  21. Next, specify the relationship between the StudentEJB and the RosterEJB. StudentEJB has a one-to-many relationship with RosterEJB; StudentEJB references RosterEJB with the CMR field rosters specified by the CMR abstract method, which returns a collection. This is specified in the relationship window (see Figure 11-15).

    Figure 11-15Figure 11-15 Specifying the student-to-roster relationship

  22. Now, generate the SQL statement. Select the StudentEJB icon, select the Entity tab, and then select Deployment Settings to display the Deployment Settings window. Provide the database setting information and then click the Database Setting button to display another Database Setting window. Enter jdbc/Cloudscape under Database JNDI Name; enter j2ee as the user name and password; and then click OK (see Figure 11-16).

    Figure 11-16Figure 11-16 Specifying database deployment settings

  23. Next, click the Create Table on Deploy and Delete Table on Undeploy buttons; then, select Generate Default SQL. If the EJB QL query was correct, the SQL Generation Complete window pops up. On the deploytool window terminal, the generated SQL statement for both finder methods should appear. Click OK, then click the OK button in the deploytool window.

  24. The LocalRosterHome interface also declared two finder methods—findByScheduleID and findByStudentID. Specify the EJB QL for these finder methods. (This could have been done when packaging RosterEJB, but we deferred this step to show how it can also be done later.) Select Finder/Select Methods to display the EJB QL window. Select the findByScheduleID(..) method; then, in the EJB-QL Query for FindByScheduleID window, enter SELECT DISTINCT OBJECTR FROM Roster AS r WHERE r.scheduleID = ?1.

  25. Select the findByStudentID(_) method under Method. Enter the query statement from the previous step, but change r.studentID to r.scheduleID (see Figure 11-17). Click OK; then select Deployment Setting. When the Deployment Setting window appears, check Create Table on Deploy and Delete on Undeploy. Then select Generate Default SQL to generate the SQL 25. statement for the finder methods. If the EJB QL was entered correctly, the SQL Generation Complete window should pop up. Click OK, and the window will disappear. Then click OK in the Deployment Setting window.

    Figure 11-17Figure 11-17 Specifying EJB QL and generating an SQL statement

  26. Select the AddressEJB icon and then the Entity tab. Select Deployment Setting to display the Deployment Setting window. Because the LocalAddressHome interface didn't declare any finder methods, don't specify any EJB QL query. Check both Create Table on Deploy and Delete Table on Undeploy; then, select Generate Default SQL. The SQL Generation Complete confirmation window should pop up. Click OK and then OK again in the Deployment Setting window.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020