Example usage for javax.persistence.criteria Root get

List of usage examples for javax.persistence.criteria Root get

Introduction

In this page you can find the example usage for javax.persistence.criteria Root get.

Prototype

<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create a path corresponding to the referenced single-valued attribute.

Usage

From source file:com.github.jinahya.persistence.ShadowTest.java

protected static List<Morton> MORTONS(final EntityManager manager, final int firstResult,
        final int maxResults) {

    final CriteriaBuilder builder = manager.getCriteriaBuilder();
    final CriteriaQuery<Morton> query = builder.createQuery(Morton.class);
    final Root<Morton> morton = query.from(Morton.class);

    query.select(morton).orderBy(builder.desc(morton.get(Morton_.id)));

    return manager.createQuery(query).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
}

From source file:com.enioka.jqm.tools.ResourceParser.java

private static JndiResourceDescriptor fromDatabase(String alias) throws NamingException {
    JndiObjectResource resource = null;/*from   w  ww  . java 2  s .  c o  m*/
    EntityManager em = null;
    try {
        // Using the horrible CriteriaBuilder API instead of a string query. This avoids classloading issues - Hibernate binds
        // the entities at run time with the thread current classloader...
        em = Helpers.getNewEm();

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<JndiObjectResource> q = cb.createQuery(JndiObjectResource.class);
        Root<JndiObjectResource> c = q.from(JndiObjectResource.class);
        ParameterExpression<String> p = cb.parameter(String.class);
        q.select(c).where(cb.equal(c.get("name"), p));

        TypedQuery<JndiObjectResource> query = em.createQuery(q);
        query.setParameter(p, alias);
        resource = query.getSingleResult();
    } catch (Exception e) {
        NamingException ex = new NamingException("Could not find a JNDI object resource of name " + alias);
        ex.setRootCause(e);
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }

    // Create the ResourceDescriptor from the JPA object
    JndiResourceDescriptor d = new JndiResourceDescriptor(resource.getType(), resource.getDescription(), null,
            resource.getAuth(), resource.getFactory(), resource.getSingleton());
    for (JndiObjectResourceParameter prm : resource.getParameters()) {
        d.add(new StringRefAddr(prm.getKey(), prm.getValue()));
    }

    return d;
}

From source file:org.ngrinder.perftest.repository.PerfTestSpecification.java

/**
 * Get the search {@link Specification} for test name and description fields.
 *
 * @param queryString query String/*from w w w.  j a  v a 2  s.c  om*/
 * @return {@link Specification}
 */
public static Specification<PerfTest> likeTestNameOrDescription(final String queryString) {
    return new Specification<PerfTest>() {
        @Override
        public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            String queryStr = ("%" + queryString + "%").toLowerCase();
            return cb.or(cb.like(cb.lower(root.get("testName").as(String.class)), queryStr),
                    cb.like(root.get("description").as(String.class), queryStr));
        }
    };
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Experience> root,
        ExperienceQueryCommand command) {
    List<Predicate> ps = Lists.newArrayListWithCapacity(2);
    if (command.getCustomers() != null) {
        ps.add(root.get(Experience_.customer).in(command.getCustomers()));
    }/*w w w.j  av  a  2s  .c  om*/
    if (!isBlank(command.getName())) {
        ps.add(cb.like(root.get(Experience_.name), command.getNameQuery()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<ExperienceTag> root,
        ExperienceTagQueryCommand command) {
    List<Predicate> ps = Lists.newArrayListWithCapacity(2);
    if (command.getCustomers() != null) {
        ps.add(root.get(ExperienceTag_.customer).in(command.getCustomers()));
    }/* w w w .j ava2 s.  c o m*/
    if (!isBlank(command.getName())) {
        ps.add(cb.like(root.get(ExperienceTag_.name), command.getNameQuery()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<PayOrder> root,
        PayOrderQueryCommand command) {// w  w w.ja va  2  s. co m
    List<Predicate> ps = Lists.newArrayListWithCapacity(3);
    if (command.getCustomer() != null) {
        ps.add(cb.equal(root.get(PayOrder_.customer), command.getCustomer()));
    }
    if (command.getPayDateStart() != null) {
        ps.add(cb.greaterThanOrEqualTo(root.get(PayOrder_.payDate), command.getPayDateStart()));
    }
    if (command.getPayDateEnd() != null) {
        ps.add(cb.lessThanOrEqualTo(root.get(PayOrder_.payDate), command.getPayDateStart()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

From source file:net.sf.gazpachoquest.qbe.RangeSpecification.java

public static <E, D extends Comparable<? super D>> Specification<E> toSpecification(final Range<E, D> range) {
    Validate.isTrue(range.isSet(), "You must pass an exploitable range");
    return new Specification<E>() {
        @Override/*from w w  w. j  a  va 2s .  c o m*/
        public Predicate toPredicate(final Root<E> root, final CriteriaQuery<?> query,
                final CriteriaBuilder builder) {
            Predicate rangePredicate = null;

            if (range.isBetween()) {
                rangePredicate = builder.between(root.get(range.getField()), range.getFrom(), range.getTo());
            } else if (range.isFromSet()) {
                // rangePredicate =
                // builder.greaterThanOrEqualTo(root.get(range.getField()),
                // range.getFrom());
                rangePredicate = builder.greaterThan(root.get(range.getField()), range.getFrom());
            } else if (range.isToSet()) {
                // rangePredicate =
                // builder.lessThanOrEqualTo(root.get(range.getField()),
                // range.getTo());
                rangePredicate = builder.lessThan(root.get(range.getField()), range.getTo());
            }

            if (rangePredicate != null) {
                if (!range.isIncludeNullSet() || Boolean.FALSE.equals(range.getIncludeNull())) {
                    return rangePredicate;
                } else {
                    return builder.or(rangePredicate, builder.isNull(root.get(range.getField())));
                }
            }

            // no range at all
            // take the opportunity to keep only null...
            if (Boolean.TRUE.equals(range.getIncludeNull())) {
                return builder.isNull(root.get(range.getField()));
            }

            // ... or non-null only...
            if (Boolean.FALSE.equals(range.getIncludeNull())) {
                return builder.isNotNull(root.get(range.getField()));
            }

            throw new IllegalStateException("You must pass an exploitable range (should not happen here)");
        }
    };
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<TaskSmsTemplate> root,
        TaskSmsTemplateQueryCommand command) {
    List<Predicate> ps = Lists.newArrayListWithCapacity(2);
    if (command.getCustomers() != null) {
        ps.add(root.get(TaskSmsTemplate_.customer).in(command.getCustomers()));
    }//from  w  ww .  j  a  va 2  s. co m
    if (!isBlank(command.getName())) {
        ps.add(cb.like(root.get(TaskSmsTemplate_.name), command.getNameQuery()));
    }
    cq.where(ps.toArray(new Predicate[ps.size()]));
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationTargetSpecification.java

private static List<Predicate> inTitleOrAddressOrName(Root<AnnotationTarget> root, CriteriaBuilder cb,
        Set<String> terms) {
    return terms.stream().map(t -> containsLike(t))
            .map(t -> cb.or(titleContains(root, cb, t),
                    (root.get(ADDRESS) != null) ? addressContains(root, cb, t) : fileNameContains(root, cb, t)))
            .collect(Collectors.toList());
}

From source file:com.netflix.genie.server.repository.jpa.ApplicationSpecs.java

/**
 * Get a specification using the specified parameters.
 *
 * @param name     The name of the application
 * @param userName The name of the user who created the application
 * @param statuses The status of the application
 * @param tags     The set of tags to search the command for
 * @return A specification object used for querying
 *//* w  w w  .j  av  a 2  s .co  m*/
public static Specification<Application> find(final String name, final String userName,
        final Set<ApplicationStatus> statuses, final Set<String> tags) {
    return new Specification<Application>() {
        @Override
        public Predicate toPredicate(final Root<Application> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(name)) {
                predicates.add(cb.equal(root.get(Application_.name), name));
            }
            if (StringUtils.isNotBlank(userName)) {
                predicates.add(cb.equal(root.get(Application_.user), userName));
            }
            if (statuses != null && !statuses.isEmpty()) {
                //Could optimize this as we know size could use native array
                final List<Predicate> orPredicates = new ArrayList<>();
                for (final ApplicationStatus status : statuses) {
                    orPredicates.add(cb.equal(root.get(Application_.status), status));
                }
                predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
            }
            if (tags != null) {
                for (final String tag : tags) {
                    if (StringUtils.isNotBlank(tag)) {
                        predicates.add(cb.isMember(tag, root.get(Application_.tags)));
                    }
                }
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}