List of usage examples for javax.persistence.criteria Root get
<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);
From source file:com.carser.viamais.vo.TransactionFilter.java
private Predicate[] getPredicates(CriteriaBuilder cb, Root<Transaction> transaction) { List<Predicate> predicates = new ArrayList<>(); if (StringUtils.hasLength(pattern)) { String[] words = pattern.split(" "); for (String word : words) { Predicate model = cb.like(transaction.get(Transaction_.car).get(Car_.model).get(Model_.name), "%" + word + "%"); Predicate customer = cb.like(transaction.get(Transaction_.customer).get(Customer_.name), "%" + word + "%"); predicates.add((cb.or(model, customer))); }/*from w w w . j a v a 2s . c o m*/ } if (StringUtils.hasLength(rg)) { predicates.add(cb.like(transaction.get(Transaction_.customer).get(Customer_.rg), "%" + rg + "%")); } if (StringUtils.hasLength(cpf)) { predicates.add(cb.like(transaction.get(Transaction_.customer).get(Customer_.cpf).as(String.class), "%" + cpf + "%")); } if (StringUtils.hasLength(renavam)) { predicates.add(cb.like(transaction.get(Transaction_.car).get(Car_.renavam), "%" + renavam + "%")); } if (StringUtils.hasLength(plate)) { predicates.add(cb.equal(transaction.get(Transaction_.car).get(Car_.licensePlate), plate)); } if (StringUtils.hasLength(chassi)) { predicates.add(cb.like(transaction.get(Transaction_.car).get(Car_.chassi), "%" + chassi + "%")); } Expression<Date> date = cb.function("date", Date.class, transaction.get(Transaction_.transactionDate)); if (startDate != null) { predicates.add(cb.greaterThanOrEqualTo(date, startDate)); } if (finalDate != null) { predicates.add(cb.lessThanOrEqualTo(date, finalDate)); } if (StringUtils.hasLength(type)) { switch (type) { case "sale": predicates.add(cb.equal(transaction.get(Transaction_.type), Sale.class.getSimpleName())); break; case "purchase": predicates.add(cb.equal(transaction.get(Transaction_.type), Purchase.class.getSimpleName())); break; default: throw new BusinessException("transaction type not supported"); } } if (StringUtils.hasLength(advertiser)) { predicates.add(cb.equal(transaction.get(Transaction_.advertiser), advertiser)); } if (StringUtils.hasLength(financial)) { predicates.add(cb.like(cb.treat(transaction, Sale.class).get(Sale_.financial), financial)); } if (seller != null) { predicates.add(cb.equal(transaction.get(Transaction_.seller).get(Seller_.id), seller)); } return (predicates.toArray(new Predicate[predicates.size()])); }
From source file:com.expressui.sample.dao.query.AccountQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Account> query, Root<Account> account) { List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(name)) { ParameterExpression<String> nameExp = builder.parameter(String.class, "name"); predicates.add(builder.like(builder.upper(account.<String>get("name")), nameExp)); }//from www .j a v a 2s. c om if (hasValue(states)) { ParameterExpression<Set> statesExp = builder.parameter(Set.class, "states"); predicates.add(builder.in(account.get("billingAddress").get("state")).value(statesExp)); } if (hasValue(country)) { ParameterExpression<Country> countryExp = builder.parameter(Country.class, "country"); predicates.add(builder.equal(account.get("billingAddress").get("country"), countryExp)); } return predicates; }
From source file:org.sloth.persistence.impl.ObservationDaoImpl.java
@Override public Collection<Observation> getByKeyWord(String key) throws NullPointerException { if (key == null) { throw new NullPointerException(); }/*from www. j a v a2 s. c o m*/ 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:com.expressui.sample.view.contact.ContactQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Contact> rootEntity) { List<Predicate> criteria = new ArrayList<Predicate>(); if (!isEmpty(lastName)) { ParameterExpression<String> p = builder.parameter(String.class, "lastName"); criteria.add(builder.like(builder.upper(rootEntity.<String>get("lastName")), p)); }/*from ww w . j a v a 2s .com*/ if (!isEmpty(states)) { ParameterExpression<Set> p = builder.parameter(Set.class, "states"); criteria.add(builder.in(rootEntity.get("mailingAddress").get("state")).value(p)); } if (!isEmpty(country)) { ParameterExpression<Country> p = builder.parameter(Country.class, "country"); criteria.add(builder.equal(rootEntity.get("mailingAddress").get("country"), p)); } return criteria; }
From source file:com.sfs.ucm.controller.SpecificationAction.java
/** * find tests associated with this artifact *//* w w w . ja va 2 s . co m*/ private List<SpecificationTest> findSpecificationTests(Specification specification) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<SpecificationTest> c = cb.createQuery(SpecificationTest.class); Root<SpecificationTest> obj = c.from(SpecificationTest.class); c.select(obj).where(cb.equal(obj.get("specification"), specification)); return em.createQuery(c).getResultList(); }
From source file:me.ineson.demo.service.utils.RestUtilsTest.java
@SuppressWarnings("unchecked") @Test//from ww w . j a v a2 s . c om public void testParseWhereClauseMultipleWithTransation() { CriteriaBuilder criteriaBuilderMock = mock(CriteriaBuilder.class); CriteriaQuery<SolarBody> criteriaQueryMock = mock(CriteriaQuery.class); Root<SolarBody> rootMock = mock(Root.class); log.info("mock root {}, builder {}", rootMock, criteriaBuilderMock); Map<String, String> translations = new HashMap<String, String>(); translations.put("cmrId", "cmr.id"); Join<Object, Object> nameFieldPath = mock(Join.class, "name field"); when(rootMock.get("name")).thenReturn(nameFieldPath); Join<Object, Object> cmrRecordPath = mock(Join.class, "cmr field"); when(rootMock.join("cmr")).thenReturn(cmrRecordPath); Join<Object, Object> idFieldPath = mock(Join.class, "id field"); when(cmrRecordPath.get("id")).thenReturn(idFieldPath); Predicate firstPredicate = mock(Predicate.class, "1st prediccate"); when(criteriaBuilderMock.equal(nameFieldPath, "test")).thenReturn(firstPredicate); Predicate secondPredicate = mock(Predicate.class, "2nd prediccate"); when(criteriaBuilderMock.equal(idFieldPath, "22")).thenReturn(secondPredicate); Predicate andedPredicates[] = { firstPredicate, secondPredicate }; Predicate finalPredicate = mock(Predicate.class, "final prediccate"); when(criteriaBuilderMock.and(andedPredicates)).thenReturn(finalPredicate); Predicate predicate = RestUtils.parseWhereClause("name=test,cmrId=22", rootMock, criteriaQueryMock, criteriaBuilderMock, translations); verify(rootMock, times(1)).get("name"); verify(rootMock, times(1)).join("cmr"); verifyNoMoreInteractions(rootMock); verify(cmrRecordPath, times(1)).get("id"); verifyNoMoreInteractions(cmrRecordPath); verify(criteriaBuilderMock, times(1)).equal(nameFieldPath, "test"); verify(criteriaBuilderMock, times(1)).equal(idFieldPath, "22"); verify(criteriaBuilderMock, times(1)).and(andedPredicates); verifyNoMoreInteractions(criteriaBuilderMock); Assert.assertEquals(finalPredicate, predicate); }
From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java
private <T> CriteriaQuery<T> addSortProperties(CriteriaQuery<T> query, Root root, Pageable pageable) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); List<javax.persistence.criteria.Order> orders = new ArrayList<>(); Iterator<Sort.Order> iterator = pageable.getSort().iterator(); Sort.Order order;//from w w w .j a va 2 s.c o m while (iterator.hasNext()) { order = iterator.next(); String property = order.getProperty(); Path path = root.get(property); if (order.isAscending()) { orders.add(builder.asc(path)); } else { orders.add(builder.desc(path)); } } return query.orderBy(orders); }
From source file:com.sfs.ucm.controller.SpecificationAction.java
/** * find tests associated with this artifact *//* w w w. j a v a 2 s. c o m*/ private List<SpecificationRuleTest> findSpecificationRuleTests(SpecificationRule specificationRule) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<SpecificationRuleTest> c = cb.createQuery(SpecificationRuleTest.class); Root<SpecificationRuleTest> obj = c.from(SpecificationRuleTest.class); c.select(obj).where(cb.equal(obj.get("specificationRule"), specificationRule)); return em.createQuery(c).getResultList(); }
From source file:sf.net.experimaestro.scheduler.Resource.java
/** * Get a resource by locator/*from w ww .ja v a2s . c o m*/ * * @param em The current entity manager * @param path The path of the resource * @return The resource or null if there is no such resource */ public static Resource getByLocator(EntityManager em, String path) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Resource> cq = cb.createQuery(Resource.class); Root<Resource> root = cq.from(Resource.class); cq.where(root.get("locator").in(cb.parameter(String.class, "locator"))); TypedQuery<Resource> query = em.createQuery(cq); query.setParameter("locator", path); List<Resource> result = query.getResultList(); assert result.size() <= 1; if (result.isEmpty()) return null; return result.get(0); }