Java Code Examples for javax.persistence.criteria.CriteriaBuilder.equal()

The following are Jave code examples for showing how to use equal() of the javax.persistence.criteria.CriteriaBuilder class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: plumdo-work   File: SimpleExpression.java   Source Code and License Vote up 17 votes
@SuppressWarnings({ "rawtypes", "unchecked" })  
public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query,  
        CriteriaBuilder builder) {  
    Path expression = null;  
    if(fieldName.contains(".")){  
        String[] names = StringUtils.split(fieldName, ".");  
        expression = root.get(names[0]);  
        for (int i = 1; i < names.length; i++) {  
            expression = expression.get(names[i]);  
        }  
    }else{  
        expression = root.get(fieldName);  
    }  
    
    switch (operator) {  
    case EQ:  
        return builder.equal(expression, value);  
    case NE:  
        return builder.notEqual(expression, value);  
    case LIKE:  
        return builder.like((Expression<String>) expression, "%" + value + "%");  
    case LT:  
        return builder.lessThan(expression, (Comparable) value);  
    case GT:  
        return builder.greaterThan(expression, (Comparable) value);  
    case LTE:  
        return builder.lessThanOrEqualTo(expression, (Comparable) value);  
    case GTE:  
        return builder.greaterThanOrEqualTo(expression, (Comparable) value);  
    default:  
        return null;  
    }  
}
 
Example 2
Project: endpoint-health   File: EndPointCheckDao.java   Source Code and License Vote up 8 votes
public Collection<EndPointCheck> findByDateRange(final EndPoint endPoint, final Date startDate,
        final Date endDate) {
    final CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
    final CriteriaQuery<EndPointCheck> criteriaQuery = criteriaBuilder.createQuery(getEntityClass());
    final Root<EndPointCheck> root = criteriaQuery
            .from(getEntityManager().getMetamodel().entity(getEntityClass()));

    final ParameterExpression<EndPoint> endPointParameter = criteriaBuilder.parameter(EndPoint.class);
    final ParameterExpression<Date> startDateParameter = criteriaBuilder.parameter(Date.class);
    final ParameterExpression<Date> endDateParameter = criteriaBuilder.parameter(Date.class);

    final Predicate endPointIdPredicate = criteriaBuilder
            .equal(root.get("endPoint"), endPointParameter);

    final Path<Date> checkDatePath = root.<Date> get("checkDate");

    final Predicate startDatePredicate = criteriaBuilder
            .greaterThanOrEqualTo(checkDatePath, startDateParameter);

    final Predicate endDatePredicate = criteriaBuilder.lessThanOrEqualTo(
        checkDatePath,
        endDateParameter);

    criteriaQuery.where(criteriaBuilder.and(endPointIdPredicate, startDatePredicate, endDatePredicate));

    criteriaQuery.orderBy(Arrays.asList(criteriaBuilder.asc(checkDatePath)));

    return getEntityManager().createQuery(criteriaQuery)
            .setParameter(endPointParameter, endPoint)
            .setParameter(startDateParameter, startDate, TemporalType.DATE)
            .setParameter(endDateParameter, endDate, TemporalType.DATE)
            .getResultList();
}
 
Example 3
Project: spring-data-examples   File: CustomerSpecifications.java   Source Code and License Vote up 6 votes
/**
 * All customers with an {@link Account} expiring before the given date.
 * 
 * @param date
 * @return
 */
public static Specification<Customer> accountExpiresBefore(final LocalDate date) {

	return new Specification<Customer>() {

		@Override
		public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

			Root<Account> accounts = query.from(Account.class);
			Path<Date> expiryDate = accounts.<Date> get("expiryDate");
			Predicate customerIsAccountOwner = cb.equal(accounts.<Customer> get("customer"), root);
			Predicate accountExpiryDateBefore = cb.lessThan(expiryDate, date.toDateTimeAtStartOfDay().toDate());

			return cb.and(customerIsAccountOwner, accountExpiryDateBefore);
		}
	};
}
 
Example 4
Project: careconnect-reference-implementation   File: RICodeSystemRepository.java   Source Code and License Vote up 6 votes
@Override
@Transactional
public CodeSystemEntity findBySystem(String system) {

    CriteriaBuilder builder = em.getCriteriaBuilder();

    CodeSystemEntity codeSystemEntity = null;
    CriteriaQuery<CodeSystemEntity> criteria = builder.createQuery(CodeSystemEntity.class);

    Root<CodeSystemEntity> root = criteria.from(CodeSystemEntity.class);
    List<Predicate> predList = new LinkedList<Predicate>();
    log.trace("FlushMode = "+em.getFlushMode());
    log.trace("Entity Manager Properties = "+ em.getProperties().toString());
    Predicate p = builder.equal(root.<String>get("codeSystemUri"),system);
    predList.add(p);
    Predicate[] predArray = new Predicate[predList.size()];
    predList.toArray(predArray);
    if (predList.size()>0)
    {
        log.trace("Found CodeSystem "+system);
        criteria.select(root).where(predArray);

        List<CodeSystemEntity> qryResults = em.createQuery(criteria).getResultList();

        for (CodeSystemEntity cme : qryResults) {
            codeSystemEntity = cme;
            break;
        }
    }
    if (codeSystemEntity == null) {
        log.info("Not found adding CodeSystem = "+system);
        codeSystemEntity = new CodeSystemEntity();
        codeSystemEntity.setCodeSystemUri(system);

        save(codeSystemEntity);

    }
    return codeSystemEntity;
}
 
Example 5
Project: sctalk   File: SimpleExpression.java   Source Code and License Vote up 6 votes
@SuppressWarnings({ "rawtypes", "unchecked" })
public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
    Path expression = null;
    if (fieldName.contains(".")) {
        
        System.out.println(root);
        String[] names = StringUtils.split(fieldName, ".");
        expression = root.get(names[0]);
        for (int i = 1; i < names.length; i++) {
            expression = expression.get(names[i]);
        }
    } else {
        expression = root.get(fieldName);
    }

    switch (operator) {
    case EQ:
        return builder.equal(expression, value);
    case NE:
        return builder.notEqual(expression, value);
    case LIKE:
        return builder.like((Expression<String>) expression, "%" + value + "%");
    case RLIKE:
        return builder.like((Expression<String>) expression, value + "%");
    case LLIKE:
        return builder.like((Expression<String>) expression, value + "%");
    case LT:
        return builder.lessThan(expression, (Comparable) value);
    case GT:
        return builder.greaterThan(expression, (Comparable) value);
    case LTE:
        return builder.lessThanOrEqualTo(expression, (Comparable) value);
    case GTE:
        return builder.greaterThanOrEqualTo(expression, (Comparable) value);
    case ISNULL:
        return builder.isNull(expression);
    case NOTNULL:
        return builder.isNotNull(expression);
    default:
        return null;
    }
}
 
Example 6
Project: stroom-query   File: QueryServiceCriteriaImpl.java   Source Code and License Vote up 6 votes
@Override
public Optional<SearchResponse> search(final ServiceUser user,
                                       final SearchRequest request){
    final String dataSourceUuid = request.getQuery().getDataSource().getUuid();

    try (final Session session = database.openSession()) {
        final CriteriaBuilder cb = session.getCriteriaBuilder();

        final CriteriaQuery<Tuple> cq = cb.createTupleQuery();
        final Root<T> root = cq.from(this.dtoClass);

        cq.multiselect(this.fields.stream()
                .map(f -> root.get(f.getName()))
                .collect(Collectors.toList()));

        final Predicate requestPredicate = getPredicate(cb, root, request.getQuery().getExpression());
        final Predicate dataSourcePredicate = cb.equal(root.get(QueryableEntity.DATA_SOURCE_UUID), dataSourceUuid);

        cq.where(cb.and(requestPredicate, dataSourcePredicate));
        final List<Tuple> tuples = session.createQuery(cq).getResultList();
        final SearchResponse searchResponse = projectResults(request, tuples);

        return Optional.of(searchResponse);
    }
}
 
Example 7
Project: plumdo-stock   File: SimpleExpression.java   Source Code and License Vote up 5 votes
@SuppressWarnings({ "rawtypes", "unchecked" })  
public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query,  
        CriteriaBuilder builder) {  
    Path expression = null;  
    if(fieldName.contains(".")){  
        String[] names = StringUtils.split(fieldName, ".");  
        expression = root.get(names[0]);  
        for (int i = 1; i < names.length; i++) {  
            expression = expression.get(names[i]);  
        }  
    }else{  
        expression = root.get(fieldName);  
    }  
    
    switch (operator) {  
    case EQ:  
        return builder.equal(expression, value);  
    case NE:  
        return builder.notEqual(expression, value);  
    case LIKE:  
        return builder.like((Expression<String>) expression, "%" + value + "%");  
    case LT:  
        return builder.lessThan(expression, (Comparable) value);  
    case GT:  
        return builder.greaterThan(expression, (Comparable) value);  
    case LTE:  
        return builder.lessThanOrEqualTo(expression, (Comparable) value);  
    case GTE:  
        return builder.greaterThanOrEqualTo(expression, (Comparable) value);  
    default:  
        return null;  
    }  
}
 
Example 8
Project: cf-mta-deploy-service   File: DeployTargetDao.java   Source Code and License Vote up 5 votes
private TypedQuery<Dto> createFindByNameQuery(String name) {
    EntityManager em = createEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Dto> query = builder.createQuery(this.classVersion);
    Root<Dto> entity = query.from(this.classVersion);

    Predicate namePredicate = builder.equal(entity.get(DEPLOY_TARGET_NAME), name);
    return em.createQuery(query.select(entity).where(namePredicate));
}
 
Example 9
Project: CriteriaBuilder   File: CriteriaServiceImpl.java   Source Code and License Vote up 5 votes
@SuppressWarnings ({ "unchecked", "rawtypes" })
protected Predicate buildPredicate(Path<T> root, SearchField field)
{
	Path<T> tt = (!field.getField().contains(".")) ? root.get(field.getField()) : fetchNestedPath(root, field.getField());
	CriteriaBuilder criteriaBuilder = this.entitymanager.getCriteriaBuilder();
	
	Class<?> javaType = tt.getJavaType();
	
	if (!classCompatibleWithOperator(javaType, field.getOperator()))
	{
		throw new RuntimeException("operator incompatible with field");
	}
	
	Object valueObject = convertStringValueToObject(field.getValue(), javaType);
	switch (field.getOperator())
	{
		case GE:
			return criteriaBuilder.greaterThan((Expression) tt, (Comparable) valueObject);
		case GTE:
			return criteriaBuilder.greaterThanOrEqualTo((Expression) tt, (Comparable) valueObject);
		case LE:
			return criteriaBuilder.lessThan((Expression) tt, (Comparable) valueObject);
		case LTE:
			return criteriaBuilder.lessThanOrEqualTo((Expression) tt, (Comparable) valueObject);
		case NE: 
               return criteriaBuilder.notEqual(tt, valueObject); 
		case EX:
			return criteriaBuilder.like((Expression) tt, "%"+field.getValue()+"%");
		default:
		{
			//EQ
			return criteriaBuilder.equal(tt, valueObject);
		}
	}
}
 
Example 10
Project: careconnect-reference-implementation   File: ValueSetDao.java   Source Code and License Vote up 5 votes
private ValueSetEntity findValueSetEntity(IdType theId) {

        ValueSetEntity valueSetEntity = null;
        // Only look up if the id is numeric else need to do a search
        if (daoutils.isNumeric(theId.getValue())) {
            valueSetEntity =(ValueSetEntity) em.find(ValueSetEntity.class, theId.getValue());
        }

        // if null try a search on strId
        if (valueSetEntity == null)
        {
            CriteriaBuilder builder = em.getCriteriaBuilder();

            CriteriaQuery<ValueSetEntity> criteria = builder.createQuery(ValueSetEntity.class);
            Root<ValueSetEntity> root = criteria.from(ValueSetEntity.class);
            List<Predicate> predList = new LinkedList<Predicate>();
            Predicate p = builder.equal(root.<String>get("strId"),theId.getValue());
            predList.add(p);
            Predicate[] predArray = new Predicate[predList.size()];
            predList.toArray(predArray);
            if (predList.size()>0)
            {
                criteria.select(root).where(predArray);

                List<ValueSetEntity> qryResults = em.createQuery(criteria).getResultList();

                for (ValueSetEntity cme : qryResults)
                {
                    valueSetEntity = cme;
                    break;
                }
            }
        }
        return valueSetEntity;
    }
 
Example 11
Project: careconnect-reference-implementation   File: RICodeSystemRepository.java   Source Code and License Vote up 5 votes
@Override
@Transactional
public SystemEntity findSystem(String system) {

    CriteriaBuilder builder = em.getCriteriaBuilder();

    SystemEntity systemEntity = null;
    CriteriaQuery<SystemEntity> criteria = builder.createQuery(SystemEntity.class);

    Root<SystemEntity> root = criteria.from(SystemEntity.class);
    List<Predicate> predList = new LinkedList<Predicate>();
    log.debug("Looking for System = " + system);

    Predicate p = builder.equal(root.<String>get("codeSystemUri"),system);
    predList.add(p);
    Predicate[] predArray = new Predicate[predList.size()];
    predList.toArray(predArray);
    if (predList.size()>0)
    {
        log.debug("Found System "+system);
        criteria.select(root).where(predArray);

        List<SystemEntity> qryResults = em.createQuery(criteria).getResultList();

        for (SystemEntity cme : qryResults) {
            systemEntity = cme;
            break;
        }
    }
    if (systemEntity == null) {
        log.info("Not found. Adding SystemEntity = "+system);
        systemEntity = new SystemEntity();
        systemEntity.setUri(system);

        em.persist(systemEntity);
    }
    return systemEntity;
}
 
Example 12
Project: OperatieBRP   File: LeveringsautorisatieFilterFactory.java   Source Code and License Vote up 5 votes
@Override
public Predicate toPredicate(final Root<?> leveringsautorisatieRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
    final Root<ToegangLeveringsAutorisatie> toegangLeveringsAutorisaties = query.from(ToegangLeveringsAutorisatie.class);
    final Predicate joinPredicateToegangLeveringsAutorisatie =
            leveringsautorisatieRoot.get(ID).in(toegangLeveringsAutorisaties.get(LEVERINGSAUTORISATIE).get(ID));
    final Predicate geautoriseerdePredicate = cb.equal(toegangLeveringsAutorisaties.get(field), value);
    return cb.and(joinPredicateToegangLeveringsAutorisatie, geautoriseerdePredicate);
}
 
Example 13
Project: OperatieBRP   File: BijhoudingsautorisatieFilterFactory.java   Source Code and License Vote up 5 votes
@Override
public Predicate toPredicate(final Root<?> leveringsautorisatieRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
    final Root<ToegangBijhoudingsautorisatie> toegangLeveringsAutorisaties = query.from(ToegangBijhoudingsautorisatie.class);
    final Predicate joinPredicateToegangLeveringsAutorisatie =
            leveringsautorisatieRoot.get(ID).in(toegangLeveringsAutorisaties.get(BIJHOUDINGSAUTORISATIE).get(ID));
    final Predicate geautoriseerdePredicate = cb.equal(toegangLeveringsAutorisaties.get(field), value);
    return cb.and(joinPredicateToegangLeveringsAutorisatie, geautoriseerdePredicate);
}
 
Example 14
Project: careconnect-reference-implementation   File: RIValueSetRepository.java   Source Code and License Vote up 5 votes
private ValueSetEntity findValueSetEntity(IdType theId) {

        ValueSetEntity valueSetEntity = null;
        // Only look up if the id is numeric else need to do a search
        if (daoutils.isNumeric(theId.getValue())) {
            valueSetEntity =(ValueSetEntity) em.find(ValueSetEntity.class, theId.getValue());
        }

        // if null try a search on strId
        if (valueSetEntity == null)
        {
            CriteriaBuilder builder = em.getCriteriaBuilder();

            CriteriaQuery<ValueSetEntity> criteria = builder.createQuery(ValueSetEntity.class);
            Root<ValueSetEntity> root = criteria.from(ValueSetEntity.class);
            List<Predicate> predList = new LinkedList<Predicate>();
            Predicate p = builder.equal(root.<String>get("strId"),theId.getValue());
            predList.add(p);
            Predicate[] predArray = new Predicate[predList.size()];
            predList.toArray(predArray);
            if (predList.size()>0)
            {
                criteria.select(root).where(predArray);

                List<ValueSetEntity> qryResults = em.createQuery(criteria).getResultList();

                for (ValueSetEntity cme : qryResults)
                {
                    valueSetEntity = cme;
                    break;
                }
            }
        }
        return valueSetEntity;
    }
 
Example 15
Project: OperatieBRP   File: AdministratieveHandelingFilterFactory.java   Source Code and License Vote up 5 votes
@Override
public Predicate toPredicate(final Root<?> administratieveHandelingRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
    final Root<PersoonAfgeleidAdministratiefHistorie> persoonHistoryRoot = query.from(PersoonAfgeleidAdministratiefHistorie.class);

    // Join persoon
    final Predicate joinPredicate = administratieveHandelingRoot.get(ID).in(persoonHistoryRoot.get(ADMINISTRATIEVE_HANDELING).get(ID));

    // Bsn
    final Predicate bsnPredicate = cb.equal(persoonHistoryRoot.get(PERSOON).get(ADMINISTRATIE_NUMMER), value);

    return cb.and(bsnPredicate, joinPredicate);
}
 
Example 16
Project: careconnect-reference-implementation   File: CodeSystemDao.java   Source Code and License Vote up 5 votes
@Override
@Transactional
public CodeSystemEntity findBySystem(String system) {

    CriteriaBuilder builder = em.getCriteriaBuilder();

    CodeSystemEntity codeSystemEntity = null;
    CriteriaQuery<CodeSystemEntity> criteria = builder.createQuery(CodeSystemEntity.class);

    Root<CodeSystemEntity> root = criteria.from(CodeSystemEntity.class);
    List<Predicate> predList = new LinkedList<Predicate>();
    log.trace("FlushMode = "+em.getFlushMode());
    log.trace("Entity Manager Properties = "+ em.getProperties().toString());
    Predicate p = builder.equal(root.<String>get("codeSystemUri"),system);
    predList.add(p);
    Predicate[] predArray = new Predicate[predList.size()];
    predList.toArray(predArray);
    if (predList.size()>0)
    {
        log.trace("Found CodeSystem "+system);
        criteria.select(root).where(predArray);

        List<CodeSystemEntity> qryResults = em.createQuery(criteria).getResultList();

        for (CodeSystemEntity cme : qryResults) {
            codeSystemEntity = cme;
            break;
        }
    }
    if (codeSystemEntity == null) {
        log.info("Not found adding CodeSystem = "+system);
        codeSystemEntity = new CodeSystemEntity();
        codeSystemEntity.setCodeSystemUri(system);

        save(codeSystemEntity);

    }
    return codeSystemEntity;
}
 
Example 17
Project: iotplatform   File: JpaRelationDao.java   Source Code and License Vote up 5 votes
private Specification<RelationEntity> getEntityFieldsSpec(EntityId from, String relationType,
    RelationTypeGroup typeGroup, ThingType childType) {
  return new Specification<RelationEntity>() {
    @Override
    public Predicate toPredicate(Root<RelationEntity> root, CriteriaQuery<?> criteriaQuery,
        CriteriaBuilder criteriaBuilder) {
      List<Predicate> predicates = new ArrayList<>();
      if (from != null) {
        Predicate fromIdPredicate = criteriaBuilder.equal(root.get("fromId"),
            UUIDConverter.fromTimeUUID(from.getId()));
        predicates.add(fromIdPredicate);
        Predicate fromEntityTypePredicate = criteriaBuilder.equal(root.get("fromType"), from.getEntityType().name());
        predicates.add(fromEntityTypePredicate);
      }
      if (relationType != null) {
        Predicate relationTypePredicate = criteriaBuilder.equal(root.get("relationType"), relationType);
        predicates.add(relationTypePredicate);
      }
      if (typeGroup != null) {
        Predicate typeGroupPredicate = criteriaBuilder.equal(root.get("relationTypeGroup"), typeGroup.name());
        predicates.add(typeGroupPredicate);
      }
      if (childType != null) {
        Predicate childTypePredicate = criteriaBuilder.equal(root.get("toType"), childType.name());
        predicates.add(childTypePredicate);
      }
      return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
    }
  };
}
 
Example 18
Project: sucok-framework   File: Equal.java   Source Code and License Vote up 4 votes
@Override
public Predicate buildJpaPredicate(CriteriaBuilder cb, Root<?> root) {
	Expression<Object> path = QueryFormHelper.getPath(root, field);
	return cb.equal(path, value);
}
 
Example 19
Project: spring-cloud-samples   File: BaseRepositoryImpl.java   Source Code and License Vote up 4 votes
@Override
public T getOneByUpdate(ID id) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = builder.createQuery(entityClass);
    Root<T> root = criteriaQuery.from(entityClass);
    criteriaQuery.select(root);
    Predicate pre = builder.equal(root.get(getPrimaryKey()).as(Integer.class), id);
    criteriaQuery.where(pre);
    TypedQuery<T> query = entityManager.createQuery(criteriaQuery);
    query.setLockMode(LockModeType.READ);
    List<T> list = query.getResultList();
    return CollectionUtils.isEmpty(list) ? null : list.get(0);
}
 
Example 20
Project: OperatieBRP   File: PersoonViewFactory.java   Source Code and License Vote up 4 votes
@Override
public Predicate toPredicate(final Root<PersoonAfnemerindicatie> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
    return cb.equal(root.join("persoon").get("id"), persoonId);
}