Example usage for javax.persistence.criteria CriteriaQuery from

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

Introduction

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

Prototype

<X> Root<X> from(Class<X> entityClass);

Source Link

Document

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.

Usage

From source file:br.com.sementesdoamanha.repository.Servidores.java

public List<Servidor> filtrados(ServidorFilter filtro) {
    //select, from, where, like... --> select(), from(), where()
    //JPQL: from Servidor

    //JPQL: select s from Servidor s where c.nome like = 'Joo%' and c.cpf = like '046.244.901-77'
    CriteriaBuilder builder = manager.getCriteriaBuilder();
    CriteriaQuery<Servidor> criteriaQuery = builder.createQuery(Servidor.class);
    Root<Servidor> s = criteriaQuery.from(Servidor.class);
    criteriaQuery.select(s);/*from  w  w w  . ja  v a2  s .  c  o m*/

    List<Predicate> predicates = new ArrayList<>();

    if (StringUtils.isNotBlank(filtro.getCpf())) {
        predicates.add(builder.equal(s.<String>get("cpf"), filtro.getCpf()));
    }
    if (StringUtils.isNotBlank(filtro.getNome())) {
        predicates
                .add(builder.like(builder.upper(s.<String>get("nome")), filtro.getNome().toUpperCase() + "%"));

    }

    criteriaQuery.where(predicates.toArray(new Predicate[0]));
    TypedQuery<Servidor> query = manager.createQuery(criteriaQuery);
    return query.getResultList();

}

From source file:pdsanchez.mywebtools.model.dao.jpa.GenericDAOJPAImpl.java

@Override
public List<T> findAll() {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return getEntityManager().createQuery(cq).getResultList();
}

From source file:top.knos.user.UserRepositoryImpl.java

public List<User> myCustomBatchOperation() {

    CriteriaQuery<User> criteriaQuery = em.getCriteriaBuilder().createQuery(User.class);
    criteriaQuery.select(criteriaQuery.from(User.class));
    return em.createQuery(criteriaQuery).getResultList();
}

From source file:de.logicline.splash.dao.UserDaoImpl.java

public UserEntity getUser(String token) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<UserEntity> cq = cb.createQuery(UserEntity.class);
    Root<UserEntity> user = cq.from(UserEntity.class);
    cq.select(user);/* w  w  w. j  a  va 2  s. c  o m*/
    cq.where(cb.equal(user.get("token"), token));
    List<UserEntity> resultList = getEntityManager().createQuery(cq).getResultList();

    UserEntity result = null;
    if (resultList != null && resultList.size() > 0) {
        result = resultList.get(0);
        return result;
    }

    return null;
}

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:de.logicline.splash.dao.UserDaoImpl.java

public UserEntity getUserByName(String username) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<UserEntity> cq = cb.createQuery(UserEntity.class);
    Root<UserEntity> user = cq.from(UserEntity.class);
    cq.select(user);//from   w  w  w  .ja v a  2 s  .c  o m
    cq.where(cb.equal(user.get("username"), username));
    List<UserEntity> resultList = getEntityManager().createQuery(cq).getResultList();

    if (resultList == null || resultList.size() == 0) {
        log.debug("UserEntity not found");
        return null;
    } else if (resultList.size() > 1) {
        log.warn("Multiple Users found for User with Username: " + username);
        return null;
    }

    return resultList.get(0);
}

From source file:net.dontdrinkandroot.persistence.dao.NumericEntityDaoTest.java

@Test
@Transactional//from  w w w .java  2  s  . c  o m
public void testQuot() {
    NumericEntity entity2 = new NumericEntity();
    entity2.setLongField(2L);
    entity2.setIntField(3);
    entity2 = this.save(entity2);

    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<Number> criteriaQuery = builder.createQuery(Number.class);
    final Root<NumericEntity> root = criteriaQuery.from(NumericEntity.class);
    final Expression<Number> quot = builder.quot(
            builder.prod(builder.literal(1f), root.get(NumericEntity_.intField)),
            root.get(NumericEntity_.longField));
    criteriaQuery.select(quot);

    final Number result = this.findSingle(criteriaQuery);
    Assert.assertEquals(Float.class, result.getClass());
    Assert.assertEquals(3 / 2f, result);
}

From source file:dtu.ds.warnme.dao.impl.EventsDaoImpl.java

@Override
public List<EventEntity> getNearestEvents(Float nLat, Float sLat, Float eLng, Float wLng,
        EventType... eventTypes) {/* w  w w.j a  v a 2s  . com*/
    CriteriaBuilder criteriaBuilder = getCriteriaBuilder();
    CriteriaQuery<EventEntity> criteriaQuery = criteriaBuilder.createQuery(EventEntity.class);
    Root<EventEntity> root = criteriaQuery.from(EventEntity.class);

    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.latitude), nLat));
    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.latitude), sLat));
    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.longitude), eLng));
    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.longitude), wLng));

    if (ArrayUtils.isNotEmpty(eventTypes)) {
        List<Predicate> eventTypePredicates = new ArrayList<Predicate>();
        for (EventType et : eventTypes) {
            eventTypePredicates.add(criteriaBuilder.equal(root.get(EventEntity_.eventType), et));
        }
        predicates.add(criteriaBuilder.and(
                criteriaBuilder.or(eventTypePredicates.toArray(new Predicate[eventTypePredicates.size()]))));
    }

    criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
    return getAllByCriteria(criteriaQuery);
}

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

/**
 * /*from  w w  w .ja v a2s. c  om*/
 * @param companyId
 * @return
 */
public boolean companyExists(Long companyId) {
    logger.info(String.format("checking if Company with ID %d exists ...", companyId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Company> from = criteria.from(Company.class);
    criteria.where(cb.equal(from.get(Company_.id), companyId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult().longValue() == 1);
}

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")));
    }//  w ww.  j  a v a2 s .co  m

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