Rapid JPA development with Rational Application Developer 8.5.x: Rapidly build web apps with Rational Application Developer's JPA Manager Beans

See how quickly you can build a web application using IBM Rational Application Developer's JPA Manager Beans and Rich Page Editor to retrieve and update from your information systems and render it professionally in a JavaServer Faces facelet.

Share:

Ali Manji (amanji@ca.ibm.com), Software Developer, IBM

author photoAli Manji is a software developer with the Rational Insight team. He has several years of experience with Rational software and in working with many IBM offerings. You can follow him on Twitter @torontoIBMer or read his developerWorks blog, Random Rational Tidbits.



30 July 2013

In Create JPA entities with Rational Application Developer 8.5.1, you made use of IBM® Rational® Application Developer to rapidly generate and customize JPA entities from an IBM® DB2® database and deployed them to an IBM®WebSphere® Application Server.

In this follow-up article, you will expand the Acme Membership application by:

  1. Generating a JPA Manager Bean from your existing JPA bean to assist with the retrieval of data.
  2. Creating a JavaServer Faces (JSF) facelet webpage using your JPA Manager Bean to view information. You will create your facelet with the extensive widget library provided to you in Rational Application Developer's Rich Page Editor.

Before you start

The tasks and steps you will carry out in this article begin where Create JPA entities with Rational Application Developer 8.5.1 left off, and you will need to first complete the steps to construct the Acme Membership application before proceeding.

You will continue to use:

  • IBM® Rational® Application Developer, Version 8.5.1
  • IBM® WebSphere® Application Server, Version 8.5.0.2 (by using the WebSphere Test Environment that is available with Rational Application Developer)
  • IBM® DB2® Enterprise Server Edition database, Version 10.1, Fix Pack 2, for Microsoft® Windows®

    Note:
    After installing both Rational Application Developer 8.5 and WebSphere Application Server 8.5, use the IBM Installation Manager to find and apply fix packs and other maintenance updates to your installation to match the levels described here.

Add JavaServer Faces capabilities to your web project

Your web project needs to incorporate a facelet to present the Acme Membership records to its administrators.

To author a facelet, you need to add JSF facets to the existing web project, AcmeMembershipApplicationWeb.

  1. In the Web perspective, in the Enterprise Explorer view, right-click the AcmeMembershipApplicationWeb project and select Properties.
  2. In the list of Properties on the left, select Project Facets.
  3. Select the check box next to JavaServer Faces in the list of Project Facets, ensure that the version is set to 2.0 as shown in Figure 1, and click OK.

Note:
You will see a message indicating that further configuration is available. You can ignore this message, as you will not need to change the default JSF implementation library.

Figure 1. Adding the Java Server Faces facet
Select the option to add the JavaServer Faces facet to your web application

Add a Named Query to your JPA bean

The facelet for your web application needs to present a list of memberships to the Acme Inc.'s Membership Administrators. You will add a simple Named Query to your existing JPA Membership Bean that retrieves all membership records.

  1. In the Web perspective, in the Enterprise Explorer view, right-click the AcmeMembershipApplicationDomain project, and select JPA Tools > Configure JPA Entities.
  2. In the list of Available JPA Entities, select the check box next to Membership, and click Next.
  3. In the list of tasks in the bottom, left column, select Named Queries, and then click the Add button.
  4. For this simple Named Query, you will not add any filters or impose any ordering on the returned results. Leave the rest of the default values for the dialog, and click OK.
  5. Click Finish.

Open your Membership JPA bean, and notice the new Named Query annotation added as highlighted in Figure 2.

Figure 2. JPA Bean updated with the @NamedQuery annotation
RAD updated your JPA bean to include the @NamedQuery annotation

Create JPA Manager Beans from your JPA beans

JPA Manager Beans provide a convenient layer of abstraction that handles the JPA programming steps that create, retrieve, update, and delete data from your underlying data store through your existing JPA beans.

This valuable function frees you from these programming steps requiring in-depth interaction with the JPA programming model and API.

JPA Manager Beans support is proprietary to Rational Application Developer and is one of the compelling ways that IBM delivers on the promise of rapid development, making your teams more productive.

  1. In the Web perspective, in the Enterprise Explorer view, right-click the AcmeMembershipApplicationDomain project, and select JPA Tools > Add JPA Manager Beans.
  2. In the list of Available JPA Entities, select the check box next to Membership, and click Next.
  3. In the list of tasks in the bottom, left column, select Other.
  4. In the main pane, select the I want the container to inject the persistence unit into my bean radio button, as shown in Figure 3.
  5. Click Finish.
Figure 3: Option for the container to manage persistence
You want your JPA Manager Beans to allow the Container to manage persistence

Expand AcmeMembershipApplicationDomain\src in the Enterprise Explorer view of either the Java or Java EE perspective to see a new package org.acme.membership.domain.controller.

This new package houses the newly generated JPA Manager Bean MembershipManager.


Create a facelet to display Acme Inc. Membership records

Create your facelet

To create your facelet, follow these steps:

  1. In the Web perspective, in the Enterprise Explorer view, right-click the AcmeMembershipApplicationWeb project, and select New > Web Page.
  2. Enter listMemberships for the File Name.
  3. Ensure that Facelet is selected in the Template list box.
  4. Click Finish. The Rich Page Editor opens the new facelet for you to begin its construction.

Add the JPA Data widget to your facelet

You will use the JPA Data widget to generate the code for your facelet to present a list of data from your data store in a professional table.

The JPA Data widget invokes the services from your JPA Manager Bean to query your JPA beans that, in turn, are responsible for obtaining the required information from your data store.

  1. Expand the Data and Services drawer in the Palette view, and click the JPA Data widget, as shown in Figure 4.
Figure 4. Add the JPA Data widget to your facelet
Where to find the JPA Data widget in RAD's extensive palette
  1. Click anywhere on the Rich Page Editor to "drop" the JPA Data widget onto your facelet. This action will launch the Add JPA data to page wizard.
  2. MembershipManager should be the only JPA Manager Bean listed for selection. Ensure that the Retrieve a list of data radio button is selected, and click Next.
  3. getMembership should be the only Named Query available in the Query Methods list box of the Select JPA Query page. Select it, and click Next.

Select the data, the format, and the header names for your data table

To satisfy your users' requirements for a professional report of memberships, you will influence the characteristics of your data table by selecting only the fields your users wish to see, change the ordering in which the fields show up, and give more meaningful names to the header names for each field.

  1. On the Configure data controls page, in the Fields to display list box, uncheck the membershipType and dependents check boxes to exclude this data from your report.
  2. Change the order in which the fields show up in your report.
    1. Select the primaryMember row, and click the up arrow to reposition this row to show up under the id row.
    2. Select the membersince row, and click the up arrow to reposition this row to show up under the primaryMember row.
  3. Give easy-to-understand labels for the fields in your report. Click on the Label value for the id field, and change the label from Id to Membership ID.
Figure 5. Configuring the layout of the data table
The changes made in the Configure data controls dialog

Figure 5 shows the effect of your changes to exclude some fields, change the order of other fields, and provide user-friendly labels for your table's columns in the Configure data controls page of the Add JPA data to page wizard.

Note:
Complex data fields are identifiable through the ellipsis button that shows to the right of the field and can have further adjustments made to them.

  1. Update the complex field primaryMember.
    1. Click the ellipsis button to the right of the primaryMember field. In the dialog box that opens, uncheck the check boxes for all the fields except firstname and lastname.
    2. Click on the Label value for the firstname field, and change the label from Firstname to First name.
    3. Click on the Label value for the lastname field, and change the label from Lastname to Last name.
    4. Click Finish.
  2. Update the complex date fields membersince and lastrenewaldate.
    1. Click the ellipsis button to the right of the membersince field. In the dialog box that opens, uncheck the check boxes for all the fields except time.
    2. Click the Label value for the time field, and change the label from Time to Member since.
    3. Click Finish.
    4. Click the ellipsis button to the right of the lastrenewaldate field. In the dialog box that opens, uncheck the check boxes for all the fields except time.
    5. Click the Label value for the time field, and change the label from Time to Last renewed on.
    6. Click Finish.

Run your facelet

Run your facelet to view the memberships in the system.

  1. In the Web perspective, in the Enterprise Explorer view, expand AcmeMembershipApplicationWeb > WebContent.
  2. Right-click your facelet listMemberships.xhtml, and select Run As > Run on Server.
  3. If the Run on Server dialog wizard appears, select WebSphere Application Server v8.5 at localhost, and check the Always use this server when running this project check box before clicking Finish.

After the WebSphere server starts up, it will run your facelet. Your facelet should list the memberships in the Acme Membership system.

Figure 6: The data table rendered in your facelet
Your data table rendered in your facelet

In Figure 6, you see that the column ordering and column header labels match the adjustments you made when using the JPA Data widget and its corresponding Add JPA data to page wizard.

Note:
You might see the same membership entry with a different Membership ID, repeated the same number of times that you ran the AddMembershipServlet servlet in the previous article.


Summary

This article explained how to use Rational Application Developer to generate JPA Manager Beans for use by a JSF facelet webpage to present information.

The JPA Manager Beans are easy to generate with Rational Application Developer's guided wizard and significantly boost your productivity by freeing you from the task of directly querying your JPA beans programmatically with the JPA API.

Rational Application Developer's Rich Page Editor also simplifies, but enhances the web development experience by providing an extensive and powerful palette of drag-and-drop widgets to build professional webpages. In this article, using the JPA Data widget meant that Rational Application Developer removed all the coding effort required to retrieve and present data from your JPA beans through your JPA Manager Beans.


Acknowledgment

The author thanks Hamid Kalantari for his review of this article and thoughtful contributions.

Resources

Learn

Get products and technologies

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Rational software on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational, DevOps
ArticleID=937155
ArticleTitle=Rapid JPA development with Rational Application Developer 8.5.x: Rapidly build web apps with Rational Application Developer's JPA Manager Beans
publish-date=07302013