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:br.com.compositeam.data.MemberDaoImpl.java

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

    /*/*w w  w .  jav a  2  s. c o m*/
     * Swap criteria statements if you would like to try out type-safe criteria queries, a new
     * feature in JPA 2.0 criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
     */

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

From source file:br.com.compositeam.data.MemberDaoImpl.java

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

    /*//from  w  w  w.  j  a va 2  s.  c  o  m
     * Swap criteria statements if you would like to try out type-safe criteria queries, a new
     * feature in JPA 2.0 criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
     */

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

From source file:br.ufba.dcc.mestrado.computacao.repository.impl.ProjectRepositoryImpl.java

@Override
public OpenHubProjectEntity findByName(String name) {
    CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<OpenHubProjectEntity> criteriaQuery = criteriaBuilder.createQuery(getEntityClass());

    Root<OpenHubProjectEntity> root = criteriaQuery.from(getEntityClass());
    criteriaQuery.select(root);/* w  w w  .j a  va2  s . co  m*/

    Predicate predicate = criteriaBuilder.equal(root.get("name"), name);
    criteriaQuery.where(predicate);

    TypedQuery<OpenHubProjectEntity> query = getEntityManager().createQuery(criteriaQuery);

    OpenHubProjectEntity projectEntity = query.getSingleResult();

    return projectEntity;
}

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

public Page<Message> findDraftPage(Member sender, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Message> criteriaQuery = criteriaBuilder.createQuery(Message.class);
    Root<Message> root = criteriaQuery.from(Message.class);
    criteriaQuery.select(root);//from  w w w.j  a v a2  s .  c  om
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forMessage")),
            criteriaBuilder.equal(root.get("isDraft"), true));
    if (sender != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("sender"), sender));
    } else {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("sender")));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

From source file:se.kth.csc.persist.JPAStore.java

@Override
public Account fetchAccountWithPrincipalName(String principalName) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Account> q = cb.createQuery(Account.class);

    Root<Account> account = q.from(Account.class);

    try {/*  w  w  w.j ava  2s  .  c o  m*/
        return entityManager
                .createQuery(
                        q.select(account).where(cb.equal(account.get(Account_.principalName), principalName)))
                .getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:net.shopxx.dao.impl.MemberDaoImpl.java

public Page<Member> findPage(Member.RankingType rankingType, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Member> criteriaQuery = criteriaBuilder.createQuery(Member.class);
    Root<Member> root = criteriaQuery.from(Member.class);
    criteriaQuery.select(root);/*  w  w  w. ja  v  a2  s  . c om*/
    if (rankingType != null) {
        switch (rankingType) {
        case point:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("point")));
            break;
        case balance:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("balance")));
            break;
        case amount:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("amount")));
            break;
        }
    }
    return super.findPage(criteriaQuery, pageable);
}

From source file:com.expressui.core.dao.security.query.RoleQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Role> query, Root<Role> role) {
    List<Predicate> predicates = new ArrayList<Predicate>();

    if (hasValue(name)) {
        ParameterExpression<String> nameExp = builder.parameter(String.class, "name");
        predicates.add(builder.like(builder.upper(role.<String>get("name")), nameExp));
    }/*w ww  . ja v a2 s. c  om*/

    if (hasValue(doesNotBelongToUser)) {
        Subquery<Role> subquery = query.subquery(Role.class);
        Root<UserRole> userRole = subquery.from(UserRole.class);
        ParameterExpression<User> userExp = builder.parameter(User.class, "doesNotBelongToUser");
        subquery.select(userRole.<Role>get("role")).where(builder.equal(userRole.get("user"), userExp));
        predicates.add(builder.not(role.in(subquery)));
    }

    return predicates;
}

From source file:org.jboss.spring.quickstarts.kitchensink.kitchensink_spring.repo.MemberDaoImpl.java

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

    /*// w  w  w . j  a va2 s . co m
     * Swap criteria statements if you would like to try out type-safe criteria queries, a new
     * feature in JPA 2.0 criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
     */

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

From source file:net.shopxx.dao.impl.ProductNotifyDaoImpl.java

public Long count(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class);
    Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class);
    criteriaQuery.select(root);//from w  ww .j a va 2  s.c  o m
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (isMarketable != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("product").get("goods").get("isMarketable"), isMarketable));
    }
    if (isOutOfStock != null) {
        Path<Integer> stock = root.get("product").get("stock");
        Path<Integer> allocatedStock = root.get("product").get("allocatedStock");
        if (isOutOfStock) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock),
                    criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock),
                    criteriaBuilder.greaterThan(stock, allocatedStock)));
        }
    }
    if (hasSent != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:net.shopxx.dao.impl.ProductNotifyDaoImpl.java

public Page<ProductNotify> findPage(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent,
        Pageable pageable) {/*from w ww .  j a  v  a  2 s  .  c  o  m*/
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class);
    Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (isMarketable != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("product").get("goods").get("isMarketable"), isMarketable));
    }
    if (isOutOfStock != null) {
        Path<Integer> stock = root.get("product").get("stock");
        Path<Integer> allocatedStock = root.get("product").get("allocatedStock");
        if (isOutOfStock) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock),
                    criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock),
                    criteriaBuilder.greaterThan(stock, allocatedStock)));
        }
    }
    if (hasSent != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}