List of usage examples for javax.persistence.criteria CriteriaQuery from
<X> Root<X> from(Class<X> entityClass);
From source file:se.kth.csc.persist.JPAStore.java
@Override public Iterable<Account> findAccounts(boolean onlyAdmin, String query) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Account> q = cb.createQuery(Account.class); Root<Account> account = q.from(Account.class); Expression<Boolean> expression = null; if (onlyAdmin) { // This looks like it could be replaced with account.get(Account_.admin), but it can't because of syntax expression = cb.equal(account.get(Account_.admin), true); }/* w w w.j ava 2 s.com*/ if (query != null) { Expression<Boolean> queryExpression = cb.like(cb.lower(account.get(Account_.name)), "%" + query.toLowerCase() + "%"); if (expression == null) { expression = queryExpression; } else { expression = cb.and(expression, queryExpression); } } if (expression != null) { q.where(expression); } return entityManager.createQuery(q.select(account)).getResultList(); }
From source file:com.saake.invoicer.sessionbean.PurchaseOrderFacade.java
@Override public List<PurchaseOrder> findAll() { CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); javax.persistence.criteria.CriteriaQuery cq = cb.createQuery(); Root<PurchaseOrder> root = cq.from(PurchaseOrder.class); cq.select(root);/*from w ww . ja va 2 s . c om*/ ParameterExpression<String> status = cb.parameter(String.class); // cq.where(cb.notEqual(invRoot.get("status"), status)); cq.where(cb.notEqual(root.get("deleted"), 'Y')); cq.orderBy(cb.desc(root.get("purchaseOrderId"))); Query query = getEntityManager().createQuery(cq); // query.setParameter(status, PaymentStatusEnum.DELETE.getValue()); List<PurchaseOrder> list = query.getResultList(); for (PurchaseOrder wo : list) { List<PurchaseOrderItems> tempDel = new ArrayList<>(); for (PurchaseOrderItems wot : wo.getPurchaseOrderItems()) { if (wot.isDeleted()) { tempDel.add(wot); } } wo.getPurchaseOrderItems().removeAll(tempDel); } return list; }
From source file:org.sloth.persistence.impl.ObservationDaoImpl.java
@Override public Collection<Observation> getByUser(User u) throws NullPointerException, IllegalArgumentException { if (u == null) { throw new NullPointerException(); }/*from w w w . ja v a 2s . c o m*/ if (u.isNew()) { throw new EntityNotKnownException(); } CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Observation> cq = cb.createQuery(Observation.class); Root<Observation> o = cq.from(Observation.class); cq.select(o).where(cb.equal(o.get(Observation_.user), u)); Collection<Observation> result = getEntityManager().createQuery(cq).getResultList(); logger.info("{} Observations by User {}.", result.size(), u); return result; }
From source file:com.aimdek.ccm.dao.impl.StatementRepositoryImpl.java
/** * {@inheritDoc}//from w w w . j av a2 s . c o m */ public Statement findLastStatement() { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Statement> query = builder.createQuery(Statement.class); Root<Statement> root = query.from(Statement.class); query.select(root); query.orderBy(builder.desc(root.get(FIELD_CONSTANT_STATEMENT_DATE))); try { return entityManager.createQuery(query).setMaxResults(1).getSingleResult(); } catch (NoResultException e) { LOGGER.error("Error while retrieving last statement", e); } return null; }
From source file:org.sloth.persistence.impl.ObservationDaoImpl.java
@Override public Collection<Observation> getByCategorie(Categorie c) throws NullPointerException, IllegalArgumentException { if (c == null) { throw new NullPointerException(); }// w w w . j a va2 s. c om if (c.isNew()) { throw new IllegalArgumentException(); } CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Observation> cq = cb.createQuery(Observation.class); Root<Observation> o = cq.from(Observation.class); cq.select(o).where(cb.equal(o.get(Observation_.categorie), c)); Collection<Observation> result = getEntityManager().createQuery(cq).getResultList(); logger.info("{} Observations in Categorie {}.", result.size(), c); return result; }
From source file:org.sloth.persistence.impl.ObservationDaoImpl.java
@Override public Collection<Observation> getByKeyWord(String key) throws NullPointerException { if (key == null) { throw new NullPointerException(); }/* w ww .j a v a 2 s . com*/ key = "%" + key.trim().replace('*', '%').toUpperCase() + "%"; CriteriaBuilder b = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Observation> q = b.createQuery(Observation.class); Root<Observation> o = q.from(Observation.class); Join<Observation, Categorie> j = o.join(Observation_.categorie); Collection<Observation> result = getEntityManager().createQuery(q.select(o).distinct(true) .where(b.or(b.like(b.upper(j.get(Categorie_.title)), key), b.like(b.upper(j.get(Categorie_.description)), key), b.like(b.upper(o.get(Observation_.title)), key), b.like(b.upper(o.get(Observation_.description)), key)))) .getResultList(); logger.info("Searched for {}. Got {} Results.", key, result.size()); return result; }
From source file:org.osiam.resource_server.storage.dao.ResourceDao.java
/** * Retrieves a single {@link ResourceEntity} by the given attribute and value. * /*from w ww .j ava2 s .c om*/ * @param attribute * The attribute of the resource entity to retrieve it by * @param value * The value of the attribute to compare it to * @param clazz * The concrete resource entity class to retrieve (may also be {@link ResourceEntity}) * @return The matching {@link ResourceEntity} * @throws ResourceNotFoundException * If no {@link ResourceEntity} could be found * @throws OsiamException * If more than 1 {@link ResourceEntity} was found */ public <T extends ResourceEntity, V> T getByAttribute(SingularAttribute<? super T, V> attribute, V value, Class<T> clazz) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(clazz); Root<T> resource = cq.from(clazz); cq.select(resource).where(cb.equal(resource.get(attribute), value)); TypedQuery<T> q = em.createQuery(cq); try { return q.getSingleResult(); } catch (NoResultException nre) { throw new ResourceNotFoundException( String.format("Resource with attribute '%s' set to '%s' not found", attribute.getName(), value), nre); } catch (NonUniqueResultException nure) { throw new OsiamException(String.format("Muliple resources with attribute '%s' set to '%s' found", attribute.getName(), value), nure); } }
From source file:org.sloth.persistence.impl.ObservationDaoImpl.java
@Override public List<Observation> getNewestObservations(int count) { if (count <= 0) { return new LinkedList<Observation>(); }/* ww w . j a va 2 s .c o m*/ CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Observation> cq = cb.createQuery(Observation.class); Root<Observation> o = cq.from(Observation.class); cq.select(o).orderBy(cb.desc(o.get(Observation_.creationDate))); List<Observation> result = getEntityManager().createQuery(cq).setMaxResults(count).getResultList(); if (result.size() < count) { logger.info("Only {} Observations in the database", result.size()); } else { logger.info("Found the {} last Observations", result.size()); } return result; }
From source file:com.orangeandbronze.jblubble.sample.PersonController.java
protected List<Person> getAllPersons() { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Person> criteriaQuery = criteriaBuilder.createQuery(Person.class); criteriaQuery.select(criteriaQuery.from(Person.class)); return entityManager.createQuery(criteriaQuery).getResultList(); }
From source file:net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper.java
/** * Find entities./*from w w w .j a v a 2 s .co m*/ * * @param em * the em * @param target * the target * @param all * the all * @param maxResults * the max results * @param firstResult * the first result * @return the list * @since 28/08/2014, 11:20:27 PM */ private List<Entity> findEntities(EntityManager em, Class<Entity> target, boolean all, int maxResults, int firstResult) { CriteriaQuery<Entity> cq = em.getCriteriaBuilder().createQuery(target); cq.select(cq.from(target)); TypedQuery<Entity> q = em.createQuery(cq); if (!all) { q.setMaxResults(maxResults); q.setFirstResult(firstResult); } return q.getResultList(); }