use of com.adeptj.modules.data.jpa.PersistenceException in project adeptj-modules by AdeptJ.
the class EclipseLinkCrudRepository method countByCriteria.
/**
* {@inheritDoc}
*/
@Override
public <T extends BaseEntity> Long countByCriteria(Class<T> entity, Map<String, Object> criteriaAttributes) {
EntityManager em = this.emf.createEntityManager();
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
Root<T> from = cq.from(entity);
return em.createQuery(cq.select(cb.count(from)).where(cb.and(JpaUtil.getPredicates(criteriaAttributes, cb, from)))).getSingleResult();
} catch (RuntimeException ex) {
LOGGER.error(ex.getMessage(), ex);
throw new PersistenceException(ex.getMessage(), ex);
} finally {
JpaUtil.closeEntityManager(em);
}
}
use of com.adeptj.modules.data.jpa.PersistenceException in project adeptj-modules by AdeptJ.
the class EclipseLinkCrudRepository method findPaginatedRecordsByCriteria.
/**
* {@inheritDoc}
*/
@Override
public <T extends BaseEntity> List<T> findPaginatedRecordsByCriteria(ReadCriteria<T> criteria) {
EntityManager em = this.emf.createEntityManager();
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(criteria.getEntity());
Root<T> root = cq.from(criteria.getEntity());
return em.createQuery(cq.where(cb.and(JpaUtil.getPredicates(criteria.getCriteriaAttributes(), cb, root)))).setFirstResult(criteria.getStartPos()).setMaxResults(criteria.getMaxResult()).getResultList();
} catch (RuntimeException ex) {
LOGGER.error(ex.getMessage(), ex);
throw new PersistenceException(ex.getMessage(), ex);
} finally {
JpaUtil.closeEntityManager(em);
}
}
use of com.adeptj.modules.data.jpa.PersistenceException in project adeptj-modules by AdeptJ.
the class EclipseLinkCrudRepository method findByINOperator.
/**
* {@inheritDoc}
*/
@Override
public <T extends BaseEntity> List<T> findByINOperator(Class<T> entity, String attributeName, List<Object> values) {
EntityManager em = this.emf.createEntityManager();
try {
CriteriaQuery<T> cq = em.getCriteriaBuilder().createQuery(entity);
Root<T> root = cq.from(entity);
return em.createQuery(cq.select(root).where(root.get(attributeName).in(values))).getResultList();
} catch (RuntimeException ex) {
LOGGER.error(ex.getMessage(), ex);
throw new PersistenceException(ex.getMessage(), ex);
} finally {
JpaUtil.closeEntityManager(em);
}
}
use of com.adeptj.modules.data.jpa.PersistenceException in project adeptj-modules by AdeptJ.
the class EclipseLinkCrudRepository method count.
/**
* {@inheritDoc}
*/
@Override
public <T extends BaseEntity> Long count(Class<T> entity) {
EntityManager em = this.emf.createEntityManager();
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
return em.createQuery(cq.select(cb.count(cq.from(entity)))).getSingleResult();
} catch (RuntimeException ex) {
LOGGER.error(ex.getMessage(), ex);
throw new PersistenceException(ex.getMessage(), ex);
} finally {
JpaUtil.closeEntityManager(em);
}
}
use of com.adeptj.modules.data.jpa.PersistenceException in project adeptj-modules by AdeptJ.
the class EclipseLinkCrudRepository method findByCriteriaAndMapConstructor.
/**
* {@inheritDoc}
*/
@Override
public <T extends BaseEntity, C> List<C> findByCriteriaAndMapConstructor(ConstructorCriteria<T, C> criteria) {
EntityManager em = this.emf.createEntityManager();
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<C> cq = cb.createQuery(criteria.getConstructorClass());
Root<T> root = cq.from(criteria.getEntity());
return em.createQuery(cq.select(cb.construct(criteria.getConstructorClass(), criteria.getSelections().stream().map(root::get).toArray(Selection[]::new))).where(JpaUtil.getPredicates(criteria.getCriteriaAttributes(), cb, root))).getResultList();
} catch (RuntimeException ex) {
LOGGER.error(ex.getMessage(), ex);
throw new PersistenceException(ex.getMessage(), ex);
} finally {
JpaUtil.closeEntityManager(em);
}
}
Aggregations