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:com.eryansky.common.orm.core.spring.data.jpa.restriction.support.EqRestriction.java

public Predicate build(Path<?> expression, Object value, CriteriaBuilder builder) {

    return value == null ? builder.isNull(expression) : builder.equal(expression, value);
}

From source file:cn.imethan.common.repository.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override/*from   w w  w .  ja  v a  2 s . co m*/
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (filters != null && !filters.isEmpty()) {

                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName, ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    try {
                        for (int i = 1; i < names.length; i++) {
                            expression = expression.get(names[i]);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case IN:
                        In in = builder.in(expression);
                        String[] valueStrings = StringUtils.split(filter.value.toString(), ",");
                        List<Long> list = new ArrayList<Long>();
                        for (String value : valueStrings) {
                            list.add(Long.valueOf(value.trim()));
                        }
                        in.value(list);

                        predicates.add(in);

                        break;
                    case NOTIN:
                        In in1 = builder.in(expression);
                        String[] valueStrings1 = StringUtils.split(filter.value.toString(), ",");
                        List<Long> list1 = new ArrayList<Long>();
                        for (String value : valueStrings1) {
                            list1.add(Long.valueOf(value.trim()));
                        }
                        in1.value(list1);

                        predicates.add(builder.not(in1));
                        break;
                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            //            //hibernate
            //            //org.hibernate.ejb.criteria.CriteriaQueryImpl
            //            if(query instanceof org.hibernate.ejb.QueryImpl) {  
            //               @SuppressWarnings("rawtypes")
            //               org.hibernate.ejb.QueryImpl hibernateQuery = (org.hibernate.ejb.QueryImpl)query;  
            //               org.hibernate.Query hQuery = hibernateQuery.getHibernateQuery();
            //               hQuery.setCacheable(true);
            //            }

            return builder.conjunction();
        }
    };
}

From source file:aode.lx.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override/*from   ww  w  . j  a  v  a2s .  co  m*/
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (Collections3.isNotEmpty(filters)) {

                List<Predicate> predicates = Lists.newArrayList();
                List<Predicate> ORpredicates = Lists.newArrayList();

                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName, ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }

                    Object value = filter.value;

                    if (expression.getJavaType().isEnum()) {
                        value = EnumUtils.valueOf(expression.getJavaType(), filter.value.toString());
                        predicates.add(builder.equal(expression, value));
                    } else {
                        // logic operator
                        switch (filter.operator) {
                        case EQ:
                            predicates.add(builder.equal(expression, filter.value));
                            break;
                        case LIKE:
                            predicates.add(builder.like(expression, "%" + filter.value + "%"));
                            break;
                        case GT:
                            predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                            break;
                        case LT:
                            predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                            break;
                        case GTE:
                            predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                            break;
                        case LTE:
                            predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                            break;
                        case NEQ:
                            predicates.add(builder.notEqual(expression, filter.value));
                            break;
                        case NOTNULL:
                            predicates.add(builder.isNotNull(expression));
                            break;
                        case ISNULL:
                            predicates.add(builder.isNull(expression));
                            break;
                        case OREQ:
                            ORpredicates.add(builder.equal(expression, filter.value));
                            break;
                        case ORLIKE:
                            ORpredicates.add(builder.like(expression, "%" + filter.value + "%"));
                            break;
                        case ORGT:
                            ORpredicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                            break;
                        case ORLT:
                            ORpredicates.add(builder.lessThan(expression, (Comparable) filter.value));
                            break;
                        case ORGTE:
                            ORpredicates
                                    .add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                            break;
                        case ORLTE:
                            ORpredicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                            break;
                        case ORNEQ:
                            ORpredicates.add(builder.notEqual(expression, filter.value));
                            break;
                        case ORNOTNULL:
                            ORpredicates.add(builder.isNotNull(expression));
                            break;
                        case ORISNULL:
                            ORpredicates.add(builder.isNull(expression));
                            break;
                        }
                    }

                }

                // ? and ???
                if (!predicates.isEmpty() && !ORpredicates.isEmpty()) {
                    return builder.and(builder.and(predicates.toArray(new Predicate[predicates.size()])),
                            builder.or(ORpredicates.toArray(new Predicate[ORpredicates.size()])));
                }
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
                if (!ORpredicates.isEmpty()) {
                    return builder.or(ORpredicates.toArray(new Predicate[ORpredicates.size()]));
                }
            }
            return builder.conjunction();
        }
    };
}

From source file:net.kaczmarzyk.spring.data.jpa.domain.EmptyResultOnTypeMismatch.java

@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    try {//from   www.  j  ava 2  s  .  c o  m
        return wrappedSpec.toPredicate(root, query, cb);
    } catch (ValueRejectedException e) {
        return cb.equal(cb.literal(0), cb.literal(1));
    }
}

From source file:org.zlogic.vogon.web.data.TransactionFilterSpecification.java

/**
 * Builds the Predicate// w ww.j av a2  s  .  com
 *
 * @param root the FinanceTransaction query root
 * @param cq the CriteriaQuery instance
 * @param cb the CriteriaBuilder instance
 * @return the Predicate of this filter
 */
@Override
public Predicate toPredicate(Root<FinanceTransaction> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
    Predicate ownerPredicate = cb.equal(root.get(FinanceTransaction_.owner), owner);
    Predicate descriptionPredicate = filterDescription != null
            ? cb.like(cb.lower(root.get(FinanceTransaction_.description)), filterDescription.toLowerCase())
            : cb.conjunction();
    Predicate tagsPredicate = cb.conjunction();
    if (filterTags != null && !filterTags.isEmpty()) {
        Set<String> filterTagsLowercase = new HashSet<>();
        for (String tag : filterTags)
            filterTagsLowercase.add(tag.toLowerCase());
        tagsPredicate = cb.lower(root.join(FinanceTransaction_.tags)).in(cb.literal(filterTagsLowercase));
    }
    Predicate datePredicate = filterDate != null
            ? cb.equal(root.get(FinanceTransaction_.transactionDate), new java.sql.Date(filterDate.getTime()))
            : cb.conjunction();
    return cb.and(ownerPredicate, descriptionPredicate, datePredicate, tagsPredicate);
}

From source file:net.przemkovv.sphinx.dao.impl.DefaultUserDAO.java

@Override
public User getUser(String email) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> user = cq.from(User.class);
    cq.where(cb.equal(user.get(User_.email), email));
    return em.createQuery(cq).getSingleResult();
}

From source file:net.przemkovv.sphinx.dao.impl.DefaultUserDAO.java

@Override
public boolean existsByEmail(String email) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<User> user = cq.from(User.class);
    cq.where(cb.equal(user.get(User_.email), email));
    cq.select(cb.count(user));//from www. j  ava 2s .c  o  m
    return em.createQuery(cq).getSingleResult().intValue() == 1;
}

From source file:com.github.lothar.security.acl.jpa.multithread.CurrentUserLastNameSpec.java

@Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    String currentUserLastName = Session.currentUserLastName();
    if (currentUserLastName == null) {
        return cb.conjunction();
    } else {//from  ww w.jav a2s . co m
        return cb.equal(root.get("lastName"), currentUserLastName);
    }
}

From source file:core.commonapp.server.dao.geo.GeoDaoHibernateImpl.java

@Override
public List<Geo> findByGeoType(GeoType type) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery(Geo.class);

    Root<Geo> root = query.from(Geo.class);
    builder.equal(root.get("geoType"), type);
    return getEntityManager().createQuery(query).getResultList();
}

From source file:net.przemkovv.sphinx.dao.impl.DefaultSolutionDAO.java

@Override
public List<Solution> getSolutionsByTask(Task task) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Solution> cq = cb.createQuery(Solution.class);
    Root<Solution> solution = cq.from(Solution.class);
    cq.where(cb.equal(solution.get(Solution_.task), task));
    return em.createQuery(cq).getResultList();
}