List of usage examples for javax.persistence.criteria Root get
<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);
From source file:br.ufba.dcc.mestrado.computacao.repository.impl.LicenseRepositoryImpl.java
@Override public OpenHubLicenseEntity findByName(String name) { CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<OpenHubLicenseEntity> criteriaQuery = criteriaBuilder.createQuery(getEntityClass()); Root<OpenHubLicenseEntity> root = criteriaQuery.from(getEntityClass()); CriteriaQuery<OpenHubLicenseEntity> select = criteriaQuery.select(root); Predicate namePredicate = criteriaBuilder.equal(root.get("name"), name); select.where(namePredicate);/* ww w.j av a 2 s .c o m*/ TypedQuery<OpenHubLicenseEntity> query = getEntityManager().createQuery(criteriaQuery); OpenHubLicenseEntity result = null; try { result = query.getSingleResult(); } catch (NoResultException ex) { } catch (NonUniqueResultException ex) { } return result; }
From source file:eu.uqasar.service.ProcessService.java
/** * //w ww. ja v a 2 s. c om * @param first * @param count * @return */ public List<Process> getAllByAscendingName(int first, int count) { logger.infof("loading all Processes ordered by ascending name ..."); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Process> criteria = cb.createQuery(Process.class); Root<Process> root = criteria.from(Process.class); criteria.orderBy(cb.asc(root.get(Process_.name))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:net.groupbuy.dao.impl.MemberDaoImpl.java
public List<Object[]> findPurchaseList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Member> member = criteriaQuery.from(Member.class); Join<Product, Order> orders = member.join("orders"); criteriaQuery.multiselect(member.get("id"), member.get("username"), member.get("email"), member.get("point"), member.get("amount"), member.get("balance"), criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(orders.<Date>get("createDate"), beginDate)); }//from www . j a v a2s . c o m if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(orders.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(orders.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(orders.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(member.get("id"), member.get("username"), member.get("email"), member.get("point"), member.get("amount"), member.get("balance")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid")))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
From source file:eu.uqasar.service.ProcessService.java
/** * // w ww .j av a 2 s . co m * @param processId * @return */ public boolean processExists(Long processId) { logger.info(String.format("checking if process with ID %d exists ...", processId)); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<Process> from = criteria.from(Process.class); criteria.where(cb.equal(from.get(Process_.id), processId)); criteria.select(cb.countDistinct(from)); return (em.createQuery(criteria).getSingleResult() == 1); }
From source file:org.zlogic.vogon.web.data.TransactionFilterSpecification.java
/** * Builds the Predicate/* w ww . j ava 2 s . co m*/ * * @param root the FinanceTransaction query root * @param cq the CriteriaQuery instance * @param cb the CriteriaBuilder instance * @return the Predicate of this filter */ @Override public Predicate toPredicate(Root<FinanceTransaction> root, CriteriaQuery<?> cq, CriteriaBuilder cb) { Predicate ownerPredicate = cb.equal(root.get(FinanceTransaction_.owner), owner); Predicate descriptionPredicate = filterDescription != null ? cb.like(cb.lower(root.get(FinanceTransaction_.description)), filterDescription.toLowerCase()) : cb.conjunction(); Predicate tagsPredicate = cb.conjunction(); if (filterTags != null && !filterTags.isEmpty()) { Set<String> filterTagsLowercase = new HashSet<>(); for (String tag : filterTags) filterTagsLowercase.add(tag.toLowerCase()); tagsPredicate = cb.lower(root.join(FinanceTransaction_.tags)).in(cb.literal(filterTagsLowercase)); } Predicate datePredicate = filterDate != null ? cb.equal(root.get(FinanceTransaction_.transactionDate), new java.sql.Date(filterDate.getTime())) : cb.conjunction(); return cb.and(ownerPredicate, descriptionPredicate, datePredicate, tagsPredicate); }
From source file:com.github.lothar.security.acl.jpa.multithread.CurrentUserLastNameSpec.java
@Override public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) { String currentUserLastName = Session.currentUserLastName(); if (currentUserLastName == null) { return cb.conjunction(); } else {/* w w w . j a va 2 s . c o m*/ return cb.equal(root.get("lastName"), currentUserLastName); } }
From source file:org.osiam.resource_server.storage.dao.ExtensionDao.java
/** * Retrieves the extension with the given URN from the database * * @param urn/*from w w w . j av a 2 s . co m*/ * the URN of the extension to look up * @param caseInsensitive * should the case of the URN be ignored * @return the extension entity */ public ExtensionEntity getExtensionByUrn(String urn, boolean caseInsensitive) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<ExtensionEntity> cq = cb.createQuery(ExtensionEntity.class); Root<ExtensionEntity> extension = cq.from(ExtensionEntity.class); Predicate predicate; if (caseInsensitive) { predicate = cb.equal(cb.lower(extension.get(ExtensionEntity_.urn)), urn.toLowerCase(Locale.ENGLISH)); } else { predicate = cb.equal(extension.get(ExtensionEntity_.urn), urn); } cq.select(extension).where(predicate); TypedQuery<ExtensionEntity> query = em.createQuery(cq); ExtensionEntity singleExtension; try { singleExtension = query.getSingleResult(); } catch (NoResultException e) { throw new OsiamException("Could not find the Extension '" + urn + "'.", e); } return singleExtension; }
From source file:eu.uqasar.service.ProductService.java
public List<Product> sortAscendingDates() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = cb.createQuery(Product.class); Root<Product> from = criteria.from(Product.class); criteria.select(from);// w w w. j a v a 2 s.c o m return em.createQuery(criteria.orderBy(cb.asc(from.get("releaseDate")))).getResultList(); }
From source file:eu.uqasar.service.ProductService.java
public List<Product> sortDescendingDates() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = cb.createQuery(Product.class); Root<Product> from = criteria.from(Product.class); criteria.select(from);/*from w w w . ja va2s . co m*/ return em.createQuery(criteria.orderBy(cb.desc(from.get("releaseDate")))).getResultList(); }
From source file:org.sloth.persistence.impl.ObservationDaoImpl.java
@Override public Collection<Observation> getByUser(User u) throws NullPointerException, IllegalArgumentException { if (u == null) { throw new NullPointerException(); }//from ww w . j a v a 2 s .c om if (u.isNew()) { throw new EntityNotKnownException(); } CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Observation> cq = cb.createQuery(Observation.class); Root<Observation> o = cq.from(Observation.class); cq.select(o).where(cb.equal(o.get(Observation_.user), u)); Collection<Observation> result = getEntityManager().createQuery(cq).getResultList(); logger.info("{} Observations by User {}.", result.size(), u); return result; }