Example usage for javax.persistence.criteria CriteriaBuilder createQuery

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

Introduction

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

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

From source file:org.wallride.repository.PostRepositoryImpl.java

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<Post> root = query.from(Post.class);
    query.select(root.get(Post_.id));//w  ww . ja va2 s.c  o  m
    query.where(cb.equal(root.get(Post_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

From source file:com.devicehive.dao.rdbms.DeviceClassDaoRdbmsImpl.java

@Override
public List<DeviceClassWithEquipmentVO> list(String name, String namePattern, String sortField,
        Boolean sortOrderAsc, Integer take, Integer skip) {
    final CriteriaBuilder cb = criteriaBuilder();
    final CriteriaQuery<DeviceClass> criteria = cb.createQuery(DeviceClass.class);
    final Root<DeviceClass> from = criteria.from(DeviceClass.class);

    final Predicate[] predicates = CriteriaHelper.deviceClassListPredicates(cb, from, ofNullable(name),
            ofNullable(namePattern));// ww  w . j  a va2 s  . co  m
    criteria.where(predicates);
    CriteriaHelper.order(cb, criteria, from, ofNullable(sortField), Boolean.TRUE.equals(sortOrderAsc));

    final TypedQuery<DeviceClass> query = createQuery(criteria);
    ofNullable(take).ifPresent(query::setMaxResults);
    ofNullable(skip).ifPresent(query::setFirstResult);

    CacheHelper.cacheable(query);
    List<DeviceClass> resultList = query.getResultList();
    Stream<DeviceClassWithEquipmentVO> objectStream = resultList.stream().map(DeviceClass::convertToVo);
    return objectStream.collect(Collectors.toList());
}

From source file:com.devicehive.dao.rdbms.NetworkDaoRdbmsImpl.java

@Override
public List<NetworkVO> list(String name, String namePattern, String sortField, boolean sortOrderAsc,
        Integer take, Integer skip, Optional<HivePrincipal> principal) {
    CriteriaBuilder cb = criteriaBuilder();
    CriteriaQuery<Network> criteria = cb.createQuery(Network.class);
    Root<Network> from = criteria.from(Network.class);

    Predicate[] nameAndPrincipalPredicates = CriteriaHelper.networkListPredicates(cb, from, ofNullable(name),
            ofNullable(namePattern), principal);
    criteria.where(nameAndPrincipalPredicates);

    CriteriaHelper.order(cb, criteria, from, ofNullable(sortField), sortOrderAsc);

    TypedQuery<Network> query = createQuery(criteria);
    cacheQuery(query, of(CacheConfig.refresh()));
    ofNullable(take).ifPresent(query::setMaxResults);
    ofNullable(skip).ifPresent(query::setFirstResult);
    List<Network> result = query.getResultList();
    Stream<NetworkVO> objectStream = result.stream().map(Network::convertNetwork);
    return objectStream.collect(Collectors.toList());
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.MultimediaDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.findAllMultimedia = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<MultimediaImpl>>() {
                @Override/*from w w w  .ja  v a 2  s . c  o m*/
                public CriteriaQuery<MultimediaImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<MultimediaImpl> criteriaQuery = cb.createQuery(MultimediaImpl.class);
                    final Root<MultimediaImpl> definitionRoot = criteriaQuery.from(MultimediaImpl.class);
                    criteriaQuery.select(definitionRoot);

                    return criteriaQuery;
                }
            });
}

From source file:org.openlmis.migration.tool.openlmis.referencedata.repository.custom.impl.OlmisFacilityTypeApprovedProductRepositoryImpl.java

@Override
public Collection<FacilityTypeApprovedProduct> searchProducts(UUID facilityId, UUID programId,
        boolean fullSupply) {
    checkNotNull(facilityId);/*from   www  .ja  v  a2  s  .  co m*/

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();

    CriteriaQuery<FacilityTypeApprovedProduct> query = builder.createQuery(FacilityTypeApprovedProduct.class);

    Root<FacilityTypeApprovedProduct> ftap = query.from(FacilityTypeApprovedProduct.class);
    Root<Facility> facility = query.from(Facility.class);

    Join<Facility, FacilityType> fft = facility.join("type");

    Join<FacilityTypeApprovedProduct, FacilityType> ft = ftap.join("facilityType");
    Join<FacilityTypeApprovedProduct, ProgramOrderable> pp = ftap.join("programOrderable");

    Join<ProgramOrderable, Program> program = pp.join("program");

    Predicate conjunction = builder.conjunction();
    if (programId != null) {
        conjunction = builder.and(conjunction, builder.equal(program.get("id"), programId));
    }
    conjunction = builder.and(conjunction, builder.equal(fft.get("id"), ft.get("id")));
    conjunction = builder.and(conjunction, builder.equal(facility.get("id"), facilityId));
    conjunction = builder.and(conjunction, builder.equal(pp.get("fullSupply"), fullSupply));
    conjunction = builder.and(conjunction, builder.isTrue(pp.get("active")));

    query.select(ftap);
    query.where(conjunction);

    Join<ProgramOrderable, OrderableDisplayCategory> category = pp.join("orderableDisplayCategory");
    Join<ProgramOrderable, Orderable> orderable = pp.join("product");

    query.orderBy(builder.asc(category.get("orderedDisplayValue").get("displayOrder")),
            builder.asc(category.get("orderedDisplayValue").get("displayName")),
            builder.asc(orderable.get("productCode")));

    return entityManager.createQuery(query).getResultList();
}

From source file:com.greendot.db.jpa.core.AbstractSearchDao.java

@Transactional(readOnly = true)
public Optional<E> findByCharacterType(final String key, final String value) {

    notNull(key, "Mandatory argument 'key' is missing.");
    notNull(value, "Mandatory argument 'value' is missing.");
    final CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    final CriteriaQuery<E> query = builder.createQuery(getEntityType());
    final Root<E> root = query.from(getEntityType());
    query.select(root).where(builder.and(builder.equal(root.get(key), value))).distinct(true);
    final List<E> results = getEntityManager().createQuery(query).getResultList();
    if ((results != null) && (results.size() == 1))
        return Optional.of(results.get(0));
    return Optional.empty();
}

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

public Long count(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class);
    Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class);
    criteriaQuery.select(root);/*from w w w .j a v  a 2s  .co m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (isMarketable != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("product").get("goods").get("isMarketable"), isMarketable));
    }
    if (isOutOfStock != null) {
        Path<Integer> stock = root.get("product").get("stock");
        Path<Integer> allocatedStock = root.get("product").get("allocatedStock");
        if (isOutOfStock) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock),
                    criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock),
                    criteriaBuilder.greaterThan(stock, allocatedStock)));
        }
    }
    if (hasSent != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

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

public Page<ProductNotify> findPage(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent,
        Pageable pageable) {//from  w w w.  j a  v a2  s.c o  m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class);
    Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (isMarketable != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("product").get("goods").get("isMarketable"), isMarketable));
    }
    if (isOutOfStock != null) {
        Path<Integer> stock = root.get("product").get("stock");
        Path<Integer> allocatedStock = root.get("product").get("allocatedStock");
        if (isOutOfStock) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock),
                    criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock),
                    criteriaBuilder.greaterThan(stock, allocatedStock)));
        }
    }
    if (hasSent != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

From source file:eu.uqasar.service.ProductService.java

public List<Product> getAllProducts() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> query = cb.createQuery(Product.class);
    query.from(Product.class);
    return em.createQuery(query).getResultList();
}

From source file:com.moderndrummer.data.MemberDaoImpl.java

@Override
public List<Member> findAllOrderedByName() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);

    criteria.select(member).orderBy(cb.asc(member.get("name")));
    return em.createQuery(criteria).getResultList();
}