List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
From source file:se.kth.csc.persist.JPAStore.java
@Override public Queue fetchQueueWithName(String name) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Queue> q = cb.createQuery(Queue.class); Root<Queue> queue = q.from(Queue.class); try {/*from ww w.jav a 2 s .com*/ return entityManager.createQuery(q.select(queue).where(cb.equal(queue.get(Queue_.name), name))) .getSingleResult(); } catch (NoResultException e) { return null; } }
From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.MultimediaDaoImpl.java
@Override public void afterPropertiesSet() throws Exception { this.findAllMultimedia = this .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<MultimediaImpl>>() { @Override/*from w w w .ja va2 s . co m*/ public CriteriaQuery<MultimediaImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<MultimediaImpl> criteriaQuery = cb.createQuery(MultimediaImpl.class); final Root<MultimediaImpl> definitionRoot = criteriaQuery.from(MultimediaImpl.class); criteriaQuery.select(definitionRoot); return criteriaQuery; } }); }
From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java
/** * Find an entity of the given type, using as filter criteria the specified * list of attribute values. The filter criteria must uniquely identify an * entity.// w w w . j av a2 s. c o m * * @param entityClass * @param params * @return * @throws BusinessException */ public <T> T findEntityByAttributes(Class<T> entityClass, Map<String, Object> params) { CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root); Assert.notNull(params); Predicate p = null; if (entityClass.isAssignableFrom(IModelWithClientId.class)) { p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId()); } for (Map.Entry<String, Object> entry : params.entrySet()) { p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue())); } cq.where(p); TypedQuery<T> query = this.getEntityManager().createQuery(cq); return (T) query.getSingleResult(); }
From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java
/** * Find a list of entities of the given type, using as filter criteria the * specified list of attribute values./* w w w . j a v a 2s. c om*/ * * @param entityClass * @param params * @return * @throws BusinessException */ public <T> List<T> findEntitiesByAttributes(Class<T> entityClass, Map<String, Object> params) { CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root); Assert.notNull(params); Predicate p = null; if (entityClass.isAssignableFrom(IModelWithClientId.class)) { p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId()); } for (Map.Entry<String, Object> entry : params.entrySet()) { p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue())); } cq.where(p); TypedQuery<T> query = this.getEntityManager().createQuery(cq); return (List<T>) query.getResultList(); }
From source file:de.ks.idnadrev.expimp.xls.XlsxExporterTest.java
protected List<Long> getAllIds() { return PersistentWork.read(em -> { CriteriaQuery<Long> criteriaQuery = em.getCriteriaBuilder().createQuery(Long.class); Root<Thought> root = criteriaQuery.from(Thought.class); Path<Long> id = root.<Long>get("id"); criteriaQuery.select(id); return em.createQuery(criteriaQuery).getResultList(); });//from w ww . j av a 2 s . c om }
From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * {@inheritDoc}//from w ww .java 2 s .c o m */ public List<Transaction> findTransactionsByStatementDateAndCreditCardId(Date startDate, Date endDate, long creditCardId) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class); Root<Transaction> root = query.from(Transaction.class); query.select(root); Predicate date = builder.greaterThan(root.<Date>get(FIELD_CONSTANT_TRANSACTION_DATE), startDate); Predicate isCreditCardId = builder.equal(root.<String>get(FIELD_CONSTANT_CREDIT_CARD_ID), creditCardId); query.where(builder.and(date, isCreditCardId)); return super.find(query); }
From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * {@inheritDoc}/*from w ww . j a v a2s . co m*/ */ public List<Transaction> getTransactions(int start, int limit, String sortField, String sortOrder, Map<String, Object> filters, long userId) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class); Root<Transaction> root = query.from(Transaction.class); query.select(root); addSorting(sortField, sortOrder, query, builder, root); addFilterCriteria(userId, filters, builder, root, query); return entityManager.createQuery(query).setFirstResult(start).setMaxResults(limit).getResultList(); }
From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * {@inheritDoc}//ww w . ja va 2 s .c o m */ public List<Transaction> searchTransaction(String searchTerm) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class); Root<Transaction> root = query.from(Transaction.class); query.select(root); Predicate customerName = builder.like(root.<String>get(FIELDCONSTANT_CUSTOMERNAME), MODULO + searchTerm + MODULO); Predicate creditCardNumber = builder.like(root.<String>get(FIELDCONSTANT_CARDNUMBER), MODULO + searchTerm + MODULO); Predicate description = builder.like(root.<String>get(FIELD_CONSTANT_DESCRIPTION), MODULO + searchTerm + MODULO); query.where(builder.or(customerName, creditCardNumber, description)); return super.find(query); }
From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * {@inheritDoc}/*from w w w . j a v a 2 s.c o m*/ */ public Transaction findLastTransaction() { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class); Root<Transaction> root = query.from(Transaction.class); query.select(root); query.orderBy(builder.desc(root.get(FIELD_CONSTANT_TRANSACTION_ID))); try { return entityManager.createQuery(query).setMaxResults(1).getSingleResult(); } catch (NoResultException e) { LOGGER.error("Error while retrieving last transaction", e); } return null; }
From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java
@Override public long findCount() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = cb.createQuery(Long.class); Root<T> customer = criteriaQuery.from(entityClass); criteriaQuery.select(cb.count(customer)); Query query = em.createQuery(criteriaQuery); return (Long) query.getSingleResult(); }