List of usage examples for javax.persistence.criteria CriteriaQuery where
CriteriaQuery<T> where(Predicate... restrictions);
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query that fetches objects based on their IDs * //from w w w .j av a 2s. c o m * @param entityManager * the entity manager * @param entityClass * the entity class * @param ids * the IDs of the desired entities * @param sortOrders * the sorting information * @param fetchJoins * the desired fetch joins * @return */ public static <ID, T> CriteriaQuery<T> createFetchQuery(EntityManager entityManager, Class<T> entityClass, List<ID> ids, SortOrders sortOrders, FetchJoinInformation[] fetchJoins) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> cq = builder.createQuery(entityClass); Root<T> root = cq.from(entityClass); boolean distinct = addFetchJoinInformation(root, fetchJoins); Expression<String> exp = root.get(DynamoConstants.ID); cq.where(exp.in(ids)); cq.distinct(distinct); return addSortInformation(builder, cq, root, sortOrders == null ? null : sortOrders.toArray()); }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query that simply selects some objects based on some filter * //w ww . j a v a 2s .com * @param filter * the filter * @param entityManager * the entity manager * @param entityClass * the entity class * @param sortOrder * the sorting information * @return */ public static <T> CriteriaQuery<T> createSelectQuery(Filter filter, EntityManager entityManager, Class<T> entityClass, FetchJoinInformation[] fetchJoins, SortOrder... sortOrders) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> cq = builder.createQuery(entityClass); Root<T> root = cq.from(entityClass); addFetchJoinInformation(root, fetchJoins); cq.select(root); Predicate p = createPredicate(filter, builder, root); if (p != null) { cq.where(p); } return addSortInformation(builder, cq, root, sortOrders); }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query that performs a count * /*from ww w . j a va 2 s . c om*/ * @param entityManager * the entity manager * @param entityClass * the entity class * @param filter * the filter to apply * @param distinct * whether to return only distinct results * @return */ public static <T> CriteriaQuery<Long> createCountQuery(EntityManager entityManager, Class<T> entityClass, Filter filter, boolean distinct) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<T> root = cq.from(entityClass); cq.select(distinct ? builder.countDistinct(root) : builder.count(root)); Predicate p = createPredicate(filter, builder, root); if (p != null) { cq.where(p); } return cq; }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query for retrieving the IDs of the entities that match the provided filter * /*ww w . j a va2 s. co m*/ * @param entityManager * the entity manager * @param entityClass * the entity class * @param filter * the filter to apply * @param sortOrder * the sorting to apply * @return */ public static <T> CriteriaQuery<Tuple> createIdQuery(EntityManager entityManager, Class<T> entityClass, Filter filter, SortOrder... sortOrders) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> cq = builder.createTupleQuery(); Root<T> root = cq.from(entityClass); // select only the ID cq.multiselect(root.get(DynamoConstants.ID)); // Set where clause Predicate p = createPredicate(filter, builder, root); if (p != null) { cq.where(p); } // add order clause - this is also important in case of an ID query // since we do need to return the correct IDs! return addSortInformation(builder, cq, root, sortOrders); }
From source file:nl.b3p.viewer.stripes.ApplicationActionBean.java
static Application findApplication(String name, String version) { EntityManager em = Stripersist.getEntityManager(); if (name != null) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery q = cb.createQuery(Application.class); Root<Application> root = q.from(Application.class); Predicate namePredicate = cb.equal(root.get("name"), name); Predicate versionPredicate = version != null ? cb.equal(root.get("version"), version) : cb.isNull(root.get("version")); q.where(cb.and(namePredicate, versionPredicate)); try {/* w ww .j a va 2 s . c om*/ return (Application) em.createQuery(q).getSingleResult(); } catch (NoResultException nre) { String decodedName = StringUtil.urlDecode(name); if (!decodedName.equals(name)) { return findApplication(decodedName, version); } } } return null; }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query used to retrieve a single entity based on a unique property value * // w ww.j a v a 2 s. c om * @param entityManager * @param entityClass * @param propertyName * @param value * @return */ public static <T> CriteriaQuery<T> createUniquePropertyQuery(EntityManager entityManager, Class<T> entityClass, String propertyName, Object value, boolean caseSensitive) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> cq = builder.createQuery(entityClass); Root<T> root = cq.from(entityClass); Predicate equals = null; if (value instanceof String && !caseSensitive) { equals = builder.equal(builder.upper(root.get(propertyName).as(String.class)), ((String) value).toUpperCase()); } else { equals = builder.equal(root.get(propertyName), value); } cq.where(equals); return cq; }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query to fetch an object based on a value of a unique property * //ww w . j av a 2 s . c om * @param entityManager * the entity manager * @param entityClass * the entity class * @param fetchJoins * the fetch joins to include * @param propertyName * name of the property to search on * @param value * value of the property to search on * @return */ public static <T> CriteriaQuery<T> createUniquePropertyFetchQuery(EntityManager entityManager, Class<T> entityClass, FetchJoinInformation[] fetchJoins, String propertyName, Object value, boolean caseSensitive) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> cq = builder.createQuery(entityClass); Root<T> root = cq.from(entityClass); addFetchJoinInformation(root, fetchJoins); Predicate equals = null; if (value instanceof String && !caseSensitive) { equals = builder.equal(builder.upper(root.get(propertyName).as(String.class)), ((String) value).toUpperCase()); } else { equals = builder.equal(root.get(propertyName), value); } cq.where(equals); cq.distinct(true); return cq; }
From source file:net.przemkovv.sphinx.dao.impl.DefaultUserDAO.java
@Override public User getUser(String email) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> cq = cb.createQuery(User.class); Root<User> user = cq.from(User.class); cq.where(cb.equal(user.get(User_.email), email)); return em.createQuery(cq).getSingleResult(); }
From source file:net.przemkovv.sphinx.dao.impl.DefaultUserDAO.java
@Override public boolean existsByEmail(String email) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> cq = cb.createQuery(Long.class); Root<User> user = cq.from(User.class); cq.where(cb.equal(user.get(User_.email), email)); cq.select(cb.count(user));/* ww w . j a v a 2 s .c o m*/ return em.createQuery(cq).getSingleResult().intValue() == 1; }
From source file:net.przemkovv.sphinx.dao.impl.DefaultSolutionDAO.java
@Override public List<Solution> getSolutionsByTask(Task task) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Solution> cq = cb.createQuery(Solution.class); Root<Solution> solution = cq.from(Solution.class); cq.where(cb.equal(solution.get(Solution_.task), task)); return em.createQuery(cq).getResultList(); }