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:org.broadleafcommerce.inventory.admin.server.service.handler.InventorySkuCustomPersistenceHandler.java

@Override
public void applyAdditionalFetchCriteria(List<FilterMapping> filterMappings, CriteriaTransferObject cto,
        PersistencePackage persistencePackage) {
    super.applyAdditionalFetchCriteria(filterMappings, cto, persistencePackage);
    // grab the fulfillment location off of the custom criteria from the
    // frontend//from ww  w  . j  a  v  a 2  s  .com
    final Long locationId = Long.parseLong(persistencePackage.getCustomCriteria()[1]);
    FilterMapping f = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty("id"))
            .withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {
                @Override
                public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder,
                        From root, String ceilingEntity, String fullPropertyName, Path explicitPath,
                        List directValues) {
                    // DetachedCriteria locationSkuIds1 =
                    // DetachedCriteria.forClass(InventoryImpl.class)
                    // .add(Restrictions.eq("fulfillmentLocation.id",
                    // locationId))
                    // .setProjection(Projections.property("sku.id"));
                    // return
                    // Subqueries.propertyNotIn(targetPropertyName,
                    // locationSkuIds);

                    CriteriaQuery<Long> q = builder.createQuery(Long.class);
                    Root<InventoryImpl> subRoot = q.from(InventoryImpl.class);
                    q.where(builder.equal(subRoot.get("fulfillmentLocation.id"), locationId));
                    q.select(subRoot.<Long>get("sku.id"));
                    return builder.not(explicitPath.in(q)); // FIXME
                }
            }));
    filterMappings.add(f);
}

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

/**
 * {@inheritDoc}// w  w  w .j a v  a2s.  c o m
 */
public EntityType findExact(String key, Object value, Class<EntityType> entityClass) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get(key), value));

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

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

/**
 * //  w  w w .  j a  v  a2 s.c  om
 * @param productId
 * @return
 */
public boolean productExists(Long productId) {
    logger.info(String.format("checking if product with ID %d exists ...", productId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Product> from = criteria.from(Product.class);
    criteria.where(cb.equal(from.get(Product_.id), productId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult() == 1);
}

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

/**
 * {@inheritDoc}//from   w w w . j a  va2 s .c  o  m
 */
public EntityType findById(IDType id, Class<EntityType> entityClass) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get(FIELD_CONSTANT_ID), id));

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

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

/**
 * {@inheritDoc}/* w  ww. j  a  v a  2s .  co m*/
 */
public List<EntityType> findExactList(String key, IDType value, Class<EntityType> entityClass) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get(key), value));

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

}

From source file:com.zergiu.tvman.dao.impl.TVShowDaoImpl.java

@Override
public Long getActiveTVShowsCount() {
    CriteriaBuilder criteria = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = criteria.createQuery(Long.class);
    Root<TVShow> show = query.from(TVShow.class);
    query.select(criteria.countDistinct(show));
    query.where(criteria.equal(show.<TVShowStatus>get("status"), TVShowStatus.Continuing));
    Long count = em.createQuery(query).getSingleResult();
    log.debug("active count: " + count);
    return count;
}

From source file:com.samples.platform.serviceprovider.library.internal.dao.PlatformDao.java

/**
 * @param ISBN//from  ww  w  .j  a  v  a  2 s  .c o  m
 *            the ISBN of the book to find.
 * @return the {@link BookType}.
 */
@Transactional(value = PersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public BookType getBookByISBN(final String ISBN) {
    BookType m = null;
    if (ISBN == null) {
        this.logger.debug("getBookByISBN: ISBN is null.");
    } else {
        CriteriaBuilder cb = this.em.getCriteriaBuilder();
        CriteriaQuery<BookType> q = cb.createQuery(BookType.class);
        Root<BookType> c = q.from(BookType.class);
        q.where(cb.equal(c.<String>get(BookType_.ISBN), ISBN));
        TypedQuery<BookType> typedQuery = this.em.createQuery(q);
        try {
            m = typedQuery.getSingleResult();
            this.logger.debug("getBookByISBN: " + ToStringBuilder.reflectionToString(m));
        } catch (NoResultException e) {
            this.logger.debug("getBookByISBN: non value found for ISBN=" + ISBN);
            m = null;
        }
    }
    return m;
}

From source file:com.samples.platform.serviceprovider.library.internal.dao.PlatformDao.java

/**
 * @param uuid/*w  w w. j  a v a  2s.c om*/
 *            the id of the book to find.
 * @return the {@link BookType}.
 */
@Transactional(value = PersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public BookType getBookById(final String uuid) {
    BookType m = null;
    if (uuid == null) {
        this.logger.debug("getBookById: UUID is null.");
    } else {
        CriteriaBuilder cb = this.em.getCriteriaBuilder();
        CriteriaQuery<BookType> q = cb.createQuery(BookType.class);
        Root<BookType> c = q.from(BookType.class);
        q.where(cb.equal(c.<String>get(EntityType_.UUID), uuid));
        TypedQuery<BookType> typedQuery = this.em.createQuery(q);
        try {
            m = typedQuery.getSingleResult();
            this.logger.debug("getBookByISBN: " + ToStringBuilder.reflectionToString(m));
        } catch (NoResultException e) {
            this.logger.debug("getBookByISBN: non value found for id=" + uuid);
            m = null;
        }
    }
    return m;
}

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));/*  ww  w.j  av  a2  s.co  m*/
    query.where(cb.equal(root.get(Post_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

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

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<CustomField> root = query.from(CustomField.class);
    query.select(root.get(CustomField_.id));
    query.where(cb.equal(root.get(CustomField_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}