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() { return entityManager; } @Transactional public T get(Long id) throws ServiceException { ... } @Transactional public List<T> getAll(Map<String, String> parameters...