Chapter 19. Building web applications

 

In this chapter

  • Integrating JPA with CDI and JSF
  • Browsing data in tables
  • Implementing long-running conversations
  • Customizing entity serialization

In this chapter, you see how Hibernate works in a typical web application environment. There are dozens of web application frameworks for Java, so we apologize if we don’t cover your favorite combination here. We discuss JPA in the standard Java Enterprise Edition environment, in particular combined with standards Contexts and Dependency Injection (CDI), JavaServer Faces (JSF), and Java API for RESTful web services (JAX-RS). As always, we show patterns you can apply in other proprietary environments.

First we revisit the persistence layer and introduce CDI management for the DAO classes. Then we extend these classes with a generic solution for sorting and paging data. This solution is useful whenever you have to display data in tables, no matter what framework you choose.

Next, you write a fully functional JSF application on top of the persistence layer and look at the Java EE conversation scope, where CDI and JSF work together to provide a simple stateful model for server-side components. If you didn’t like the stateful EJBs with the extended persistence context in the last chapter, maybe these conversation examples with detached entity state on the server are what you’re looking for.

19.1. Integrating JPA with CDI

Path: /apps/app-web/src/main/java/org/jpwh/web/dao/EntityManagerProducer.java

Path: /apps/app-web/src/main/java/org/jpwh/web/dao/GenericDAO.java

public interface GenericDAO<T, ID extends Serializable>
    extends Serializable {
<enter/>
    void joinTransaction();
<enter/>
    // ...
}

Path: /apps/app-web/src/main/java/org/jpwh/web/dao/GenericDAOImpl.java

public abstract class GenericDAOImpl<T, ID extends Serializable>
        implements GenericDAO<T, ID> {
<enter/>
    protected final EntityManager em;
    protected final Class<T> entityClass;
<enter/>
    protected GenericDAOImpl(EntityManager em, Class<T> entityClass) {
        this.em = em;
        this.entityClass = entityClass;
    }
<enter/>
    public EntityManager getEntityManager() {
        return em;
    }
<enter/>
    @Override
    public void joinTransaction() {
        if (!em.isJoinedToTransaction())
            em.joinTransaction();
    }
    // ...
}

Path: /apps/app-web/src/main/java/org/jpwh/web/dao/ItemDAOImpl.java

public class ItemDAOImpl
    extends GenericDAOImpl<Item, Long>
    implements ItemDAO {
<enter/>
    @Inject
    public ItemDAOImpl(EntityManager em) {
        super(em, Item.class);
    }
<enter/>
    // ...
<enter/>
}

19.2. Paging and sorting data

Path: /apps/app-web/src/main/java/org/jpwh/web/dao/Page.java

Path: /apps/app-web/src/main/java/org/jpwh/web/dao/ItemDAO.java

public interface ItemDAO extends GenericDAO<Item, Long> {
<enter/>
    List<ItemBidSummary> getItemBidSummaries(Page page);
<enter/>
    // ...
<enter/>
}

Path: /apps/app-web/src/main/java/org/jpwh/web/dao/ItemDAOImpl.java

Path: /apps/app-web/src/main/java/org/jpwh/web/dao/OffsetPage.java

Path: /apps/app-web/src/main/java/org/jpwh/web/dao/SeekPage.java

Path: /apps/app-web/src/test/java/org/jpwh/test/service/PagingTest.java

Path: /apps/app-web/src/test/java/org/jpwh/test/service/PagingTest.java

19.3. Building JSF applications

19.4. Serializing domain model data