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:org.openmeetings.app.data.conference.Roommanagement.java

/**
 * /* w w  w. j a  v  a  2 s.  co  m*/
 * @param rooms_organisation_id
 * @return
 */
public Rooms_Organisation getRoomsOrganisationById(long rooms_organisation_id) {
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Rooms_Organisation> cq = cb.createQuery(Rooms_Organisation.class);
        Root<Rooms_Organisation> c = cq.from(Rooms_Organisation.class);
        Predicate condition = cb.equal(c.get("rooms_organisation_id"), rooms_organisation_id);
        cq.where(condition);
        TypedQuery<Rooms_Organisation> q = em.createQuery(cq);
        List<Rooms_Organisation> ll = q.getResultList();

        if (ll.size() > 0) {
            return ll.get(0);
        }
    } catch (Exception ex2) {
        log.error("[getRoomsByOrganisation] ", ex2);
    }
    return null;
}

From source file:com.yunguchang.data.ApplicationRepository.java

public List<TBusApplyinfoEntity> getApplicationWithDetailByScheduleId(String scheduleId,
        PrincipalExt principalExt) {/*from   w  w  w. j a  v  a2 s  .c om*/
    applySecurityFilter("applications", principalExt);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class);
    Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class);
    Fetch<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleFetch = applyRoot
            .fetch(TBusApplyinfoEntity_.schedule, JoinType.LEFT);
    applyRoot.fetch(TBusApplyinfoEntity_.fleet);
    applyRoot.fetch(TBusApplyinfoEntity_.passenger);
    applyRoot.fetch(TBusApplyinfoEntity_.coordinator);
    applyRoot.fetch(TBusApplyinfoEntity_.department);
    Join<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleJoin = (Join<TBusApplyinfoEntity, TBusScheduleRelaEntity>) scheduleFetch;
    Predicate predicate = cb.conjunction();
    predicate = cb.and(predicate, cb.equal(scheduleJoin.get(TBusScheduleRelaEntity_.uuid), scheduleId));
    cq.where(predicate);
    return em.createQuery(cq).getResultList();
}

From source file:gov.guilin.dao.impl.ProductDaoImpl.java

public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
    Root<Product> product = criteriaQuery.from(Product.class);
    Join<Product, OrderItem> orderItems = product.join("orderItems");
    Join<Product, gov.guilin.entity.Order> order = orderItems.join("order");
    criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"),
            product.get("fullName"), product.get("price"),
            criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder
                    .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate));
    }/*from   w w  w .  java 2  s .c o  m*/
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate));
    }
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid));
    criteriaQuery.where(restrictions);
    criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"),
            product.get("price"));
    criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(
            criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))));
    TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (count != null && count >= 0) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:com.yunguchang.data.ApplicationRepository.java

public List<TRsDriverinfoEntity> listAllCandidateDrivers(String[] applicationIds, String carId, String keyword,
        Integer offset, Integer limit, PrincipalExt principalExt) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TRsDriverinfoEntity> cq = cb.createQuery(TRsDriverinfoEntity.class);
    Root<TRsDriverinfoEntity> driverRoot = cq.from(TRsDriverinfoEntity.class);
    Root<TAzCarinfoEntity> carRoot = cq.from(TAzCarinfoEntity.class);
    cq.select(driverRoot);/*www  .j a v  a2 s.  com*/

    Subquery<TBusScheduleCarEntity> overlapScheduleCarSubQuery = cq.subquery(TBusScheduleCarEntity.class);

    applyOverlapScheduleCarSubquery(overlapScheduleCarSubQuery, applicationIds, carRoot, driverRoot, cb,
            principalExt);

    Subquery<TRsDriverinfoEntity> subqueryLicense = cq.subquery(TRsDriverinfoEntity.class);
    Root<TAzJzRelaEntity> licenseMapping = subqueryLicense.from(TAzJzRelaEntity.class);
    subqueryLicense.select(driverRoot);
    subqueryLicense.where(

            cb.equal(carRoot.get(TAzCarinfoEntity_.xszcx), licenseMapping.get(TAzJzRelaEntity_.xszcx)),
            cb.equal(licenseMapping.get(TAzJzRelaEntity_.jzyq),
                    driverRoot.get(TRsDriverinfoEntity_.drivecartype)));

    Predicate predicate = cb.and(cb.equal(carRoot.get(TAzCarinfoEntity_.id), carId),
            cb.equal(driverRoot.get(TRsDriverinfoEntity_.department), carRoot.get(TAzCarinfoEntity_.sysOrg)),
            cb.equal(driverRoot.get(TRsDriverinfoEntity_.enabled), "1"),
            cb.or(cb.exists(subqueryLicense), cb.isNull(driverRoot.get(TRsDriverinfoEntity_.drivecartype))

            ),

            cb.not(cb.exists(overlapScheduleCarSubQuery))

    );

    if (keyword != null) {
        predicate = cb.and(predicate,
                cb.or(cb.like(driverRoot.get(TRsDriverinfoEntity_.drivername), "%" + keyword + "%")));
    }

    cq.where(predicate);
    cq.orderBy(cb
            .asc(cb.function("casttogbk", String.class, cb.trim(driverRoot.get(TRsDriverinfoEntity_.drivername))

    )));
    TypedQuery<TRsDriverinfoEntity> query = em.createQuery(cq);
    if (offset != null) {
        query.setFirstResult(offset);
    }

    if (limit != null) {
        query.setMaxResults(limit);
    }

    return query.getResultList();

}

From source file:com.yunguchang.data.ApplicationRepository.java

public List<TBusApplyinfoEntity> getApplicationForSync(DateTime startTime, DateTime endTime, String status,
        Boolean isSend, PrincipalExt principalExt) {
    if (startTime == null) {
        startTime = DateTimeUtil.appStartTime;
    }//ww  w  . j  a v  a2 s  . c om
    if (endTime == null) {
        endTime = DateTimeUtil.appEndTime;
    }
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class);
    Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class);
    applyRoot.fetch(TBusApplyinfoEntity_.passenger);
    applyRoot.fetch(TBusApplyinfoEntity_.coordinator);
    applyRoot.fetch(TBusApplyinfoEntity_.department);
    applyRoot.fetch(TBusApplyinfoEntity_.senduser, JoinType.LEFT);

    Predicate predicate = cb.conjunction();

    predicate = cb.and(predicate, cb.or(cb.isNull(applyRoot.get(TBusApplyinfoEntity_.updateBySync)),
            cb.isFalse(applyRoot.get(TBusApplyinfoEntity_.updateBySync))));
    if (status != null) {
        predicate = cb.and(predicate,
                // "2" ->  "3" -> ? "4" -> ?
                cb.equal(applyRoot.get(TBusApplyinfoEntity_.status), status));
    }
    if (isSend != null && isSend) {
        predicate = cb.and(predicate, cb.equal(applyRoot.get(TBusApplyinfoEntity_.issend), "1") // 
        );
    }
    if ("4".equals(status)) {
        Fetch<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleFetch = applyRoot
                .fetch(TBusApplyinfoEntity_.schedule, JoinType.LEFT);
        Join<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleJoin = (Join<TBusApplyinfoEntity, TBusScheduleRelaEntity>) scheduleFetch;
        scheduleJoin.fetch(TBusScheduleRelaEntity_.reciveuser, JoinType.LEFT);
        Fetch<TBusScheduleRelaEntity, TBusScheduleCarEntity> fetchScheduleCar = scheduleFetch
                .fetch(TBusScheduleRelaEntity_.scheduleCars, JoinType.LEFT);
        fetchScheduleCar.fetch(TBusScheduleCarEntity_.car, JoinType.LEFT).fetch(TAzCarinfoEntity_.depot,
                JoinType.LEFT);
        fetchScheduleCar.fetch(TBusScheduleCarEntity_.driver, JoinType.LEFT)
                .fetch(TRsDriverinfoEntity_.department, JoinType.LEFT);
        predicate = cb.and(predicate,
                cb.or(cb.between(applyRoot.get(TBusApplyinfoEntity_.updatedate), startTime, endTime),
                        cb.between(scheduleJoin.get(TBusScheduleRelaEntity_.updatedate), startTime, endTime)));
    } else {
        predicate = cb.and(predicate,
                cb.between(applyRoot.get(TBusApplyinfoEntity_.updatedate), startTime, endTime));
    }

    cq.where(predicate);

    TypedQuery<TBusApplyinfoEntity> query = em.createQuery(cq);

    //        applySecurityFilter("applications", principalExt);
    return query.getResultList();
}

From source file:eu.domibus.common.dao.ErrorLogDao.java

public List<ErrorLogEntry> findPaged(int from, int max, String column, boolean asc,
        HashMap<String, Object> filters) {
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<ErrorLogEntry> cq = cb.createQuery(ErrorLogEntry.class);
    Root<ErrorLogEntry> ele = cq.from(ErrorLogEntry.class);
    cq.select(ele);//from   w  w  w .j  ava  2s  .  c om
    List<Predicate> predicates = new ArrayList<Predicate>();
    for (Map.Entry<String, Object> filter : filters.entrySet()) {
        if (filter.getValue() != null) {
            if (filter.getValue() instanceof String) {
                if (!filter.getValue().toString().isEmpty()) {
                    switch (filter.getKey().toString()) {
                    case "":
                        break;
                    case "timestampFrom":
                        predicates.add(cb.greaterThanOrEqualTo(ele.<Date>get("timestamp"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "timestampTo":
                        predicates.add(cb.lessThanOrEqualTo(ele.<Date>get("timestamp"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "notifiedFrom":
                        predicates.add(cb.greaterThanOrEqualTo(ele.<Date>get("notified"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "notifiedTo":
                        predicates.add(cb.lessThanOrEqualTo(ele.<Date>get("notified"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    default:
                        predicates.add(cb.like(ele.<String>get(filter.getKey()), (String) filter.getValue()));
                        break;
                    }
                }
            } else {
                predicates.add(cb.equal(ele.<String>get(filter.getKey()), filter.getValue()));
            }
        }
    }
    cq.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    if (column != null) {
        if (asc) {
            cq.orderBy(cb.asc(ele.get(column)));
        } else {
            cq.orderBy(cb.desc(ele.get(column)));
        }

    }
    final TypedQuery<ErrorLogEntry> query = this.em.createQuery(cq);
    query.setFirstResult(from);
    query.setMaxResults(max);
    return query.getResultList();
}

From source file:ca.uhn.fhir.jpa.dao.BaseHapiFhirDao.java

protected TagDefinition getTag(TagTypeEnum theTagType, String theScheme, String theTerm, String theLabel) {
    CriteriaBuilder builder = myEntityManager.getCriteriaBuilder();
    CriteriaQuery<TagDefinition> cq = builder.createQuery(TagDefinition.class);
    Root<TagDefinition> from = cq.from(TagDefinition.class);

    //@formatter:off
    if (isNotBlank(theScheme)) {
        cq.where(builder.and(builder.equal(from.get("myTagType"), theTagType),
                builder.equal(from.get("mySystem"), theScheme), builder.equal(from.get("myCode"), theTerm)));
    } else {/*w  w w .  j  a v a2 s  . co  m*/
        cq.where(builder.and(builder.equal(from.get("myTagType"), theTagType),
                builder.isNull(from.get("mySystem")), builder.equal(from.get("myCode"), theTerm)));
    }
    //@formatter:on

    TypedQuery<TagDefinition> q = myEntityManager.createQuery(cq);
    try {
        return q.getSingleResult();
    } catch (NoResultException e) {
        TagDefinition retVal = new TagDefinition(theTagType, theScheme, theTerm, theLabel);
        myEntityManager.persist(retVal);
        return retVal;
    }
}

From source file:org.openmeetings.app.data.user.Usermanagement.java

public Users getUserByIdAndDeleted(Long id) throws Exception {
    log.debug("Usermanagement.getUserById");

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Users> cq = cb.createQuery(Users.class);
    Root<Users> c = cq.from(Users.class);
    Predicate condition = cb.equal(c.get("user_id"), id);
    cq.where(condition);/* www  .  j a  v  a 2 s  . c  o m*/
    TypedQuery<Users> q = em.createQuery(cq);
    Users u = null;
    try {
        u = q.getSingleResult();
    } catch (NoResultException e) {
        // u=null}
    }

    return u;

}

From source file:org.openmeetings.app.data.user.Usermanagement.java

/**
 * @author o.becherer Find User by LoginName (test existence of a active
 *         user with login - name/*from w w w  .j a  v  a  2  s. c o m*/
 */
// -----------------------------------------------------------------------------------------------------
public Users getUserByLogin(String login) throws Exception {
    log.debug("Usermanagement.getUserByLogin : " + login);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Users> cq = cb.createQuery(Users.class);
    Root<Users> c = cq.from(Users.class);
    Predicate condition = cb.equal(c.get("deleted"), "false");
    Predicate subCondition = cb.equal(c.get("login"), login);
    cq.where(condition, subCondition);
    TypedQuery<Users> q = em.createQuery(cq);
    Users u = null;
    try {
        u = q.getSingleResult();
    } catch (NoResultException e) {
        // u=null}
    }

    return u;

}

From source file:org.openmeetings.app.data.user.Usermanagement.java

/**
 * /*from w w w  .  j ava2s  .c  o m*/
 * Find User by Id
 */
// -----------------------------------------------------------------------------------------------------
public Users getUserById(Long id) {
    log.debug("Usermanagement.getUserById");

    if (id == null || id <= 0) {
        return null;
    }
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Users> cq = cb.createQuery(Users.class);
    Root<Users> c = cq.from(Users.class);
    Predicate condition = cb.equal(c.get("deleted"), "false");
    Predicate subCondition = cb.equal(c.get("user_id"), id);
    cq.where(condition, subCondition);
    TypedQuery<Users> q = em.createQuery(cq);
    Users u = null;
    try {
        u = q.getSingleResult();
    } catch (NoResultException e) {
        // u=null}
    } catch (NonUniqueResultException ex) {
    }

    return u;
}