Search in sources :

Example 11 with Predicate

use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.

the class PredicateTest method testNotMultipleOr.

/**
	 * Check complicated not.
	 */
@Test
public void testNotMultipleOr() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
    Root<Order> orderRoot = orderCriteria.from(Order.class);
    orderCriteria.select(orderRoot);
    Predicate p1 = builder.equal(orderRoot.get("id"), "order-1");
    Predicate p2 = builder.equal(orderRoot.get("id"), "order-2");
    Predicate p3 = builder.equal(orderRoot.get("id"), "order-3");
    final Predicate compoundPredicate = builder.or(p1, p2, p3).not();
    // negated OR should become an AND
    assertEquals(Predicate.BooleanOperator.AND, compoundPredicate.getOperator());
    orderCriteria.where(compoundPredicate);
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertEquals(0, orders.size());
    em.getTransaction().commit();
    em.close();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 12 with Predicate

use of javax.persistence.criteria.Predicate in project c4sg-services by Code4SocialGood.

the class UserSpecification method toPredicate.

@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
    Predicate resultPredicate = null;
    List<Predicate> predicates = buildPredicates(root, cb);
    if (!predicates.isEmpty()) {
        resultPredicate = cb.and(predicates.toArray(new Predicate[predicates.size()]));
    }
    return resultPredicate;
}
Also used : Predicate(javax.persistence.criteria.Predicate)

Example 13 with Predicate

use of javax.persistence.criteria.Predicate in project robo4j by Robo4J.

the class DefaultRepository method findByFields.

@SuppressWarnings("unchecked")
@Override
public <T> List<T> findByFields(Class<T> clazz, Map<String, Object> map, int limit, SortType sort) {
    EntityManager em = dataSourceContext.getEntityManager(clazz);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(clazz);
    Root<T> rs = cq.from(clazz);
    List<Predicate> predicates = map.entrySet().stream().map(e -> cb.equal(rs.get(e.getKey()), e.getValue())).collect(Collectors.toList());
    CriteriaQuery<T> cq2 = cq.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(getOrderById(cb, rs, sort)).select(rs);
    TypedQuery<T> tq = em.createQuery(cq2);
    //@formatter:off
    return tq.setMaxResults(limit).getResultList();
//@formatter:on
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) List(java.util.List) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) SortType(com.robo4j.db.sql.support.SortType) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) TypedQuery(javax.persistence.TypedQuery) DataSourceContext(com.robo4j.db.sql.support.DataSourceContext) Root(javax.persistence.criteria.Root) EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Predicate(javax.persistence.criteria.Predicate)

Example 14 with Predicate

use of javax.persistence.criteria.Predicate in project deltaspike by apache.

the class QueryCriteria method createQuery.

@Override
public TypedQuery<R> createQuery() {
    try {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<R> query = createCriteriaQuery(builder);
        From<C, C> root = query.from(entityClass);
        if (selections.size() == 1) {
            Selection<?>[] selections = prepareSelections(query, builder, root);
            query.select((Selection<? extends R>) selections[0]);
        }
        if (selections.size() > 1) {
            query.multiselect(prepareSelections(query, builder, root));
        }
        List<Predicate> predicates = predicates(builder, root);
        query.distinct(distinct);
        if (!predicates.isEmpty()) {
            query.where(predicates.toArray(new Predicate[predicates.size()]));
        }
        applyProcessors(query, builder, root);
        return (TypedQuery<R>) entityManager.createQuery(query);
    } catch (RuntimeException e) {
        log.log(Level.SEVERE, "Exception while creating JPA query", e);
        throw e;
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TypedQuery(javax.persistence.TypedQuery) Selection(javax.persistence.criteria.Selection) QuerySelection(org.apache.deltaspike.data.api.criteria.QuerySelection) Predicate(javax.persistence.criteria.Predicate)

Example 15 with Predicate

use of javax.persistence.criteria.Predicate in project ice by JBEI.

the class FolderDAO method getCanEditFolders.

/**
     * Retrieves folders that the specified account owns, or has write privileges on based on the permissions
     *
     * @param account       account that is expected to have write privileges on the folders that are returned
     * @param accountGroups groups that account belongs to that is expected to have write privileges
     * @return list of folders that the account or groups that the account belongs to has write privileges on
     * @throws DAOException
     */
public List<Folder> getCanEditFolders(Account account, Set<Group> accountGroups) {
    try {
        CriteriaQuery<Folder> query = getBuilder().createQuery(Folder.class);
        Root<Permission> from = query.from(Permission.class);
        Join<Permission, Folder> folder = from.join("folder");
        // where ((account = account or group in groups) and canWrite)) or is owner
        Predicate predicate = getBuilder().and(getBuilder().or(getBuilder().equal(from.get("account"), account), from.get("group").in(accountGroups)), getBuilder().equal(from.get("canWrite"), true), getBuilder().isNotNull(from.get("folder")));
        query.select(folder).where(predicate);
        return currentSession().createQuery(query).list();
    } catch (HibernateException e) {
        Logger.error(e);
        throw new DAOException(e);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) HibernateException(org.hibernate.HibernateException) Predicate(javax.persistence.criteria.Predicate)

Aggregations

Predicate (javax.persistence.criteria.Predicate)35 EntityManager (javax.persistence.EntityManager)19 Test (org.junit.Test)16 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)15 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)11 Order (org.hibernate.jpa.test.metamodel.Order)8 HibernateException (org.hibernate.HibernateException)7 DAOException (org.jbei.ice.storage.DAOException)7 ArrayList (java.util.ArrayList)4 TypedQuery (javax.persistence.TypedQuery)3 List (java.util.List)2 Map (java.util.Map)2 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)2 TestForIssue (org.hibernate.testing.TestForIssue)2 SampleRequest (org.jbei.ice.lib.dto.sample.SampleRequest)2 Request (org.jbei.ice.storage.model.Request)2 Function (com.google.common.base.Function)1 Transactional (com.google.inject.persist.Transactional)1 DataSourceContext (com.robo4j.db.sql.support.DataSourceContext)1 SortType (com.robo4j.db.sql.support.SortType)1