TOTD#93showed how to get started with Java EE 6using NetBeans6.8 M1 andGlassFish v3 bybuilding a simple Servlet 3.0 + JPA 2.0 webapplication. TOTD#94 built upon it by using Java Server Faces 2 instead ofServlet 3.0 for displaying the results. However we are still using aPOJOfor all the database interactions. This works fine if we are onlyreading values from the database but that's not how a typical webapplication behaves. The web application would typically perform allCRUD operations. More typically they like to perform one or more CRUDoperations within the context of a transaction. And how do you dotransactions in the context of a web application ? Java EE 6 comes toyour rescue.

The EJB 3.1specification (another new specification in Java EE 6) allowPOJO classes to be annotated with @EJB and bundled withinWEB-INF/classes of a WAR file. And so you get all transactionalcapabilities in your web application very easily.

This TipOf The Day (TOTD) shows howto enhance the application created in TOTD #94 and use EJB 3.1 insteadof the JSF managed beanforperforming the business logic. There are two ways to achieve thispattern as described below.

Lets call this TOTD #95.1
  1. The easiest way to back a JSF page with an EJB is toconvert the managed bean into an EJB by adding @javax.ejb.Statelessannotation. So change the "StateList" class from TOTD #94 asshown below:

    @javax.ejb.Stateless
    @ManagedBean
    public class StateList {
    @PersistenceUnit
    EntityManagerFactory emf;

    public ListgetStates() {
    returnemf.createEntityManager().createNamedQuery("States.findAll").getResultList();
    }
    }
    The change is highlighted in bold, and that's it!
Because of "Deploy-on-save" feature in NetBeans and GlassFish v3, theapplication is autodeployed. Otherwise right-click on the project andselect Run (default shortcut "F6"). As earlier, the results can be seenat "http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or"http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml"and looks like:



The big difference this time is that the business logic is executed byan EJB in a fully transactional manner. Even though the logic in thiscase is a single read-only operation to the database, but you get theidea :)

Alternatively, you can use the delegate pattern in the managed bean asdescribed below. Lets call this #95.2.
  1. Right-click on the project, select "New", "Session Bean..." and create a stateless session bean by selecting the options asshown below:



    This creates a stateless session with the name "StateBeanBean" (bug#170392 for redundant "Bean" in the name).
  2. Simplify your managed bean by refactoring all the businesslogic to the EJB as shown below:

    @Stateless
    public class StateBeanBean {
    @PersistenceUnit
    EntityManagerFactory emf;

    public ListgetStates() {
    returnemf.createEntityManager().createNamedQuery("States.findAll").getResultList();
    }
    }
    and

    @ManagedBean
    public class StateList {
    @EJB StateBeanBean bean;

    public ListgetStates() {
    return bean.getStates();
    }
    }

    In fact the EJB code can be further simplified to:

    @Stateless
    public class StateBeanBean {
    @PersistenceContext
    EntityManager em;

    public ListgetStates() {
    return em.createNamedQuery("States.findAll").getResultList();
    }
    }

    The changes are highlighted in bold.
If the application is already running then Deploy-on-Savewould have automatically deployed the entire application. Otherwiseright-click on the project and select Run (default shortcut "F6").Again, theresults can be seen at"http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or"http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml"and are displayed as shown in the screenshot above.

The updated directory structure looks like:



The important point to note is that our EJB is bundled in the WAR fileand no additional deployment descriptors were added or existing onesmodified to achieve that. Now, that's really clean :)

The next blog in this series will show how managed beans can bereplaced with WebBeans, err JCDI.

Also refer to other Java EE 6blog entries.

Please leave suggestions on other TOTD thatyou'd like to see.A complete archive of all the tips is available here.

Technorati: totdglassfishv3 mysql javaee6 javaserverfacesjpa2 ejb netbeans


More...