Example usage for javax.persistence TypedQuery getResultList

List of usage examples for javax.persistence TypedQuery getResultList

Introduction

In this page you can find the example usage for javax.persistence TypedQuery getResultList.

Prototype

List<X> getResultList();

Source Link

Document

Execute a SELECT query and return the query results as a typed List.

Usage

From source file:mobile.service.core.ClientLogService.java

/**
 * ?ClientLog/*from w  ww  .ja  va2  s. c o m*/
 * 
 * @param pageIndex ?1
 * @param pageSize ??
 * @param from ??
 * @return
 */
public static MobilePage<ClientLog> getPage(int pageIndex, int pageSize, String from) {
    String contentHql = " from MobileClientLog where 1=1 ";
    if (StringUtils.isNotBlank(from)) {
        contentHql += " and device = :device ";
    }
    String countHql = " select count(id) " + contentHql;
    contentHql = contentHql + " order by id desc ";

    TypedQuery<Long> countTypedQuery = JPA.em().createQuery(countHql, Long.class);
    TypedQuery<MobileClientLog> contentTypedQuery = JPA.em().createQuery(contentHql, MobileClientLog.class)
            .setFirstResult((pageIndex - 1) * pageSize).setMaxResults(pageSize);

    if (StringUtils.isNotBlank(from)) {
        countTypedQuery.setParameter("device", from);
        contentTypedQuery.setParameter("device", from);
    }

    Long count = countTypedQuery.getSingleResult();
    List<MobileClientLog> contentList = contentTypedQuery.getResultList();

    List<ClientLog> voList = new ArrayList<ClientLog>();
    if (CollectionUtils.isNotEmpty(contentList)) {
        for (MobileClientLog po : contentList) {
            voList.add(ClientLog.create(po));
        }
    }

    MobilePage<ClientLog> page = new MobilePage<ClientLog>(count, voList);
    return page;
}

From source file:net.triptech.buildulator.model.Person.java

/**
 * Find a person by their email address. If none is found null is returned.
 *
 * @param emailAddress the email address
 * @return the person//from   w  w w  .  ja  v  a  2s  . c o m
 */
public static Person findByEmailAddress(final String emailAddress) {

    Person person = null;

    if (StringUtils.isBlank(emailAddress)) {
        throw new IllegalArgumentException("The email address argument is required");
    }

    TypedQuery<Person> q = entityManager().createQuery(
            "SELECT p FROM Person" + " AS p WHERE LOWER(p.emailAddress) = LOWER(:emailAddress)", Person.class);
    q.setParameter("emailAddress", emailAddress);

    List<Person> people = q.getResultList();

    if (people != null && people.size() > 0) {
        person = people.get(0);
    }
    return person;
}

From source file:net.triptech.buildulator.model.Person.java

/**
 * Find a person by their openId identifier. If none is found null is returned.
 *
 * @param openIdIdentifier the openId identifier
 * @return the person/*from  www  .j  av a  2 s . c o m*/
 */
public static Person findByOpenIdIdentifier(final String openIdIdentifier) {

    Person person = null;

    if (StringUtils.isBlank(openIdIdentifier)) {
        throw new IllegalArgumentException("The openIdIdentifier identifier argument is required");
    }

    TypedQuery<Person> q = entityManager().createQuery(
            "SELECT p FROM Person" + " AS p WHERE LOWER(p.openIdIdentifier) = LOWER(:openIdIdentifier)",
            Person.class);
    q.setParameter("openIdIdentifier", openIdIdentifier);

    List<Person> people = q.getResultList();

    if (people != null && people.size() > 0) {
        person = people.get(0);
    }
    return person;
}

From source file:net.triptech.metahive.model.Submission.java

/**
 * Find submission entries.//  w w  w  . j av a2s.c  o  m
 *
 * @param filter the submission filter
 * @param firstResult the first result
 * @param maxResults the max results
 * @return the list
 */
public static List<Submission> findSubmissionEntries(final SubmissionFilter filter, final int firstResult,
        final int maxResults) {

    StringBuilder sql = new StringBuilder("SELECT s FROM Submission s");
    sql.append(buildWhere(filter));
    sql.append(" ORDER BY s.created ASC");

    TypedQuery<Submission> q = entityManager().createQuery(sql.toString(), Submission.class)
            .setFirstResult(firstResult).setMaxResults(maxResults);

    HashMap<String, Long> variables = buildVariables(filter);
    for (String variable : variables.keySet()) {
        q.setParameter(variable, variables.get(variable));
    }

    return q.getResultList();
}

From source file:net.triptech.metahive.model.KeyValue.java

/**
 * Find key values for the supplied Record.
 *
 * @param record the record// www .j  a  v a 2s.  c o m
 * @return the key value
 */
public static List<KeyValue> findKeyValues(final Record record) {

    List<KeyValue> keyValues = new ArrayList<KeyValue>();

    if (record == null) {
        throw new IllegalArgumentException("A valid record is required");
    }

    StringBuilder sql = new StringBuilder();

    sql.append("SELECT k FROM KeyValue AS k JOIN k.record r");
    sql.append(" WHERE r.id = :recordId");

    TypedQuery<KeyValue> q = entityManager().createQuery(sql.toString(), KeyValue.class);

    q.setParameter("recordId", record.getId());

    if (q.getResultList() != null) {
        for (KeyValue keyValue : q.getResultList()) {
            keyValues.add(keyValue);
        }
    }
    return keyValues;
}

From source file:au.org.ands.vocabs.toolkit.db.AccessPointUtils.java

/** Get all access points for a version.
 * @param version The version./*from  ww w. ja v a  2s  .c  o  m*/
 * @return The list of access points for this version.
 */
public static List<AccessPoint> getAccessPointsForVersion(final Version version) {
    EntityManager em = DBContext.getEntityManager();
    TypedQuery<AccessPoint> q = em.createNamedQuery(AccessPoint.GET_ACCESSPOINTS_FOR_VERSION, AccessPoint.class)
            .setParameter(AccessPoint.GET_ACCESSPOINTS_FOR_VERSION_VERSIONID, version.getId());
    List<AccessPoint> aps = q.getResultList();
    em.close();
    return aps;
}

From source file:net.triptech.metahive.model.KeyValue.java

/**
 * Find key values for the supplied Record.
 *
 * @param record the record//w w  w  .jav a  2s .com
 * @param definitions the list of definitions to lookup
 * @return the key value
 */
public static List<KeyValue> findKeyValues(final Record record, final List<Definition> definitions) {

    List<KeyValue> keyValues = new ArrayList<KeyValue>();

    if (record == null) {
        throw new IllegalArgumentException("A valid record is required");
    }

    if (definitions != null && definitions.size() > 0) {

        StringBuilder sql = new StringBuilder();
        StringBuilder where = new StringBuilder();

        sql.append("SELECT k FROM KeyValue AS k JOIN k.record r");
        sql.append(" LEFT JOIN k.definition d WHERE r.id = :recordId");

        sql.append(" AND (");
        for (Definition definition : definitions) {
            if (where.length() > 0) {
                where.append(" OR ");
            }
            where.append("d.id = ");
            where.append(definition.getId());
        }
        sql.append(where.toString());
        sql.append(")");

        TypedQuery<KeyValue> q = entityManager().createQuery(sql.toString(), KeyValue.class);

        q.setParameter("recordId", record.getId());

        if (q.getResultList() != null) {
            for (KeyValue keyValue : q.getResultList()) {
                keyValues.add(keyValue);
            }
        }
    }
    return keyValues;
}

From source file:net.triptech.metahive.model.KeyValue.java

/**
 * Find the related key values based on the supplied key value.
 *
 * @param def the definition//from  w w w  . j a  v a  2  s  .c o  m
 * @param primaryId the primary record id
 * @param secondaryId the secondary record id
 * @param tertiaryId the tertiary record id
 * @return the key value
 */
public static List<KeyValue> findRelatedKeyValues(final KeyValue keyValue) {

    List<KeyValue> relatedKeyValues = new ArrayList<KeyValue>();

    if (keyValue == null) {
        throw new IllegalArgumentException("A valid key value is required");
    }

    StringBuilder sql = new StringBuilder();
    HashMap<String, Object> variables = new HashMap<String, Object>();

    sql.append("SELECT k FROM KeyValue AS k LEFT JOIN k.definition d");
    sql.append(" WHERE d.id = :definition AND k.primaryRecordId = :primary");

    variables.put("definition", keyValue.getDefinition().getId());
    variables.put("primary", keyValue.getPrimaryRecordId());

    Applicability applicability = keyValue.getDefinition().getApplicability();

    if (applicability == Applicability.RECORD_SECONDARY) {
        sql.append(" AND k.secondaryRecordId = :secondary");
        variables.put("secondary", keyValue.getSecondaryRecordId());
    }
    if (applicability == Applicability.RECORD_TERTIARY) {
        sql.append(" AND k.tertiaryRecordId = :tertiary");
        variables.put("tertiary", keyValue.getTertiaryRecordId());
    }

    TypedQuery<KeyValue> q = entityManager().createQuery(sql.toString(), KeyValue.class);

    for (String key : variables.keySet()) {
        q.setParameter(key, variables.get(key));
    }

    if (q.getResultList() != null) {
        for (KeyValue kv : q.getResultList()) {
            relatedKeyValues.add(kv);
        }
    }
    System.out.println("Values: " + relatedKeyValues.size());

    return relatedKeyValues;
}

From source file:models.GroupMember.java

/**
 * /*ww  w .  java2s  .  c  o m*/
 * @param pageIndex 1
 * @param pageSize ??
 * @param groupId Id
 * @param excludeId GroupMember Id,null?
 * @return
 */
public static Page<GroupMember> queryPageByGroupId(int start, int pageSize, Long groupId, Long excludeId) {
    String hql = " from GroupMember where group.id = :groupId ";
    if (null != excludeId) {
        hql += " and id <> :excludeId";
    }
    String countHql = "select count(id) " + hql;

    TypedQuery<Long> countTypedQuery = JPA.em().createQuery(countHql, Long.class).setParameter("groupId",
            groupId);
    TypedQuery<GroupMember> contentTypedQuery = JPA.em().createQuery(hql, GroupMember.class)
            .setParameter("groupId", groupId).setFirstResult((start - 1) * pageSize).setMaxResults(pageSize);

    if (null != excludeId) {
        countTypedQuery.setParameter("excludeId", excludeId);
        contentTypedQuery.setParameter("excludeId", excludeId);
    }

    Long count = countTypedQuery.getSingleResult();
    List<GroupMember> resultList = contentTypedQuery.getResultList();
    return new Page<GroupMember>(Constants.SUCESS, count, resultList);
}

From source file:au.org.ands.vocabs.toolkit.db.AccessPointUtils.java

/** Get all access points of a certain type for a version.
 * @param version The version.//ww w  .  j a v a2 s . co m
 * @param type The type of access point to look for.
 * @return The list of access points for this version.
 */
public static List<AccessPoint> getAccessPointsForVersionAndType(final Version version, final String type) {
    EntityManager em = DBContext.getEntityManager();
    TypedQuery<AccessPoint> q = em
            .createNamedQuery(AccessPoint.GET_ACCESSPOINTS_FOR_VERSION_AND_TYPE, AccessPoint.class)
            .setParameter(AccessPoint.GET_ACCESSPOINTS_FOR_VERSION_AND_TYPE_VERSIONID, version.getId())
            .setParameter(AccessPoint.GET_ACCESSPOINTS_FOR_VERSION_AND_TYPE_TYPE, type);
    List<AccessPoint> aps = q.getResultList();
    em.close();
    return aps;
}