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.
- 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 the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.