List of usage examples for javax.persistence EntityManager getCriteriaBuilder
public CriteriaBuilder getCriteriaBuilder();
CriteriaBuilder
for the creation of CriteriaQuery
objects. From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
@Override public <T extends BaseEntity> Long count(Class<T> entity) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); try {//from www.j av a 2 s . c o m CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> cq = cb.createQuery(Long.class); return em.createQuery(cq.select(cb.count(cq.from(entity)))).getSingleResult(); } catch (Exception ex) { // NOSONAR throw new JpaException(ex); } finally { JpaUtil.closeEntityManager(em); } }
From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}/*from w w w .j av a2 s . c om*/ */ @Override public <T extends BaseEntity> List<T> findAll(Class<T> entity) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); try { CriteriaQuery<T> cq = em.getCriteriaBuilder().createQuery(entity); return em.createQuery(cq.select(cq.from(entity))).getResultList(); } catch (Exception ex) { // NOSONAR throw new JpaException(ex); } finally { JpaUtil.closeEntityManager(em); } }
From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}/*from w w w. j a v a 2s . c o m*/ */ @Override public <T extends BaseEntity> List<T> findPaginatedRecords(Class<T> entity, int startPos, int maxResult) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); try { CriteriaQuery<T> cq = em.getCriteriaBuilder().createQuery(entity); return em.createQuery(cq.select(cq.from(entity))).setFirstResult(startPos).setMaxResults(maxResult) .getResultList(); } catch (Exception ex) { // NOSONAR throw new JpaException(ex); } finally { JpaUtil.closeEntityManager(em); } }
From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}/*from ww w . ja v a 2s . c o m*/ */ @Override public <T extends BaseEntity> List<T> findByCriteria(ReadCriteria<T> criteria) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); 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(Predicates.from(criteria.getCriteriaAttributes(), cb, root)))) .getResultList(); } catch (Exception ex) { // NOSONAR throw new JpaException(ex); } finally { JpaUtil.closeEntityManager(em); } }
From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}/*from w ww .j a va 2 s . c o m*/ */ @Override public <T extends BaseEntity> List<T> findPaginatedRecordsByCriteria(ReadCriteria<T> criteria) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); 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(Predicates.from(criteria.getCriteriaAttributes(), cb, root)))) .setFirstResult(criteria.getStartPos()).setMaxResults(criteria.getMaxResult()).getResultList(); } catch (Exception ex) { // NOSONAR throw new JpaException(ex); } finally { JpaUtil.closeEntityManager(em); } }
From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}//from w ww . j ava 2 s .co m */ @Override public <T extends BaseEntity> List<T> findByINOperator(Class<T> entity, String attributeName, List<Object> values) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); try { CriteriaQuery<T> cq = em.getCriteriaBuilder().createQuery(entity); Root<T> root = cq.from(entity); Predicate predicate = root.get(attributeName).in(values); return em.createQuery(cq.select(root).where(predicate)).getResultList(); } catch (Exception ex) { // NOSONAR throw new JpaException(ex); } finally { JpaUtil.closeEntityManager(em); } }
From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}// w w w. ja va 2s . c o m */ @Override public <T extends BaseEntity, C> List<C> findByCriteriaAndMapConstructor(ConstructorCriteria<T, C> criteria) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); 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(Predicates.from(criteria.getCriteriaAttributes(), cb, root))).getResultList(); } catch (Exception ex) { // NOSONAR throw new JpaException(ex); } finally { JpaUtil.closeEntityManager(em); } }
From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}/* w ww.ja v a 2s .com*/ */ @Override public <T extends BaseEntity> List<Tuple> findByTupleQuery(TupleQueryCriteria<T> criteria) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); try { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<T> root = cq.from(criteria.getEntity()); return em .createQuery(cq .multiselect(criteria.getSelections().stream().map(root::get).toArray(Selection[]::new)) .where(cb.and(Predicates.from(criteria.getCriteriaAttributes(), cb, root)))) .getResultList(); } catch (Exception ex) { // NOSONAR throw new JpaException(ex); } finally { JpaUtil.closeEntityManager(em); } }
From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}// ww w . j ava 2 s .c om */ @Override public <T extends BaseEntity> int deleteByCriteria(DeleteCriteria<T> criteria) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); try { em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaDelete<T> cd = cb.createCriteriaDelete(criteria.getEntity()); Root<T> root = cd.from(criteria.getEntity()); int rowsDeleted = em .createQuery(cd.where(cb.and(Predicates.from(criteria.getCriteriaAttributes(), cb, root)))) .executeUpdate(); em.getTransaction().commit(); LOGGER.debug("deleteByCriteria: No. of rows deleted: [{}]", rowsDeleted); return rowsDeleted; } catch (Exception ex) { // NOSONAR Transactions.markRollback(em); throw new JpaException(ex); } finally { Transactions.rollback(em); JpaUtil.closeEntityManager(em); } }
From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}//from ww w. java 2 s . c o m */ @Override public <T extends BaseEntity> int updateByCriteria(UpdateCriteria<T> criteria) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); try { em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaUpdate<T> cu = cb.createCriteriaUpdate(criteria.getEntity()); criteria.getUpdateAttributes().forEach(cu::set); Root<T> root = cu.from(criteria.getEntity()); int rowsUpdated = em .createQuery(cu.where(cb.and(Predicates.from(criteria.getCriteriaAttributes(), cb, root)))) .executeUpdate(); em.getTransaction().commit(); LOGGER.debug("No. of rows updated: {}", rowsUpdated); return rowsUpdated; } catch (Exception ex) { // NOSONAR Transactions.markRollback(em); throw new JpaException(ex); } finally { Transactions.rollback(em); JpaUtil.closeEntityManager(em); } }