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:core.commonapp.server.dao.security.PermissionSecurityGroupDaoHibernateImpl.java

@Override
public List<PermissionSecurityGroup> findAllBySecurityGroup(SecurityGroup securityGroup, boolean includeOld) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<PermissionSecurityGroup> query = builder.createQuery(getPersistClass());
    Root<PermissionSecurityGroup> root = query.from(getPersistClass());
    query.where(builder.equal(root.get("securityGroup"), securityGroup));

    if (!includeOld) {
        query.where(builder.isNull(root.get("thruDate")));
    }//from   www .jav a2  s.co  m

    return (List<PermissionSecurityGroup>) getEntityManager().createQuery(query).getResultList();
}

From source file:com.ttech.cordovabuild.domain.application.ApplicationRepositoryImpl.java

@Override
public ApplicationBuilt getLatestBuilt(Application application) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ApplicationBuilt> cq = cb.createQuery(ApplicationBuilt.class);
    Root<ApplicationBuilt> from = cq.from(ApplicationBuilt.class);
    cq = cq.where(cb.equal(from.get(ApplicationBuilt_.application), application))
            .orderBy(cb.desc(from.get(ApplicationBuilt_.startDate)));
    try {//from w  w  w.j av  a  2s. c  om
        return em.createQuery(cq).setMaxResults(1).getSingleResult();
    } catch (NoResultException e) {
        throw new ApplicationBuiltNotFoundException(e);
    }
}

From source file:org.businessmanager.dao.security.UserDaoImpl.java

@Override
public User findUserByName(String username) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<User> query = queryBuilder.createQuery(User.class);
    Root<User> userQuery = query.from(User.class);
    query.select(userQuery).where(queryBuilder.equal(userQuery.get(User_.username), username));

    try {//from  ww w. j a  v a2  s .  c om
        return getEntityManager().createQuery(query).getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:com.netflix.genie.core.jpa.specifications.JpaJobSpecs.java

/**
 * Generate a criteria query predicate for a where clause based on the given parameters.
 *
 * @param root        The root to use/*  w w w. j av  a 2 s  . c o  m*/
 * @param cb          The criteria builder to use
 * @param id          The job id
 * @param name        The job name
 * @param user        The user who created the job
 * @param statuses    The job statuses
 * @param tags        The tags for the jobs to find
 * @param clusterName The cluster name
 * @param cluster     The cluster the job should have been run on
 * @param commandName The command name
 * @param command     The command the job should have been run with
 * @param minStarted  The time which the job had to start after in order to be return (inclusive)
 * @param maxStarted  The time which the job had to start before in order to be returned (exclusive)
 * @param minFinished The time which the job had to finish after in order to be return (inclusive)
 * @param maxFinished The time which the job had to finish before in order to be returned (exclusive)
 * @return The specification
 */
public static Predicate getFindPredicate(final Root<JobEntity> root, final CriteriaBuilder cb, final String id,
        final String name, final String user, final Set<JobStatus> statuses, final Set<String> tags,
        final String clusterName, final ClusterEntity cluster, final String commandName,
        final CommandEntity command, final Date minStarted, final Date maxStarted, final Date minFinished,
        final Date maxFinished) {
    final List<Predicate> predicates = new ArrayList<>();
    if (StringUtils.isNotBlank(id)) {
        predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.id), id));
    }
    if (StringUtils.isNotBlank(name)) {
        predicates
                .add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.name), name));
    }
    if (StringUtils.isNotBlank(user)) {
        predicates
                .add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.user), user));
    }
    if (statuses != null && !statuses.isEmpty()) {
        final List<Predicate> orPredicates = statuses.stream()
                .map(status -> cb.equal(root.get(JobEntity_.status), status)).collect(Collectors.toList());
        predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
    }
    if (tags != null && !tags.isEmpty()) {
        predicates.add(cb.like(root.get(JobEntity_.tags), JpaSpecificationUtils.getTagLikeString(tags)));
    }
    if (cluster != null) {
        predicates.add(cb.equal(root.get(JobEntity_.cluster), cluster));
    }
    if (StringUtils.isNotBlank(clusterName)) {
        predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.clusterName),
                clusterName));
    }
    if (command != null) {
        predicates.add(cb.equal(root.get(JobEntity_.command), command));
    }
    if (StringUtils.isNotBlank(commandName)) {
        predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.commandName),
                commandName));
    }
    if (minStarted != null) {
        predicates.add(cb.greaterThanOrEqualTo(root.get(JobEntity_.started), minStarted));
    }
    if (maxStarted != null) {
        predicates.add(cb.lessThan(root.get(JobEntity_.started), maxStarted));
    }
    if (minFinished != null) {
        predicates.add(cb.greaterThanOrEqualTo(root.get(JobEntity_.finished), minFinished));
    }
    if (maxFinished != null) {
        predicates.add(cb.lessThan(root.get(JobEntity_.finished), maxFinished));
    }
    return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}

From source file:com.github.aynu.mosir.core.enterprise.persistence.SmartRepositoryProducer.java

/**
 * ???// w w w . ja v  a2s.co m
 * @return ?()
 */
@Produces
public SmartRepository<Testee, TesteeFilter> createTestee() {
    final SmartRepositoryListener<Testee, TesteeFilter> listener = new AbstractSmartRepositoryListener<Testee, TesteeFilter>() {
        @Override
        public CriteriaQuery<Testee> query(final CriteriaBuilder builder, final CriteriaQuery<Testee> query,
                final Root<Testee> root, final TesteeFilter filter) {
            query.select(root);
            if (StringUtils.isNotEmpty(filter.getCode())) {
                query.where(builder.equal(root.get("code"), filter.getCode()));
            }
            return query.orderBy(createOrders(builder, root, filter));
        }
    };
    return new SmartRepositoryImpl<>(Testee.class, manager, listener);
}

From source file:eu.uqasar.service.company.CompanyService.java

/**
 * /*from   w  w w  .ja  v a2  s . c o m*/
 * @param innovationObjective
 * @return
 */
public List<Company> getAllByAscendingNameFiltered(CompanyFilterStructure filter, int first, int count) {
    logger.infof("loading all Companies ordered by ascending name matching the given filter %s...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Company> criteria = cb.createQuery(Company.class);
    Root<Company> root = criteria.from(Company.class);

    criteria.where(cb.or(cb.equal(root.get(Company_.name), filter.getName()),
            cb.equal(root.get(Company_.shortName), filter.getShortName()),
            cb.equal(root.get(Company_.country), filter.getCountry()))

    );
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}

From source file:core.commonapp.server.dao.security.UserLoginSecurityGroupDaoHibernateImpl.java

@Override
public List<UserLoginSecurityGroup> findByUserLoginAndSecurityGroup(UserLogin userLogin,
        SecurityGroup securityGroup, boolean includeOld) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<UserLoginSecurityGroup> query = builder.createQuery(getPersistClass());
    Root<UserLoginSecurityGroup> root = query.from(getPersistClass());
    query.where(builder.equal(root.get("securityGroup"), securityGroup),
            builder.equal(root.get("userLogin"), userLogin),
            (includeOld ? builder.isNotNull(root.get("thruDate")) : builder.isNull(root.get("thruDate"))));

    return (List<UserLoginSecurityGroup>) getEntityManager().createQuery(query).getResultList();
}

From source file:core.commonapp.server.dao.security.UserLoginSecurityGroupDaoHibernateImpl.java

@Override
public List<UserLoginSecurityGroup> findBySecurityGroup(SecurityGroup securityGroup, boolean includeOld) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<UserLoginSecurityGroup> query = builder.createQuery(getPersistClass());
    Root<UserLoginSecurityGroup> root = query.from(getPersistClass());
    query.where(builder.equal(root.get("securityGroup"), securityGroup));

    if (!includeOld) {
        query.where(builder.isNull(root.get("thruDate")));
    }//from  ww  w.ja v  a2 s.  c om

    return (List<UserLoginSecurityGroup>) getEntityManager().createQuery(query).getResultList();
}

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

public Page<Deposit> findPage(Member member, Pageable pageable) {
    if (member == null) {
        return new Page<Deposit>(Collections.<Deposit>emptyList(), 0, pageable);
    }//from w w  w .ja  v a  2s  .c om
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Deposit> criteriaQuery = criteriaBuilder.createQuery(Deposit.class);
    Root<Deposit> root = criteriaQuery.from(Deposit.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    return super.findPage(criteriaQuery, pageable);
}

From source file:org.businessmanager.dao.security.PermissionDaoImpl.java

@Override
public Permission findPermissionByName(String name) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Permission> query = queryBuilder.createQuery(Permission.class);
    Root<Permission> permissionQuery = query.from(Permission.class);
    query.select(permissionQuery).where(queryBuilder.equal(permissionQuery.get(Permission_.name), name));

    try {/*from  ww w.j a v a2s. c  om*/
        return getEntityManager().createQuery(query).setMaxResults(1).getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}