Home > Articles > Programming > Java

  • Print
  • + Share This
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.

  • + Share This
  • 🔖 Save To Your Account