List of usage examples for javax.persistence.criteria CriteriaQuery from
<X> Root<X> from(Class<X> entityClass);
From source file:example.springdata.jpa.showcase.snippets.CustomerSpecifications.java
/** * All customers with an {@link Account} expiring before the given date. * /*from w w w . j a va 2 s.c o m*/ * @param date * @return */ public static Specification<Customer> accountExpiresBefore(final LocalDate date) { return new Specification<Customer>() { @Override public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Root<Account> accounts = query.from(Account.class); Path<Date> expiryDate = accounts.<Date>get("expiryDate"); Predicate customerIsAccountOwner = cb.equal(accounts.<Customer>get("customer"), root); Predicate accountExpiryDateBefore = cb.lessThan(expiryDate, date.toDateTimeAtStartOfDay().toDate()); return cb.and(customerIsAccountOwner, accountExpiryDateBefore); } }; }
From source file:com.enioka.jqm.tools.ResourceParser.java
private static JndiResourceDescriptor fromDatabase(String alias) throws NamingException { JndiObjectResource resource = null;// w ww. j av a 2 s .c om EntityManager em = null; try { // Using the horrible CriteriaBuilder API instead of a string query. This avoids classloading issues - Hibernate binds // the entities at run time with the thread current classloader... em = Helpers.getNewEm(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<JndiObjectResource> q = cb.createQuery(JndiObjectResource.class); Root<JndiObjectResource> c = q.from(JndiObjectResource.class); ParameterExpression<String> p = cb.parameter(String.class); q.select(c).where(cb.equal(c.get("name"), p)); TypedQuery<JndiObjectResource> query = em.createQuery(q); query.setParameter(p, alias); resource = query.getSingleResult(); } catch (Exception e) { NamingException ex = new NamingException("Could not find a JNDI object resource of name " + alias); ex.setRootCause(e); throw ex; } finally { if (em != null) { em.close(); } } // Create the ResourceDescriptor from the JPA object JndiResourceDescriptor d = new JndiResourceDescriptor(resource.getType(), resource.getDescription(), null, resource.getAuth(), resource.getFactory(), resource.getSingleton()); for (JndiObjectResourceParameter prm : resource.getParameters()) { d.add(new StringRefAddr(prm.getKey(), prm.getValue())); } return d; }
From source file:com.github.jinahya.persistence.ShadowTest.java
protected static List<Morton> MORTONS(final EntityManager manager, final int firstResult, final int maxResults) { final CriteriaBuilder builder = manager.getCriteriaBuilder(); final CriteriaQuery<Morton> query = builder.createQuery(Morton.class); final Root<Morton> morton = query.from(Morton.class); query.select(morton).orderBy(builder.desc(morton.get(Morton_.id))); return manager.createQuery(query).setFirstResult(firstResult).setMaxResults(maxResults).getResultList(); }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Create a query for fetching a single object * /*from w w w . j a v a2s .c o m*/ * @param entityManager * the entity manager * @param entityClass * the entity class * @param id * ID of the object to return * @param fetchJoins * fetch joins to include * @return */ public static <ID, T> CriteriaQuery<T> createFetchSingleObjectQuery(EntityManager entityManager, Class<T> entityClass, ID id, FetchJoinInformation[] fetchJoins) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> cq = builder.createQuery(entityClass); Root<T> root = cq.from(entityClass); addFetchJoinInformation(root, fetchJoins); cq.where(builder.equal(root.get(DynamoConstants.ID), id)); return cq; }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query that fetches objects based on their IDs * /*from w ww.j av a 2 s . c om*/ * @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 performs a count * /*from w w w. j a v a2s. 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 that simply selects some objects based on some filter * /*from ww w.j ava 2 s . c o m*/ * @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 for retrieving the IDs of the entities that match the provided filter * //ww w . j a v a 2s . c o 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:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query used to retrieve a single entity based on a unique property value * //w w w. j a v a 2 s .com * @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 * // w w w. j av a2 s . co m * @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; }