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:org.openmeetings.app.data.basic.Sessionmanagement.java

public Sessiondata getSessionByHash(String SID) {
    try {/*from   ww  w  .  jav a2s .c o m*/
        log.debug("updateUser User SID: " + SID);

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
        Root<Sessiondata> c = cq.from(Sessiondata.class);
        Predicate condition = cb.equal(c.get("session_id"), SID);
        cq.where(condition);

        TypedQuery<Sessiondata> q = em.createQuery(cq);

        List<Sessiondata> fullList = q.getResultList();
        if (fullList.size() == 0) {
            log.error("Could not find session to update: " + SID);
            return null;
        }

        Sessiondata sd = fullList.get(0);

        return sd;
    } catch (Exception ex2) {
        log.error("[updateUser]: ", ex2);
    }
    return null;
}

From source file:com.expressui.sample.view.account.AccountQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Account> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(name)) {
        ParameterExpression<String> p = builder.parameter(String.class, "name");
        criteria.add(builder.like(builder.upper(rootEntity.<String>get("name")), p));
    }/*ww  w .  ja  va  2s .  co  m*/
    if (!isEmpty(states)) {
        ParameterExpression<Set> p = builder.parameter(Set.class, "states");
        criteria.add(builder.in(rootEntity.get("billingAddress").get("state")).value(p));
    }
    if (!isEmpty(country)) {
        ParameterExpression<Country> p = builder.parameter(Country.class, "country");
        criteria.add(builder.equal(rootEntity.get("billingAddress").get("country"), p));
    }

    return criteria;
}

From source file:eu.uqasar.service.user.UserService.java

public List<User> getAllExceptAndFilter(Collection<User> usersToExclude, final String filter) {
    if (usersToExclude == null || usersToExclude.isEmpty()) {
        return this.getAll();
    }/*  w  ww  . j  a  v  a2s .co  m*/
    logger.infof("loading all Users except %s ...", usersToExclude);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    if (filter != null && !filter.isEmpty()) {

        Expression<String> literal = cb.upper(cb.literal(LIKE_WILDCARD + filter + LIKE_WILDCARD));
        Predicate likeFirstName = cb.like(cb.upper(root.get(User_.firstName)), literal);
        Predicate likeLastName = cb.like(cb.upper(root.get(User_.lastName)), literal);
        Predicate likeUserName = cb.like(cb.upper(root.get(User_.userName)), literal);
        Predicate orLikeName = cb.or(likeFirstName, likeLastName, likeUserName);
        query.where(cb.and(cb.not(root.in(usersToExclude)), orLikeName));
    } else {
        query.where(cb.not(root.in(usersToExclude)));
    }
    return em.createQuery(query).getResultList();
}

From source file:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * Get the latest date of measurement snapshots
 * @return//from  w w w. j  a  v  a 2  s  .c o m
 */
private Date getLatestDate() {
    logger.info("Get the latest date from CUBES measurements...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    query.select(root);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).setMaxResults(1).getSingleResult().getTimeStamp();
}

From source file:org.openmeetings.app.data.basic.Sessionmanagement.java

/**
 * update the session every time a user makes a request
 * /*from w  w  w. j  av  a 2 s .  co m*/
 * @param SID
 */
private void updatesession(String SID) {
    try {
        // log.debug("****** updatesession: "+SID);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
        Root<Sessiondata> c = cq.from(Sessiondata.class);
        Predicate condition = cb.equal(c.get("session_id"), SID);
        cq.where(condition);

        TypedQuery<Sessiondata> q = em.createQuery(cq);

        List<Sessiondata> fullList = q.getResultList();
        if (fullList.size() == 0) {
            log.error("Found NO session to updateSession: ");

        } else {
            // log.debug("Found session to updateSession: ");
            Sessiondata sd = fullList.iterator().next();
            // log.debug("Found session to updateSession sd "+sd.getUser_id()+" "+sd.getSession_id());
            sd.setRefresh_time(new Date());

            if (sd.getId() == null) {
                em.persist(sd);
            } else {
                if (!em.contains(sd)) {
                    em.merge(sd);
                }
            }
        }

    } catch (Exception ex2) {
        log.error("[updatesession]: ", ex2);
    }
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * SELECT e FROM jpa_query_employee e WHERE e.name= :name
 *//*from  w  ww . ja  v a2s .  c om*/
@Transactional
public void doParameterQuery() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> e = c.from(Employee.class);

    // parameter, equal to ":name"
    ParameterExpression<String> paraName = cb.parameter(String.class, "name");

    // e.name = ":name"
    c.select(e).where(cb.equal(e.get("name"), paraName));

    // set param value
    TypedQuery<Employee> query = em.createQuery(c);
    query.setParameter("name", "employee_1");
    List<Employee> result = query.getResultList();
    ResultViewer.showResult(result, "criteria query : parameter query");
}

From source file:ch.sdi.plugins.oxwall.job.OxSqlJob.java

/**
 * @see ch.sdi.core.intf.SqlJob#isAlreadyPresent(ch.sdi.core.impl.data.Person)
 *///from  w  w w  .  j  av a  2 s. c  o  m
@Override
public boolean isAlreadyPresent(Person<?> aPerson) throws SdiException {
    if (myDryRun && !myCheckDuplicateOnDryRun) {
        myLog.debug("DryRun is active. Not checking for duplicate person");
        return false;
    } // if myDryRun

    CriteriaBuilder cb = myEntityManager.getCriteriaBuilder();

    CriteriaQuery<OxUser> criteria = cb.createQuery(OxUser.class);
    Root<OxUser> root = criteria.from(OxUser.class);
    ParameterExpression<String> mailParam = cb.parameter(String.class);
    criteria.select(root).where(cb.equal(root.get("email"), mailParam));

    TypedQuery<OxUser> queryEMail = myEntityManager.createQuery(criteria);

    queryEMail.setParameter(mailParam, aPerson.getEMail());
    List<OxUser> results = queryEMail.getResultList();

    if (results.size() > 0) {
        myLog.debug("given Person is already present: " + results.get(0));
        return true;
    } // if results.size() > 0

    return false;
}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * load specifications//ww w. ja v a  2  s.  c  om
 */
private void loadList() throws UCMException {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Specification> c = cb.createQuery(Specification.class);
    Root<Specification> obj = c.from(Specification.class);
    c.select(obj).where(cb.equal(obj.get("project"), this.project)).orderBy(cb.asc(obj.get("id")));
    this.specifications = em.createQuery(c).getResultList();

}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * Load resources/*from  www.  ja v a  2  s  .co m*/
 * 
 * @param project
 */
private void loadFeatures(final Project project) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Feature> c = cb.createQuery(Feature.class);
    Root<Feature> obj = c.from(Feature.class);
    c.select(obj);
    c.where(cb.equal(obj.get("project"), project));
    c.orderBy(cb.asc(obj.get("id")));
    this.features = em.createQuery(c).getResultList();
}

From source file:org.openmeetings.app.data.basic.Sessionmanagement.java

public Boolean updateUserRemoteSession(String SID, String sessionXml) {
    try {/*from  w  w  w.  j a v a  2 s . c o m*/
        log.debug("updateUser User SID: " + SID);

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
        Root<Sessiondata> c = cq.from(Sessiondata.class);
        Predicate condition = cb.equal(c.get("session_id"), SID);
        cq.where(condition);

        TypedQuery<Sessiondata> q = em.createQuery(cq);
        List<Sessiondata> fullList = q.getResultList();

        if (fullList.size() == 0) {
            log.error("Could not find session to update: " + SID);
            return false;
        } else {
            // log.error("Found session to update: "+SID);
        }
        Sessiondata sd = fullList.get(0);
        // log.debug("Found session to update: "+sd.getSession_id()+
        // " userId: "+USER_ID);

        sd.setRefresh_time(new Date());
        sd.setSessionXml(sessionXml);

        if (sd.getId() == null) {
            em.persist(sd);
        } else {
            if (!em.contains(sd)) {
                em.merge(sd);
            }
        }
        // log.debug("session updated User: "+USER_ID);
        return true;
    } catch (Exception ex2) {
        log.error("[updateUserRemoteSession]: ", ex2);
    }
    return null;
}