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:br.ufba.dcc.mestrado.computacao.repository.impl.ProjectRepositoryImpl.java

@Override
public OpenHubProjectEntity findByName(String name) {
    CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<OpenHubProjectEntity> criteriaQuery = criteriaBuilder.createQuery(getEntityClass());

    Root<OpenHubProjectEntity> root = criteriaQuery.from(getEntityClass());
    criteriaQuery.select(root);//from  w w w  . j  a  va 2s. c  o m

    Predicate predicate = criteriaBuilder.equal(root.get("name"), name);
    criteriaQuery.where(predicate);

    TypedQuery<OpenHubProjectEntity> query = getEntityManager().createQuery(criteriaQuery);

    OpenHubProjectEntity projectEntity = query.getSingleResult();

    return projectEntity;
}

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 ww w .  j  a v a  2s .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: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)
 *//* ww w  .  j av a  2 s  . 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:net.groupbuy.dao.impl.ReviewDaoImpl.java

public Page<Review> findPage(Member member, Product product, Type type, Boolean isShow, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
    Root<Review> root = criteriaQuery.from(Review.class);
    criteriaQuery.select(root);//w ww  . java2  s  .  com
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (type == Type.positive) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
    } else if (type == Type.moderate) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
    } else if (type == Type.negative) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
    }
    if (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

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

public Long count(Member member, Product product, Type type, Boolean isShow) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
    Root<Review> root = criteriaQuery.from(Review.class);
    criteriaQuery.select(root);/*from   w ww.j av  a  2  s.  com*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (type == Type.positive) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
    } else if (type == Type.moderate) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
    } else if (type == Type.negative) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
    }
    if (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

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

public List<Review> findList(Member member, Product product, Type type, Boolean isShow, Integer count,
        List<Filter> filters, List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
    Root<Review> root = criteriaQuery.from(Review.class);
    criteriaQuery.select(root);//w  w w .j a  v a  2 s .  c  om
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (type == Type.positive) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
    } else if (type == Type.moderate) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
    } else if (type == Type.negative) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
    }
    if (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, null, count, filters, orders);
}

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

/**
 * SELECT e FROM jpa_query_employee e WHERE e.name='employee_1'
 *///from   w  w  w.  jav a  2s  . co  m
@Transactional
public void doSimple2() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> e = c.from(Employee.class);
    c.select(e).where(cb.equal(e.get("name"), "employee_1"));

    showResult(c);
}

From source file:org.openmeetings.app.data.basic.Configurationmanagement.java

public List<Configuration> getConfigurations(int start, int max, String orderby, boolean asc) {
    try {//from  ww  w .jav a  2 s.co m
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Configuration> cq = cb.createQuery(Configuration.class);
        Root<Configuration> c = cq.from(Configuration.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<Configuration> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Configuration> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getConfigurations]", ex2);
    }
    return null;
}

From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java

public <K> T findBy(String fieldName, Object value, Class<K> valueClass) {
    EntityManager em = getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);
    cq.select(root).where(cb.equal(root.get(fieldName).as(valueClass), value));
    List<T> list = em.createQuery(cq).getResultList();
    return list.isEmpty() ? null : list.get(0);
}

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

protected Long performDistinctCountWithEqualPredicate(final Attribute<T, ?> attr, Object value) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<T> root = query.from(clazz);
    query.where(cb.equal(root.get(attr.getName()), value));
    query.select(cb.countDistinct(root));
    return em.createQuery(query).getSingleResult();
}