List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
From source file:cn.guoyukun.spring.jpa.repository.support.SimpleBaseRepository.java
/** * Creates a {@link TypedQuery} for the given {@link Specification} and {@link Sort}. * //from ww w .ja v a 2s . co m * @param spec can be {@literal null}. * @param sort can be {@literal null}. * @return */ protected TypedQuery<M> getQuery(Specification<M> spec, Sort sort) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<M> query = builder.createQuery(getDomainClass()); Root<M> root = applySpecificationToCriteria(spec, query); query.select(root); if (sort != null) { query.orderBy(toOrders(sort, root, builder)); } return applyRepositoryMethodMetadata(em.createQuery(query)); }
From source file:org.openregistry.core.repository.jpa.JpaPersonRepository.java
@Override public List<Person> findByUnknownIdentifier(final String identifierValue) throws RepositoryAccessException { final CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); final CriteriaQuery<JpaPersonImpl> c = criteriaBuilder.createQuery(JpaPersonImpl.class); final Root<JpaPersonImpl> person = c.from(JpaPersonImpl.class); final Join<JpaPersonImpl, JpaIdentifierImpl> identifier = person.join(JpaPersonImpl_.identifiers); c.select(person).distinct(true) .where(criteriaBuilder.like(identifier.get(JpaIdentifierImpl_.value), identifierValue + "%")); final List<JpaPersonImpl> persons = this.entityManager.createQuery(c).getResultList(); return new ArrayList<Person>(persons); }
From source file:se.kth.csc.persist.JPAStore.java
@Override public QueuePosition fetchQueuePositionWithQueueAndUser(String queueName, String userName) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<QueuePosition> q = cb.createQuery(QueuePosition.class); Root<QueuePosition> queuePosition = q.from(QueuePosition.class); Join<QueuePosition, Queue> queue = queuePosition.join(QueuePosition_.queue); Join<QueuePosition, Account> account = queuePosition.join(QueuePosition_.account); try {/* w ww . j a v a 2 s.co m*/ return entityManager .createQuery( q.select(queuePosition) .where(cb.and(cb.equal(queue.get(Queue_.name), queueName), cb.equal(account.get(Account_.principalName), userName)))) .getSingleResult(); } catch (NoResultException e) { return null; } }
From source file:org.apache.ambari.server.orm.dao.ServiceConfigDAO.java
@RequiresSession public List<ServiceConfigEntity> getLastServiceConfigVersionsForGroups(Collection<Long> configGroupIds) { if (configGroupIds == null || configGroupIds.isEmpty()) { return Collections.emptyList(); }/*from w ww. ja va 2 s .co m*/ CriteriaBuilder cb = entityManagerProvider.get().getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<ServiceConfigEntity> groupVersion = cq.from(ServiceConfigEntity.class); cq.multiselect(groupVersion.get("groupId").alias("groupId"), cb.max(groupVersion.<Long>get("version")).alias("lastVersion")); cq.where(groupVersion.get("groupId").in(configGroupIds)); cq.groupBy(groupVersion.get("groupId")); List<Tuple> tuples = daoUtils.selectList(entityManagerProvider.get().createQuery(cq)); List<ServiceConfigEntity> result = new ArrayList<ServiceConfigEntity>(); //subquery look to be very poor, no bulk select then, cache should help here as result size is naturally limited for (Tuple tuple : tuples) { CriteriaQuery<ServiceConfigEntity> sce = cb.createQuery(ServiceConfigEntity.class); Root<ServiceConfigEntity> sceRoot = sce.from(ServiceConfigEntity.class); sce.where(cb.and(cb.equal(sceRoot.get("groupId"), tuple.get("groupId")), cb.equal(sceRoot.get("version"), tuple.get("lastVersion")))); sce.select(sceRoot); result.add(daoUtils.selectSingle(entityManagerProvider.get().createQuery(sce))); } return result; }
From source file:com.moderndrummer.data.MemberDaoImpl.java
@Override public List<Member> findAllCreatedMembersByFromAndToDate(Date fromDate, Date toDate) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Member> criteria = cb.createQuery(Member.class); Root<Member> member = criteria.from(Member.class); Path<Date> dateCreatedPath = member.get("createdDate"); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.lessThanOrEqualTo(dateCreatedPath, toDate)); predicates.add(cb.greaterThanOrEqualTo(dateCreatedPath, fromDate)); criteria.where(predicates.toArray(new Predicate[predicates.size()])); criteria.select(member).orderBy(cb.asc(member.get("name"))); return em.createQuery(criteria).getResultList(); }
From source file:org.easy.criteria.CriteriaProcessor.java
/** * Counts the result found for the given criteria * /*from w w w.j a va 2 s. com*/ * @param criteria * @param distinct * @return */ public <T> long count(final CriteriaComposer<T> criteria, boolean distinct) { log.trace("CriteriaProcessor.count"); Preconditions.checkNotNull(criteria); Class<T> forClass = criteria.getEntityClass(); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class); Root<T> root = criteriaQuery.from(forClass); log.debug("root =" + forClass.getName()); if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(root)); else criteriaQuery.select(criteriaBuilder.count(root)); List<Predicate> predicates = new ArrayList<Predicate>(); if (criteria != null) { criteria.generateJoins(root); criteria.generateWhere(criteriaBuilder, predicates); } criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])); TypedQuery<Long> query = entityManager.createQuery(criteriaQuery); long result = query.getSingleResult(); log.debug("CriteriaProcessor.count =" + result); return result; }
From source file:com.sfs.ucm.controller.SpecificationAction.java
/** * Load resources//from w w w. j av a 2 s. c o m * * @param project */ private void loadFeatures(final Project project) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Feature> c = cb.createQuery(Feature.class); Root<Feature> obj = c.from(Feature.class); c.select(obj); c.where(cb.equal(obj.get("project"), project)); c.orderBy(cb.asc(obj.get("id"))); this.features = em.createQuery(c).getResultList(); }
From source file:com.hiperium.dao.common.generic.GenericDAO.java
/** * /* w ww . j av a 2s. c o m*/ * @param bypassCache * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected List<T> findAll(boolean bypassCache) { CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(); criteriaQuery.select(criteriaQuery.from(this.entityClass)); Query query = this.entityManager.createQuery(criteriaQuery); if (bypassCache) { this.setBypassCahe(query); } return query.getResultList(); }
From source file:org.apereo.portal.portlet.dao.jpa.JpaPortletDefinitionDao.java
@Override public void afterPropertiesSet() throws Exception { this.nameParameter = this.createParameterExpression(String.class, "name"); this.titleParameter = this.createParameterExpression(String.class, "title"); this.findAllPortletDefinitions = this .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletDefinitionImpl>>() { @Override//from w w w. j a v a 2 s .co m public CriteriaQuery<PortletDefinitionImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<PortletDefinitionImpl> criteriaQuery = cb .createQuery(PortletDefinitionImpl.class); final Root<PortletDefinitionImpl> definitionRoot = criteriaQuery .from(PortletDefinitionImpl.class); criteriaQuery.select(definitionRoot); addFetches(definitionRoot); return criteriaQuery; } }); this.findDefinitionByNameQuery = this .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletDefinitionImpl>>() { @Override public CriteriaQuery<PortletDefinitionImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<PortletDefinitionImpl> criteriaQuery = cb .createQuery(PortletDefinitionImpl.class); final Root<PortletDefinitionImpl> definitionRoot = criteriaQuery .from(PortletDefinitionImpl.class); criteriaQuery.select(definitionRoot); addFetches(definitionRoot); criteriaQuery .where(cb.equal(definitionRoot.get(PortletDefinitionImpl_.name), nameParameter)); return criteriaQuery; } }); this.findDefinitionByNameOrTitleQuery = this .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletDefinitionImpl>>() { @Override public CriteriaQuery<PortletDefinitionImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<PortletDefinitionImpl> criteriaQuery = cb .createQuery(PortletDefinitionImpl.class); final Root<PortletDefinitionImpl> definitionRoot = criteriaQuery .from(PortletDefinitionImpl.class); criteriaQuery.select(definitionRoot); addFetches(definitionRoot); criteriaQuery.where(cb.or( cb.equal(definitionRoot.get(PortletDefinitionImpl_.name), nameParameter), cb.equal(definitionRoot.get(PortletDefinitionImpl_.title), titleParameter))); return criteriaQuery; } }); this.searchDefinitionByNameOrTitleQuery = this .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletDefinitionImpl>>() { @Override public CriteriaQuery<PortletDefinitionImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<PortletDefinitionImpl> criteriaQuery = cb .createQuery(PortletDefinitionImpl.class); final Root<PortletDefinitionImpl> definitionRoot = criteriaQuery .from(PortletDefinitionImpl.class); criteriaQuery.select(definitionRoot); addFetches(definitionRoot); criteriaQuery.where( cb.or(cb.like(definitionRoot.get(PortletDefinitionImpl_.name), nameParameter), cb.like(definitionRoot.get(PortletDefinitionImpl_.title), titleParameter))); return criteriaQuery; } }); }
From source file:ch.sdi.plugins.oxwall.job.OxSqlJob.java
/** * @see ch.sdi.core.intf.SqlJob#isAlreadyPresent(ch.sdi.core.impl.data.Person) *//*from w w w .j av a 2 s . c o m*/ @Override public boolean isAlreadyPresent(Person<?> aPerson) throws SdiException { if (myDryRun && !myCheckDuplicateOnDryRun) { myLog.debug("DryRun is active. Not checking for duplicate person"); return false; } // if myDryRun CriteriaBuilder cb = myEntityManager.getCriteriaBuilder(); CriteriaQuery<OxUser> criteria = cb.createQuery(OxUser.class); Root<OxUser> root = criteria.from(OxUser.class); ParameterExpression<String> mailParam = cb.parameter(String.class); criteria.select(root).where(cb.equal(root.get("email"), mailParam)); TypedQuery<OxUser> queryEMail = myEntityManager.createQuery(criteria); queryEMail.setParameter(mailParam, aPerson.getEMail()); List<OxUser> results = queryEMail.getResultList(); if (results.size() > 0) { myLog.debug("given Person is already present: " + results.get(0)); return true; } // if results.size() > 0 return false; }