List of usage examples for javax.persistence TypedQuery unwrap
<T> T unwrap(Class<T> cls);
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
private void showResult(CriteriaQuery c) { TypedQuery query = em.createQuery(c); // result/*from w w w . ja va2s . c o m*/ List result = query.getResultList(); // sql string String sql = query.unwrap(org.hibernate.Query.class).getQueryString(); ResultViewer.showResult(result, sql); }
From source file:gov.gtas.repository.CaseDispositionRepositoryImpl.java
@Override public Pair<Long, List<Case>> findByCriteria(CaseRequestDto dto) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Case> q = cb.createQuery(Case.class); Root<Case> root = q.from(Case.class); List<Predicate> predicates = new ArrayList<>(); TypedQuery<Case> typedQuery = em.createQuery(q); // sorting//w w w .j av a 2 s . c om if (dto.getSort() != null) { List<Order> orders = new ArrayList<>(); for (SortOptionsDto sort : dto.getSort()) { Expression<?> e = root.get(sort.getColumn()); Order order; if ("desc".equalsIgnoreCase(sort.getDir())) { order = cb.desc(e); } else { order = cb.asc(e); } orders.add(order); } q.orderBy(orders); } if (dto.getFlightId() != null) { predicates.add(cb.equal(root.<Long>get("flightId"), dto.getFlightId())); } if (dto.getPaxId() != null) { predicates.add(cb.equal(root.<Long>get("paxId"), dto.getPaxId())); } if (dto.getPaxName() != null) { String likeString = String.format("%%%s%%", dto.getPaxName().toUpperCase()); predicates.add(cb.like(root.<String>get("paxName"), likeString)); } if (dto.getLastName() != null) { // map this to full pax name String likeString = String.format("%%%s%%", dto.getLastName().toUpperCase()); predicates.add(cb.like(root.<String>get("paxName"), likeString)); } if (dto.getStatus() != null) { String likeString = String.format("%%%s%%", dto.getStatus().toUpperCase()); predicates.add(cb.like(root.<String>get("status"), likeString)); } if (dto.getFlightNumber() != null) { predicates.add(cb.equal(root.<Long>get("flightNumber"), dto.getFlightNumber())); } if (dto.getRuleCatId() != null) { predicates.add(cb.equal(root.<Long>get("highPriorityRuleCatId"), dto.getRuleCatId())); } Predicate etaCondition; if (dto.getEtaStart() != null && dto.getEtaEnd() != null) { Path<Date> eta = root.<Date>get("flightETADate"); Predicate startPredicate = cb.or(cb.isNull(eta), cb.greaterThanOrEqualTo(eta, dto.getEtaStart())); Predicate endPredicate = cb.or(cb.isNull(eta), cb.lessThanOrEqualTo(eta, dto.getEtaEnd())); etaCondition = cb.and(startPredicate, endPredicate); predicates.add(etaCondition); } q.select(root).where(predicates.toArray(new Predicate[] {})); typedQuery = em.createQuery(q); // total count CriteriaQuery<Long> countQuery = cb.createQuery(Long.class); countQuery.select(cb.count(countQuery.from(Case.class))).where(predicates.toArray(new Predicate[] {})); Long count = em.createQuery(countQuery).getSingleResult(); // pagination int pageNumber = dto.getPageNumber(); int pageSize = dto.getPageSize(); int firstResultIndex = (pageNumber - 1) * pageSize; typedQuery.setFirstResult(firstResultIndex); typedQuery.setMaxResults(dto.getPageSize()); logger.debug(typedQuery.unwrap(org.hibernate.Query.class).getQueryString()); List<Case> results = typedQuery.getResultList(); return new ImmutablePair<>(count, results); }
From source file:org.artificer.repository.hibernate.query.ArtificerToHibernateQueryVisitor.java
/** * Execute the query and return the results. * @param args/*from www . j av a 2s . com*/ * @return List<ArtificerArtifact> * @throws ArtificerException */ public List<ArtifactSummary> query(ArtificerQueryArgs args) throws ArtificerException { if (this.error != null) { throw this.error; } // filter out the trash (have to do this here since 'from' can be overridden at several points in the visitor) predicates.add(criteriaBuilder.equal(from.get("trashed"), Boolean.valueOf(false))); // build the full set of constraints and query.where(compileAnd(predicates)); // First, select the total count, without paging query.select(criteriaBuilder.count(from)).distinct(true); totalSize = (Long) entityManager.createQuery(query).getSingleResult(); // Setup the select. Note that we're only grabbing the fields we need for the summary. query.multiselect(from.get("uuid"), from.get("name"), from.get("description"), from.get("model"), from.get("type"), from.get("derived"), from.get("expandedFromArchive"), from.get("createdBy").get("lastActionTime"), from.get("createdBy").get("username"), from.get("modifiedBy").get("lastActionTime")).distinct(true); if (args.getOrderBy() != null) { String propName = orderByMap.get(args.getOrderBy()); if (propName != null) { if (args.getOrderAscending()) { query.orderBy(criteriaBuilder.asc(path(propName))); } else { query.orderBy(criteriaBuilder.desc(path(propName))); } } } TypedQuery q = entityManager.createQuery(query); args.applyPaging(q); q.unwrap(org.hibernate.Query.class).setCacheable(true); return q.getResultList(); }