February 28, 2021
Hot Topics:

Introducing JBoss(r) Seam

  • By Jim Farley
  • Send Email »
  • More Articles »
Practical Tip: Be careful about SQL reserved words used as EJB entity bean class or property names. Persistence engines may try to map them directly to auto-generated columns/tables, resulting in unexpected SQLExceptions. Notice that we called our GadgetBean property "description", rather than "desc". This is longer to type, but "desc" is reserved in some databases. If you decided to auto-generate the schema, a property called "desc" could be mapped into a column named "DESC", and problems could ensue. We're being extra careful here by using explicit @Column EJB3 annotations to map the properties to columns in our database model, so even if we auto-generated the schema, we're sure not to run into issues.

In order to implement the functionality we've laid out for our Gadget Catalog, we'll need to be able to get a list of all gadgets currently in the database, and we'll need to be able to add a new Gadget to the database. Using a fairly typical "session façade" pattern for EJBs, we create a GadgetAdminBean session EJB to provide these functions. The code for this is shown in Listing 2.

Listing 2. GadgetAdminBean Session EJB

public class GadgetAdminBean implements IGadgetAdminBean {
   @PersistenceContext (unitName= "gadgetDatabase")
   private EntityManager mEntityManager;

   /** Retrieve all gadgets from the catalog,
       ordered by description*/
   public List<GadgetBean> getAllGadgets() {
   List<GadgetBean> gadgets = new ArrayList<GadgetBean>();
   try {
      Query q =
         mEntityManager.createQuery("select g from GadgetBean " +
                                    "g order by g.description");
         List gList = q.getResultList();
         Iterator i = gList.iterator();
         while (i.hasNext()) {
            gadgets. add ( (GadgetBean) i.next());

      catch (Exception e) {
         e.printStackTrace ();
      return gadgets;

   /** Insert a new gadget into the catalog */
   public void newGadget(GadgetBean g) {
      try {
         mEntityManager. persist(g);
      catch (Exception e) {
         e.printStackTrace ();

This session EJB uses standard EJB 3.0 and Java Persistence API (JPA) calls to implement the required functions. We've marked this as a stateless session bean using the EJB 3.0 @Stateless annotation on the class declaration. We're also using the JPA @Persistence-Context annotation to inject a JPA EntityManager into this session bean, allowing us to perform the persistence operations necessary to query and insert into the gadget database. We're referencing a persistence unit named "gadgetDatabase", so we'll need to define a persistence unit with this name in the persistence.xml deployment file when we package up these EJBs.

The getAllGadgets () method loads the entire Gadget Catalog using a JPA query created from the EntityManager. The newGadget () method persists a new gadget (in the form of a GadgetBean) using the EntityManager.

These two EJBs seem to take care of our current needs in terms of persistence operations, so now we can turn our attention to the UI. To implement the UI we specified in the pageflow design earlier, we create two JSF pages, one for each of the pages we specified. The first JSF page displays the list of gadgets in the database along with a link to create a new gadget. In building these pages, let's assume we can access the persistence functionality we built earlier through a JSF managed bean named "gadgetAdmin". Our gadget list JSF is shown in Listing 3. It simply uses a JSF data table component to iterate through the gadgets returned from the getAllGadgets () operation on the gadgetAdmin bean, displaying each gadget as a row in a table. Then, at the bottom of the table, we generate a link that invokes a JSF action named "addGadget".

Listing 3. Gadget List JSF Page

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

   <title>Gadget List</title>

      <!-- Show the current gadget catalog -->
      <h:dataTable value="#{gadgetAdmin.allGadgets}" var="g">
         <h : column>
            <f:facet name="header">
               <h:outputText value="Type" />
            </f: facet>
            <h:outputText value="#{g.type}" />
         </h: column>
         <h: column>
            <f:facet name="header">
               <h:outputText value="Description" />
            </f: facet>
            <h:outputText value="#{g.description}" />
         </h: column>
      </h: dataTable>
         <!-- Link to add a new gadget -->
         <h:commandLink action="addGadget">
            <h:outputText value="Add a new gadget" />
      </h: form>
   </f: view>

The addGadget action is supposed to bring us to the second page in our pageflow, the gadget entry form. The JSF page that implements this, addGadget.jsp, is shown in Listing 4.

Page 3 of 6

This article was originally published on December 7, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date