List of usage examples for javax.persistence.criteria Root get
<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);
From source file:com.sfs.captor.controller.RequirementAction.java
/** * find tests associated with this artifact *//*from w w w . j a va 2 s .co 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 w w w .ja va 2 s.c o m*/ * @param metric * @param date * @return * @throws uQasarException */ public int countMeasurementsByMetricAndDate(String metric, Date date) throws uQasarException { logger.info("Count measurements for metric: " + metric + " and date: " + date); 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_.jiraMetric), metric); Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.timeStamp), date); Predicate condition3 = cb.and(condition, condition2); query.where(condition3); query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp))); Integer res = em.createQuery(query).getResultList().size(); logger.info("Results' count: " + res); return res; }
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 w ww . jav a 2 s .co m*/ 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; }
From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.ConferenceUserDaoImpl.java
@Override public void afterPropertiesSet() throws Exception { this.emailParameter = this.createParameterExpression(String.class, "email"); this.getUsersByPrimaryEmailQuery = this .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<ConferenceUserImpl>>() { @Override/*from w w w . jav a 2 s. c om*/ public CriteriaQuery<ConferenceUserImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<ConferenceUserImpl> criteriaQuery = cb .createQuery(ConferenceUserImpl.class); final Root<ConferenceUserImpl> definitionRoot = criteriaQuery .from(ConferenceUserImpl.class); criteriaQuery.select(definitionRoot); criteriaQuery .where(cb.equal(definitionRoot.get(ConferenceUserImpl_.email), emailParameter)); return criteriaQuery; } }); this.getUsersByAnyEmailQuery = this .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<ConferenceUserImpl>>() { @Override public CriteriaQuery<ConferenceUserImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<ConferenceUserImpl> criteriaQuery = cb .createQuery(ConferenceUserImpl.class); final Root<ConferenceUserImpl> definitionRoot = criteriaQuery .from(ConferenceUserImpl.class); criteriaQuery.select(definitionRoot); criteriaQuery.where( cb.or(cb.equal(definitionRoot.get(ConferenceUserImpl_.email), emailParameter), cb.isMember(emailParameter, definitionRoot.get(ConferenceUserImpl_.additionalEmails)))); return criteriaQuery; } }); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * use customize object in select// w ww .j a v a 2 s. com */ public void doSelect3() { CriteriaBuilder cb = em.getCriteriaBuilder(); // return type is a customized object CriteriaQuery<EmployeeBasicInfo> c = cb.createQuery(EmployeeBasicInfo.class); Root<Employee> e = c.from(Employee.class); // method 1 : must use multiple select, cannot use select method, because of strong type check c.multiselect(e.get("name")); showResult(c); // method 2 : use select and cb.construct c.select(cb.construct(EmployeeBasicInfo.class, e.get("name"))); showResult(c); // method 3 : use multiple select and cb.construct // NOTE : UNSUPPORT! // CriteriaQuery<EmployeeBasicInfo> c2 = cb.createQuery(EmployeeBasicInfo.class); // Root<Employee> e2 = c2.from(Employee.class); // c2.multiselect(cb.construct(EmployeeBasicInfo.class, e2.get("name"))); // showResult(c2); }
From source file:eu.uqasar.service.dataadapter.JiraDataService.java
/** * /* ww w. j a v a 2 s . c o m*/ * @param metric * @return * @throws uQasarException */ public int countMeasurementsPerProjectByMetricWithinPeriod(Project project, String metric, String period) throws uQasarException { logger.info("Count measurements for metric: " + metric); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class); Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class); Predicate condition1 = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric); Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.project), project); Date from = getDateForPeriod(period); Date to = DateTime.now().toDate(); Predicate condition3 = cb.between(root.get(JiraMetricMeasurement_.timeStamp), from, to); Predicate condition4 = cb.and(condition1, condition2, condition3); query.where(condition4); query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp))); Integer res = em.createQuery(query).getResultList().size(); logger.info("Results' count: " + res); return res; }
From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java
/** * Adds the sorting./* w w w . j a v a2 s .co m*/ * * @param sortField the sort field * @param sortOrder the sort order * @param query the query * @param builder the builder * @param root the root */ private void addSorting(String sortField, String sortOrder, CriteriaQuery<User> query, CriteriaBuilder builder, Root<User> root) { boolean isSet = FALSE; if (CommonUtil.isNotNull(sortField) && !sortField.contains(FIELDCONSTANT_CARDNUMBER) && !sortField.contains(FIELDCONSTANT_CREDITLIMIT) && !sortField.contains(FIELDCONSTANT_BALANCE)) { isSet = TRUE; if (sortOrder.startsWith(SORT_ORDER_ASCENDING)) { query.orderBy(builder.asc(root.get(sortField))); } else { query.orderBy(builder.desc(root.get(sortField))); } } if (!isSet) { query.orderBy(builder.desc(root.get(FIELD_CONSTANT_CREATED_AT))); } }
From source file:eu.uqasar.service.dataadapter.JiraDataService.java
/** * @param metric/*from www . ja va2s.co m*/ * @param project * @return * @throws uQasarException */ public List<JiraMetricMeasurement> getMeasurementsPerProjectByMetricWithLatestDate(Project project, String metric) throws uQasarException { logger.info("Count measurements for project + " + project.getAbbreviatedName() + " and metric: " + metric + "with latest date"); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class); Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class); Predicate condition1 = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric); Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.project), project); Predicate condition4 = cb.and(condition1, condition2); query.where(condition4); query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp))); List<JiraMetricMeasurement> returnResults = em.createQuery(query).getResultList(); Date newDate = null; for (JiraMetricMeasurement jMM : returnResults) { if (newDate == null || jMM.getTimeStamp().compareTo(newDate) > 0) { newDate = jMM.getTimeStamp(); } } return getMeasurementsByMetricAndDate(metric, newDate); }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private Product getProduct_Mercifully() { return transactionTemplate.execute(new TransactionCallback<Product>() { @Override//from w ww . j ava 2s .c om public Product doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> query = cb.createQuery(Product.class); Root<Product> productRoot = query.from(Product.class); Join<Product, WarehouseProductInfo> warehouseProductInfoJoin = productRoot .join(Product_.warehouseProductInfo); query.select(productRoot).where(cb.and(cb.equal(productRoot.get(Product_.code), "tvCode"), cb.gt(warehouseProductInfoJoin.get(WarehouseProductInfo_.quantity), 50))); return entityManager.createQuery(query).getSingleResult(); } }); }
From source file:org.oncoblocks.centromere.jpa.QueryCriteriaSpecification.java
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { String key = queryCriteria.getKey(); Object value = queryCriteria.getValue(); Evaluation eval = queryCriteria.getEvaluation(); Path path = null;// w ww .jav a 2 s. c o m if (key.contains(".")) { String[] bits = key.split("\\."); path = root.join(bits[0]).get(bits[1]); } else { path = root.get(key); } logger.debug(String.format("[CENTROMERE] Converting QueryCriteria to JPA specification: %s", queryCriteria.toString())); switch (eval) { case EQUALS: return criteriaBuilder.equal(path, value); case NOT_EQUALS: return criteriaBuilder.notEqual(path, value); case IN: return path.in(value); case NOT_IN: return criteriaBuilder.not(path.in(value)); case IS_NULL: return criteriaBuilder.isNull(path); case NOT_NULL: return criteriaBuilder.isNotNull(path); case GREATER_THAN: return criteriaBuilder.greaterThan(path, value.toString()); case GREATER_THAN_EQUALS: return criteriaBuilder.greaterThanOrEqualTo(path, value.toString()); case LESS_THAN: return criteriaBuilder.lessThan(path, value.toString()); case LESS_THAN_EQUALS: return criteriaBuilder.lessThanOrEqualTo(path, value.toString()); case BETWEEN: return criteriaBuilder.and(criteriaBuilder.greaterThan(path, ((List<?>) value).get(0).toString()), criteriaBuilder.lessThan(path, ((List<?>) value).get(1).toString())); case OUTSIDE: return criteriaBuilder.or(criteriaBuilder.greaterThan(path, ((List<?>) value).get(1).toString()), criteriaBuilder.lessThan(path, ((List<?>) value).get(0).toString())); case BETWEEN_INCLUSIVE: return criteriaBuilder.and( criteriaBuilder.greaterThanOrEqualTo(path, ((List<?>) value).get(0).toString()), criteriaBuilder.lessThanOrEqualTo(path, ((List<?>) value).get(1).toString())); case OUTSIDE_INCLUSIVE: return criteriaBuilder.or( criteriaBuilder.greaterThanOrEqualTo(path, ((List<?>) value).get(1).toString()), criteriaBuilder.lessThanOrEqualTo(path, ((List<?>) value).get(0).toString())); case LIKE: return criteriaBuilder.like(path, "%" + value.toString() + "%"); case NOT_LIKE: return criteriaBuilder.notLike(path, "%" + value.toString() + "%"); case STARTS_WITH: return criteriaBuilder.like(path, value.toString() + "%"); case ENDS_WITH: return criteriaBuilder.like(path, "%" + value.toString()); default: return criteriaBuilder.equal(root.get(queryCriteria.getKey()), queryCriteria.getValue()); } }