Chapter 10. The Seam Application Framework

Seam makes it really easy to create applications by writing
plain Java classes with annotations, which don't need to extend
any special interfaces or superclasses. But we can simplify
some common programming tasks even further, by providing a set
of pre-built components which can be re-used either by
configuration in components.xml (for very
simple cases) or extension.

The Seam Application Framework can reduce
the amount of code you need to write when doing basic database
access in a web application, using either Hibernate or JPA.

We should emphasize that the framework is extremely simple,
just a handful of simple classes that are easy to understand
and extend. The "magic" is in Seam itself—the same magic
you use when creating any Seam application even without using
this framework.

10.1. Introduction

The components provided by the Seam application framework
may be used in one of two different approaches. The first
way is to install and configure an instance of the component
in components.xml, just like we have
done with other kinds of built-in Seam components. For
example, the following fragment from
components.xml installs a component
which can perform basic CRUD operations for a
Contact entity:

The second approach has one huge advantage: you can easily add
extra functionality, and override the built-in functionality
(the framework classes were carefully designed for extension
and customization).

A second advantage is that your classes may be EJB stateful
sessin beans, if you like. (They do not have to be, they
can be plain JavaBean components if you prefer.)

At this time, the Seam Application Framework provides just four
built-in components: EntityHome and
HibernateEntityHome for CRUD, along with
EntityQuery and HibernateEntityQuery
for queries.

The Home and Query components are written so that they can function
with a scope of session, event or conversation. Which scope you
use depends upon the state model you wish to use in your application.

The Seam Application Framework only works with Seam-managed
persistence contexts. By default, the components will look
for a persistence context named entityManager.

10.2. Home objects

A Home object provides persistence operations for a particular entity
class. Suppose we have our trusty Person class:

A Home object provides the following operations: persist(),
remove(), update() and
getInstance(). Before you can call the
remove(), or update() operations, you
must first set the identifier of the object you are interested in, using the
setId() method.

Well, that lets us create new Person entries. Yes,
that is all the code that is required! Now, if we want to be able to
display, update and delete pre-existing Person
entries in the database, we need to be able to pass the entry
identifier to the PersonHome. Page parameters
are a great way to do that:

Real search screens let the user enter a bunch of optional search criteria
to narrow the list of results returned. The Query object lets you specify
optional "restrictions" to support this important usecase:

The examples in this section have all shown reuse by configuration. However,
reuse by extension is equally possible for Query objects.

10.4. Controller objects

A totally optional part of the Seam Application Framework is the class
Controller and its subclasses
EntityControllerHibernateEntityController and
BusinessProcessController. These classes provide
nothing more than some convenience methods for access to commonly
used built-in components and methods of built-in components. They help
save a few keystrokes (characters can add up!) and provide a great
launchpad for new users to explore the rich functionality built in
to Seam.

For example, here is what RegisterAction from the
Seam registration example would look like: