Example usage for javax.persistence.criteria CriteriaBuilder equal

List of usage examples for javax.persistence.criteria CriteriaBuilder equal

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder equal.

Prototype

Predicate equal(Expression<?> x, Object y);

Source Link

Document

Create a predicate for testing the arguments for equality.

Usage

From source file:net.shopxx.dao.impl.ArticleDaoImpl.java

public Page<Article> findPage(ArticleCategory articleCategory, Tag tag, Boolean isPublication,
        Pageable pageable) {/*from   ww  w  . jav  a  2 s.  co m*/
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);
    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 (pageable == null
            || ((StringUtils.isEmpty(pageable.getOrderProperty()) || pageable.getOrderDirection() == null)
                    && CollectionUtils.isEmpty(pageable.getOrders()))) {
        criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")),
                criteriaBuilder.desc(root.get("createDate")));
    }
    return super.findPage(criteriaQuery, pageable);
}

From source file:net.shopxx.dao.impl.ArticleDaoImpl.java

public List<Article> findList(ArticleCategory articleCategory, Boolean isPublication,
        Article.GenerateMethod generateMethod, Date beginDate, Date endDate, Integer first, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);//from w  w w . jav a2s .  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 (isPublication != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isPublication"), isPublication));
    }
    if (generateMethod != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("generateMethod"), generateMethod));
    }
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, first, count, null, null);
}

From source file:ch.sdi.plugins.oxwall.job.OxSqlJob.java

/**
 * Checks if the given hash is present in the ow_base_avatar table
 *
 * @param aHash/*from  ww w  . j a v  a  2 s . co  m*/
 * @return
 */
public boolean isAvatarHashPresent(Long aHash) {
    if (myDryRun) {
        myLog.debug("DryRun is active. Not checking for duplicate avatar hash");
        return false;
    } // if myDryRun

    CriteriaBuilder cb = myEntityManager.getCriteriaBuilder();

    CriteriaQuery<OxAvatar> criteria = cb.createQuery(OxAvatar.class);
    Root<OxAvatar> root = criteria.from(OxAvatar.class);
    ParameterExpression<Long> avatarParam = cb.parameter(Long.class);
    avatarParam = cb.parameter(Long.class);
    criteria.select(root).where(cb.equal(root.get("hash"), avatarParam));
    TypedQuery<OxAvatar> query = myEntityManager.createQuery(criteria);
    query.setParameter(avatarParam, aHash);
    List<OxAvatar> results = query.getResultList();

    if (results.size() > 0) {
        myLog.debug("given avatar hash is already present: " + aHash);
        return true;
    } // if results.size() > 0

    return false;
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * subquery(equivalent to dowhere1) : exists
 * SELECT e FROM jpa_query_employee e WHERE EXISTS (SELECT p FROM e.projects p WHERE p.name = :projectname)
 *///from  w  ww.  j a v a 2s  .c o m
@Transactional
public void doWhere2() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> e = c.from(Employee.class);

    // subquery
    Subquery<Project> sq = c.subquery(Project.class);
    // Root<Project> p = sq.from(Project.class);
    Root<Employee> se = sq.correlate(e);
    Join<Employee, Project> p = se.join("projects");
    sq.select(p).where(cb.equal(p.get("name"), cb.parameter(String.class, "projectname")));

    //
    c.select(e).where(cb.exists(sq));
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the list of serviceIds available.
 *
 * @param modelVersion//from  www .  ja  va  2s . c o m
 *            the model version.
 * @return the list of serviceIds available.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<String> getServiceIds(final String modelVersion) {
    final List<String> value = new ArrayList<String>();
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<ServiceType> q = cb.createQuery(ServiceType.class);
    final Root<ServiceType> f = q.from(ServiceType.class);
    q.where(cb.equal(f.<String>get(ServiceType_.modelVersion), modelVersion));
    q.orderBy(cb.asc(f.<String>get(ServiceType_.serviceId)), cb.asc(f.<String>get(ServiceType_.namespace)));
    final TypedQuery<ServiceType> typedQuery = this.em.createQuery(q);
    final List<ServiceType> services = typedQuery.getResultList();
    services.stream().filter(s -> Objects.nonNull(s.getServiceId())).forEach(s -> value.add(s.getServiceId()));
    return value;
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the list of target name spaces available.
 *
 * @param modelVersion/*from   www  .  j  a  va2  s  .c om*/
 *            the model version.
 * @return the list of target name spaces available.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<String> getTargetNamespaces(final String modelVersion) {
    final List<String> value = new ArrayList<String>();
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<ClusterType> q = cb.createQuery(ClusterType.class);
    final Root<ClusterType> f = q.from(ClusterType.class);
    q.where(cb.equal(f.<String>get(ClusterType_.modelVersion), modelVersion));
    q.orderBy(cb.asc(f.<String>get(ClusterType_.name)), cb.asc(f.<String>get(ClusterType_.id)));
    final TypedQuery<ClusterType> typedQuery = this.em.createQuery(q);
    final List<ClusterType> cluster = typedQuery.getResultList();
    cluster.stream().filter(c -> Objects.nonNull(c.getName())).forEach(c -> value.add(c.getName()));
    return value;
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the {@link ClusterType} with targetNamespace of the modelVersion.
 *
 * @param modelVersion/*w w  w  .  ja v  a  2 s.  c  o  m*/
 *            the model version.
 * @param targetNamespace
 *            the target namespace.
 * @return the {@link ClusterType} with targetNamespace of the modelVersion.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public ClusterType getClusterByTargetNamespace(final String modelVersion, final String targetNamespace) {
    ClusterType value = null;
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<ClusterType> q = cb.createQuery(ClusterType.class);
    final Root<ClusterType> f = q.from(ClusterType.class);
    q.where(cb.equal(f.<String>get(ClusterType_.modelVersion), modelVersion),
            cb.equal(f.<String>get(ClusterType_.name), targetNamespace));
    final TypedQuery<ClusterType> typedQuery = this.em.createQuery(q);
    final List<ClusterType> list = typedQuery.getResultList();
    if (Objects.nonNull(list) && list.size() != 1) {
        value = list.get(0);
        EagerLoader.load(value);
    }
    return value;
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the {@link ServiceType} with serviceId of the modelVersion.
 *
 * @param modelVersion//from   ww w. j  a  v a  2 s .  c o  m
 *            the model version.
 * @param serviceId
 *            the serviceId.
 * @return the {@link ServiceType} with serviceId of the modelVersion.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public ServiceType getServiceByServiceId(final String modelVersion, final String serviceId) {
    ServiceType value = null;
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<ServiceType> q = cb.createQuery(ServiceType.class);
    final Root<ServiceType> f = q.from(ServiceType.class);
    q.where(cb.equal(f.<String>get(ServiceType_.modelVersion), modelVersion),
            cb.equal(f.<String>get(ServiceType_.serviceId), serviceId));
    final TypedQuery<ServiceType> typedQuery = this.em.createQuery(q);
    final List<ServiceType> list = typedQuery.getResultList();
    if (Objects.nonNull(list) && list.size() == 1) {
        value = list.get(0);
        EagerLoader.load(value);
    }
    return value;
}

From source file:de.ks.idnadrev.information.view.InformationOverviewDS.java

private List<InformationPreviewItem> getResults(String name, List<String> tagNames, Category category,
        EntityManager em, CriteriaBuilder builder, Class<? extends Information<?>> clazz) {
    CriteriaQuery<InformationPreviewItem> query = builder.createQuery(InformationPreviewItem.class);
    Root<? extends Information<?>> root = query.from(clazz);

    ArrayList<Predicate> filters = new ArrayList<>();
    if (!name.isEmpty()) {
        filters.add(builder.like(builder.lower(root.<String>get(KEY_NAME)), name));
    }//from  w w w  . j  a v  a2 s .  co  m
    if (!tagNames.isEmpty()) {
        List<Tag> tags = getTags(tagNames, em);
        SetJoin<TextInfo, Tag> tagJoin = root.joinSet(KEY_TAGS);
        filters.add(tagJoin.in(tags));
    }
    if (category != null) {
        filters.add(builder.equal(root.get(KEY_CATEGORY), category));
    }
    query.distinct(true);

    query.where(filters.toArray(new Predicate[filters.size()]));
    query.select(
            builder.construct(InformationPreviewItem.class, root.get(KEY_NAME), root.get(KEY_CREATIONTIME)));
    List<InformationPreviewItem> resultList = em.createQuery(query).getResultList();
    return resultList;
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Long waitingShippingCount(Member member) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);//  www .  j av a2s.co m
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.cancelled),
            criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.paid),
            criteriaBuilder.equal(root.get("shippingStatus"), ShippingStatus.unshipped));
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
            criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}