Creating a session bean facade

One of the best practices of developing entities for separation of concerns and maintainable code and as a result better performance is to wrap the entities in a session bean facade. With a Session Facade, fewer remote method calls are required, and an outer transaction context is created with which each get method invocation does not start a new transaction. Session Facade is one of the core Java EE design patterns (http://www.oracle.com/technetwork/java/sessionfacade-141285.html). Create a CatalogSessionBeanFacade session bean class in the org.jboss.ejb3.model package, as shown in the following screenshot. The Session Facade class can also be created in a different package (such as org.jboss.ejb3.view):

Creating a session bean facade

The session bean class is annotated with the @Stateless annotation:

@Stateless
public class CatalogSessionBeanFacade {}

In the bean session, we use an EntityManager to create, remove, find, and query persistence entity instances. Inject a EntityManager using the @PersistenceContext annotation. Specify the unitName as the unitName configured in persistence.xml. Next, specify the getAllEditions, getAllSections, getAllArticles, getAllCatalogs get methods to fetch the collection of entities. The get methods get all entities' collections with the named queries specified in the entities. The createNamedQuery method of EntityManager is used to create a Query object from a named query. Specify the TransactionAttribute annotation's TransactionAttributeType enumeration to REQUIRES_NEW, which has the advantage that if a transaction is rolled back due to an error in a different transaction context from which the session bean is invoked, it does not affect the session bean.

To demonstrate the use of the entities, create the test data with the createTestData convenience method in the session bean. Alternatively, a unit test or an extension class can also be used. Create a Catalog entity and set the journal using the setJournal method. We do not set the id for the Catalog entity as we use the GenerationType.AUTO generation strategy for the ID column. Persist the entity using the persist method of the EntityManager object. However, the persist method does not persist the entity to the database. It only makes the entity instance managed and adds it to the persistence context. The EntityManager.flush() method is not required to be invoked to synchronize the entity with the database as EntityManager is configured with FlushModeType as AUTO (the other setting being COMMIT) and a flush will be done automatically when the EntityManager.persist() is invoked:

Catalog catalog1 = new Catalog();
catalog1.setJournal("Oracle Magazine");
em.persist(catalog1);

Similarly, create and persist an Edition entity object. Add the Catalog object: catalog1 using the setCatalog method of the Edition entity class:

Edition edition = new Edition();
edition.setEdition("January/February 2009");
edition.setCatalog(catalog1);
em.persist(edition);

Likewise add the Section and Article entity instances. Add another Catalog object, but without any associated Edition, Section, or Article entities:

Catalog catalog2 = new Catalog();
catalog2.setJournal("Linux Magazine");
em.persist(catalog2);

Next, we will delete data with the deleteSomeData method, wherein we first create a Query object using the named query findCatalogByJournal. Specify the journal to delete with the setParameter method of the Query object. Get the List result with the getResultList method of the Query object. Iterate the List result and remove the Catalog objects with the remove method of the EntityManager object. The remove method only removes the Catalog object from the persistence context:

public void deleteSomeData() {
  // remove a catalog
  Query q = em.createNamedQuery("findCatalogByJournal");
  //q.setParameter("journal", "Linux Magazine");
  q.setParameter("journal", "Oracle Magazine");
  List<Catalog> catalogs = q.getResultList();
  for (Catalog catalog : catalogs) {
    em.remove(catalog);
  }
}

The CatalogSessionBeanFacade session bean class is available in the code downloaded for the chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset