Example usage for javax.persistence EntityManager getCriteriaBuilder

List of usage examples for javax.persistence EntityManager getCriteriaBuilder

Introduction

In this page you can find the example usage for javax.persistence EntityManager getCriteriaBuilder.

Prototype

public CriteriaBuilder getCriteriaBuilder();

Source Link

Document

Return an instance of CriteriaBuilder for the creation of CriteriaQuery objects.

Usage

From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java

/**
 * Builds the Typed Query/*from  w  w w  . j  av  a2 s  .co m*/
 * @return
 */
public TypedQuery<Connection> buildSingleRelationship() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class);
    Root<Connection> connection = criteria.from(Connection.class);

    Predicate predicate = null;
    if (this.sender != null && this.receiver != null) {
        predicate = cb.equal(connection.get(Connection_.senderId), sender.getId());
        predicate = cb.and(predicate, cb.equal(connection.get(Connection_.receiverId), receiver.getId()));
    }

    CriteriaQuery<Connection> select = criteria.select(connection).distinct(true);
    select.where(predicate);
    TypedQuery<Connection> typedQuery = em.createQuery(select);

    return typedQuery;
}

From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java

public TypedQuery<Connection> buildFilter() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class);
    Root<Connection> connection = criteria.from(Connection.class);
    Join<Connection, Profile> receiver = connection.join(Connection_.receiver);
    ////ww  w  . j  a v a  2 s.  com
    CriteriaQuery<Connection> select = criteria.select(connection);
    select.where(buildPredicateFilter(cb, receiver, connection));
    select.orderBy(cb.asc(receiver.get(Profile_.fullName)));
    //
    TypedQuery<Connection> typedQuery = em.createQuery(select);
    if (this.limit > 0) {
        typedQuery.setFirstResult((int) offset);
        typedQuery.setMaxResults((int) limit);
    }
    //
    return typedQuery;
}

From source file:org.apache.ranger.service.XTrxLogService.java

@Override
public VXTrxLogList searchXTrxLogs(SearchCriteria searchCriteria) {
    EntityManager em = daoMgr.getEntityManager();
    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<VXXTrxLog> selectCQ = criteriaBuilder.createQuery(VXXTrxLog.class);
    Root<VXXTrxLog> rootEntityType = selectCQ.from(VXXTrxLog.class);
    Predicate predicate = generatePredicate(searchCriteria, em, criteriaBuilder, rootEntityType);

    selectCQ.where(predicate);/*from   w  w  w.j a va2 s.  c  o  m*/
    if ("asc".equalsIgnoreCase(searchCriteria.getSortType())) {
        selectCQ.orderBy(criteriaBuilder.asc(rootEntityType.get("createTime")));
    } else {
        selectCQ.orderBy(criteriaBuilder.desc(rootEntityType.get("createTime")));
    }
    int startIndex = searchCriteria.getStartIndex();
    int pageSize = searchCriteria.getMaxRows();
    List<VXXTrxLog> resultList = em.createQuery(selectCQ).setFirstResult(startIndex).setMaxResults(pageSize)
            .getResultList();

    List<VXTrxLog> trxLogList = new ArrayList<VXTrxLog>();
    for (VXXTrxLog xTrxLog : resultList) {
        VXTrxLog trxLog = mapCustomViewToViewObj(xTrxLog);

        if (trxLog.getUpdatedBy() != null) {
            XXPortalUser xXPortalUser = rangerDaoManager.getXXPortalUser()
                    .getById(Long.parseLong(trxLog.getUpdatedBy()));
            if (xXPortalUser != null) {
                trxLog.setOwner(xXPortalUser.getLoginId());
            }
        }

        trxLogList.add(trxLog);
    }

    VXTrxLogList vxTrxLogList = new VXTrxLogList();
    vxTrxLogList.setStartIndex(startIndex);
    vxTrxLogList.setPageSize(pageSize);
    vxTrxLogList.setVXTrxLogs(trxLogList);
    return vxTrxLogList;
}

From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java

/**
 * Builds the Typed Query//from   w  w  w.jav  a2s  .co m
 * @return
 */
public TypedQuery<Long> buildCount() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Connection> connection = criteria.from(Connection.class);

    Predicate predicate = null;
    //owner
    if (this.owner != null) {
        predicate = cb.equal(connection.get(Connection_.senderId), owner.getId());
    }
    //status
    if (this.status != null) {
        if (Relationship.Type.PENDING.equals(this.status)) {
            predicate = cb.and(predicate, addInClause(cb, connection.get(Connection_.status), types));
        } else {
            predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status));
        }
    }

    CriteriaQuery<Long> select = criteria.select(cb.countDistinct(connection));
    select.where(predicate);

    return em.createQuery(select);
}

From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java

public TypedQuery<Connection> buildLastConnections() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class);
    Root<Connection> connection = criteria.from(Connection.class);

    Predicate predicate = null;/* www . j a  va 2s. co  m*/
    //owner
    if (this.owner != null) {
        predicate = cb.equal(connection.get(Connection_.senderId), owner.getId());
    }
    //status
    if (this.status != null) {
        predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status));
    }

    CriteriaQuery<Connection> select = criteria.select(connection).distinct(true);
    select.where(predicate);
    select.orderBy(cb.desc(connection.<Long>get(Connection_.id)));

    TypedQuery<Connection> typedQuery = em.createQuery(select);
    if (this.limit > 0) {
        typedQuery.setFirstResult((int) offset);
        typedQuery.setMaxResults((int) limit);
    }

    return typedQuery;
}

From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java

/**
 * Builds the Typed Query/*  w w w  . j a va2 s  .co  m*/
 * @return
 */
public TypedQuery<Connection> build() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class);
    Root<Connection> connection = criteria.from(Connection.class);

    Predicate predicate = null;
    //owner
    if (this.owner != null) {
        predicate = cb.equal(connection.get(Connection_.senderId), owner.getId());
    }
    //status
    if (this.status != null) {
        if (Relationship.Type.PENDING.equals(this.status)) {
            predicate = cb.and(predicate, addInClause(cb, connection.get(Connection_.status), types));
        } else {
            predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status));
        }
    }

    CriteriaQuery<Connection> select = criteria.select(connection).distinct(true);
    select.where(predicate);

    TypedQuery<Connection> typedQuery = em.createQuery(select);
    if (this.limit > 0) {
        typedQuery.setFirstResult((int) offset);
        typedQuery.setMaxResults((int) limit);
    }

    return typedQuery;
}

From source file:net.echinopsii.ariane.community.plugin.rabbitmq.directory.RabbitmqDirectoryBootstrap.java

private void plugDirectoryJPAProvider() {
    Company pivotal = null;/*from  ww w.j  av a2  s .  c  o  m*/
    Application rabbitmq = null;

    directoryJpaProvider.addSubPersistenceBundle(FrameworkUtil.getBundle(RabbitmqDirectoryBootstrap.class));

    EntityManager em = directoryJpaProvider.createEM();
    CriteriaBuilder builder = em.getCriteriaBuilder();

    CriteriaQuery<Company> cmpCriteria = builder.createQuery(Company.class);
    Root<Company> cmpRoot = cmpCriteria.from(Company.class);
    cmpCriteria.select(cmpRoot).where(builder.equal(cmpRoot.<String>get("name"), "Pivotal"));
    TypedQuery<Company> cmpQuery = em.createQuery(cmpCriteria);
    try {
        pivotal = cmpQuery.getSingleResult();
        log.debug("Pivotal company already defined ...");
    } catch (NoResultException e) {
        log.debug("Pivotal company will be defined ...");
    } catch (Exception e) {
        throw e;
    }

    CriteriaQuery<Application> appCriteria = builder.createQuery(Application.class);
    Root<Application> appRoot = appCriteria.from(Application.class);
    appCriteria.select(appRoot).where(builder.equal(appRoot.<String>get("name"), "RabbitMQ"));
    TypedQuery<Application> appQuery = em.createQuery(appCriteria);
    try {
        rabbitmq = appQuery.getSingleResult();
        log.debug("RabbitMQ application already defined ...");
    } catch (NoResultException e) {
        log.debug("RabbitMQ application will be defined ...");
    } catch (Exception e) {
        throw e;
    }

    em.getTransaction().begin();

    if (pivotal == null) {
        pivotal = new Company().setNameR("Pivotal").setDescriptionR("Pivotal");
        em.persist(pivotal);
    }

    if (rabbitmq == null) {
        rabbitmq = new Application().setNameR("RabbitMQ").setCompanyR(pivotal).setShortNameR("RabbitMQ")
                .setColorCodeR("ff6600").setDescriptionR("Robust messaging for applications");
        em.persist(rabbitmq);
    }

    if (!pivotal.getApplications().contains(rabbitmq)) {
        pivotal.getApplications().add(rabbitmq);
    }

    em.flush();
    em.getTransaction().commit();
}

From source file:fr.amapj.service.services.authentification.PasswordManager.java

/**
 * Retrouve l'utilisateur avec cet e-mail
 * Retourne null si non trouv ou autre problme
 *///from  ww  w . ja  v  a  2  s. c  om
private Utilisateur findUser(String email, EntityManager em) {
    if ((email == null) || email.equals("")) {
        return null;
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();

    CriteriaQuery<Utilisateur> cq = cb.createQuery(Utilisateur.class);
    Root<Utilisateur> root = cq.from(Utilisateur.class);

    // On ajoute la condition where 
    cq.where(cb.equal(root.get(Utilisateur.P.EMAIL.prop()), email));

    List<Utilisateur> us = em.createQuery(cq).getResultList();
    if (us.size() == 0) {
        return null;
    }

    if (us.size() > 1) {
        logger.warn("Il y a plusieurs utilisateurs avec l'adresse " + email);
        return null;
    }

    return us.get(0);
}

From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java

/**
 * list all entity//w w w  .j av a 2 s .com
 *
 * @return
 */
public List<T> findAll() {
    EntityManager em = getEntityManager();
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return em.createQuery(cq).getResultList();
}

From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java

/**
 * find entity in the range//from ww w . j a  v  a  2 s . c  om
 *
 * @param range
 * @return
 */
public List<T> findRange(int[] range) {
    EntityManager em = getEntityManager();
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0]);
    q.setFirstResult(range[0]);
    return q.getResultList();
}