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:com.soen.ebanking.dao.ObjectDao.java

public List getAllObjects(Class<T> ClassName, String tableName) {
    EntityManager em = this.getEMF().createEntityManager();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<T> query = qb.createQuery(ClassName);
    //Root<T> entities = query.from(ClassName);
    List<T> result = em.createQuery(query).getResultList();

    return result;
}

From source file:com.boxedfolder.carrot.repository.impl.AnalyticsLogRepositoryImpl.java

private TypedQuery<Long> getCountQuery(Class clazz) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    query.select(builder.count(query.from(clazz)));
    return entityManager.createQuery(query);
}

From source file:com.fantasy.stataggregator.entities.dao.AbstractRepository.java

public CriteriaQuery<T> getCriteriaQuery() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    return cb.createQuery(entityClass);
}

From source file:com.sixsq.slipstream.persistence.Run.java

public static List<RunView> viewList(User user, String moduleResourceUri, Integer offset, Integer limit,
        String cloudServiceName) throws ConfigurationException, ValidationException {
    List<RunView> views = null;
    EntityManager em = PersistenceUtil.createEntityManager();
    try {/*ww  w. j  a v  a  2  s .  c  o  m*/
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Run> critQuery = builder.createQuery(Run.class);
        Root<Run> rootQuery = critQuery.from(Run.class);
        critQuery.select(rootQuery);
        Predicate where = viewListCommonQueryOptions(builder, rootQuery, user, moduleResourceUri,
                cloudServiceName);
        if (where != null) {
            critQuery.where(where);
        }
        critQuery.orderBy(builder.desc(rootQuery.get("startTime")));
        TypedQuery<Run> query = em.createQuery(critQuery);
        if (offset != null) {
            query.setFirstResult(offset);
        }
        query.setMaxResults((limit != null) ? limit : DEFAULT_LIMIT);
        List<Run> runs = query.getResultList();
        views = convertRunsToRunViews(runs);
    } finally {
        em.close();
    }
    return views;
}

From source file:aka.pirana.jdoc.JChart.java

private XYDataset SampleGenerator() {
    manager.getTransaction().begin();//from   ww  w  .ja va2 s .c  o  m
    CriteriaBuilder cb = manager.getCriteriaBuilder();
    CriteriaQuery<Consultation> query = cb.createQuery(Consultation.class);
    Root<Consultation> sm = query.from(Consultation.class);
    query.where(cb.equal(sm.get("patient.id"), id.toString()));
    TypedQuery<Consultation> q = manager.createQuery(query);
    List<Consultation> results = q.getResultList();
    XYSeries xyWeightSeries = new XYSeries("Weight");
    XYSeries xyFatSeries = new XYSeries("Fat");
    XYSeries xyMuscleSeries = new XYSeries("Muscle");
    XYSeries xyWaterSeries = new XYSeries("Water");
    int i = 2;
    results.stream().map((result) -> {
        xyWeightSeries.add(i, result.getC_weight());
        return result;
    }).map((result) -> {
        xyFatSeries.add(i, result.getC_gm());
        return result;
    }).map((result) -> {
        xyMuscleSeries.add(i, result.getC_mm());
        return result;
    }).forEach((result) -> {
        xyWaterSeries.add(i, result.getC_hm());
    });
    XYSeriesCollection xyseriescollection = new XYSeriesCollection(xyWeightSeries);
    xyseriescollection.addSeries(xyFatSeries);
    xyseriescollection.addSeries(xyMuscleSeries);
    xyseriescollection.addSeries(xyWaterSeries);
    return xyseriescollection;
}

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

@Override
@Transactional(readOnly = true)/*from ww  w.  j av a 2  s .c  om*/
public ExampleEnum findMaxEnum() {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<ExampleEnum> criteriaQuery = builder.createQuery(ExampleEnum.class);
    final Root<ExampleGeneratedIdEntity> root = criteriaQuery.from(this.entityClass);

    criteriaQuery.select(root.get(ExampleGeneratedIdEntity_.exampleEnum));

    final List<ExampleEnum> enums = this.getEntityManager().createQuery(criteriaQuery).getResultList();
    ExampleEnum max = null;
    for (final ExampleEnum exampleEnum : enums) {
        if (max == null || exampleEnum.ordinal() > max.ordinal()) {
            max = exampleEnum;
        }
    }

    return max;
}

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

@Override
@Transactional//from  w ww.ja va  2s  .  com
public List<SecurityGroup> findAll() {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<SecurityGroup> query = builder.createQuery(SecurityGroup.class);

    List<SecurityGroup> securityGroups = getEntityManager().createQuery(query).getResultList();

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

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

    return securityGroups;
}

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

public Page<Receiver> findPage(Member member, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Receiver> criteriaQuery = criteriaBuilder.createQuery(Receiver.class);
    Root<Receiver> root = criteriaQuery.from(Receiver.class);
    criteriaQuery.select(root);/*from w  ww. j a  v  a2 s .c  o m*/
    if (member != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    }
    return super.findPage(criteriaQuery, pageable);
}

From source file:org.osiam.resource_server.storage.dao.ExtensionDao.java

/**
 * Retrieves the extension with the given URN from the database
 *
 * @param urn/*from   w w  w . jav a 2  s.c o m*/
 *        the URN of the extension to look up
 * @param caseInsensitive
 *        should the case of the URN be ignored
 * @return the extension entity
 */
public ExtensionEntity getExtensionByUrn(String urn, boolean caseInsensitive) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ExtensionEntity> cq = cb.createQuery(ExtensionEntity.class);
    Root<ExtensionEntity> extension = cq.from(ExtensionEntity.class);

    Predicate predicate;
    if (caseInsensitive) {
        predicate = cb.equal(cb.lower(extension.get(ExtensionEntity_.urn)), urn.toLowerCase(Locale.ENGLISH));
    } else {
        predicate = cb.equal(extension.get(ExtensionEntity_.urn), urn);
    }

    cq.select(extension).where(predicate);

    TypedQuery<ExtensionEntity> query = em.createQuery(cq);

    ExtensionEntity singleExtension;

    try {
        singleExtension = query.getSingleResult();
    } catch (NoResultException e) {
        throw new OsiamException("Could not find the Extension '" + urn + "'.", e);
    }

    return singleExtension;
}

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

@Override
public YhteystietoArvo findByOrganisaatioAndNimi(String organisaatioOid, String nimi) {

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

    Root<YhteystietoArvo> root = query.from(YhteystietoArvo.class);
    query.select(root);/*from w w w  .ja va 2 s.  c om*/

    Predicate organisaatioEquals = cb.equal(root.get("organisaatio").get("oid"), organisaatioOid);
    Predicate nameEquals = cb.equal(root.get("kentta").get("nimi"), nimi);

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

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

    //        Organisaatio org = organisaatioDAO.findBy("oid", organisaatioOid).get(0);
    //        Query query = getEntityManager().createQuery("SELECT x FROM YhteystietoArvo x " +
    //                "WHERE x.kentta.nimi = :nimi AND x.organisaatio.id = :organisaatioId");
    //        query.setParameter("nimi", nimi);
    //        query.setParameter("organisaatioId", org.getId());
    //        return (YhteystietoArvo) query.getSingleResult();
}