List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
From source file:com.yunguchang.data.ApplicationRepository.java
public List<TBusApproveSugEntity> listApplyApproveInfo(DateTime startTime, DateTime endTime, PrincipalExt principalExtOrNull) { if (startTime == null) { startTime = DateTimeUtil.appStartTime; }/*from w w w . ja va 2 s . co m*/ if (endTime == null) { endTime = DateTimeUtil.appEndTime; } CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<TBusApproveSugEntity> cq = cb.createQuery(TBusApproveSugEntity.class); Root<TBusApproveSugEntity> approveRoot = cq.from(TBusApproveSugEntity.class); approveRoot.fetch(TBusApproveSugEntity_.application); approveRoot.fetch(TBusApproveSugEntity_.user, JoinType.LEFT); cq.select(approveRoot); Predicate predicate = cb.and( cb.between(approveRoot.get(TBusApproveSugEntity_.operatedate), new Timestamp(startTime.getMillis()), new Timestamp(endTime.getMillis())), cb.or(cb.isNull(approveRoot.get(TBusApproveSugEntity_.updateBySync)), cb.isFalse(approveRoot.get(TBusApproveSugEntity_.updateBySync)))); cq.where(predicate); TypedQuery<TBusApproveSugEntity> query = em.createQuery(cq); applySecurityFilter("applications", principalExtOrNull); return query.getResultList(); }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private static List<Long> filterResourceIdsByLastUpdated(EntityManager theEntityManager, final DateRangeParam theLastUpdated, Collection<Long> thePids) { CriteriaBuilder builder = theEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceTable> from = cq.from(ResourceTable.class); cq.select(from.get("myId").as(Long.class)); List<Predicate> lastUpdatedPredicates = createLastUpdatedPredicates(theLastUpdated, builder, from); lastUpdatedPredicates.add(from.get("myId").as(Long.class).in(thePids)); cq.where(SearchBuilder.toArray(lastUpdatedPredicates)); TypedQuery<Long> query = theEntityManager.createQuery(cq); List<Long> resultList = query.getResultList(); return resultList; }
From source file:cn.buk.hotel.dao.HotelDaoImpl.java
@Override public List<HotelInfo> searchAvailableHotel(HotelSearchCriteria sc) { List<HotelInfo> hotelInfos = null; try {/*from w w w .ja v a 2s.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:com.movies.dao.impl.BaseDaoImpl.java
@Override public <T> List<T> getObjectsByCriteria(Map<String, Object> map, Class returnClass, List<SingularAttribute> singleAttributes, List<ListAttribute> listAttributes, List<SetAttribute> setAttributes) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(returnClass).distinct(true); Root<T> root = cq.from(returnClass); if (CollectionUtils.isNotEmpty(singleAttributes)) { for (SingularAttribute attribute : singleAttributes) { root.join(attribute);//from ww w . j a va 2 s .c o m root.fetch(attribute); } } if (CollectionUtils.isNotEmpty(listAttributes)) { for (ListAttribute attribute : listAttributes) { root.join(attribute, JoinType.LEFT); root.fetch(attribute, JoinType.LEFT); } } if (CollectionUtils.isNotEmpty(setAttributes)) { for (SetAttribute attribute : setAttributes) { root.join(attribute, JoinType.LEFT); root.fetch(attribute, JoinType.LEFT); } } Set<Entry<String, Object>> set = map.entrySet(); int numberOfClauses = set.size(); Predicate[] predicates = new Predicate[numberOfClauses]; int i = 0; for (Entry<String, Object> entry : set) { String key = entry.getKey(); if (MovieConstants.NAME_FIELD.equals(key) || MovieConstants.SURNAME_FIELD.equals(key)) { predicates[i++] = cb.like(cb.upper(root.<String>get(key)), LIKE + entry.getValue() + LIKE); } else if (MovieConstants.MOVIE_DIRECTOR_FIELD.equals(key)) { //predicates[i++] = cb.equal( ,entry.getValue()); } else { predicates[i++] = cb.equal(root.get(key), entry.getValue()); } } return em.createQuery(cq.select(root).where(predicates)).getResultList(); }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateId(Set<Long> theExistingPids, Set<Long> thePids) { if (thePids == null || thePids.isEmpty()) { return Collections.emptySet(); }/* w w w . j a v a 2 s. co m*/ CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceTable> from = cq.from(ResourceTable.class); cq.select(from.get("myId").as(Long.class)); Predicate typePredicate = builder.equal(from.get("myResourceType"), myResourceName); Predicate idPrecidate = from.get("myId").in(thePids); cq.where(builder.and(typePredicate, idPrecidate)); TypedQuery<Long> q = myEntityManager.createQuery(cq); HashSet<Long> found = new HashSet<Long>(q.getResultList()); if (!theExistingPids.isEmpty()) { theExistingPids.retainAll(found); } return found; }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateComposite(RuntimeSearchParam theParamDef, Set<Long> thePids, List<? extends IQueryParameterType> theNextAnd) { CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceTable> from = cq.from(ResourceTable.class); cq.select(from.get("myId").as(Long.class)); IQueryParameterType or = theNextAnd.get(0); if (!(or instanceof CompositeParam<?, ?>)) { throw new InvalidRequestException("Invalid type for composite param (must be " + CompositeParam.class.getSimpleName() + ": " + or.getClass()); }// ww w.j av a2 s. c om CompositeParam<?, ?> cp = (CompositeParam<?, ?>) or; RuntimeSearchParam left = theParamDef.getCompositeOf().get(0); IQueryParameterType leftValue = cp.getLeftValue(); Predicate leftPredicate = createCompositeParamPart(builder, from, left, leftValue); RuntimeSearchParam right = theParamDef.getCompositeOf().get(1); IQueryParameterType rightValue = cp.getRightValue(); Predicate rightPredicate = createCompositeParamPart(builder, from, right, rightValue); Predicate type = builder.equal(from.get("myResourceType"), myResourceName); if (thePids.size() > 0) { Predicate inPids = (from.get("myResourcePid").in(thePids)); cq.where(builder.and(type, leftPredicate, rightPredicate, inPids)); } else { cq.where(builder.and(type, leftPredicate, rightPredicate)); } TypedQuery<Long> q = myEntityManager.createQuery(cq); return new HashSet<Long>(q.getResultList()); }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateLanguage(Set<Long> thePids, List<List<? extends IQueryParameterType>> theList) { if (theList == null || theList.isEmpty()) { return thePids; }/* w ww . ja v a 2s . c om*/ if (theList.size() > 1) { throw new InvalidRequestException( "Language parameter can not have more than one AND value, found " + theList.size()); } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceTable> from = cq.from(ResourceTable.class); cq.select(from.get("myId").as(Long.class)); Set<String> values = new HashSet<String>(); for (IQueryParameterType next : theList.get(0)) { if (next instanceof StringParam) { String nextValue = ((StringParam) next).getValue(); if (isBlank(nextValue)) { continue; } values.add(nextValue); } else { throw new InternalErrorException("Lanugage parameter must be of type " + StringParam.class.getCanonicalName() + " - Got " + next.getClass().getCanonicalName()); } } if (values.isEmpty()) { return thePids; } Predicate typePredicate = builder.equal(from.get("myResourceType"), myResourceName); Predicate langPredicate = from.get("myLanguage").as(String.class).in(values); Predicate masterCodePredicate = builder.and(typePredicate, langPredicate); if (thePids.size() > 0) { Predicate inPids = (from.get("myId").in(thePids)); cq.where(builder.and(masterCodePredicate, inPids)); } else { cq.where(masterCodePredicate); } TypedQuery<Long> q = myEntityManager.createQuery(cq); return new HashSet<Long>(q.getResultList()); }
From source file:ch.puzzle.itc.mobiliar.business.server.boundary.ServerView.java
public List<ServerTuple> getNodeServers(String hostFilter, String appServerFilter, String runtimeFilter, String nodeFilter, String contextFilter) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ServerTuple> q = cb.createQuery(ServerTuple.class); //get Node ResType Root<ResourceEntity> node = q.from(ResourceEntity.class); Join<ResourceEntity, ResourceTypeEntity> nodeType = node.join("resourceType", JoinType.LEFT); Join<ResourceEntity, ReleaseEntity> nodeRelease = node.join("release", JoinType.LEFT); //get Props on node Join<ResourceEntity, ResourceContextEntity> resCtx = node.join("contexts", JoinType.LEFT); Join<ResourceContextEntity, ContextEntity> nodeCtx = resCtx.join("context", JoinType.LEFT); Join<ContextEntity, ContextEntity> nodeDomain = nodeCtx.join("parent", JoinType.LEFT); Join<ResourceContextEntity, PropertyEntity> nodeProp = resCtx.join("properties", JoinType.LEFT); Join<PropertyEntity, PropertyDescriptorEntity> nodePropType = nodeProp.join("descriptor", JoinType.LEFT); //get AppServer Join<ResourceEntity, ConsumedResourceRelationEntity> nodeResRel = node.join("consumedSlaveRelations", JoinType.LEFT);/* w w w. j a v a 2 s . co m*/ Join<ConsumedResourceRelationEntity, ResourceEntity> appServer = nodeResRel.join("masterResource", JoinType.LEFT); Join<ResourceEntity, ReleaseEntity> asRelease = appServer.join("release", JoinType.LEFT); Join<ResourceEntity, ResourceTypeEntity> asType = appServer.join("resourceType", JoinType.LEFT); //get Runtime of as Join<ResourceEntity, ConsumedResourceRelationEntity> asResRel = appServer.join("consumedMasterRelations", JoinType.LEFT); Join<ConsumedResourceRelationEntity, ResourceEntity> asRuntime = asResRel.join("slaveResource", JoinType.LEFT); Join<ResourceEntity, ResourceTypeEntity> runtimeType = asRuntime.join("resourceType", JoinType.LEFT); q.select(cb.construct(ServerTuple.class, nodeProp.get("value"), appServer.get("name"), appServer.get("id"), asRelease.get("name"), asRuntime.get("name"), node.get("name"), node.get("id"), nodeRelease.get("name"), nodeDomain.get("name"), nodeDomain.get("id"), nodeCtx.get("name"), nodeCtx.get("id"), cb.literal(1) // true )); Predicate p = cb.and(cb.equal(nodeType.get("name"), DefaultResourceTypeDefinition.NODE.name()), cb.or(cb.equal(asType.get("name"), DefaultResourceTypeDefinition.APPLICATIONSERVER.name()), cb.isNull(asType.get("name")) //nodes without appServer ), cb.or(cb.equal(runtimeType.get("name"), ResourceTypeEntity.RUNTIME), cb.isNull(runtimeType.get("name"))), cb.isNotNull(nodeProp.get("value")), cb.equal(nodePropType.get("propertyName"), "hostName") ); p = addFilters(p, cb, hostFilter, appServerFilter, runtimeFilter, nodeFilter, contextFilter, nodeProp.<String>get("value"), appServer.<String>get("name"), asRuntime.<String>get("name"), node.<String>get("name"), nodeCtx.<String>get("name")); q.where(p); TypedQuery<ServerTuple> query = entityManager.createQuery(q); List<ServerTuple> servers = query.getResultList(); return servers; }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateDate(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { if (theList == null || theList.isEmpty()) { return thePids; }//w w w . j av a 2s.c o m CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamDate> from = cq.from(ResourceIndexedSearchParamDate.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType params = nextOr; Predicate p = createPredicateDate(builder, from, params); codePredicates.add(p); } Predicate masterCodePredicate = builder.or(codePredicates.toArray(new Predicate[0])); Predicate type = builder.equal(from.get("myResourceType"), myResourceName); Predicate name = builder.equal(from.get("myParamName"), theParamName); if (thePids.size() > 0) { Predicate inPids = (from.get("myResourcePid").in(thePids)); cq.where(builder.and(type, name, masterCodePredicate, inPids)); } else { cq.where(builder.and(type, name, masterCodePredicate)); } TypedQuery<Long> q = myEntityManager.createQuery(cq); return new HashSet<Long>(q.getResultList()); }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateString(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { if (theList == null || theList.isEmpty()) { return thePids; }//w w w. ja v a2 s. com CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamString> from = cq.from(ResourceIndexedSearchParamString.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType theParameter = nextOr; Predicate singleCode = createPredicateString(theParameter, theParamName, builder, from); codePredicates.add(singleCode); } Predicate masterCodePredicate = builder.or(codePredicates.toArray(new Predicate[0])); Predicate type = builder.equal(from.get("myResourceType"), myResourceName); Predicate name = builder.equal(from.get("myParamName"), theParamName); if (thePids.size() > 0) { Predicate inPids = (from.get("myResourcePid").in(thePids)); cq.where(builder.and(type, name, masterCodePredicate, inPids)); } else { cq.where(builder.and(type, name, masterCodePredicate)); } TypedQuery<Long> q = myEntityManager.createQuery(cq); return new HashSet<Long>(q.getResultList()); }