Document Information


1.  Introduction

2.  Understanding Java Platform, Enterprise Edition

3.  Creating Your First Java EE Application

4.  Creating Your Second Web Application

Creating the firstcup Project

Create the Web Application Project

Creating the Enterprise Bean

Create the DukesBirthdayBean Enterprise Bean Class

Add a Logger Instance to

Add a Business Method to DukesBirthdayBean that Gets the Average Age Difference of firstcup Users

Add a Business Method for Calculating the Age Difference Between Duke and the User

Creating the Web Client

Creating a Resource Bundle

Create a Resource Bundle

Configuring the Resource Bundle in the Configuration File

Create a Configuration File

Configure the Resource Bundle

Creating the DukesBDay Managed Bean Class

Create the Managed Bean Class

Add an Enterprise Bean Reference

Add Properties to the Bean

Get the Age Difference from the DukesBirthdayBean Enterprise Bean

Creating the Facelets Client

Resource Libraries in firstcup

The inputDate Composite Component

Create the inputDate Composite Component

The Facelets Web Interface

Create the XHTML Files

Set the Welcome File in the web.xml Deployment Descriptor

Modify the XHTML Files

Add the Form to greeting.xhtml

Add the Form to response.html

Building, Packaging, Deploying, and Running the firstcup Web Application

Build, Package, and Deploy the firstcup Web Application

Run the firstcup Application

5.  Next Steps


Creating the Java Persistence API Entity

The Java Persistence API allows you to create and use Java programming language classes that represent data in a database table. A Java Persistence API entity is a lightweight, persistent Java programming language object that represents data in a data store. Entities can be created, modified, and removed from the data store by calling the operations of the Java Persistence API entity manager. Entities, or the data encapsulated by the persistent fields or properties of a entity, can be queried using the Java Persistence Query Language (JPQL), a language similar to SQL that operates on entities.

In firstcup, there is a single entity that defines one query.

Create the FirstcupUser Entity Class

The FirstcupUser Java Persistence API entity represents a particular firstcup user, and stores the user's birthday and the difference in age between the user and Duke. FirstcupUser also defines a Java Persistence API query used to calculate the average age difference of all users.

  1. Select the firstcup project in the Projects tab.
  2. From the File menu, choose New File.
  3. In the Categories pane, select Persistence.
  4. In the File Types pane, select Entity Class.
  5. Click Next.
  6. In the Class Name field, type FirstcupUser.
  7. In the Package field, type firstcup.entity.
  8. Click Next.
  9. Select jdbc/__default from the Data Source menu.
  10. For Table Generation Strategy, select Drop and Create.
  11. Click Finish.

    You should now see the file inside the firstcup.entity package in the Projects tab. The file should also be open in the editor pane.

Add Properties to the FirstcupUser Entity

Create the FirstcupUser entity's two properties: birthday, of type java.util.Calendar; and ageDifference, of type int.

The birthday property must be annotated with the javax.persistence.Temporal annotation to mark the property as a date field in the underlying database table. All persistent fields or properties of type java.util.Calendar or java.util.Date must be annotated with @Temporal.

  1. In below the id field definition, copy and paste in the following field definitions for birthday and ageDifference:
        protected Calendar birthday;
        protected int ageDifference;
  2. Below the getter and setter methods for the id property, copy and paste in the following getter and setter methods for the birthday and ageDifference properties:
        public Calendar getBirthday() {
            return birthday;
        public void setBirthday(Calendar birthday) {
            this.birthday = birthday;
        public int getAgeDifference() {
            return ageDifference;
        public void setAgeDifference(int ageDifference) {
            this.ageDifference = ageDifference;
  3. Right-click in the editor window and select Format.
  4. Right-click in the editor window and select Fix Imports.

Add Constructors to the FirstcupUser Entity

Create two constructors for FirstcupUser: one that takes no arguments, and another that takes two arguments.

  1. Below the field definitions in the FirstcupUser class, add the following constructors:.
        public FirstcupUser() {
        public FirstcupUser(Date date, int ageDifference) {
            Calendar cal = new GregorianCalendar();
  2. Right-click in the editor window and select Format.
  3. Right-click in the editor window and select Fix Imports, then click OK.

Add a Named Query to the FirstcupUser Entity

Add a JPQL named query to the FirstcupUser entity that returns the average age difference of all firstcup users.

This query uses the AVG aggregate function to return the average of all the values of the ageDifference property of the FirstcupUser entities.

  1. Directly before the class definition, copy and paste in the following code:
    query="SELECT AVG(u.ageDifference) FROM FirstcupUser u")

    The @NamedQuery annotation appears just before the class definition of the entity, and has two required attributes: name, with the unique name for this query; and query, the JPQL query definition.

  2. Right-click in the editor window and select Format.
  3. Right-click in the editor window and select Fix Imports.
  4. From the File menu, choose Save.