List of usage examples for javax.persistence.criteria CriteriaBuilder equal
Predicate equal(Expression<?> x, Object y);
From source file:org.businessmanager.dao.GenericDaoImpl.java
public List<T> findByAssignedEntity(SetAttribute<T, ?> setAttribute, Long entityId) { CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass()); Root<T> rootQuery = criteriaQuery.from(getPersistenceClass()); CriteriaQuery<T> select = criteriaQuery.select(rootQuery); Join<T, ?> memberJoin = rootQuery.join(setAttribute); Path<?> nameField = memberJoin.get("id"); Predicate nameEquals = queryBuilder.equal(nameField, entityId); criteriaQuery.where(nameEquals);/*from w ww . j a v a 2 s . co m*/ TypedQuery<T> typedQuery = getEntityManager().createQuery(select); return typedQuery.getResultList(); }
From source file:org.businessmanager.dao.GenericDaoImpl.java
public List<T> findByAssignedEntity(ListAttribute<T, ?> listAttribute, Long entityId) { CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass()); Root<T> rootQuery = criteriaQuery.from(getPersistenceClass()); CriteriaQuery<T> select = criteriaQuery.select(rootQuery); Join<T, ?> memberJoin = rootQuery.join(listAttribute); Path<?> nameField = memberJoin.get("id"); Predicate nameEquals = queryBuilder.equal(nameField, entityId); criteriaQuery.where(nameEquals);// w w w. jav a 2s . co m TypedQuery<T> typedQuery = getEntityManager().createQuery(select); return typedQuery.getResultList(); }
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;/*from w w w .ja v 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()); } }
From source file:eu.uqasar.service.user.UserService.java
public User getByFullNameWithUsername(final String fullNameWithUsername) { logger.infof("loading User with fullName and username %s ...", fullNameWithUsername); User entity = null;/*from w w w . ja v a 2s . co m*/ CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> query = cb.createQuery(User.class); Root<User> root = query.from(User.class); Expression<String> fullName = cb.concat(cb.concat(root.get(User_.firstName), " "), root.get(User_.lastName)); Expression<String> userName = cb.concat(" (", cb.concat(root.get(User_.userName), ")")); query.where(cb.equal(cb.lower(cb.concat(fullName, userName)), fullNameWithUsername.toLowerCase())); List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList(); if (!resultList.isEmpty()) { return resultList.get(0); } return entity; }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * subquery: in// w ww . ja va 2 s . c o m * SELECT e FROM jpa_query_employee e WHERE e IN (SELECT emp FROM jpa_query_project p JOIN p.employees pe WHERE p.name = :projectname) */ @Transactional public void doWhere1() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); // subquery Subquery<Employee> sq = c.subquery(Employee.class); Root<Project> p = sq.from(Project.class); Join<Project, Employee> pe = p.join("employees"); sq.select(pe).where(cb.equal(p.get("name"), cb.parameter(String.class, "projectname"))); // c.select(e).where(cb.in(e).value(sq)); }
From source file:com.qpark.eip.core.spring.statistics.dao.StatisticsLoggingDao.java
/** * Add the {@link SystemUserLogType} to the database. * * @param log//w w w. jav a2 s.c o m * the {@link SystemUserLogType} to add. */ private void addChannelInvocation(final SystemUserLogType log) { /* Setup context and version. */ log.setContext(this.getContextName()); log.setVersion(this.getContextVersion()); if (log.getUserName() != null && log.getUserName().trim().length() == 0) { log.setUserName(null); } /* Setup to search existing one. */ final CriteriaBuilder cb = this.em.getCriteriaBuilder(); final CriteriaQuery<SystemUserLogType> q = cb.createQuery(SystemUserLogType.class); final Root<SystemUserLogType> c = q.from(SystemUserLogType.class); final List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.equal(c.<String>get(SystemUserLogType_.context), log.getContext())); predicates.add(cb.equal(c.<String>get(SystemUserLogType_.version), log.getVersion())); if (log.getUserName() == null) { predicates.add(cb.isNull(c.<String>get(SystemUserLogType_.userName))); } else { predicates.add(cb.equal(c.<String>get(SystemUserLogType_.userName), log.getUserName())); } predicates.add(cb.equal(c.<String>get(SystemUserLogType_.serviceName), log.getServiceName())); predicates.add(cb.equal(c.<String>get(SystemUserLogType_.operationName), log.getOperationName())); predicates.add(cb.between(c.<Date>get(SystemUserLogType_.logDateItem), getDayStart(log.getLogDateItem()), getDayEnd(log.getLogDateItem()))); q.where(predicates.toArray(new Predicate[predicates.size()])); q.orderBy(cb.desc(c.<Long>get(SystemUserLogType_.hjid))); TypedQuery<SystemUserLogType> typedQuery = this.em.createQuery(q); SystemUserLogType persistence = null; synchronized (StatisticsLoggingDao.class) { try { persistence = typedQuery.getSingleResult(); if (persistence == null) { /* Not found -> persist */ persistence = log; this.setupSystemUserLog(persistence, null); this.em.persist(persistence); } else { /* Found -> add and merge */ this.setupSystemUserLog(persistence, log); this.em.merge(persistence); } } catch (final NoResultException e) { /* Not found -> persist */ persistence = log; this.setupSystemUserLog(persistence, null); this.em.persist(persistence); } catch (final NonUniqueResultException e) { /* Found more */ typedQuery = this.em.createQuery(q); final List<SystemUserLogType> list = typedQuery.getResultList(); SystemUserLogType l; for (int i = 0; i < list.size(); i++) { l = list.get(i); if (persistence == null && l.getHjid() != null) { persistence = l; break; } } if (persistence != null) { /* Found more -> condense to first valid one -> merge. */ this.setupSystemUserLog(persistence, log); for (int i = list.size() - 1; i >= 0; i--) { l = list.get(i); if (l != null && l.getHjid() != null) { if (persistence.getHjid().equals(l.getHjid())) { } else { this.setupSystemUserLog(persistence, l); list.remove(i); this.em.remove(l); } } } this.em.merge(persistence); } else { /* Found more -> no valid one in list -> persist. */ persistence = log; this.setupSystemUserLog(persistence, null); this.em.persist(persistence); } } } this.logger.debug("addChannelInvocation SystemUserLog {} {} {} {} {} {}", this.contextNameProvider.getContextName(), this.contextNameProvider.getContextVersion(), String.valueOf(persistence.getUserName()), persistence.getServiceName(), persistence.getOperationName(), persistence.getLogDate().toXMLFormat()); }
From source file:com.sfs.ucm.controller.SpecificationAction.java
/** * find tests associated with this artifact *///from w ww .jav a2 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:se.kth.csc.persist.JPAStore.java
@Override public QueuePosition fetchQueuePositionWithQueueAndUser(String queueName, String userName) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<QueuePosition> q = cb.createQuery(QueuePosition.class); Root<QueuePosition> queuePosition = q.from(QueuePosition.class); Join<QueuePosition, Queue> queue = queuePosition.join(QueuePosition_.queue); Join<QueuePosition, Account> account = queuePosition.join(QueuePosition_.account); try {// w w w.j a v a 2 s. co m return entityManager .createQuery( q.select(queuePosition) .where(cb.and(cb.equal(queue.get(Queue_.name), queueName), cb.equal(account.get(Account_.principalName), userName)))) .getSingleResult(); } catch (NoResultException e) { return null; } }
From source file:in.bookmylab.jpa.JpaDAO.java
public List<ResourceBooking> searchResourceBooking(BookingSearchInput si) { EntityManager em = emf.createEntityManager(); try {//from w ww .j ava2s .co m CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<ResourceBooking> cq = cb.createQuery(ResourceBooking.class); Root<ResourceBooking> s = cq.from(ResourceBooking.class); List<Predicate> pred = new ArrayList<Predicate>(); if (si.userId != null) { pred.add(cb.equal(s.<User>get("user").<Integer>get("userId"), si.userId)); } if (si.bookingDateFrom != null) { pred.add(cb.greaterThanOrEqualTo(s.<Date>get("bookingDate"), si.bookingDateFrom)); } if (si.bookingDateTo != null) { pred.add(cb.lessThanOrEqualTo(s.<Date>get("bookingDate"), si.bookingDateTo)); } if (!StringUtils.isEmpty(si.lab)) { pred.add(cb.equal(s.<String>get("lab"), si.lab)); } if (!StringUtils.isEmpty(si.status)) { pred.add(cb.equal(s.<String>get("status"), si.status)); } cq.select(s).where(pred.toArray(new Predicate[] {})); return em.createQuery(cq).getResultList(); } finally { em.close(); } }
From source file:net.shopxx.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, Tag tag, Boolean isPublication, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);//from ww w. j a v a 2s .co m Predicate restrictions = criteriaBuilder.conjunction(); if (articleCategory != null) { Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class); Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory), criteriaBuilder.like(subqueryRoot.<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.in(root.get("articleCategory")).value(subquery)); } if (tag != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.join("tags"), tag)); } if (isPublication != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), isPublication)); } criteriaQuery.where(restrictions); if (orders == null || orders.isEmpty()) { criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")), criteriaBuilder.desc(root.get("createDate"))); } return super.findList(criteriaQuery, null, count, filters, orders); }