Example usage for javax.persistence.criteria CriteriaBuilder equal

List of usage examples for javax.persistence.criteria CriteriaBuilder equal

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder equal.

Prototype

Predicate equal(Expression<?> x, Object y);

Source Link

Document

Create a predicate for testing the arguments for equality.

Usage

From source file:net.groupbuy.dao.impl.ProductDaoImpl.java

public List<Product> search(String keyword, Boolean isGift, Integer count) {
    if (StringUtils.isEmpty(keyword)) {
        return Collections.<Product>emptyList();
    }//  w ww .java  2s.  com
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Product> criteriaQuery = criteriaBuilder.createQuery(Product.class);
    Root<Product> root = criteriaQuery.from(Product.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (pattern.matcher(keyword).matches()) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.equal(root.get("id"), Long.valueOf(keyword)),
                        criteriaBuilder.like(root.<String>get("sn"), "%" + keyword + "%"),
                        criteriaBuilder.like(root.<String>get("fullName"), "%" + keyword + "%")));
    } else {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.like(root.<String>get("sn"), "%" + keyword + "%"),
                        criteriaBuilder.like(root.<String>get("fullName"), "%" + keyword + "%")));
    }
    if (isGift != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isGift"), isGift));
    }
    criteriaQuery.where(restrictions);
    criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")));
    return super.findList(criteriaQuery, null, count, null, null);
}

From source file:com.deloitte.smt.service.SignalDetectionService.java

/**
 * @param searchDto/*from   www.j av a 2s . c o  m*/
 * @param criteriaBuilder
 * @param criteriaQuery
 * @param rootSignalDetection
 * @param predicates
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addPts(SearchDto searchDto, CriteriaBuilder criteriaBuilder, CriteriaQuery criteriaQuery,
        Root<SignalDetection> rootSignalDetection, List<Predicate> predicates) {
    if (!CollectionUtils.isEmpty(searchDto.getPts())) {
        Root<Pt> rootPt = criteriaQuery.from(Pt.class);
        Predicate signalDetectionPtEquals = criteriaBuilder.equal(rootSignalDetection.get("id"),
                rootPt.get(SmtConstant.DETECTION_ID.getDescription()));

        Predicate hlgtNameIn = criteriaBuilder.isTrue(rootPt.get("ptName").in(searchDto.getPts()));

        predicates.add(signalDetectionPtEquals);
        predicates.add(hlgtNameIn);
    }
}

From source file:com.deloitte.smt.service.SignalDetectionService.java

/**
 * @param searchDto//  www .  j a  v a2s  . c  o m
 * @param criteriaBuilder
 * @param criteriaQuery
 * @param rootSignalDetection
 * @param predicates
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addHlts(SearchDto searchDto, CriteriaBuilder criteriaBuilder, CriteriaQuery criteriaQuery,
        Root<SignalDetection> rootSignalDetection, List<Predicate> predicates) {
    if (!CollectionUtils.isEmpty(searchDto.getHlts())) {
        Root<Hlt> rootHlt = criteriaQuery.from(Hlt.class);
        Predicate signalDetectionHltEquals = criteriaBuilder.equal(rootSignalDetection.get("id"),
                rootHlt.get(SmtConstant.DETECTION_ID.getDescription()));

        Predicate hltNameIn = criteriaBuilder.isTrue(rootHlt.get("hltName").in(searchDto.getHlts()));

        predicates.add(signalDetectionHltEquals);
        predicates.add(hltNameIn);
    }
}

From source file:com.deloitte.smt.service.SignalDetectionService.java

/**
 * @param searchDto// www  . j  a v a2 s  . c  o m
 * @param criteriaBuilder
 * @param criteriaQuery
 * @param rootSignalDetection
 * @param predicates
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addSocs(SearchDto searchDto, CriteriaBuilder criteriaBuilder, CriteriaQuery criteriaQuery,
        Root<SignalDetection> rootSignalDetection, List<Predicate> predicates) {
    if (!CollectionUtils.isEmpty(searchDto.getSocs())) {
        Root<Soc> rootSoc = criteriaQuery.from(Soc.class);
        Predicate signalDetectionSocEquals = criteriaBuilder.equal(rootSignalDetection.get("id"),
                rootSoc.get(SmtConstant.DETECTION_ID.getDescription()));

        Predicate socNameIn = criteriaBuilder.isTrue(rootSoc.get("socName").in(searchDto.getSocs()));

        predicates.add(signalDetectionSocEquals);
        predicates.add(socNameIn);
    }
}

From source file:com.deloitte.smt.service.SignalDetectionService.java

/**
 * @param searchDto/*from w w  w.  j  a v a2 s.c om*/
 * @param criteriaBuilder
 * @param criteriaQuery
 * @param rootSignalDetection
 * @param predicates
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addHlgts(SearchDto searchDto, CriteriaBuilder criteriaBuilder, CriteriaQuery criteriaQuery,
        Root<SignalDetection> rootSignalDetection, List<Predicate> predicates) {
    if (!CollectionUtils.isEmpty(searchDto.getHlgts())) {
        Root<Hlgt> rootHlgt = criteriaQuery.from(Hlgt.class);
        Predicate signalDetectionHlgtEquals = criteriaBuilder.equal(rootSignalDetection.get("id"),
                rootHlgt.get(SmtConstant.DETECTION_ID.getDescription()));

        Predicate hlgtNameIn = criteriaBuilder.isTrue(rootHlgt.get("hlgtName").in(searchDto.getHlgts()));

        predicates.add(signalDetectionHlgtEquals);
        predicates.add(hlgtNameIn);
    }
}

From source file:com.deloitte.smt.service.SignalDetectionService.java

/**
 * @param searchDto// w ww .j a v a2  s. com
 * @param criteriaBuilder
 * @param criteriaQuery
 * @param rootIngredient
 * @param predicates
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addLicenses(SearchDto searchDto, CriteriaBuilder criteriaBuilder, CriteriaQuery criteriaQuery,
        Root<SignalDetection> rootSignalDetection, List<Predicate> predicates) {
    if (!CollectionUtils.isEmpty(searchDto.getLicenses())) {
        Root<License> rootLicense = criteriaQuery.from(License.class);
        Predicate ingredientLicenseEquals = criteriaBuilder.equal(rootSignalDetection.get("id"),
                rootLicense.get("ingredientId"));

        Predicate licenseNameIn = criteriaBuilder
                .isTrue(rootLicense.get("licenseName").in(searchDto.getLicenses()));

        predicates.add(ingredientLicenseEquals);
        predicates.add(licenseNameIn);
    }
}

From source file:com.deloitte.smt.service.SignalDetectionService.java

/**
 * @param searchDto/*from  ww w. j av a 2s .  co m*/
 * @param criteriaBuilder
 * @param criteriaQuery
 * @param rootIngredient
 * @param predicates
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addProducts(SearchDto searchDto, CriteriaBuilder criteriaBuilder, CriteriaQuery criteriaQuery,
        Root<SignalDetection> rootSignalDetection, List<Predicate> predicates) {
    if (!CollectionUtils.isEmpty(searchDto.getProducts())) {
        Root<Product> rootProduct = criteriaQuery.from(Product.class);
        Predicate ingredientProductEquals = criteriaBuilder.equal(rootSignalDetection.get("id"),
                rootProduct.get("detectionId"));

        Predicate productNameIn = criteriaBuilder
                .isTrue(rootProduct.get("productName").in(searchDto.getProducts()));

        predicates.add(ingredientProductEquals);
        predicates.add(productNameIn);
    }
}

From source file:org.openmeetings.app.data.calendar.daos.AppointmentDaoImpl.java

public List<Appointment> getAppointmentsByCritAndCat(Long cat_id) {
    try {//from ww  w .java 2  s. c om

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Appointment> cq = cb.createQuery(Appointment.class);
        Root<Appointment> c = cq.from(Appointment.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        Predicate subCondition = cb.equal(c.get("categoryId"), cat_id);
        cq.where(condition, subCondition);
        TypedQuery<Appointment> q = em.createQuery(cq);
        List<Appointment> listAppoints = q.getResultList();

        return listAppoints;
    } catch (Exception ex2) {
        log.error("[getAppointements]: ", ex2);
    }
    return null;
}

From source file:com.hiperium.dao.common.generic.GenericDAO.java

/**
 * /* www .  java 2  s . c om*/
 * @param criteriaBuilder
 * @param criteriaQuery
 * @param root
 * @param entity
 */
private <E> void constructQuery(CriteriaBuilder criteriaBuilder, final CriteriaQuery<E> criteriaQuery,
        Root<E> root, E entity) {
    Map<String, Object> properties = this.getEntityProperties(entity);
    CriteriaFieldConditions criteria = new CriteriaFieldConditions(properties);
    List<Predicate> predicateList = new ArrayList<Predicate>();
    // Add equality conditions conditions
    Set<String> equalityKeys = criteria.equality.keySet();
    for (String key : equalityKeys) {
        predicateList.add(criteriaBuilder.equal(root.get(key), criteria.equality.get(key)));
    }
    // Add like conditions
    Set<String> likeKeys = criteria.like.keySet();
    for (String key : likeKeys) {
        predicateList.add(criteriaBuilder.like(root.<String>get(key), criteria.like.get(key).toString()));
    }
    // Add composite conditions, only with equality conditions
    Set<String> compositeKeys = criteria.composite.keySet();
    for (String key : compositeKeys) {
        Object value = criteria.composite.get(key);
        try {
            if (value.toString().startsWith("class java.util")) {
                continue;
            } else if (StringUtils.containsIgnoreCase(key, PK_OBJECT_NAME)) {
                Map<String, Object> propsComposites = this.getEntityProperties(value, key);
                CriteriaFieldConditions compositeCriteria = new CriteriaFieldConditions(propsComposites);
                if (!compositeCriteria.equality.isEmpty()) {
                    Set<String> equalityKeysPk = compositeCriteria.equality.keySet();
                    for (String keyPk : equalityKeysPk) {
                        String pkValueName = keyPk.replace(PK_OBJECT_NAME.concat("."), "");
                        predicateList.add(criteriaBuilder.equal(root.get(PK_OBJECT_NAME).get(pkValueName),
                                compositeCriteria.equality.get(keyPk)));
                    }
                }
                if (!compositeCriteria.like.isEmpty()) {
                    Set<String> likeKeysPK = compositeCriteria.like.keySet();
                    for (String keyPk : likeKeysPK) {
                        String pkValueName = keyPk.replace(PK_OBJECT_NAME.concat("."), "");
                        Expression<String> expression = root.<String>get(PK_OBJECT_NAME).get(pkValueName);
                        predicateList.add(
                                criteriaBuilder.like(expression, compositeCriteria.like.get(keyPk).toString()));
                    }
                }
            }
        } catch (RuntimeException e) {
            continue;
        }
    }
    // Adding where stuff is necessary
    if (predicateList.size() == 1) {
        criteriaQuery.where(predicateList.get(0));
    } else if (predicateList.size() > 1) {
        Predicate[] predicateCriteria = new Predicate[predicateList.size()];
        predicateCriteria = predicateList.toArray(predicateCriteria);
        criteriaQuery.where(criteriaBuilder.and(predicateCriteria));
    }
}

From source file:org.openregistry.core.repository.jpa.JpaPersonRepository.java

public List<Person> searchByCriteria(final SearchCriteria searchCriteria) throws RepositoryAccessException {
    final String givenName = searchCriteria.getGivenName();
    final String familyName = searchCriteria.getFamilyName();
    final Date birthDate = searchCriteria.getDateOfBirth();
    final String searchCriteriaName = searchCriteria.getName();

    // search by role criteria
    final String sponsorNetID = searchCriteria.getSponsorNetID();
    final OrganizationalUnit organizationalUnit = searchCriteria.getOrganizationalUnit();
    final Date roleExpDate = searchCriteria.getRoleExpDate();
    final Type roleType = searchCriteria.getAffiliationType();

    final CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();

    final CriteriaQuery<JpaPersonImpl> c = criteriaBuilder.createQuery(JpaPersonImpl.class);
    c.distinct(true);//from w  w w.j a va  2s .co  m
    final Root<JpaPersonImpl> person = c.from(JpaPersonImpl.class);
    final Join<JpaPersonImpl, JpaNameImpl> name = person.join(JpaPersonImpl_.names);
    final Join<JpaPersonImpl, JpaRoleImpl> role = person.join(JpaPersonImpl_.roles);

    //        person.fetch(JpaPersonImpl_.names);
    //        person.fetch(JpaPersonImpl_.roles);
    //        person.fetch(JpaPersonImpl_.identifiers);

    final Predicate pBirthDate;
    if (birthDate != null) {
        pBirthDate = criteriaBuilder.equal(person.get(JpaPersonImpl_.dateOfBirth), birthDate);
    } else {
        pBirthDate = null;
    }

    Predicate combined = null;

    if (StringUtils.hasText(givenName) && StringUtils.hasText(familyName)) {
        //          final Predicate pGivenName = criteriaBuilder.equal(name.get(JpaNameImpl_.given), givenName );
        Expression<String> pattern = criteriaBuilder.literal((String) givenName + "%");
        final Predicate pGivenName = criteriaBuilder.like(name.get(JpaNameImpl_.given), pattern);
        final Predicate pFamilyName = criteriaBuilder.equal(name.get(JpaNameImpl_.family), familyName);

        combined = criteriaBuilder.and(pGivenName, pFamilyName);
    } else if (StringUtils.hasText(givenName)) {
        combined = criteriaBuilder.equal(name.get(JpaNameImpl_.given), givenName);
    } else if (StringUtils.hasText(familyName)) {
        combined = criteriaBuilder.equal(name.get(JpaNameImpl_.family), familyName);
    } else if (StringUtils.hasText(searchCriteriaName)) {
        final Predicate pGivenName = criteriaBuilder.equal(name.get(JpaNameImpl_.given), searchCriteriaName);
        final Predicate pFamilyName = criteriaBuilder.equal(name.get(JpaNameImpl_.family), searchCriteriaName);
        combined = criteriaBuilder.or(pGivenName, pFamilyName);
    }

    Predicate pRoleCombined = null;

    if (roleType != null) {
        //final Join<JpaPersonImpl,JpaRoleImpl> role = person.join(JpaPersonImpl_.roles);
        //final Join<JpaPersonImpl,JpaRoleImpl> role = name.join(JpaPersonImpl_.roles);
        pRoleCombined = criteriaBuilder.equal(role.get(JpaRoleImpl_.affiliationType), roleType);
    }

    if (organizationalUnit != null && StringUtils.hasText(organizationalUnit.getName())) {
        Predicate orgUnitP = criteriaBuilder.equal(role.get(JpaRoleImpl_.organizationalUnit),
                organizationalUnit);
        if (pRoleCombined != null)
            pRoleCombined = criteriaBuilder.and(pRoleCombined, orgUnitP);
        else
            pRoleCombined = orgUnitP;
        //pRoleCombined = criteriaBuilder.and(pRoleCombined, orgUnitP);
    }

    if (StringUtils.hasText(sponsorNetID)) {
        Person person1 = findByIdentifier(Type.IdentifierTypes.NETID.name(), sponsorNetID);
        if (person1 != null) {
            Predicate sponsorP = criteriaBuilder.equal(role.get(JpaRoleImpl_.sponsorId), person1.getId());
            if (pRoleCombined != null)
                pRoleCombined = criteriaBuilder.and(pRoleCombined, sponsorP);
            else
                pRoleCombined = sponsorP;
        } else {
            pRoleCombined = criteriaBuilder.or();
        }

    }
    if (roleExpDate != null) {
        Predicate expDateP = criteriaBuilder.between(role.get(JpaRoleImpl_.end), new Date(), roleExpDate);
        if (pRoleCombined != null)
            pRoleCombined = criteriaBuilder.and(pRoleCombined, expDateP);
        else
            pRoleCombined = expDateP;
    }

    Predicate pComplete = criteriaBuilder.and();

    if (pBirthDate != null)
        pComplete = criteriaBuilder.and(pComplete, pBirthDate);
    if (combined != null)
        pComplete = criteriaBuilder.and(pComplete, combined);
    if (pRoleCombined != null)
        pComplete = criteriaBuilder.and(pComplete, pRoleCombined);

    c.select(person).where(pComplete);

    //        if (pBirthDate != null && combined != null) {
    //            c.select(person).where(criteriaBuilder.and(pBirthDate, combined));
    //        } else if (pBirthDate != null) {
    //            c.select(person).where(pBirthDate);
    //        } else {
    //            c.select(person).where(combined);
    //        }

    final List<JpaPersonImpl> persons = this.entityManager.createQuery(c).setMaxResults(MAX_QUERY_LIMIT)
            .getResultList();

    return new ArrayList<Person>(persons);
}