Example usage for javax.persistence.criteria CriteriaBuilder createQuery

List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder createQuery.

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

From source file:ispok.dao.TournamentHibernateJpaDao.java

@Override
public Long getCount(Map<String, Object> filters) {

    EntityManager em = getEntityManager();

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<Tournament> t = cq.from(Tournament.class);

    cq.select(cb.count(t));// ww  w  .  j ava  2s  . c  o m

    TypedQuery<Long> tq = em.createQuery(cq);
    return tq.getSingleResult();
}

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

@Override
@Transactional(readOnly = true)/*  ww  w .java  2s. co m*/
public ExampleGeneratedIdEntity findWithOthersFetchJoin(final Long id) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<ExampleGeneratedIdEntity> criteriaQuery = builder
            .createQuery(ExampleGeneratedIdEntity.class);
    final Root<ExampleGeneratedIdEntity> root = criteriaQuery.from(this.entityClass);
    root.fetch(ExampleGeneratedIdEntity_.otherEntities);

    criteriaQuery.where(builder.equal(root.get(ExampleGeneratedIdEntity_.id), id));

    return this.findSingle(criteriaQuery);
}

From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoElementtiDAOImpl.java

@Override
public List<YhteystietoElementti> findByLisatietoIdAndKentanNimi(String yhteystietojenTyyppiOid,
        String kentanNimi) {/*w  ww .  j  a v a 2s .c  o  m*/

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<YhteystietoElementti> query = cb.createQuery(YhteystietoElementti.class);

    Root<YhteystietoElementti> root = query.from(YhteystietoElementti.class);
    query.select(root);

    Predicate tyyppiEquals = cb.equal(root.get("yhteystietojenTyyppi").get("oid"), yhteystietojenTyyppiOid);
    Predicate nameEquals = cb.equal(root.get("nimi"), kentanNimi);

    Predicate whereClause = cb.and(tyyppiEquals, nameEquals);
    query.where(whereClause);

    return getEntityManager().createQuery(query).getResultList();

    //        YhteystietojenTyyppi ytT =  yttDao.findBy("oid", yhteystietojenTyyppiOid).get(0);
    //        Query query = getEntityManager().createQuery(
    //                "SELECT ltk FROM YhteystietoElementti ltk " +
    //                "WHERE ltk.yhteystietojenTyyppi.id = :yhteystietojenTyyppiId " +
    //                "AND ltk.nimi = :kentanNimi"
    //                );
    //        return query
    //                .setParameter("yhteystietojenTyyppiId", ytT.getId())
    //                .setParameter("kentanNimi", kentanNimi)
    //                .getResultList();
}

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

@Override
@Transactional//from   w ww  . j  a va  2  s  .  c o  m
public Set<SecurityGroup> findAllByUserLogin(UserLogin userLogin) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<SecurityGroup> query = builder.createQuery(SecurityGroup.class);

    Root<SecurityGroup> root = query.from(SecurityGroup.class);
    Join<SecurityGroup, UserLoginSecurityGroup> join = root.join("userLoginSecurityGroup");

    builder.equal(join.get("userLogin.userLoginId"), userLogin.getUserLoginId());
    builder.isNull(join.get("thruDate"));

    //        criteria.setFetchMode("userLoginSecurityGroups", FetchMode.SELECT);
    //        criteria.setFetchMode("permissionSecurityGroups", FetchMode.SELECT);

    Set<SecurityGroup> securityGroups = new HashSet<SecurityGroup>(
            getEntityManager().createQuery(query).getResultList());

    for (SecurityGroup securityGroup : securityGroups) {
        securityGroup.getPermissionSecurityGroups().size();
        securityGroup.getUserLoginSecurityGroups().size();
    }

    return securityGroups;
}

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

@Override
@Transactional(readOnly = true)/*from   w  w  w  .jav a 2  s .c  om*/
public List<ExampleGeneratedIdEntity> findByOtherText(final String text) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<ExampleGeneratedIdEntity> criteriaQuery = builder.createQuery(this.entityClass);
    criteriaQuery.distinct(true);
    final Root<ExampleGeneratedIdEntity> root = criteriaQuery.from(this.entityClass);

    final ListJoin<ExampleGeneratedIdEntity, ExampleIdEntity> join = root
            .join(ExampleGeneratedIdEntity_.otherEntities);

    criteriaQuery.where(builder.equal(join.get(ExampleIdEntity_.text), text));

    return this.find(criteriaQuery);
}

From source file:de.egore911.persistence.selector.AbstractSelector.java

private TypedQuery<T> buildQuery() {
    EntityManager em = EntityManagerUtil.getEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = builder.createQuery(getEntityClass());
    Root<T> from = cq.from(getEntityClass());
    List<Predicate> predicates = generatePredicateList(builder, from, cq);
    cq.where(predicates.toArray(new Predicate[predicates.size()]));
    cq.orderBy(generateOrderList(builder, from));
    cq.select(from);//from   ww w  .ja va2 s. c  o m
    return em.createQuery(cq);
}

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

public Page<Consultation> findPage(Member member, Product product, Boolean isShow, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Consultation> criteriaQuery = criteriaBuilder.createQuery(Consultation.class);
    Root<Consultation> root = criteriaQuery.from(Consultation.class);
    criteriaQuery.select(root);/* w w w  . j a  v a 2 s .  co  m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forConsultation")));
    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 (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.ConsultationDaoImpl.java

public Long count(Member member, Product product, Boolean isShow) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Consultation> criteriaQuery = criteriaBuilder.createQuery(Consultation.class);
    Root<Consultation> root = criteriaQuery.from(Consultation.class);
    criteriaQuery.select(root);/*  ww w .  j a  v  a2 s.co m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forConsultation")));
    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 (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:org.wallride.repository.CustomFieldRepositoryImpl.java

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<CustomField> root = query.from(CustomField.class);
    query.select(root.get(CustomField_.id));
    query.where(cb.equal(root.get(CustomField_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

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

public List<Consultation> findList(Member member, Product product, Boolean isShow, Integer count,
        List<Filter> filters, List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Consultation> criteriaQuery = criteriaBuilder.createQuery(Consultation.class);
    Root<Consultation> root = criteriaQuery.from(Consultation.class);
    criteriaQuery.select(root);/*ww w.  j  ava 2s  .  co m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forConsultation")));
    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 (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, null, count, filters, orders);
}