Creating CDI services

In creating the services that interact with JPA, we decided to abstract some methods out into a parent service that all our services can inherit. It wasn't necessary to develop it in this way, as it is all down to personal preference in the end.

  1. Our parent service will inject an EntityManager for JPA calls, and will take advantage of Java Generics to provide some common methods for retrieving an entity by an identifier and retrieving all instances of an entity type:
    public abstract class AbstractService<T> { @Inject @BookDB private EntityManager entityManager; private Class<T> entityClass; public AbstractService() {} public AbstractService(Class<T> entityClass) { this.entityClass = entityClass; } protected EntityManager getEntityManager() ...

Get JBoss Weld CDI for Java Platform now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.