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:nc.noumea.mairie.organigramme.core.dao.PersistentManager.java

@SuppressWarnings("unchecked")
private TypedQuery<T> constructTypedQueryByPropertyOrderBy(Class<? extends T> classe, String property,
        Object value, String orderByProperty) {
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<T> c = (CriteriaQuery<T>) qb.createQuery(classe);
    Root<T> p = (Root<T>) c.from(classe);
    Predicate condition = qb.equal(p.get(property), value);
    c.where(condition);//from   w  w  w . ja v  a  2  s.  c  o  m
    if (orderByProperty != null) {
        c.orderBy(qb.asc(p.get(orderByProperty)));
    }
    TypedQuery<T> q = em.createQuery(c);
    return q;
}

From source file:com.fantasy.stataggregator.workers.StatCompilerTask.java

@Override
public void setYear(int year) throws Exception {
    this.year = year;

    GameDataRepository gdr = ctx.getBean(GameDataRepository.class);
    if (year == Integer.MAX_VALUE) {
        gamesData = gdr.findAll();/*ww w  .j av  a  2s  . c om*/
    } else {
        SimpleDateFormat sdf = ctx.getBean(SimpleDateFormat.class);
        sdf.applyLocalizedPattern("yyyyMMdd");

        Date min = sdf.parse(year + START_OF_YEAR);
        Date max = sdf.parse(year + END_OF_YEAR);

        CriteriaBuilder cb = gdr.getCriteriaBuilder();
        CriteriaQuery<GameData> cq = gdr.getCriteriaQuery();
        Root<GameData> gameData = gdr.getRoot();
        Path<GameDataPK> pk = gameData.get(GameData_.gameDataPK);
        cq.select(gameData).where(cb.equal(pk.get(GameDataPK_.year), year))
                .orderBy(cb.asc(pk.get(GameDataPK_.gameIdentifier)));

        gamesData = gdr.getCriteriaList(cq);
        System.out.println(gamesData.size());
    }
}

From source file:eu.uqasar.service.ProductService.java

/**
 * /*from w  ww . ja  va2s  . co m*/
 * @param productId
 * @return
 */
public boolean productExists(Long productId) {
    logger.info(String.format("checking if product with ID %d exists ...", productId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Product> from = criteria.from(Product.class);
    criteria.where(cb.equal(from.get(Product_.id), productId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult() == 1);
}

From source file:com.ims.service.OrderService.java

public List<PurchaseOrder> searchPurchaseOrderList(final OrderSearchCriteria orderSearchCriteria) {
    List<PurchaseOrder> purchaseOrders = new ArrayList<PurchaseOrder>();
    Specification<PurchaseOrder> speci = new Specification<PurchaseOrder>() {
        @Override/* www  .  j av a2s  . co  m*/
        public Predicate toPredicate(Root<PurchaseOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> predicates = new ArrayList<Predicate>();
            if (StringUtils.isNotEmpty(orderSearchCriteria.getPurchaseOrderStatus())) {
                predicates.add(cb.equal(root.get("status"), orderSearchCriteria.getPurchaseOrderStatus()));
            }

            if (StringUtils.isNotEmpty(orderSearchCriteria.getPurchaseOrderNum())) {
                predicates.add(cb.like(root.<String>get("purchaseOrderNumber"),
                        "%" + orderSearchCriteria.getPurchaseOrderNum() + "%"));
            }

            if (StringUtils.isNotEmpty(orderSearchCriteria.getProformaInvoiceNum())) {
                predicates.add(cb.like(root.<String>get("piNumber"),
                        "%" + orderSearchCriteria.getProformaInvoiceNum() + "%"));
            }

            query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])))
                    .orderBy(cb.desc(root.get("changeLog").get("createdDate")));
            return null;
        }
    };

    return purchaseOrderRepository.findAll(speci);
}

From source file:de.egore911.hexanalyzer.persistence.selector.PluginSelector.java

@Nonnull
@Override//from ww  w. j  ava 2 s . c  o m
protected List<Predicate> generatePredicateList(@Nonnull CriteriaBuilder builder,
        @Nonnull Root<PluginEntity> from, @Nonnull CriteriaQuery<?> criteriaQuery) {
    List<Predicate> predicates = super.generatePredicateList(builder, from, criteriaQuery);

    if (extensionPoint != null) {
        builder.equal(from.get(PluginEntity_.extensionPoint), extensionPoint);
    }

    if (StringUtils.isNotEmpty(search)) {
        String likePattern = '%' + search + '%';
        predicates.add(builder.or(builder.like(from.get(PluginEntity_.qualifiedName), likePattern),
                builder.like(from.get(PluginEntity_.source), likePattern)));
    }

    return predicates;
}

From source file:csns.model.core.dao.jpa.UserDaoImpl.java

@Override
public List<User> getUsers(Long ids[]) {
    if (ids == null || ids.length < 1)
        return new ArrayList<User>();

    CriteriaBuilder cbuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> cquery = cbuilder.createQuery(User.class);
    Root<User> user = cquery.from(User.class);

    Predicate criteria = cbuilder.equal(user.get("id"), ids[0]);
    for (int i = 1; i < ids.length; ++i)
        criteria = cbuilder.or(criteria, cbuilder.equal(user.get("id"), ids[i]));
    cquery.where(criteria);//  w ww.  j  ava  2s .  c o m

    cquery.orderBy(cbuilder.asc(user.get("lastName")), cbuilder.asc(user.get("firstName")));

    return entityManager.createQuery(cquery).getResultList();
}

From source file:org.sloth.persistence.impl.UserDaoImpl.java

@Override
public User getByMail(String mail) {
    if (mail == null) {
        throw new NullPointerException();
    }/* ww w. j a  v a2  s .  c o m*/
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> user = cq.from(User.class);
    cq.select(user).where(cb.equal(user.get(User_.mail), mail));
    User result = null;
    try {
        result = getEntityManager().createQuery(cq).getSingleResult();
    } catch (NoResultException e) {
        logger.info("User with address {} not found", mail);
    } catch (NonUniqueResultException e) {
        logger.warn("Corrupt Database", e);
    }
    return result;
}

From source file:com.zergiu.tvman.dao.impl.TVShowDaoImpl.java

@Override
public Long getActiveTVShowsCount() {
    CriteriaBuilder criteria = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = criteria.createQuery(Long.class);
    Root<TVShow> show = query.from(TVShow.class);
    query.select(criteria.countDistinct(show));
    query.where(criteria.equal(show.<TVShowStatus>get("status"), TVShowStatus.Continuing));
    Long count = em.createQuery(query).getSingleResult();
    log.debug("active count: " + count);
    return count;
}

From source file:org.wallride.repository.CategoryRepositoryImpl.java

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<Category> root = query.from(Category.class);
    query.select(root.get(Category_.id));
    query.where(cb.equal(root.get(Category_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

From source file:com.samples.platform.serviceprovider.library.internal.dao.PlatformDao.java

/**
 * @param ISBN//from w  w w  . j av  a 2 s.  c o  m
 *            the ISBN of the book to find.
 * @return the {@link BookType}.
 */
@Transactional(value = PersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public BookType getBookByISBN(final String ISBN) {
    BookType m = null;
    if (ISBN == null) {
        this.logger.debug("getBookByISBN: ISBN is null.");
    } else {
        CriteriaBuilder cb = this.em.getCriteriaBuilder();
        CriteriaQuery<BookType> q = cb.createQuery(BookType.class);
        Root<BookType> c = q.from(BookType.class);
        q.where(cb.equal(c.<String>get(BookType_.ISBN), ISBN));
        TypedQuery<BookType> typedQuery = this.em.createQuery(q);
        try {
            m = typedQuery.getSingleResult();
            this.logger.debug("getBookByISBN: " + ToStringBuilder.reflectionToString(m));
        } catch (NoResultException e) {
            this.logger.debug("getBookByISBN: non value found for ISBN=" + ISBN);
            m = null;
        }
    }
    return m;
}