List of usage examples for javax.persistence.criteria Root alias
Selection<X> alias(String name);
From source file:org.jdal.dao.jpa.JpaUtils.java
/** * Copy criteria without selection and order. * @param from source Criteria./* w ww. j a v a 2s. c o m*/ * @param to destination Criteria. */ private static void copyCriteriaWithoutSelectionAndOrder(CriteriaQuery<?> from, CriteriaQuery<?> to) { if (isEclipseLink(from) && from.getRestriction() != null) { // EclipseLink adds roots from predicate paths to critera. Skip copying // roots as workaround. } else { // Copy Roots for (Root<?> root : from.getRoots()) { Root<?> dest = to.from(root.getJavaType()); dest.alias(getOrCreateAlias(root)); copyJoins(root, dest); } } to.groupBy(from.getGroupList()); to.distinct(from.isDistinct()); if (from.getGroupRestriction() != null) to.having(from.getGroupRestriction()); Predicate predicate = from.getRestriction(); if (predicate != null) to.where(predicate); }
From source file:com.zero.dao.impl.BaseDaoImpl.java
protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) { Assert.notNull(criteriaQuery);//from w w w .j a v a 2 s.co m Assert.notNull(criteriaQuery.getSelection()); Assert.notEmpty(criteriaQuery.getRoots()); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); addRestrictions(criteriaQuery, filters); CriteriaQuery<Long> countCriteriaQuery = criteriaBuilder.createQuery(Long.class); for (Root<?> root : criteriaQuery.getRoots()) { Root<?> dest = countCriteriaQuery.from(root.getJavaType()); dest.alias(getAlias(root)); copyJoins(root, dest); } Root<?> countRoot = getRoot(countCriteriaQuery, criteriaQuery.getResultType()); countCriteriaQuery.select(criteriaBuilder.count(countRoot.get("id").<String>get("stcd"))); if (criteriaQuery.getGroupList() != null) { countCriteriaQuery.groupBy(criteriaQuery.getGroupList()); } if (criteriaQuery.getGroupRestriction() != null) { countCriteriaQuery.having(criteriaQuery.getGroupRestriction()); } if (criteriaQuery.getRestriction() != null) { countCriteriaQuery.where(criteriaQuery.getRestriction()); } return entityManager.createQuery(countCriteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
From source file:net.groupbuy.dao.impl.BaseDaoImpl.java
protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) { Assert.notNull(criteriaQuery);/*from ww w . ja va 2s .c o m*/ Assert.notNull(criteriaQuery.getSelection()); Assert.notEmpty(criteriaQuery.getRoots()); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); addRestrictions(criteriaQuery, filters); CriteriaQuery<Long> countCriteriaQuery = criteriaBuilder.createQuery(Long.class); for (Root<?> root : criteriaQuery.getRoots()) { Root<?> dest = countCriteriaQuery.from(root.getJavaType()); dest.alias(getAlias(root)); copyJoins(root, dest); } Root<?> countRoot = getRoot(countCriteriaQuery, criteriaQuery.getResultType()); countCriteriaQuery.select(criteriaBuilder.count(countRoot)); if (criteriaQuery.getGroupList() != null) { countCriteriaQuery.groupBy(criteriaQuery.getGroupList()); } if (criteriaQuery.getGroupRestriction() != null) { countCriteriaQuery.having(criteriaQuery.getGroupRestriction()); } if (criteriaQuery.getRestriction() != null) { countCriteriaQuery.where(criteriaQuery.getRestriction()); } return entityManager.createQuery(countCriteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
From source file:cn.buk.hotel.dao.HotelDaoImpl.java
@Override public List<HotelInfo> searchAvailableHotel(HotelSearchCriteria sc) { List<HotelInfo> hotelInfos = null; try {//from ww w. ja v a2s. c o m //body CriteriaBuilder cb = getEm().getCriteriaBuilder(); CriteriaQuery<HotelInfo> cq = cb.createQuery(HotelInfo.class); Root<HotelInfo> root = cq.from(HotelInfo.class); root.alias("h"); List<Predicate> predicates = new ArrayList<Predicate>(); Predicate predicate = cb.equal(root.get(HotelInfo_.cityId), sc.getCityId()); predicates.add(predicate); /** * ratePlanStatus-1???) yfddai 2015-1-8 */ predicate = cb.notEqual(root.get(HotelInfo_.ratePlanStatus), -1); predicates.add(predicate); if (sc.getDistrictId() > 0) { predicate = cb.equal(root.get(HotelInfo_.areaId), sc.getDistrictId()); predicates.add(predicate); } if (sc.getHotelName() != null && sc.getHotelName().trim().length() > 0) { predicate = cb.like(root.get(HotelInfo_.hotelName), "%" + sc.getHotelName() + "%"); predicates.add(predicate); } if (sc.getStar() != null && sc.getStar().length() > 0) { Join<HotelInfo, HotelAward> hotelAward = root.join("hotelAwards", JoinType.LEFT); hotelAward.alias("ha"); predicates.add(cb.equal(hotelAward.get("provider"), "HotelStarRate")); String[] stars = sc.getStar().split(","); Predicate p0 = cb.disjunction(); for (String star : stars) { if (star.length() == 0) continue; int starLevel = Integer.parseInt(star); if (starLevel == 2) p0 = cb.or(p0, cb.le(hotelAward.get(HotelAward_.rating), starLevel)); else p0 = cb.or(p0, cb.equal(hotelAward.get("rating"), starLevel)); } predicates.add(p0); } if (sc.getZoneId() > 0) { Join<HotelInfo, HotelAddressZone> hotelZone = root.join(HotelInfo_.hotelAddressZones, JoinType.LEFT); hotelZone.alias("hz"); predicate = cb.equal(hotelZone.get(HotelAddressZone_.zoneCode), sc.getZoneId()); predicates.add(predicate); } // count items CriteriaQuery<Long> cq0 = cb.createQuery(Long.class); Root<HotelInfo> root0 = cq0.from(HotelInfo.class); root0.alias("h"); if (sc.getStar() != null && sc.getStar().length() > 0) { Join<HotelInfo, HotelAward> hotelAward0 = root0.join("hotelAwards", JoinType.LEFT); hotelAward0.alias("ha"); } if (sc.getZoneId() > 0) { Join<HotelInfo, HotelAddressZone> hotelZone0 = root0.join(HotelInfo_.hotelAddressZones, JoinType.LEFT); hotelZone0.alias("hz"); } cq0.select(cb.count(root0)).where(predicates.toArray(new Predicate[0])); Long count = getEm().createQuery(cq0).getSingleResult(); sc.getPage().setRowCount(count.intValue()); int firstPosition = (sc.getPage().getPageNo() - 1) * sc.getPage().getPageSize(); cq.select(root).where(predicates.toArray(new Predicate[0])); hotelInfos = getEm().createQuery(cq).setFirstResult(firstPosition) .setMaxResults(sc.getPage().getPageSize()).getResultList(); } catch (PersistenceException e) { logger.error(e.getMessage()); } return hotelInfos == null ? new ArrayList<HotelInfo>() : hotelInfos; }
From source file:org.agric.oxm.utils.JpaUtils.java
/** * Copy Criteria without Selection/*from w w w . j a va 2 s . c om*/ * * @param from * source Criteria * @param to * destination Criteria */ public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) { // Copy Roots for (Root<?> root : from.getRoots()) { Root<?> dest = to.from(root.getJavaType()); dest.alias(getOrCreateAlias(root)); copyJoins(root, dest); } to.groupBy(from.getGroupList()); to.distinct(from.isDistinct()); to.having(from.getGroupRestriction()); to.where(from.getRestriction()); to.orderBy(from.getOrderList()); }
From source file:org.jboss.pressgang.ccms.filter.utils.JPAUtils.java
/** * Copy Criteria without Selection//from w w w .j a va2s . com * * @param from source Criteria * @param to destination Criteria */ public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) { // Copy Roots for (Root<?> root : from.getRoots()) { Root<?> dest = to.from(root.getJavaType()); dest.alias(getOrCreateAlias(root)); copyJoins(root, dest); } if (from.getGroupList() != null) to.groupBy(from.getGroupList()); to.distinct(from.isDistinct()); if (from.getGroupRestriction() != null) to.having(from.getGroupRestriction()); if (from.getRestriction() != null) to.where(from.getRestriction()); if (from.getOrderList() != null) to.orderBy(from.getOrderList()); }