Example usage for javax.persistence.criteria CriteriaQuery where

List of usage examples for javax.persistence.criteria CriteriaQuery where

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery where.

Prototype

CriteriaQuery<T> where(Predicate... restrictions);

Source Link

Document

Modify the query to restrict the query result according to the conjunction of the specified restriction predicates.

Usage

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// w w  w .  j av a  2 s .c  o m
    // 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.june.app.board.repository.jpa.BoardRepositoryImpl.java

@Override
public Collection<Board> boardListWithPaging(Board vo) {
    int bbsId = vo.getBbsId();
    int pageSize = vo.getPageSize();
    int pageNumber = (int) vo.getPageIndex();

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Board> criteriaQuery = criteriaBuilder.createQuery(Board.class);

    Root<Board> from = criteriaQuery.from(Board.class);
    CriteriaQuery<Board> select = criteriaQuery.select(from);
    if (bbsId > 0) {
        criteriaQuery.where(criteriaBuilder.equal(from.get("bbsId"), bbsId));
    }//  w  w w .  j ava2 s. c  om
    /**list desc for date*/
    criteriaQuery.orderBy(criteriaBuilder.desc(from.get("frstRegistPnttm")));
    TypedQuery<Board> typedQuery = em.createQuery(select);

    typedQuery.setFirstResult((pageNumber - 1) * pageSize);
    typedQuery.setMaxResults(pageSize);

    Collection<Board> fooList = typedQuery.getResultList();

    return fooList;

}

From source file:org.fao.geonet.repository.HarvesterSettingRepositoryOverridesImpl.java

@Override
public List<HarvesterSetting> findRoots() {
    CriteriaBuilder criteriaBuilder = _entityManager.getCriteriaBuilder();
    CriteriaQuery<HarvesterSetting> query = criteriaBuilder.createQuery(HarvesterSetting.class);

    Root<HarvesterSetting> root = query.from(HarvesterSetting.class);
    query.where(criteriaBuilder.isNull(root.get(HarvesterSetting_.parent)));

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

From source file:org.fao.geonet.repository.HarvesterSettingRepositoryOverridesImpl.java

@Override
public List<HarvesterSetting> findAllChildren(int parentid) {
    CriteriaBuilder criteriaBuilder = _entityManager.getCriteriaBuilder();
    CriteriaQuery<HarvesterSetting> query = criteriaBuilder.createQuery(HarvesterSetting.class);

    Root<HarvesterSetting> root = query.from(HarvesterSetting.class);
    query.where(criteriaBuilder.equal(root.get(HarvesterSetting_.parent), parentid));

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

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

public List<Product> getAllByDescendingNameFiltered(ProductFilterStructure filter, int first, int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> root = criteria.from(Product.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//from   w ww . j  a v  a2s.  c o  m

    criteria.orderBy(cb.desc(root.get(Product_.releaseDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

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

public List<Product> getAllByAscendingNameFiltered(
        eu.uqasar.web.pages.products.panels.ProductFilterStructure filter, int first, int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//from w ww .j a va2s  .  c  o  m

    criteria.orderBy(cb.asc(from.get(Product_.releaseDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java

public TypedQuery<Connection> buildFilter() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class);
    Root<Connection> connection = criteria.from(Connection.class);
    Join<Connection, Profile> receiver = connection.join(Connection_.receiver);
    ///* w ww  .jav a 2 s .c  o  m*/
    CriteriaQuery<Connection> select = criteria.select(connection);
    select.where(buildPredicateFilter(cb, receiver, connection));
    select.orderBy(cb.asc(receiver.get(Profile_.fullName)));
    //
    TypedQuery<Connection> typedQuery = em.createQuery(select);
    if (this.limit > 0) {
        typedQuery.setFirstResult((int) offset);
        typedQuery.setMaxResults((int) limit);
    }
    //
    return typedQuery;
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Page<Order> findPage(Member member, Pageable pageable) {
    if (member == null) {
        return new Page<Order>(Collections.<Order>emptyList(), 0, pageable);
    }//w w w .java2 s.  co  m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    return super.findPage(criteriaQuery, pageable);
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public List<Order> findList(Member member, Integer count, List<Filter> filters,
        List<net.groupbuy.Order> orders) {
    if (member == null) {
        return Collections.<Order>emptyList();
    }//from w  ww . j  a va2  s  .  c o  m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    return super.findList(criteriaQuery, null, count, filters, orders);
}

From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java

/**
 * Builds the Typed Query/* w ww .j a v a  2  s  . com*/
 * @return
 */
public TypedQuery<Connection> buildSingleRelationship() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class);
    Root<Connection> connection = criteria.from(Connection.class);

    Predicate predicate = null;
    if (this.sender != null && this.receiver != null) {
        predicate = cb.equal(connection.get(Connection_.senderId), sender.getId());
        predicate = cb.and(predicate, cb.equal(connection.get(Connection_.receiverId), receiver.getId()));
    }

    CriteriaQuery<Connection> select = criteria.select(connection).distinct(true);
    select.where(predicate);
    TypedQuery<Connection> typedQuery = em.createQuery(select);

    return typedQuery;
}