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:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute// w  w w . j  ava 2s.com
public List<T> findByAttribute(String attributeName, Object attributeValue, long first, long count,
        String... fg) {
    LOGGER.debug("Searching {} bu attribute {} : {}, fg {}",
            new Object[] { persistentClass, attributeName, attributeValue, fg });

    if (fg != null) {
        entityManager.pushFetchPlan();
        entityManager.getFetchPlan().addFetchGroups(fg);
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> q = cb.createQuery(persistentClass);
    Root<T> p = q.from(persistentClass);
    q.where(cb.equal(p.get(attributeName), attributeValue));

    TypedQuery<T> query = entityManager.createQuery(q);

    OrmUtils.firstCount(query, first, count);
    List<T> resultList = query.getResultList();

    if (fg != null) {
        entityManager.popFetchPlan();
    }

    return resultList;

}

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

public TypedQuery<Long> buildFilterCount() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Connection> relationship = criteria.from(Connection.class);
    Join<Connection, Profile> receiver = relationship.join(Connection_.receiver);
    CriteriaQuery<Long> select = criteria.select(cb.countDistinct(relationship));
    ///*from  ww  w .  j  a  v  a2 s . c o  m*/
    select.where(buildPredicateFilter(cb, receiver, relationship));
    //
    return em.createQuery(select);
}

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

/**
 * Builds the Typed Query//from  w  w  w . j av  a2  s .  c o m
 * @return
 */
public TypedQuery<Long> buildCount() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Connection> connection = criteria.from(Connection.class);

    Predicate predicate = null;
    //owner
    if (this.owner != null) {
        predicate = cb.equal(connection.get(Connection_.senderId), owner.getId());
    }
    //status
    if (this.status != null) {
        if (Relationship.Type.PENDING.equals(this.status)) {
            predicate = cb.and(predicate, addInClause(cb, connection.get(Connection_.status), types));
        } else {
            predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status));
        }
    }

    CriteriaQuery<Long> select = criteria.select(cb.countDistinct(connection));
    select.where(predicate);

    return em.createQuery(select);
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute/* w w  w  .  j a v a 2 s.  c o m*/
public List<T> findByExample(final T example, final T example2, final OrderBy<T> sortParam, final long first,
        final long count, final String... fg) {
    if (fg != null) {
        entityManager.pushFetchPlan();
        entityManager.getFetchPlan().addFetchGroups(fg);
    }

    OpenJPACriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> q = cb.createQuery(persistentClass);
    Root<T> from = q.from(persistentClass);
    q.where(newQbePredicates(cb, from, example, example2));
    if (sortParam != null) {
        List<Order> orderList = sortParam.orders(cb, from);
        if (orderList != null && !orderList.isEmpty()) {
            q.orderBy(orderList);
        }
    }

    TypedQuery<T> query = entityManager.createQuery(q);
    OrmUtils.firstCount(query, first, count);

    List<T> resultList = query.getResultList();

    if (fg != null) {
        entityManager.popFetchPlan();
    }
    return resultList;
}

From source file:org.broadleafcommerce.cms.admin.server.handler.StructuredContentItemCriteriaCustomPersistenceHandler.java

@Override
public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao,
        RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {//from  www .  j a va  2s  .  c om
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(
                StructuredContentItemCriteria.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        StructuredContentItemCriteria adminInstance = (StructuredContentItemCriteria) dynamicEntityDao
                .retrieve(Class.forName(entity.getType()[0]), primaryKey);

        if (adminInstance.getStructuredContent().getLockedFlag()) {
            /*
            This may be an attempt to delete a target item criteria off an otherwise un-edited, production StructuredContent instance
             */
            CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder();
            CriteriaQuery<StructuredContent> query = criteriaBuilder.createQuery(StructuredContent.class);
            Root<StructuredContentImpl> root = query.from(StructuredContentImpl.class);
            query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE),
                    criteriaBuilder.equal(root.get("originalItemId"),
                            adminInstance.getStructuredContent().getId())));
            query.select(root);
            TypedQuery<StructuredContent> scQuery = dynamicEntityDao.getStandardEntityManager()
                    .createQuery(query);
            try {
                StructuredContent myContent = scQuery.getSingleResult();
                for (StructuredContentItemCriteria itemCriteria : myContent.getQualifyingItemCriteria()) {
                    if (itemCriteria.getOrderItemMatchRule().equals(adminInstance.getOrderItemMatchRule())
                            && itemCriteria.getQuantity().equals(adminInstance.getQuantity())) {
                        myContent.getQualifyingItemCriteria().remove(itemCriteria);
                        return;
                    }
                }
                throw new RuntimeException("Unable to find an item criteria to delete");
            } catch (Exception e) {
                throw new IllegalArgumentException("Unable to update a locked record");
            }
        }

        dynamicEntityDao.remove(adminInstance);
    } catch (Exception e) {
        LOG.error("Unable to execute persistence activity", e);
        throw new ServiceException("Unable to remove entity for " + entity.getType()[0], e);
    }
}

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

public TypedQuery<Connection> buildLastConnections() {
    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;// w w w  .jav  a2  s . co m
    //owner
    if (this.owner != null) {
        predicate = cb.equal(connection.get(Connection_.senderId), owner.getId());
    }
    //status
    if (this.status != null) {
        predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status));
    }

    CriteriaQuery<Connection> select = criteria.select(connection).distinct(true);
    select.where(predicate);
    select.orderBy(cb.desc(connection.<Long>get(Connection_.id)));

    TypedQuery<Connection> typedQuery = em.createQuery(select);
    if (this.limit > 0) {
        typedQuery.setFirstResult((int) offset);
        typedQuery.setMaxResults((int) limit);
    }

    return typedQuery;
}

From source file:com.aimdek.ccm.dao.impl.test.UsersDaoImplTest.java

/**
 * Test user like.//ww w.j  av  a2  s .c om
 */
@Test
public void testUserLike() {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.where(builder.like(root.<String>get("lastName"), "test%"));
    List<User> user = entityManager.createQuery(query).getResultList();

    assertEquals(10, user.size());
}

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

public long countAllFiltered(final ProcessesFilterStructure filter) {
    logger.infof("counting all Processes matching the filter %s ...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Process> from = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*ww w  .  j av a  2 s .  c  om*/
    criteria.select(cb.countDistinct(from));
    return em.createQuery(criteria).getSingleResult();
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute//w w  w.  jav  a 2  s.  c  om
public List<T> findByFullTexts(final String terms, final OrderBy<T> orders, final long first, final long count,
        final String... fg) {

    LOGGER.debug("searching by text {} terms {}, fg {}", new Object[] { persistentClass, terms, fg });

    if (settings.returnsNullIfTermsAreNull() && Strings.isNullOrEmpty(terms)) {
        return Collections.emptyList();
    }

    if (fg != null) {
        entityManager.pushFetchPlan();
        entityManager.getFetchPlan().addFetchGroups(fg);
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> q = cb.createQuery(persistentClass);
    Root<T> p = q.from(persistentClass);
    q.where(newFullTextPredicate(cb, p, terms));

    if (orders != null) {
        List<Order> orderList = orders.orders(cb, p);
        if (orderList != null && !orderList.isEmpty()) {
            q.orderBy(orderList);
        }
    }

    TypedQuery<T> query = entityManager.createQuery(q);

    OrmUtils.firstCount(query, first, count);
    List<T> resultList = query.getResultList();

    if (fg != null) {
        entityManager.popFetchPlan();
    }

    return resultList;
}

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

/**
 * Builds the Typed Query// ww  w  . j  a v a 2s.c  o  m
 * @return
 */
public TypedQuery<Connection> build() {
    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;
    //owner
    if (this.owner != null) {
        predicate = cb.equal(connection.get(Connection_.senderId), owner.getId());
    }
    //status
    if (this.status != null) {
        if (Relationship.Type.PENDING.equals(this.status)) {
            predicate = cb.and(predicate, addInClause(cb, connection.get(Connection_.status), types));
        } else {
            predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status));
        }
    }

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

    TypedQuery<Connection> typedQuery = em.createQuery(select);
    if (this.limit > 0) {
        typedQuery.setFirstResult((int) offset);
        typedQuery.setMaxResults((int) limit);
    }

    return typedQuery;
}