Example usage for javax.persistence.criteria CriteriaQuery where

List of usage examples for javax.persistence.criteria CriteriaQuery where

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery where.

Prototype

CriteriaQuery<T> where(Predicate... restrictions);

Source Link

Document

Modify the query to restrict the query result according to the conjunction of the specified restriction predicates.

Usage

From source file:com.carser.viamais.vo.TransactionFilter.java

public Long count(final EntityManager em) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<Transaction> transaction = cq.from(Transaction.class);
    cq.select(cb.countDistinct((transaction)));
    cq.where(getPredicates(cb, transaction));
    return em.createQuery(cq).getSingleResult();
}

From source file:com.qpark.eip.core.spring.lockedoperation.dao.LockedOperationDaoImpl.java

/**
 * @see com.qpark.eip.core.spring.lockedoperation.dao.LockableOperationDao#unlockOperation(com.qpark.eip.core.spring.lockedoperation.LockableOperation)
 *///from   ww w  .java  2s  . c om
@Override
@Transactional(value = EipLockedoperationConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public boolean unlockOperation(final LockableOperation operation) {
    this.logger.debug("+unlock# {} {} {}",
            new Object[] { this.hostName, operation.getUUID(), operation.getName() });
    boolean unlocked = false;
    String logString = getOperationLockString(operation);
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<OperationLockControllType> q = cb.createQuery(OperationLockControllType.class);
    Root<OperationLockControllType> c = q.from(OperationLockControllType.class);
    q.where(cb.equal(c.<String>get("operationName"), logString));
    TypedQuery<OperationLockControllType> typedQuery = this.em.createQuery(q);
    try {
        List<OperationLockControllType> locks = typedQuery.getResultList();
        for (OperationLockControllType lock : locks) {
            this.em.remove(this.em.merge(lock));
        }
        unlocked = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        this.logger.debug("-unlock# {} {} {} {}", new Object[] { this.hostName,
                unlocked ? "unlocked" : "not unlocked", operation.getUUID(), operation.getName() });
    }
    return unlocked;
}

From source file:com.qpark.eip.core.spring.lockedoperation.dao.LockedOperationDaoImpl.java

/**
 * @see com.qpark.eip.core.spring.lockedoperation.dao.LockableOperationDao#isLockedByThisServer(com.qpark.eip.core.spring.lockedoperation.LockableOperation)
 *//*from  w w  w.  j a  va  2 s . c o m*/
@Override
@Transactional(value = EipLockedoperationConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public synchronized boolean isLockedByThisServer(final LockableOperation operation) {
    boolean lockedByThisServer = false;
    OperationLockControllType value = null;
    String logString = getOperationLockString(operation);
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<OperationLockControllType> q = cb.createQuery(OperationLockControllType.class);
    Root<OperationLockControllType> c = q.from(OperationLockControllType.class);
    q.where(cb.equal(c.<String>get("operationName"), logString));
    TypedQuery<OperationLockControllType> typedQuery = this.em.createQuery(q);
    try {
        value = typedQuery.getSingleResult();
        if (value != null) {
            if (value.getServerIpAddress().equals(this.hostAddress)) {
                lockedByThisServer = true;
            }
            this.logger.debug(" isLockedByThisServer# {} opeation {} {} locked by: {} {}",
                    new Object[] { lockedByThisServer, operation.getUUID(), operation.getName(),
                            value.getServerName(), value.getLockDate() });
        }
    } catch (NoResultException e) {
        lockedByThisServer = false;
    } catch (NonUniqueResultException e) {
        lockedByThisServer = false;
    }
    return lockedByThisServer;
}

From source file:org.fao.geonet.repository.HarvesterSettingRepositoryOverridesImpl.java

@Override
public List<HarvesterSetting> findAllByNames(List<String> names) {
    CriteriaBuilder criteriaBuilder = _entityManager.getCriteriaBuilder();
    CriteriaQuery<HarvesterSetting> query = criteriaBuilder.createQuery(HarvesterSetting.class);

    Root<HarvesterSetting> root = query.from(HarvesterSetting.class);
    query.select(root);/*from   www. j a  v  a  2 s .  c  o  m*/
    if (CollectionUtils.isNotEmpty(names)) {
        query.where(root.get(HarvesterSetting_.name).in(names));
    }

    return _entityManager.createQuery(query).getResultList();
}

From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java

/**
 * {@inheritDoc}//from  w ww  .  j  ava  2  s.c  o  m
 */
public List<User> getCustomers(int start, int limit, String sortField, String sortOrder,
        Map<String, Object> filters) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Predicate isCustomerRole = builder.equal(root.get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER);
    Predicate hasCreditCard = builder.equal(root.get(FIELD_CONSTANT_HAS_CREDIT_CARD), TRUE);
    query.where(builder.and(isCustomerRole, hasCreditCard));

    query.select(root);

    addSorting(sortField, sortOrder, query, builder, root);
    addFilterCriteria(filters, builder, root, query);
    return entityManager.createQuery(query).setFirstResult(start).setMaxResults(limit).getResultList();
}

From source file:com.qpark.eip.core.spring.lockedoperation.dao.LockedOperationDaoImpl.java

/**
 * @see com.qpark.eip.core.spring.lockedoperation.dao.LockableOperationDao#isLockedOperation(com.qpark.eip.core.spring.lockedoperation.LockableOperation)
 *//*from  www .  j  a  v a2 s  . c o  m*/
@Override
@Transactional(value = EipLockedoperationConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public synchronized boolean isLockedOperation(final LockableOperation operation) {
    this.logger.debug("+isLocked# {} {} {}",
            new Object[] { this.hostName, operation.getUUID(), operation.getName() });
    if (this.firstRun) {
        this.unlockOperationOnServerStart();
        this.firstRun = false;
    }
    boolean locked = false;
    String logString = getOperationLockString(operation);
    OperationLockControllType value = null;
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<OperationLockControllType> q = cb.createQuery(OperationLockControllType.class);
    Root<OperationLockControllType> c = q.from(OperationLockControllType.class);
    q.where(cb.equal(c.<String>get("operationName"), logString));
    TypedQuery<OperationLockControllType> typedQuery = this.em.createQuery(q);
    try {
        value = typedQuery.getSingleResult();
        if (value != null) {
            this.logger.debug(" isLocked# opeation {} {} locked by: {} {}", new Object[] { operation.getUUID(),
                    operation.getName(), value.getServerName(), value.getLockDate() });
            locked = true;
        }
    } catch (NoResultException e) {
        locked = false;
    } catch (NonUniqueResultException e) {
        locked = true;
    }
    this.logger.debug("-isLocked# {} {} {} {}", new Object[] { this.hostName, locked ? "locked" : "not locked",
            operation.getUUID(), operation.getName() });
    return locked;
}

From source file:fr.amapj.service.services.authentification.PasswordManager.java

/**
 * Retrouve l'utilisateur avec ce resetPasswordSald
 * Retourne null si non trouv ou autre problme
 *//*from  w  w  w  .j  av  a 2 s.  c  om*/
@DbRead
public Utilisateur findUserWithResetPassword(String resetPasswordSalt) {
    EntityManager em = TransactionHelper.getEm();

    if ((resetPasswordSalt == null) || resetPasswordSalt.equals("")) {
        return null;
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();

    CriteriaQuery<Utilisateur> cq = cb.createQuery(Utilisateur.class);
    Root<Utilisateur> root = cq.from(Utilisateur.class);

    // On ajoute la condition where 
    cq.where(cb.equal(root.get(Utilisateur.P.RESETPASSWORDSALT.prop()), resetPasswordSalt));

    List<Utilisateur> us = em.createQuery(cq).getResultList();
    if (us.size() == 0) {
        return null;
    }

    if (us.size() > 1) {
        logger.warn("Il y a plusieurs utilisateurs avec le salt " + resetPasswordSalt);
        return null;
    }

    Utilisateur u = us.get(0);

    if (u.getEtatUtilisateur() == EtatUtilisateur.INACTIF) {
        return null;
    }

    return u;
}

From source file:com.qpark.eip.core.spring.lockedoperation.dao.LockedOperationDaoImpl.java

/**
 * @see com.qpark.eip.core.spring.lockedoperation.dao.LockableOperationDao#lockOperation(com.qpark.eip.core.spring.lockedoperation.LockableOperation)
 *//*w ww .j av  a  2s. c  o m*/
@Override
@Transactional(value = EipLockedoperationConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public synchronized boolean lockOperation(final LockableOperation operation) {
    this.logger.debug("+lock# {} {} {}",
            new Object[] { this.hostName, operation.getUUID(), operation.getName() });
    if (this.firstRun) {
        this.unlockOperationOnServerStart();
        this.firstRun = false;
    }
    boolean locked = false;
    String logString = getOperationLockString(operation);
    OperationLockControllType value = null;
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<OperationLockControllType> q = cb.createQuery(OperationLockControllType.class);
    Root<OperationLockControllType> c = q.from(OperationLockControllType.class);
    q.where(cb.equal(c.<String>get("operationName"), logString));
    TypedQuery<OperationLockControllType> typedQuery = this.em.createQuery(q);
    try {
        value = typedQuery.getSingleResult();
        this.logger.debug(" lock# opeation {} {} locked by: {} {}", new Object[] { operation.getUUID(),
                operation.getName(), value.getServerName(), value.getLockDate() });
    } catch (NoResultException e) {
        value = null;
    } catch (NonUniqueResultException e) {
        return false;
    }
    if (value == null) {
        value = new OperationLockControllType();
        value.setOperationName(logString);
        value.setServerName(this.hostName);
        value.setServerIpAddress(this.hostAddress);
        value.setLockDate(DateUtil.get(new Date()));
        this.em.persist(value);
        locked = true;
    }
    this.logger.debug("-lock# {} {} {} {}", new Object[] { this.hostName, locked ? "locked" : "not locked",
            operation.getUUID(), operation.getName() });
    return locked;
}

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

public <T extends Persistable> List<T> getAllExcept(Class<T> clazz, Collection<T> toExclude) {
    if (toExclude == null || toExclude.isEmpty()) {
        return getAll(clazz);
    }// w w w  .  j av  a 2  s  .c o m
    logger.infof("loading all %s except %s ...", getReadableClassName(clazz), toExclude);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(clazz);
    Root<T> root = query.from(clazz);
    query.where(cb.not(root.in(toExclude)));
    return em.createQuery(query).getResultList();
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute// w w  w  . ja v  a2  s .  co  m
public long countByAttribute(String attributeName, Object attributeValue) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> q = cb.createQuery(Long.class);

    Root<T> p = q.from(persistentClass);
    final Expression<Long> count = newCountExpression(cb, p);
    q.select(count);
    q.where(cb.equal(p.get(attributeName), attributeValue));

    try {
        return entityManager.createQuery(q).getSingleResult();
    } catch (NoResultException e) {
        return 0;
    }
}