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();
}
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;
}
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
}
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;
}
}
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);
}
}
Aggregations