Example usage for javax.persistence.criteria Root get

List of usage examples for javax.persistence.criteria Root get

Introduction

In this page you can find the example usage for javax.persistence.criteria Root get.

Prototype

<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create a path corresponding to the referenced single-valued attribute.

Usage

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);/* w ww  .  java 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();
}

From source file:org.sloth.persistence.impl.ReportDaoImpl.java

@Override
public Collection<Report> getByUser(User u) throws NullPointerException, IllegalArgumentException {
    if (u == null) {
        throw new NullPointerException();
    }//from ww  w .ja v a  2 s .c om
    if (u.isNew()) {
        throw new IllegalArgumentException();
    }
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Report> cq = cb.createQuery(Report.class);
    Root<Report> r = cq.from(Report.class);
    cq.select(r).where(cb.equal(r.get(Report_.author), u));
    Collection<Report> result = getEntityManager().createQuery(cq).getResultList();
    logger.info("Found {} Reports by {}.", result.size(), u);
    return result;
}

From source file:org.sloth.persistence.impl.ReportDaoImpl.java

@Override
public Collection<Report> getByObservation(Observation o)
        throws NullPointerException, IllegalArgumentException {
    if (o == null) {
        throw new NullPointerException();
    }/*w ww  .  j a  v  a2s . com*/
    if (o.isNew()) {
        throw new IllegalArgumentException();
    }
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Report> cq = cb.createQuery(Report.class);
    Root<Report> r = cq.from(Report.class);
    cq.select(r).where(cb.equal(r.get(Report_.observation), o));
    Collection<Report> result = getEntityManager().createQuery(cq).getResultList();
    logger.info("{} Reports for Observation {}.", result.size(), o);
    return result;
}

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

@Override
public List<YhteystietoElementti> findByLisatietoIdAndKentanNimi(String yhteystietojenTyyppiOid,
        String kentanNimi) {/*from w  ww. ja  va2  s.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:com.github.aynu.mosir.core.enterprise.persistence.SmartRepositoryProducer.java

/**
 * ???//from www.  j  a va 2s  .  com
 * @return ?()
 */
@Produces
public SmartRepository<Testee, TesteeFilter> createTestee() {
    final SmartRepositoryListener<Testee, TesteeFilter> listener = new AbstractSmartRepositoryListener<Testee, TesteeFilter>() {
        @Override
        public CriteriaQuery<Testee> query(final CriteriaBuilder builder, final CriteriaQuery<Testee> query,
                final Root<Testee> root, final TesteeFilter filter) {
            query.select(root);
            if (StringUtils.isNotEmpty(filter.getCode())) {
                query.where(builder.equal(root.get("code"), filter.getCode()));
            }
            return query.orderBy(createOrders(builder, root, filter));
        }
    };
    return new SmartRepositoryImpl<>(Testee.class, manager, listener);
}

From source file:com.moderndrummer.data.MemberDaoImpl.java

@Override
public List<Member> findAllOrderedByName() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);

    criteria.select(member).orderBy(cb.asc(member.get("name")));
    return em.createQuery(criteria).getResultList();
}

From source file:org.oncoblocks.centromere.jpa.CentromereJpaRepository.java

/**
 * Returns a unsorted list of distinct values of the requested field.
 *
 * @param field Model field name.//  ww w.  j a v  a2s  . c om
 * @return Sorted list of distinct values of {@code field}.
 */
public Iterable<Object> distinct(String field) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(metadata.getJavaType());
    Root<T> root = query.from(metadata.getJavaType());
    query.select(root.get(field)).distinct(true);
    return (List) entityManager.createQuery(query).getResultList();
}

From source file:com.excilys.ebi.sample.jpa.query.benchmark.repository.impl.JPACriteriaRepository.java

@Override
public List<Song> getSongsBySameArtistOrderBySongTitle(final Integer songId) {

    // select s.artist.songs from Song s where s.id=?

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Song> query = builder.createQuery(Song.class);

    Root<Song> song1 = query.from(Song.class);
    Join<Artist, Song> artistSongs = song1.join(Song_.artist).join(Artist_.songs);

    query.select(artistSongs).where(builder.equal(song1.get(Song_.id), songId))
            .orderBy(builder.asc(artistSongs.get(Song_.title)));
    return em.createQuery(query).getResultList();
}

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

@Test
@Transactional//from  ww  w  . jav a2  s .co  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:com.moderndrummer.data.MemberDaoImpl.java

@Override
public Member findByEmail(String email) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = builder.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);

    criteria.select(member).where(builder.equal(member.get("email"), email));
    return em.createQuery(criteria).getSingleResult();
}