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:com.github.aynu.mosir.core.enterprise.persistence.StandardRepositoryImpl.java

/** {@inheritDoc} */
@Override/* w w  w. ja v  a 2  s .c o  m*/
public R findOne(final String name, final R object, final String... fields) throws PersistenceException {
    try {
        final TypedQuery<R> query = getManager().createNamedQuery(name, getEntityClass());
        for (final Entry<String, Object> entry : filter(object, fields).entrySet()) {
            query.setParameter(entry.getKey(), entry.getValue());
        }
        return query.getSingleResult();
    } catch (final IllegalArgumentException | IllegalStateException
            | javax.persistence.PersistenceException e) {
        LOG.warn(e.toString(), e);
        throw new PersistenceException(e);
    }
}

From source file:com.epam.ipodromproject.repository.jpa.JPAHorseRepository.java

@Override
public long getPagesOfHorses(String partOfName) {
    TypedQuery<Long> query = entityManager.createNamedQuery("Horse.findNumberOfHorsesByPartOfName", Long.class);
    StringBuilder partOfNameBuilder = new StringBuilder("%");
    query.setParameter("partOfName", partOfNameBuilder.append(partOfName).append("%").toString());
    long pages = 1;
    try {//from   www .j  av  a2 s .  c  o m
        pages = query.getSingleResult();
    } catch (Exception e) {
        pages = (long) 1;
    }
    return pages;
}

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

@Override
public ConcurrentMap<TermsOfConditionStatus, AtomicLong> fetchTermsOfCompetitionStatistic() {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<EvaluationAnswer> cq = cb.createQuery(EvaluationAnswer.class);
    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> questionnaireType = cb.parameter(QuestionnaireType.class,
            "questionnaireType");
    cq.select(evaluationAnswer);/* w  w w  .j a  v  a  2 s.  com*/
    cq.where(cb.equal(questionDef.get(EvaluationQuestionDef_.questionnaireType), questionnaireType));
    TypedQuery<EvaluationAnswer> query = entityManager.createQuery(cq);
    query.setParameter("questionnaireType", QuestionnaireType.TERMS_OF_CONDITION);
    List<EvaluationAnswer> evaluationAnswers = query.getResultList();

    ConcurrentMap<TermsOfConditionStatus, AtomicLong> distributionByTermsOfCompetitionStatus = new ConcurrentHashMap<>();

    List<EvaluationQuestionnaireDefEnum> questionnaireDef = Arrays
            .asList(EvaluationQuestionnaireDefEnum.values());
    long numberOfQuestion = questionnaireDef.stream()
            .filter(q -> q.getQuestionnaireType().equals(QuestionnaireType.TERMS_OF_CONDITION))
            .collect(Collectors.counting());

    List<MemberRegister> memberRegisters = evaluationAnswers.stream()
            .filter(StreamUtil.distinctByKey((EvaluationAnswer e) -> e.getMemberRegister().getId()))
            .map(EvaluationAnswer::getMemberRegister).collect(Collectors.toList());
    memberRegisters.stream().forEach((memberRegister) -> {
        TermsOfConditionStatus termsOfConditionStatus = TermsOfConditionStatus.NONE;
        if (evaluationAnswers.stream().filter(m -> m.getMemberRegister().equals(memberRegister))
                .count() == numberOfQuestion) {
            boolean isValid = evaluationAnswers.stream()
                    .filter(m -> m.getMemberRegister().equals(memberRegister))
                    .allMatch(e -> e.getAnswer().getLabel()
                            .equals(messageSource.getMessage("QuestionChoicesDef.yes", null, Locale.ENGLISH)));
            termsOfConditionStatus = isValid ? TermsOfConditionStatus.VALID : TermsOfConditionStatus.INVALID;
        }
        distributionByTermsOfCompetitionStatus.putIfAbsent(termsOfConditionStatus, new AtomicLong(0));
        distributionByTermsOfCompetitionStatus.get(termsOfConditionStatus).incrementAndGet();
    });

    return distributionByTermsOfCompetitionStatus;
}

From source file:org.openmeetings.app.data.calendar.daos.AppointmentCategoryDaoImpl.java

public AppointmentCategory getAppointmentCategoryById(Long categoryId) {
    try {/*from  ww  w  .  j  av  a 2  s  .com*/
        log.debug("getAppointmentCategoryById: " + categoryId);

        String hql = "select app from AppointmentCategory app " + "WHERE app.deleted <> :deleted "
                + "AND app.categoryId = :categoryId";

        TypedQuery<AppointmentCategory> query = em.createQuery(hql, AppointmentCategory.class);
        query.setParameter("deleted", "true");
        query.setParameter("categoryId", categoryId);

        AppointmentCategory appointCategory = null;
        try {
            appointCategory = query.getSingleResult();
        } catch (NoResultException ex) {
        }

        return appointCategory;
    } catch (Exception ex2) {
        log.error("[getAppointmentCategoryById]: " + ex2);
    }
    return null;
}

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

@Override
public List<Assignment> searchAssignments(String term, String type, User instructor, int maxResults) {
    TypedQuery<Assignment> query = entityManager.createNamedQuery("assignment.search", Assignment.class);
    if (maxResults > 0)
        query.setMaxResults(maxResults);

    return query.setParameter("term", term).setParameter("type", type)
            .setParameter("instructorId", instructor.getId()).getResultList();
}

From source file:csns.model.assessment.dao.jpa.RubricDaoImpl.java

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

From source file:org.jasig.portlet.attachment.dao.jpa.JpaAttachmentDao.java

private TypedQuery<Attachment> createQuery(String select, Map<String, String> params) {
    final TypedQuery<Attachment> query = this.getEntityManager().createNamedQuery(select, Attachment.class);
    for (String key : params.keySet()) {
        query.setParameter(key, params.get(key));
    }//  w  ww  .  ja v a  2  s  . c  o m
    return query;
}

From source file:org.openmeetings.app.data.user.Statemanagement.java

/**
 * Get all state-Object/*w w w.j  a v a 2  s.  c  o m*/
 * 
 * @return List of State Objects or null
 */
public List<States> getStates() {
    try {
        TypedQuery<States> query = em.createQuery("select c from States as c where c.deleted <> :deleted",
                States.class);
        query.setParameter("deleted", "true");
        List<States> ll = query.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("getStates", ex2);
    }
    return null;
}

From source file:energy.usef.agr.repository.DeviceMessageRepository.java

/**
 * Find the device messages having the data matching the method parameters.
 *
 * @param connectionEntityAddress {@link String} optional entity address of the connection.
 * @param deviceMessageStatus     {@link DeviceMessageStatus} optional status of the device message.
 * @return a {@link List} of {@link DeviceMessage}.
 *///from  w  w w  . j a va 2  s .  c  o  m
public List<DeviceMessage> findDeviceMessages(String connectionEntityAddress,
        DeviceMessageStatus deviceMessageStatus) {
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT dm FROM DeviceMessage dm ");
    sql.append("WHERE 1 = 1 ");
    if (StringUtils.isNotBlank(connectionEntityAddress)) {
        sql.append("  AND dm.udi.connection.entityAddress = :connectionEntityAddress ");
    }
    if (deviceMessageStatus != null) {
        sql.append("  AND dm.deviceMessageStatus = :status ");
    }

    TypedQuery<DeviceMessage> query = getEntityManager().createQuery(sql.toString(), DeviceMessage.class);
    if (StringUtils.isNotBlank(connectionEntityAddress)) {
        query.setParameter("connectionEntityAddress", connectionEntityAddress);
    }
    if (deviceMessageStatus != null) {
        query.setParameter("status", deviceMessageStatus);
    }
    return query.getResultList();
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.BasicSkillRepositoryImplementation.java

@Override
public List<BasicSkill> getSkillsNotInVacancy(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<BasicSkill> list = new LinkedList<>();
    try {/*from w  ww  .j  a va2s  .  co  m*/
        em.getTransaction().begin();

        TypedQuery<BasicSkill> query = em.createNamedQuery("BasicSkill.findSkillsNotInVacancy",
                BasicSkill.class);
        query.setParameter("vacancy", vacancy);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}