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:net.groupbuy.dao.impl.FriendLinkDaoImpl.java

public List<FriendLink> findList(Type type) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<FriendLink> criteriaQuery = criteriaBuilder.createQuery(FriendLink.class);
    Root<FriendLink> root = criteriaQuery.from(FriendLink.class);
    criteriaQuery.select(root);/* w w w .  j a  v  a  2  s.co  m*/
    if (type != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("type"), type));
    }
    criteriaQuery.orderBy(criteriaBuilder.asc(root.get("order")));
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
}

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

public List<Navigation> findList(Position position) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Navigation> criteriaQuery = criteriaBuilder.createQuery(Navigation.class);
    Root<Navigation> root = criteriaQuery.from(Navigation.class);
    criteriaQuery.select(root);/*from w  w  w.  j  a v a 2  s .c o m*/
    if (position != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("position"), position));
    }
    criteriaQuery.orderBy(criteriaBuilder.asc(root.get("order")));
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
}

From source file:net.carinae.dev.async.dao.QueuedTaskHolderDaoJPA2.java

@Override
public QueuedTaskHolder findRandomStalledTask() {

    Calendar TOO_LONG_AGO = Calendar.getInstance();
    TOO_LONG_AGO.add(Calendar.SECOND, -7200);

    // select qth from QueuedTask where 
    //      qth.startedStamp != null AND
    //      qth.startedStamp < TOO_LONG_AGO
    CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<QueuedTaskHolder> cq = cb.createQuery(QueuedTaskHolder.class);
    Root<QueuedTaskHolder> qth = cq.from(QueuedTaskHolder.class);
    cq.select(qth).where(cb.and(cb.isNull(qth.get(QueuedTaskHolder_.completedStamp)),
            cb.lessThan(qth.get(QueuedTaskHolder_.startedStamp), TOO_LONG_AGO)));

    List<QueuedTaskHolder> stalledTasks = this.entityManager.createQuery(cq).getResultList();

    if (stalledTasks.isEmpty()) {
        return null;
    } else {/*from   w  ww . j  a  v  a  2s .  c  o m*/
        Random rand = new Random(System.currentTimeMillis());
        return stalledTasks.get(rand.nextInt(stalledTasks.size()));
    }

}

From source file:csns.model.core.dao.jpa.UserDaoImpl.java

@Override
public List<User> getUsers(Long ids[]) {
    if (ids == null || ids.length < 1)
        return new ArrayList<User>();

    CriteriaBuilder cbuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> cquery = cbuilder.createQuery(User.class);
    Root<User> user = cquery.from(User.class);

    Predicate criteria = cbuilder.equal(user.get("id"), ids[0]);
    for (int i = 1; i < ids.length; ++i)
        criteria = cbuilder.or(criteria, cbuilder.equal(user.get("id"), ids[i]));
    cquery.where(criteria);/*from w  w w .j a  v  a  2 s .c om*/

    cquery.orderBy(cbuilder.asc(user.get("lastName")), cbuilder.asc(user.get("firstName")));

    return entityManager.createQuery(cquery).getResultList();
}

From source file:controllers.AuthController.java

@UnitOfWork
@FilterWith(CTCheck.class)
public Result login(Context context, Session session, User req) {
    EntityManager entitymanager = entitiyManagerProvider.get();
    CriteriaBuilder cb = entitymanager.getCriteriaBuilder();

    try {/*from w  w  w.jav  a  2s. c  om*/
        CriteriaQuery<User> query = cb.createQuery(User.class);
        Root<User> a = query.from(User.class);
        query.where(cb.equal(a.get(User_.email), req.getEmail()),
                cb.equal(a.get(User_.password), req.getPassword()));
        User u = entitymanager.createQuery(query).getSingleResult();
        String token = SessionIdentifierGenerator.nextSessionId();
        context.addCookie(Cookie.builder("token", token).build());
        session.put("token", token);
        session.put("email", u.getEmail());
        session.put("id", String.valueOf(u.getId()));
        ninjaCache.set(token, u.getId());
        return Results.json().render(new RespAuth(u.getId(), token, u.getEmail()));
    } catch (Exception e) {
        e.printStackTrace();
        return Results.json().status(400).render(new JSendResp(400, e));
    }
}

From source file:nc.noumea.mairie.organigramme.core.dao.PersistentManager.java

@SuppressWarnings("unchecked")
private TypedQuery<T> constructTypedQueryByPropertyOrderBy(Class<? extends T> classe, String property,
        Object value, String orderByProperty) {
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<T> c = (CriteriaQuery<T>) qb.createQuery(classe);
    Root<T> p = (Root<T>) c.from(classe);
    Predicate condition = qb.equal(p.get(property), value);
    c.where(condition);/*ww w.  jav a  2s  .c om*/
    if (orderByProperty != null) {
        c.orderBy(qb.asc(p.get(orderByProperty)));
    }
    TypedQuery<T> q = em.createQuery(c);
    return q;
}

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

/**
 * {@inheritDoc}/*w  ww.  j  a  v  a 2s.  c  om*/
 */
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();
}

From source file:org.jboss.spring3_2.example.MatrixVariables.repo.MemberDaoImpl.java

public List<Member> findByNameAndEmail(String name, String email) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = builder.createQuery(Member.class);
    EntityType<Member> type = em.getMetamodel().entity(Member.class);
    Root<Member> member = criteria.from(Member.class);
    if (name != null && !name.isEmpty() && name != "")
        criteria.where(builder.like(member.get(type.getDeclaredSingularAttribute("name", String.class)),
                "%" + name + "%"));
    if (email != null && !email.isEmpty() && email != "")
        criteria.where(builder.like(member.get(type.getDeclaredSingularAttribute("email", String.class)),
                "%" + email + "%"));
    return em.createQuery(criteria).getResultList();
}

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

/**
 * {@inheritDoc}//from ww  w  . j av  a  2s . c  o m
 */
public EntityType findById(IDType id, 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(FIELD_CONSTANT_ID), id));

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