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.groupbuy.dao.impl.ReceiverDaoImpl.java

public Page<Receiver> findPage(Member member, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Receiver> criteriaQuery = criteriaBuilder.createQuery(Receiver.class);
    Root<Receiver> root = criteriaQuery.from(Receiver.class);
    criteriaQuery.select(root);/*from   w w w  .  j a  va2 s.c o m*/
    if (member != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    }
    return super.findPage(criteriaQuery, pageable);
}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> isActived(final Boolean activated) throws ArgumentNullOrEmptyException {
    if (activated == null) {
        throw new ArgumentNullOrEmptyException("activated");
    }/*from   ww w.  j  a va 2  s .c  om*/

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {

            return cb.equal(deviceRoot.<Boolean>get("isActivated"), activated);
        }
    };
}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> isInMaintetance(final Boolean inMaintenance) throws ArgumentNullOrEmptyException {
    if (inMaintenance == null) {
        throw new ArgumentNullOrEmptyException("inMaintenance");
    }/*from   w w  w . j a  v a2  s . co  m*/

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {

            return cb.equal(deviceRoot.<Boolean>get("inMaintenance"), inMaintenance);
        }
    };
}

From source file:com.expressui.sample.dao.query.RelatedContactsQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Contact> query,
        Root<Contact> contact) {// ww w .ja v  a 2  s .  co m
    List<Predicate> predicates = new ArrayList<Predicate>();

    if (hasValue(account)) {
        ParameterExpression<Account> accountExp = builder.parameter(Account.class, "account");
        predicates.add(builder.equal(contact.get("account"), accountExp));
    }

    return predicates;
}

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

private List<Predicate> getFilterPredicates(final ProductFilterStructure filter, CriteriaBuilder cb,
        Root<Product> from) {/*from ww w.ja  va  2s . c  o m*/
    List<Predicate> predicates = new ArrayList<>();
    if (filter == null) {
        return predicates;
    }

    if (filter.getReleaseDate() != null) {
        predicates.add(cb.equal(from.get(Product_.releaseDate), filter.getReleaseDate()));
    }
    if (filter.getVersion() != null) {
        predicates.add(cb.equal(from.get(Product_.version), filter.getVersion()));
    }

    if (!StringUtils.isEmpty(filter.getName())) {
        Predicate firstName = cb.like(cb.lower(from.get(Product_.name)),
                LIKE_WILDCARD + filter.getName().toLowerCase() + LIKE_WILDCARD);
        predicates.add(firstName);
    }
    return predicates;
}

From source file:com.qpark.eip.core.spring.auth.dao.AuthorityDao.java

/**
 * Get the total number of calls for that date.
 *
 * @param userName/*ww  w  .j  a v  a 2  s  .c  om*/
 *            the user name.
 * @param serviceName
 *            the service name.
 * @param operationName
 *            the operation name.
 * @param date
 *            the date the calls are recorded.
 * @return the number of calls.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public int getCurrentRequestNumber(final String userName, final String serviceName, final String operationName,
        final Date date) {
    int requests = 0;
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<SystemUserLogType> q = cb.createQuery(SystemUserLogType.class);
    Root<SystemUserLogType> c = q.from(SystemUserLogType.class);
    q.where(cb.equal(c.<String>get(SystemUserLogType_.context), this.contextNameProvider.getContextName()),
            cb.equal(c.<String>get(SystemUserLogType_.userName), userName),
            cb.equal(c.<String>get(SystemUserLogType_.serviceName), serviceName),
            cb.equal(c.<String>get(SystemUserLogType_.operationName), operationName),
            cb.equal(c.<Date>get(SystemUserLogType_.logDateItem), date));

    TypedQuery<SystemUserLogType> typedQuery = this.em.createQuery(q);
    try {
        SystemUserLogType log = typedQuery.getSingleResult();
        requests = log.getRequestsGranted();
    } catch (Exception e) {
        requests = 0;
    }
    return requests;
}

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

@Override
public Collection<Observation> getByUser(User u) throws NullPointerException, IllegalArgumentException {
    if (u == null) {
        throw new NullPointerException();
    }// w ww.j  a v  a 2s  .c  om
    if (u.isNew()) {
        throw new EntityNotKnownException();
    }
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Observation> cq = cb.createQuery(Observation.class);
    Root<Observation> o = cq.from(Observation.class);
    cq.select(o).where(cb.equal(o.get(Observation_.user), u));
    Collection<Observation> result = getEntityManager().createQuery(cq).getResultList();
    logger.info("{} Observations by User {}.", result.size(), u);
    return result;
}

From source file:com.toptal.dao.UserDaoImpl.java

@Transactional
@Override//from w  w  w. j  a  va2s.  c  o m
public final User saveIgnoringPassword(final User user) {
    final CriteriaBuilder builder = this.manager.getCriteriaBuilder();
    final CriteriaUpdate<User> update = builder.createCriteriaUpdate(User.class);
    final Root<User> root = update.from(User.class);
    update.set(root.get(NAME_FIELD), user.getName());
    update.set(root.get("role"), user.getRole());
    update.where(builder.equal(root.get("id"), user.getId()));
    this.manager.createQuery(update).executeUpdate();
    return this.manager.find(User.class, user.getId());
}

From source file:dao.jpa.JpaUtilsTest.java

@Test
@Transactional//  ww w . java  2 s  .  c  o  m
public void testCountFromComplexCriteria() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Book> bookQuery = cb.createQuery(Book.class);
    Root<Book> root = bookQuery.from(Book.class);
    Join<Book, Category> join = root
            .join(em.getMetamodel().entity(Book.class).getSingularAttribute("category", Category.class));
    bookQuery.where(cb.equal(join.<String>get("name"), "Java"));
    bookQuery.from(Author.class);
    bookQuery.select(root);
    CriteriaQuery<Long> countQuery = JpaUtils.countCriteria(em, bookQuery);
    Long result = Long.valueOf(em.createQuery(bookQuery).getResultList().size());

    assertEquals(result, (Long) em.createQuery(countQuery).getSingleResult());
}

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

@Override
public Collection<Observation> getByCategorie(Categorie c)
        throws NullPointerException, IllegalArgumentException {
    if (c == null) {
        throw new NullPointerException();
    }/*  w  w w. j a v  a 2 s  . c o  m*/
    if (c.isNew()) {
        throw new IllegalArgumentException();
    }
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Observation> cq = cb.createQuery(Observation.class);
    Root<Observation> o = cq.from(Observation.class);
    cq.select(o).where(cb.equal(o.get(Observation_.categorie), c));
    Collection<Observation> result = getEntityManager().createQuery(cq).getResultList();
    logger.info("{} Observations in Categorie {}.", result.size(), c);
    return result;
}