Example usage for javax.persistence.criteria CriteriaQuery select

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

Introduction

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

Prototype

CriteriaQuery<T> select(Selection<? extends T> selection);

Source Link

Document

Specify the item that is to be returned in the query result.

Usage

From source file:com.beto.test.securityinterceptor.model.dao.generic.AbstractDao.java

@SuppressWarnings("unchecked")
public List<T> findAll() {

    try {/*from   ww w  .  j  a va 2s  .  c o m*/
        CriteriaQuery<Object> cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return (List<T>) getEntityManager().createQuery(cq).getResultList();
    } catch (NoResultException e) {
        LOGGER.debug("NO DATA FOUND...");
    }
    return null;
}

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

public Page<Coupon> findPage(Boolean isEnabled, Boolean isExchange, Boolean hasExpired, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Coupon> criteriaQuery = criteriaBuilder.createQuery(Coupon.class);
    Root<Coupon> root = criteriaQuery.from(Coupon.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (isEnabled != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isEnabled"), isEnabled));
    }//from w  w w  .j ava 2 s  .  com
    if (isExchange != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isExchange"), isExchange));
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, root.get("endDate").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("endDate"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("endDate").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("endDate"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

From source file:net.osgiliath.jpa.repository.impl.HelloJpaRepository.java

@Override
public Collection<? extends HelloEntity> findByHelloObjectMessage(String message_p) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<HelloEntity> cq = cb.createQuery(HelloEntity.class);
    Root<HelloEntity> helloObject = cq.from(HelloEntity.class);
    cq.select(helloObject);
    Predicate where = cb.equal(helloObject.get("helloMessage"), message_p);
    cq.where(where);//from  ww  w .java 2s  . c o m
    TypedQuery<HelloEntity> q = entityManager.createQuery(cq);
    List<HelloEntity> result = q.getResultList();
    return result;
}

From source file:net.przemkovv.sphinx.dao.impl.DefaultUserDAO.java

@Override
public boolean existsByEmail(String email) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<User> user = cq.from(User.class);
    cq.where(cb.equal(user.get(User_.email), email));
    cq.select(cb.count(user));
    return em.createQuery(cq).getSingleResult().intValue() == 1;
}

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

public List<ContactEntity> findByName(String name) {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<ContactEntity> cq = cb.createQuery(ContactEntity.class);
    Root<ContactEntity> userInfo = cq.from(ContactEntity.class);
    cq.select(userInfo);
    cq.where(cb.like(userInfo.<String>get("lastName"), "%" + name + "%"));
    List<ContactEntity> result = getEntityManager().createQuery(cq).getResultList();
    return result;
}

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

@Override
@Transactional(readOnly = true)//from  w w w .j av a 2 s  . c  o m
public List<T> getAll() {
    CriteriaBuilder builder = manager.getCriteriaBuilder();
    CriteriaQuery<T> criteria = builder.createQuery(entityClazz);
    Root<T> root = criteria.from(entityClazz);
    criteria.select(root);
    return manager.createQuery(criteria).getResultList();
}

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

public ContactEntity getContactByUserId(String userId) {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<ContactEntity> cq = cb.createQuery(ContactEntity.class);
    Root<ContactEntity> userInfo = cq.from(ContactEntity.class);
    cq.select(userInfo);
    cq.where(cb.equal(userInfo.get("userIdFk"), userId));
    List<ContactEntity> resultList = em.createQuery(cq).getResultList();

    ContactEntity result = null;//from   w  w  w . ja  v a  2 s . co m
    if (resultList != null && resultList.size() > 0) {
        result = resultList.get(0);
    }

    return result;
}

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);
    cq.where(cb.equal(user.get("token"), token));
    List<UserEntity> resultList = getEntityManager().createQuery(cq).getResultList();

    UserEntity result = null;// w  ww.j  a v  a  2s. com
    if (resultList != null && resultList.size() > 0) {
        result = resultList.get(0);
        return result;
    }

    return null;
}

From source file:net.przemkovv.sphinx.dao.impl.DefaultGenericDAO.java

@Override
public Long countAll() {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    cq.select(cb.count(cq.from(type)));
    return getEntityManager().createQuery(cq).getSingleResult();
}

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);
    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;
    }/*  w w w  .j a  va 2  s .c  o m*/

    return resultList.get(0);
}