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:de.egore911.hexanalyzer.persistence.selector.PluginSelector.java

@Nonnull
@Override//from   w  w w  .  j  a  v  a  2  s.  c  om
protected List<Predicate> generatePredicateList(@Nonnull CriteriaBuilder builder,
        @Nonnull Root<PluginEntity> from, @Nonnull CriteriaQuery<?> criteriaQuery) {
    List<Predicate> predicates = super.generatePredicateList(builder, from, criteriaQuery);

    if (extensionPoint != null) {
        builder.equal(from.get(PluginEntity_.extensionPoint), extensionPoint);
    }

    if (StringUtils.isNotEmpty(search)) {
        String likePattern = '%' + search + '%';
        predicates.add(builder.or(builder.like(from.get(PluginEntity_.qualifiedName), likePattern),
                builder.like(from.get(PluginEntity_.source), likePattern)));
    }

    return predicates;
}

From source file:net.shopxx.dao.impl.StatisticDaoImpl.java

public List<Statistic> analyze(Statistic.Period period, Date beginDate, Date endDate) {
    Assert.notNull(period);/*from  w w  w .  jav  a  2s.co m*/

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Statistic> criteriaQuery = criteriaBuilder.createQuery(Statistic.class);
    Root<Statistic> root = criteriaQuery.from(Statistic.class);
    switch (period) {
    case year:
        criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"),
                criteriaBuilder.sum(root.<Long>get("registerMemberCount")),
                criteriaBuilder.sum(root.<Long>get("createOrderCount")),
                criteriaBuilder.sum(root.<Long>get("completeOrderCount")),
                criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")),
                criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount"))));
        criteriaQuery.groupBy(root.get("year"));
        break;
    case month:
        criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"),
                criteriaBuilder.sum(root.<Long>get("registerMemberCount")),
                criteriaBuilder.sum(root.<Long>get("createOrderCount")),
                criteriaBuilder.sum(root.<Long>get("completeOrderCount")),
                criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")),
                criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount"))));
        criteriaQuery.groupBy(root.get("year"), root.get("month"));
        break;
    case day:
        criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"),
                root.get("day"), root.<Long>get("registerMemberCount"), root.<Long>get("createOrderCount"),
                root.<Long>get("completeOrderCount"), root.<BigDecimal>get("createOrderAmount"),
                root.<BigDecimal>get("completeOrderAmount")));
        break;
    }
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        Calendar calendar = DateUtils.toCalendar(beginDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.greaterThan(root.<Integer>get("year"), year),
                        criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year),
                                criteriaBuilder.greaterThan(root.<Integer>get("month"), month)),
                        criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year),
                                criteriaBuilder.equal(root.<Integer>get("month"), month),
                                criteriaBuilder.greaterThanOrEqualTo(root.<Integer>get("day"), day))));
    }
    if (endDate != null) {
        Calendar calendar = DateUtils.toCalendar(endDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.lessThan(root.<Integer>get("year"), year),
                        criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year),
                                criteriaBuilder.lessThan(root.<Integer>get("month"), month)),
                        criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year),
                                criteriaBuilder.equal(root.<Integer>get("month"), month),
                                criteriaBuilder.lessThanOrEqualTo(root.<Integer>get("day"), day))));
    }
    criteriaQuery.where(restrictions);
    return entityManager.createQuery(criteriaQuery).getResultList();
}

From source file:de.egore911.persistence.selector.AbstractSelector.java

@Nonnull
protected List<Order> generateOrderList(@Nonnull CriteriaBuilder builder, @Nonnull Root<T> from) {
    if (StringUtils.isNotEmpty(sortColumn)) {
        if (!Boolean.FALSE.equals(ascending)) {
            return Collections.singletonList(builder.asc(from.get(sortColumn)));
        }/* w  w w .  j  a v a  2  s. c  om*/
        return Collections.singletonList(builder.desc(from.get(sortColumn)));
    }
    return getDefaultOrderList(builder, from);
}

From source file:se.kth.csc.persist.JPAStore.java

@Override
public Queue fetchQueueWithName(String name) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Queue> q = cb.createQuery(Queue.class);
    Root<Queue> queue = q.from(Queue.class);
    try {/*from   w  ww .  j  a  va  2 s. c  om*/
        return entityManager.createQuery(q.select(queue).where(cb.equal(queue.get(Queue_.name), name)))
                .getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:de.hopmann.repositories.cran.service.CRANPackageListingService.java

public void removeCurrentListing() {
    // TODO Bulk removal, JPQL not cascading
    // entityManager.createQuery("DELETE FROM DependencyEntity").executeUpdate();
    // or/*ww w.  jav  a2 s .c  om*/
    // entityManager.createQuery("DELETE FROM PackageEntity").executeUpdate();

    // Alternative

    // CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    //
    // CriteriaDelete<PackageEntity> delete = cb
    // .createCriteriaDelete(PackageEntity.class);
    // Root<PackageEntity> p = delete.from(PackageEntity.class);
    // delete.where(cb.isFalse(p.get(PackageEntity_.archived)));
    //
    // entityManager.createQuery(delete).executeUpdate();

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<CRANPackageEntity> query = cb.createQuery(CRANPackageEntity.class);
    Root<CRANPackageEntity> p = query.from(CRANPackageEntity.class);
    query.where(cb.isFalse(p.get(CRANPackageEntity_.archived)));

    List<CRANPackageEntity> resultList = entityManager.createQuery(query).getResultList();

    for (CRANPackageEntity cRANPackageEntity : resultList) {
        entityManager.remove(cRANPackageEntity);
    }
}

From source file:com.aimdek.ccm.dao.impl.BulkUploadRepositoryImpl.java

/**
 * Adds the sorting.//  ww  w .  jav a  2  s  . c  o m
 *
 * @param sortField
 *            the sort field
 * @param sortOrder
 *            the sort order
 * @param query
 *            the query
 */
private void addSorting(String sortField, String sortOrder, CriteriaQuery query, CriteriaBuilder builder,
        Root<BulkUpload> root) {
    if (CommonUtil.isNotNull(sortField)) {
        if (sortOrder.startsWith(SORT_ORDER_ASCENDING)) {
            query.orderBy(builder.asc(root.get(sortField)));
        } else {
            query.orderBy(builder.desc(root.get(sortField)));
        }
    }
}

From source file:com.netflix.genie.server.metrics.impl.JobCountManagerImpl.java

/**
 * {@inheritDoc}/*from  w  w  w .j  a v  a 2  s . com*/
 */
@Override
@Transactional(readOnly = true)
//TODO: Move to specification
public int getNumInstanceJobs(final String hostName, final Long minStartTime, final Long maxStartTime)
        throws GenieException {
    LOG.debug("called");

    final String finalHostName;
    // initialize host name
    if (StringUtils.isBlank(hostName)) {
        finalHostName = NetUtil.getHostName();
    } else {
        finalHostName = hostName;
    }
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    final Root<Job> j = cq.from(Job.class);
    cq.select(cb.count(j));
    final Predicate runningStatus = cb.equal(j.get(Job_.status), JobStatus.RUNNING);
    final Predicate initStatus = cb.equal(j.get(Job_.status), JobStatus.INIT);
    final List<Predicate> predicates = new ArrayList<>();
    predicates.add(cb.equal(j.get(Job_.hostName), finalHostName));
    predicates.add(cb.or(runningStatus, initStatus));
    if (minStartTime != null) {
        predicates.add(cb.greaterThanOrEqualTo(j.get(Job_.started), new Date(minStartTime)));
    }
    if (maxStartTime != null) {
        predicates.add(cb.lessThan(j.get(Job_.started), new Date(maxStartTime)));
    }
    //documentation says that by default predicate array is conjuncted together
    cq.where(predicates.toArray(new Predicate[predicates.size()]));
    final TypedQuery<Long> query = this.em.createQuery(cq);
    //Downgrading to an int since all the code seems to want ints
    //Don't feel like changing everthing right now can figure out later
    //if need be
    return query.getSingleResult().intValue();
}

From source file:com.sixsq.slipstream.persistence.Run.java

private static Predicate viewListCommonQueryOptions(CriteriaBuilder builder, Root<Run> rootQuery, User user,
        String moduleResourceUri, String cloudServiceName) {
    Predicate where = null;/*from   w  w  w.  jav  a 2s. c o  m*/
    if (!user.isSuper()) {
        where = andPredicate(builder, where, builder.equal(rootQuery.get("user_"), user.getName()));
    }
    if (moduleResourceUri != null && !"".equals(moduleResourceUri)) {
        where = andPredicate(builder, where,
                builder.equal(rootQuery.get("moduleResourceUri"), moduleResourceUri));
    }
    if (cloudServiceName != null && !"".equals(cloudServiceName)) {
        // TODO: Replace the 'like' by an 'equals'
        where = andPredicate(builder, where,
                builder.like(rootQuery.<String>get("cloudServiceNames"), "%" + cloudServiceName + "%"));
    }
    return where;
}

From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java

private Predicate isOneOfOr(String field, Collection collection, Root<Order> root, Predicate predicate,
        CriteriaBuilder builder) {//  w w  w . ja  va 2 s .  co  m
    return !isEmpty(collection) ? builder.or(predicate, root.get(field).in(collection)) : predicate;
}

From source file:com.expressui.core.dao.security.query.RelatedUsersQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<User> query, Root<User> user) {
    List<Predicate> predicates = new ArrayList<Predicate>();

    if (hasValue(role)) {
        ParameterExpression<Role> roleExp = builder.parameter(Role.class, "role");
        predicates.add(builder.equal(user.join("userRoles").get("role"), roleExp));
    }/* w ww . j  av  a  2 s . c o m*/

    if (hasValue(doesNotBelongToRole)) {
        Subquery<User> subquery = query.subquery(User.class);
        Root userRole = subquery.from(UserRole.class);
        ParameterExpression<Role> roleExp = builder.parameter(Role.class, "doesNotBelongToRole");
        subquery.select(userRole.get("user")).where(builder.equal(userRole.get("role"), roleExp));
        predicates.add(builder.not(user.in(subquery)));
    }

    return predicates;
}