Databases Reference
In-Depth Information
5.
Provide a getEntityManager() method that returns a reference to the used
entity manager. The subclasses of this class will use this method for obtaining
the entity manager reference that is used to build database queries.
6.
Provide a getEntityClass() method that returns the type of the entity.
Subclasses use this method to build database queries by using the
Criteria API.
The source code of the BaseRepositoryImpl class is given as follows:
public abstract class BaseRepositoryImpl<T extends BaseEntity, ID
extends Serializable> implements BaseRepository<T, ID> {
private Class<T> entityClass;
@PersistenceContext(unitName = "pu")
private EntityManager em;
public BaseDAOImpl() {
this.entityClass = ((Class<T>) ((ParameterizedType)
getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public T deleteById(ID id) {
T entity = findById(id);
if (entity != null) {
em.remove(entity);
}
return entity;
}
@Override
public List<T> findAll() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> query = cb.createQuery(entityClass);
Root<T> root = query.from(entityClass);
return em.createQuery(query).getResultList();
}
@Override
public T findById(ID id) {
return em.find(getEntityClass(), id);
 
Search WWH ::




Custom Search