Previous Next vertical dots separating previous/next from contents/index/pdf

6. Connecting Entities to a User Interface

Time to complete this step: 30 minutes

Test the application

In this step, we will import some readymade web components like DAO classes, JSPs & servlets. We will also create server configurations, deploy and run the JPA web application.

Import and Review web components (JSF pages, managed beans etc.) and configuration files

The DAO layer is responsible for providing access to the backend database using Kodo JPA which is BEA System's implementation of Sun's Java Persistence API (JPA) specification for the transparent persistence of Java objects.

For the web application project workshop-jpa-tutorial the Data Access Layer objects are in the package com.bea.dao. Review the objects in this package.

  1. DAOFactory.java - The DAOFactory is a factory class for creating DAO instances, which uses JPA for persistence. It defines public methods for retrieving the data access objects.

    public CustomerDAO getCustomerDAO() {
    return new CustomerDAO_Kodo();
    }

    public OrderDAO getOrderDAO() {
    return new OrderDAO_Kodo();
    }
  2. DAOHelper.java - This class creates an EntityManagerFactory instance using the persistence unit name as defined in the persistence.xml. Also it has a method to close the EntityManagerFactory that has been instantiated.

    • public static EntityManagerFactory createEntityManagerFactory (String name)
      {

      EntityManagerFactory emf = Persistence.createEntityManagerFactory(name);
      return emf;
      }
    • public static void closeEntityManagerFactory (EntityManagerFactory emf) {
      try{
      if(emf != null){
      emf.close();
      }
      }catch(Exception e){ }
      }

  3. CutomerDAO.java - The CustomerDAO is an interface which defines methods for retrieving the customer details.

    • public interface CustomerDAO {
    • public void addCustomer(Customer customer) throws DAOException;
      public Customer getCustomer(Integer customerId) throws DAOException;
      public Customer[] getAllCustomers( ) throws DAOException;
      }

  4. CutomerDAO_Kodo.java - The CustomerDAO_Kodo class implements the CustomerDAO interface and uses the JPA API for retrieving and storing Customer data.
    • For accessing the database the JPA API implements the following initial steps:
      • Create EntityManagerFactory instance for a particular persistence unit.
      • Create EntityManager to access a database in a particular unit of work.

        EntityManager em = null;
        EntityManagerFactory emf = null;

        emf = DAOHelper.createEntityManagerFactory("unitK");
        em = emf.createEntityManager();

    • The method getCustomer(...) uses the Query object to get information from the database
      Query query = em.createQuery(
      "SELECT c FROM Customer c where c.customerid = ?1")
      .setParameter(1, customerId);

      List results = query.getResultList ();
      for (Object res : results)
      customer = (Customer) res;
    • The method addCustomer(...) stores the Customer data in the database.
      tx.begin();
      em.persist(customer);
      tx.commit ();
  1. Faces config defines Managed Beans and Navigation Rules in faces-config.xml file.
  2. The web/Resources folder contains the faces-config.xml file with Managed Bean configurations and Navigation Rules for the web application.
  3. Open the web/WEB-INF/config/faces-config.xml file and review the Faces configurations. It defines Customer and CustomerManagedBean as managed beans in the request scope. Also, defines DAOFactory as a managed bean in the application scope.
  4. It defines Navigation Rules for addCustomer.jsp and viewAllCustomers.jsp. The navigation case for addCustomer.jsp specifies /pages/viewAllCustomers.jsp as the destination page while the outcome is success.

  5. Similarly, it defines the navigation case for viewAllCustomers.jsp describing /pages/viewOrders.jsp as the destination page while the outcome is orderList.

Create Server Configurations

Workshop has the ability to run and debug applications on most of the popular web containers that are in use today. Before you can run this sample application, you must have a server installed.

Click on the Servers tab. If the Servers view tab is not displayed, click Window > Open Perspective > Workshop to display it (or click Window > Show Views > Other then expand Server, click on Servers and click OK).

Right click on the Servers tab and choose New > Server.

Choose Apache>Tomcat v5.5 from the list of Servers. Click Next.


Select the project workshop-jpa-tutorial from the Available projects list in the left pane and click Add to move it to the right pane. Click Finish.


The new server is displayed in the Servers view.

Deploy the JPA web application

To run the application, right-click on the project workshop-jpa-tutorial in the AppXplorer and choose Run As > Run on Server. Use the default Choose an existing server setting for localhost > Tomcat v5.5 @localhost.

Click Finish.

Once the server starts successfully, the test browser pane opens. Enter the address http://localhost:8080/workshop-jpa-tutorial/pages/addCustomer.jsf in the address line of the test browser pane. The Console view displays the server output. Once the Tomcat server starts completely, the addCustomer.jsf page is called in browser.

Run the application

Once the addCustomer JSF page loads, you will see the screen below.

To add a new customer, enter name Bob and click Submit.

The application calls addCustomer( ) action method of Customer managed bean and forwards the request to viewAllCustomers.jsp, if customer is added successfully. The viewAllCustomers.jsp gets the customers list using the <h:dataTable> component and displays it as shown below:

Click View Orders button for the customer name JOHN to get the list of order(s) placed by JOHN.

Click Back to browse the previous page and get list of orders placed by other customers.

Once you are done, stop the Tomcat Server.

Click one of the following arrows to navigate through the tutorial:

 

 

Skip navigation bar   Back to Top