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: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: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));// w  w  w .ja  va 2  s. c o  m

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

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

public long count() {
    EntityManager em = EntityManagerUtil.getEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = builder.createQuery(Long.class);
    Root<T> from = cq.from(getEntityClass());
    List<Predicate> predicates = generatePredicateList(builder, from, cq);
    cq.where(predicates.toArray(new Predicate[predicates.size()]));
    cq.select(builder.count(from));/* w  w w  .j  a v a  2 s  .c  o  m*/
    TypedQuery<Long> q = em.createQuery(cq);
    return q.getSingleResult();
}

From source file:com.aimdek.ccm.dao.impl.BasicAbstractGenericDaoImpl.java

/**
 * {@inheritDoc}/* ww  w . j  av  a  2s.  c  o m*/
 */
public List<EntityType> retrieveAll(Class<EntityType> entityClass) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    return entityManager.createQuery(query).getResultList();
}

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

@Override
@Transactional//  ww  w. j av  a  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: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);/* ww w. ja va 2s.  c  o  m*/

    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();
}

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

@Override
public List<YhteystietoElementti> findAllKaytossa() {
    //        Query query = getEntityManager().createQuery("SELECT x FROM YhteystietoElementti x where x.kaytossa = true");
    //        return query.getResultList();

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

    Root<YhteystietoElementti> root = query.from(YhteystietoElementti.class);
    query.select(root);/* ww w .ja  v  a  2s. com*/

    Predicate whereClause = cb.equal(root.get("kaytossa"), true);
    query.where(whereClause);

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

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

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<Post> root = query.from(Post.class);
    query.select(root.get(Post_.id));/*from w w w.j av a 2  s .c  o m*/
    query.where(cb.equal(root.get(Post_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

From source file:com.aimdek.ccm.dao.impl.BasicAbstractGenericDaoImpl.java

/**
 * {@inheritDoc}//from   ww w .  j  a  va 2 s  .  c o m
 */
public EntityType findExact(String key, Object value, Class<EntityType> entityClass) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get(key), value));

    return entityManager.createQuery(query).getSingleResult();
}

From source file:com.greendot.db.jpa.core.AbstractSearchDao.java

@Transactional(readOnly = true)
public Optional<E> findByCharacterType(final String key, final String value) {

    notNull(key, "Mandatory argument 'key' is missing.");
    notNull(value, "Mandatory argument 'value' is missing.");
    final CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    final CriteriaQuery<E> query = builder.createQuery(getEntityType());
    final Root<E> root = query.from(getEntityType());
    query.select(root).where(builder.and(builder.equal(root.get(key), value))).distinct(true);
    final List<E> results = getEntityManager().createQuery(query).getResultList();
    if ((results != null) && (results.size() == 1))
        return Optional.of(results.get(0));
    return Optional.empty();
}