Example usage for javax.persistence TypedQuery setParameter

List of usage examples for javax.persistence TypedQuery setParameter

Introduction

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

Prototype

TypedQuery<X> setParameter(int position, Object value);

Source Link

Document

Bind an argument value to a positional parameter.

Usage

From source file:hr.diskobolos.persistence.impl.EvaluationAnswerPersistenceImpl.java

@Override
public Map<MemberRegister, Integer> fetchTotalPointsPerMemberRegister(QuestionnaireType questionnaireType) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> cq = cb.createTupleQuery();
    Root<EvaluationAnswer> evaluationAnswer = cq.from(EvaluationAnswer.class);
    Join<EvaluationAnswer, QuestionChoicesDef> choiceDef = evaluationAnswer.join(EvaluationAnswer_.answer);
    Join<QuestionChoicesDef, EvaluationQuestionDef> questionDef = choiceDef
            .join(QuestionChoicesDef_.evaluationQuestionDef);
    ParameterExpression<QuestionnaireType> questionnaireTypeParam = cb.parameter(QuestionnaireType.class,
            "questionnaireType");
    Expression<Integer> value = evaluationAnswer.get(EvaluationAnswer_.answer).get(QuestionChoicesDef_.value)
            .as(Integer.class);
    cq.multiselect(evaluationAnswer.get(EvaluationAnswer_.memberRegister).alias("memberRegister"),
            value.alias("value"));
    cq.where(cb.equal(questionDef.get(EvaluationQuestionDef_.questionnaireType), questionnaireTypeParam));
    TypedQuery<Tuple> query = entityManager.createQuery(cq);
    query.setParameter("questionnaireType", questionnaireType);
    List<Tuple> result = query.getResultList();
    Map<MemberRegister, Integer> pointsPerMemberRegister = result.stream().collect(Collectors.groupingBy(t -> {
        return t.get("memberRegister", MemberRegister.class);
    }, Collectors.summingInt(t -> t.get("value", Integer.class))));

    return pointsPerMemberRegister;
}

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

public ErrorValues getErrorValuesById(Long errorvalues_id) {
    try {/*from   w  w w  . j av a2s .c o  m*/
        String hql = "select c from ErrorValues as c " + " where c.errorvalues_id = :errorvalues_id "
                + " AND c.deleted <> :deleted";
        TypedQuery<ErrorValues> query = em.createQuery(hql, ErrorValues.class);
        query.setParameter("errorvalues_id", errorvalues_id);
        query.setParameter("deleted", "true");
        ErrorValues e = null;
        try {
            e = query.getSingleResult();
        } catch (NoResultException ex) {
        }
        return e;
    } catch (Exception ex2) {
        log.error("[getErrorValuesById]", ex2);
    }
    return null;
}

From source file:csns.model.academics.dao.jpa.ProgramDaoImpl.java

@Override
public List<Program> searchPrograms(String term, int maxResults) {
    TypedQuery<Program> query = entityManager.createNamedQuery("program.search", Program.class);
    if (maxResults > 0)
        query.setMaxResults(maxResults);
    return query.setParameter("term", term).getResultList();
}

From source file:example.springdata.jpa.showcase.before.CustomerServiceImpl.java

@Override
public List<Customer> findByLastname(String lastname, int page, int pageSize) {

    TypedQuery<Customer> query = em.createQuery("select c from Customer c where c.lastname = ?1",
            Customer.class);

    query.setParameter(1, lastname);
    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);/*from w  w  w  .j  ava  2 s.c  o  m*/

    return query.getResultList();
}

From source file:org.mitre.uma.repository.impl.JpaPermissionRepository.java

@Override
public PermissionTicket getByTicket(String ticket) {
    TypedQuery<PermissionTicket> query = em.createNamedQuery(PermissionTicket.QUERY_TICKET,
            PermissionTicket.class);
    query.setParameter(PermissionTicket.PARAM_TICKET, ticket);
    return JpaUtil.getSingleResult(query.getResultList());
}

From source file:org.mitre.openid.connect.repository.impl.JpaWhitelistedSiteRepository.java

@Override
@Transactional(value = "defaultTransactionManager")
public Collection<WhitelistedSite> getByCreator(String creatorId) {
    TypedQuery<WhitelistedSite> query = manager.createNamedQuery(WhitelistedSite.QUERY_BY_CREATOR,
            WhitelistedSite.class);
    query.setParameter(WhitelistedSite.PARAM_USER_ID, creatorId);

    return query.getResultList();
}

From source file:com.simplecrud.dao.LogInDao.java

/**
 * Find the user if exist in the database:
 *
 * @param username/*from  w  w  w .  ja v a 2 s .  c  o m*/
 * @param password
 * @return Object
 */
public Object getSpecificUsername(String username, String password) {

    TypedQuery<NewMember> q = em.createQuery(
            "SELECT n.username FROM NewMember n WHERE n.username = :username AND n.password = :password",
            NewMember.class);
    q.setParameter("username", username);
    q.setParameter("password", password);
    return q.getSingleResult();
}

From source file:org.cleverbus.core.common.dao.ExternalCallDaoJpaImpl.java

@Override
@Nullable//from  www  .  ja v  a 2  s . c o  m
@SuppressWarnings("unchecked")
public ExternalCall findConfirmation(int interval) {
    // find confirmation that was lastly processed before specified interval
    Date lastUpdateLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT c " + "FROM " + ExternalCall.class.getName() + " c "
            + "WHERE c.operationName = :operationName" + "     AND c.state = :state"
            + "     AND c.lastUpdateTimestamp < :lastUpdateTimestamp" + " ORDER BY c.creationTimestamp";

    TypedQuery<ExternalCall> q = em.createQuery(jSql, ExternalCall.class);
    q.setParameter("operationName", ExternalCall.CONFIRM_OPERATION);
    q.setParameter("state", ExternalCallStateEnum.FAILED);
    q.setParameter("lastUpdateTimestamp", new Timestamp(lastUpdateLimit.getTime()));
    q.setMaxResults(1);
    List<ExternalCall> extCalls = q.getResultList();

    if (extCalls.isEmpty()) {
        return null;
    } else {
        return extCalls.get(0);
    }
}

From source file:org.mitre.openid.connect.repository.impl.JpaWhitelistedSiteRepository.java

@Override
@Transactional(value = "defaultTransactionManager")
public WhitelistedSite getByClientId(String clientId) {
    TypedQuery<WhitelistedSite> query = manager.createNamedQuery(WhitelistedSite.QUERY_BY_CLIENT_ID,
            WhitelistedSite.class);
    query.setParameter(WhitelistedSite.PARAM_CLIENT_ID, clientId);
    return JpaUtil.getSingleResult(query.getResultList());
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.PersistentDocumentStoreController.java

/**
 * Gets the size of a document store (number of documents contained therein).
 * @param em the {@link EntityManager} to use.
 * @param store the {@link PersistentDocumentStore} whose size is desired.
 * @return the {@link Long} count of documents.
 *///from   w  w  w .ja v  a2  s.  co  m
public long getSize(EntityManager em, PersistentDocumentStore store) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(store, CannedMessages.NULL_ARGUMENT, "store");

    TypedQuery<byte[]> query = em
            .createQuery("SELECT doc.id FROM PersistentDocument doc " + "WHERE doc.store=:store", byte[].class);
    query.setParameter("store", store);

    return query.getResultList().size();
}