List of usage examples for javax.persistence.criteria CriteriaBuilder equal
Predicate equal(Expression<?> x, Object y);
From source file:com.jaxio.jpa.querybyexample.JpaUtil.java
public <E> Predicate stringPredicate(Expression<String> path, Object attrValue, SearchMode searchMode, SearchParameters sp, CriteriaBuilder builder) { if (sp.isCaseInsensitive()) { path = builder.lower(path);//from w w w. ja v a 2 s . c o m attrValue = ((String) attrValue).toLowerCase(LocaleContextHolder.getLocale()); } switch (searchMode != null ? searchMode : sp.getSearchMode()) { case EQUALS: return builder.equal(path, attrValue); case ENDING_LIKE: return builder.like(path, "%" + attrValue); case STARTING_LIKE: return builder.like(path, attrValue + "%"); case ANYWHERE: return builder.like(path, "%" + attrValue + "%"); case LIKE: return builder.like(path, (String) attrValue); // assume user provide the wild cards default: throw new IllegalStateException("expecting a search mode!"); } }
From source file:com.dbs.sdwt.jpa.JpaUtil.java
public <E> Predicate stringPredicate(Expression<String> path, Object attrValue, SearchMode searchMode, SearchParameters sp, CriteriaBuilder builder) { if (sp.isCaseInsensitive()) { path = builder.lower(path);/*from w w w . j a v a2 s.c om*/ attrValue = ((String) attrValue).toLowerCase(LocaleContextHolder.getLocale()); } switch (searchMode != null ? searchMode : sp.getSearchMode()) { case EQUALS: return builder.equal(path, attrValue); case ENDING_LIKE: return builder.like(path, "%" + attrValue); case STARTING_LIKE: return builder.like(path, attrValue + "%"); case ANYWHERE: return builder.like(path, "%" + attrValue + "%"); case LIKE: return builder.like(path, (String) attrValue); // assume user provide the wild cards case NOT_IN: return builder.notLike(path, (String) attrValue); default: throw new IllegalStateException("expecting a search mode!"); } }
From source file:com.sfs.captor.controller.RequirementAction.java
/** * find tests associated with this artifact *///w w w .j av a2s . c o m private List<RequirementTest> findRequirementTests(Requirement requirement) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<RequirementTest> c = cb.createQuery(RequirementTest.class); Root<RequirementTest> obj = c.from(RequirementTest.class); c.select(obj).where(cb.equal(obj.get("requirement"), requirement)); return em.createQuery(c).getResultList(); }
From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java
@Override public Specification<Device> forManufacturer(final Manufacturer manufacturer) throws ArgumentNullOrEmptyException { if (manufacturer == null) { throw new ArgumentNullOrEmptyException("manufacturer"); }/* w ww . j a v a2 s . c o m*/ return new Specification<Device>() { @Override public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) { final Subquery<Long> subquery = query.subquery(Long.class); final Root<DeviceModel> deviceModelRoot = subquery.from(DeviceModel.class); subquery.select(deviceModelRoot.get("id").as(Long.class)); subquery.where(cb.equal(cb.upper(deviceModelRoot.get("manufacturerId").<String>get("name")), manufacturer.getName().toUpperCase())); return cb.in(deviceRoot.get("deviceModel").get("id").as(Long.class)).value(subquery); } }; }
From source file:com.ims.service.SupportingDataService.java
/** * SQL: select * from ims_product_info where CATEGORY_CODE='xxx' and PRODUCT_CODE like '%yyy%' and (CUST_PROD_CODE like '%aaa%' or CUST_PROD_SECOND_CODE like '%aaa%') * ??//w w w. ja v a2 s . c o m * * @param prodSearchCriteria * @return */ public List<ProductInfo> findProductInfoListFrom(final ProdSearchCriteria prodSearchCriteria) { Specification<ProductInfo> speci = new Specification<ProductInfo>() { @Override public Predicate toPredicate(Root<ProductInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> predicates = new ArrayList<Predicate>(); Path<String> custProdCode = root.<String>get(ProductInfo.CUST_PROD_CODE); Path<String> custProdCode2 = root.<String>get(ProductInfo.CUST_SEC_PROD_CODE); Predicate orClause = null; if (StringUtils.isNotBlank(prodSearchCriteria.getProdCategoryCode())) { predicates.add(cb.equal(root.get(ProductInfo.CATEGORY_CODE), prodSearchCriteria.getProdCategoryCode())); } if (StringUtils.isNotEmpty(prodSearchCriteria.getProdCode())) { predicates.add(cb.like(root.<String>get(ProductInfo.PRODUCT_CODE), getLikeString(prodSearchCriteria.getProdCode()))); } if (StringUtils.isNotBlank(prodSearchCriteria.getCustProdCode())) { //??orpredicates?where? query.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); //Predicate p1 = cb.or(cb.like(custProdCode, getLikeString(prodSearchCriteria.getCustProdCode())), cb.like(custProdCode2, getLikeString(prodSearchCriteria.getCustProdCode()))); //predicates.add(p1); //??orPredicate?where????query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])),orClause); String param = getLikeString(prodSearchCriteria.getCustProdCode()); orClause = cb.or(cb.like(custProdCode, param), cb.like(custProdCode2, param)); } //select * from ims_product_info where CATEGORY_CODE=? and (PRODUCT_CODE like ?) and (CUST_PROD_CODE like ? or CUST_PROD_SECOND_CODE like ?) //?? //query.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); //?? if (orClause != null) { query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])), orClause); } else { query.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); } return null; } }; return productInfoRepository.findAll(speci); }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a predicate based on a "Compare" filter * // w ww .j a va2s . c o m * @param builder * @param root * @param filter * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) private static Predicate createComparePredicate(CriteriaBuilder builder, Root<?> root, Filter filter) { Compare compare = (Compare) filter; Expression<Comparable> property = (Expression) getPropertyPath(root, compare.getPropertyId()); Object value = compare.getValue(); // number representation may contain locale specific separators. // Here, we remove // those and make sure a period is used in all cases if (value instanceof String) { // strip out any "%" sign from decimal fields value = ((String) value).replace('%', ' ').trim(); String str = (String) value; if (str != null && org.apache.commons.lang.StringUtils .isNumeric(str.replaceAll("\\.", "").replaceAll(",", ""))) { // first remove all periods (which may be used as // thousand // separators), then replace comma by period str = str.replaceAll("\\.", "").replace(',', '.'); value = str; } } switch (compare.getOperation()) { case EQUAL: return builder.equal(property, value); case GREATER: return builder.greaterThan(property, (Comparable) value); case GREATER_OR_EQUAL: return builder.greaterThanOrEqualTo(property, (Comparable) value); case LESS: return builder.lessThan(property, (Comparable) value); case LESS_OR_EQUAL: return builder.lessThanOrEqualTo(property, (Comparable) value); default: return null; } }
From source file:dao.jpa.TestJpaDao.java
@Test @Transactional/*from w ww. j av a 2 s . c o m*/ public void testCopy() { EntityManager em = bookDao.getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> criteria = cb.createQuery(Book.class); // Fetch join Root<Book> root = criteria.from(Book.class); Path<String> path = root.join("author").<String>get("name"); root.fetch("author"); criteria.select(root); // SubQuery Subquery<String> sq = criteria.subquery(String.class); Root<Author> author = sq.from(Author.class); sq.select(author.<String>get("name")); sq.where(cb.equal(author.<String>get("name"), "Rod")); criteria.where(cb.in(path).value(sq)); CriteriaQuery<Book> copy = cb.createQuery(Book.class); JpaUtils.copyCriteria(criteria, copy); List<Book> copyBooks = em.createQuery(copy).getResultList(); List<Book> books = em.createQuery(criteria).getResultList(); assertEquals(books, copyBooks); }
From source file:com.sfs.captor.controller.RequirementAction.java
/** * find tests associated with this artifact */// w ww . j a v a 2s .c o m private List<RequirementRuleTest> findRequirementRuleTests(RequirementRule requirementRule) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<RequirementRuleTest> c = cb.createQuery(RequirementRuleTest.class); Root<RequirementRuleTest> obj = c.from(RequirementRuleTest.class); c.select(obj).where(cb.equal(obj.get("requirementRule"), requirementRule)); return em.createQuery(c).getResultList(); }
From source file:eu.uqasar.service.dataadapter.JiraDataService.java
/** * /*from ww w.java2 s. c o m*/ * @param adapter * @return */ public int countAllByAdapter(AdapterSettings adapter) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class); Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class); Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.adapter), adapter); query.where(condition); return em.createQuery(query).getResultList().size(); }
From source file:com.eu.evaluation.server.dao.AbstractDAO.java
public boolean isUnique(T entity, String... propertys) { if (propertys == null || propertys.length == 0) { return true; }//from ww w. j a v a 2 s . c om try { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = cb.createQuery(entityClass); Root<T> root = criteriaQuery.from(entityClass); Predicate predicate = null; for (String property : propertys) { if (predicate == null) { predicate = cb.equal(root.get(property), PropertyUtils.getProperty(entity, property)); } else { predicate = cb.and(predicate, cb.equal(root.get(property), PropertyUtils.getProperty(entity, property))); } } if (!StringUtils.isBlank(entity.getId())) { predicate = cb.and(predicate, cb.notEqual(root.get("id"), entity.getId())); } criteriaQuery.where(predicate); TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery); List<T> result = typedQuery.getResultList(); return result.isEmpty(); } catch (Exception e) { e.printStackTrace(); ReflectionUtils.handleReflectionException(e); } return false; }