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.clustercontrol.notify.monitor.util.QueryUtil.java

public static List<EventLogEntity> getEventLogByHighPriorityFilter(String[] facilityIds, Integer priority,
        Long outputFromDate, Long outputToDate, Long generationFromDate, Long generationToDate,
        String application, String message, Integer confirmFlg, String confirmUser, Boolean orderByFlg) {

    HinemosEntityManager em = new JpaTransactionManager().getEntityManager();

    StringBuffer sbJpql = new StringBuffer();
    sbJpql.append("SELECT a FROM EventLogEntity a WHERE true = true");
    // ID//from  w  w  w  . j  ava 2s  . co m
    if (facilityIds != null && facilityIds.length > 0) {
        sbJpql.append(" AND a.id.facilityId IN ("
                + HinemosEntityManager.getParamNameString("facilityId", facilityIds) + ")");
    }
    // ??
    if (priority != null) {
        sbJpql.append(" AND a.priority = :priority");
    }
    // ?
    if (outputFromDate != null) {
        sbJpql.append(" AND a.id.outputDate >= :outputFromDate");
    }
    // ?
    if (outputToDate != null) {
        sbJpql.append(" AND a.id.outputDate <= :outputToDate");
    }
    // 
    if (generationFromDate != null) {
        sbJpql.append(" AND a.generationDate >= :generationFromDate");
    }
    // 
    if (generationToDate != null) {
        sbJpql.append(" AND a.generationDate <= :generationToDate");
    }
    // 
    if (application != null && !"".equals(application)) {
        sbJpql.append(" AND a.application like :application");
    }
    // 
    if (message != null && !"".equals(message)) {
        sbJpql.append(" AND a.message like :message");
    }
    // ?
    if (confirmFlg != null) {
        sbJpql.append(" AND a.confirmFlg = :confirmFlg");
    }
    // ?
    if (confirmUser != null) {
        sbJpql.append(" AND a.confirmUser = :confirmUser");
    }
    // 
    if (orderByFlg) {
        sbJpql.append(" ORDER BY a.id.outputDate DESC");
    }
    TypedQuery<EventLogEntity> typedQuery = em.createQuery(sbJpql.toString(), EventLogEntity.class);
    // ID
    if (facilityIds != null && facilityIds.length > 0) {
        typedQuery = HinemosEntityManager.appendParam(typedQuery, "facilityId", facilityIds);
    }
    // ??
    if (priority != null) {
        typedQuery = typedQuery.setParameter("priority", priority);
    }
    // ?
    if (outputFromDate != null) {
        typedQuery = typedQuery.setParameter("outputFromDate", outputFromDate);
    }
    // ?
    if (outputToDate != null) {
        typedQuery = typedQuery.setParameter("outputToDate", outputToDate);
    }
    // 
    if (generationFromDate != null) {
        typedQuery = typedQuery.setParameter("generationFromDate", generationFromDate);
    }
    // 
    if (generationToDate != null) {
        typedQuery = typedQuery.setParameter("generationToDate", generationToDate);
    }
    // 
    if (application != null && !"".equals(application)) {
        typedQuery = typedQuery.setParameter("application", application);
    }
    // 
    if (message != null && !"".equals(message)) {
        typedQuery = typedQuery.setParameter("message", message);
    }
    // ?
    if (confirmFlg != null) {
        typedQuery = typedQuery.setParameter("confirmFlg", confirmFlg);
    }
    // ?
    if (confirmUser != null) {
        typedQuery = typedQuery.setParameter("confirmUser", confirmUser);
    }
    typedQuery = typedQuery.setMaxResults(1);

    return typedQuery.getResultList();
}

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

/**
 * Finds the opinion mined corpus associated with a given corpus, lexicon, and engine code.
 * @param em the {@link EntityManager} to use.
 * @param corpus the {@link DocumentCorpus} being used to mine opinion.
 * @param lexicon the {@link AspectLexicon} being built.
 * @return the {@link AspectOpinionMinedCorpus} object found, if any; {@code null} otherwise.
 *//*w ww .j a v a 2  s.co  m*/
public AspectOpinionMinedCorpus findMinedCorpus(EntityManager em, DocumentCorpus corpus, AspectLexicon lexicon,
        String engineCode) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(corpus, CannedMessages.NULL_ARGUMENT, "corpus");
    Validate.notNull(lexicon, CannedMessages.NULL_ARGUMENT, "lexicon");

    TypedQuery<AspectOpinionMinedCorpus> query = em.createQuery(
            "SELECT mc FROM AspectOpinionMinedCorpus mc "
                    + "WHERE mc.baseStore=:corpus AND :lexicon MEMBER OF mc.referencedObjects",
            AspectOpinionMinedCorpus.class);
    query.setParameter("corpus", corpus).setParameter("lexicon", lexicon);

    for (AspectOpinionMinedCorpus minedCorpus : query.getResultList()) {
        if (StringUtils.equals(engineCode, minedCorpus.getEngineCode())) {
            return minedCorpus;
        }
    }

    return null;
}

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

@Override
public List<UserSkill> getSkills(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<UserSkill> list = new LinkedList<>();
    try {/*from  w ww.  j a v  a  2s.  c  o  m*/
        em.getTransaction().begin();

        TypedQuery<UserSkill> query = em.createNamedQuery("UserSkill.findByEmployee", UserSkill.class);
        query.setParameter("employee", employee);
        list = query.getResultList();

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

    return list;
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.ConferenceUserDaoImpl.java

@Override
public Set<ConferenceUser> getUsersByAnyEmail(String email) {
    final TypedQuery<ConferenceUserImpl> query = this.createQuery(this.getUsersByAnyEmailQuery);
    query.setParameter(this.emailParameter, email.toUpperCase());

    final List<ConferenceUserImpl> resultList = query.getResultList();
    return new LinkedHashSet<ConferenceUser>(resultList);
}

From source file:kirchnerei.note.model.DataService.java

public Long storeNote(NoteData noteData) {
    EntityManager em = null;/*from   w  w  w  .j a  v  a2 s . com*/
    EntityTransaction tx = null;
    try {
        em = entityService.get();
        tx = em.getTransaction();
        tx.begin();
        // search for category
        String category = noteData.getCategory();
        if (StringUtils.isEmpty(category)) {
            LogUtils.warn(log, "note without category is not allow [%s]", noteData);
            return null;
        }
        TypedQuery<Category> q = em.createNamedQuery("findCategory", Category.class);
        q.setParameter("category", category);
        Category cat = getSingleResult(q);
        if (cat == null) {
            // a new category
            cat = new Category();
            cat.setTitle(category);
            em.persist(cat);
        }
        final Note note;
        if (NumberUtils.isEmpty(noteData.getId())) {
            // create a new note
            note = beanCopy.copy(noteData, Note.class, noteProperties);
            note.setCategory(cat);
            cat.getNotes().add(note);
        } else {
            // update an existed note
            note = em.find(Note.class, noteData.getId());
            beanCopy.copy(noteData, note, noteProperties);
            if (!NumberUtils.compare(note.getCategory().getId(), cat.getId())) {
                // move to other category
                note.getCategory().getNotes().remove(note);
                cat.getNotes().add(note);
                note.setCategory(cat);
            }
        }
        EntityService.commit(tx);
        return note.getId();
    } catch (Exception e) {
        EntityService.rollback(tx);
        return null;
    } finally {
        EntityService.close(em);
    }
}

From source file:com.clustercontrol.notify.monitor.util.QueryUtil.java

public static List<StatusInfoEntity> getStatusInfoByHighPriorityFilter(String[] facilityIds,
        Long outputFromDate, Long outputToDate, Long generationFromDate, Long generationToDate,
        String application, String message, String ownerRoleId, boolean orderFlg) {

    HinemosEntityManager em = new JpaTransactionManager().getEntityManager();

    StringBuffer sbJpql = new StringBuffer();
    sbJpql.append("SELECT a FROM StatusInfoEntity a WHERE true = true");
    // ID//  ww  w .j  a  v a2s .  c om
    if (facilityIds != null && facilityIds.length > 0) {
        sbJpql.append(" AND a.id.facilityId IN ("
                + HinemosEntityManager.getParamNameString("facilityId", facilityIds) + ")");
    }
    // ?
    if (outputFromDate != null) {
        sbJpql.append(" AND a.outputDate >= :outputFromDate");
    }
    // ?
    if (outputToDate != null) {
        sbJpql.append(" AND a.outputDate <= :outputToDate");
    }
    // 
    if (generationFromDate != null) {
        sbJpql.append(" AND a.generationDate >= :generationFromDate");
    }
    // 
    if (generationToDate != null) {
        sbJpql.append(" AND a.generationDate <= :generationToDate");
    }
    // 
    if (application != null && !"".equals(application)) {
        sbJpql.append(" AND a.application like :application");
    }
    // 
    if (message != null && !"".equals(message)) {
        sbJpql.append(" AND a.message like :message");
    }
    // ID
    if (ownerRoleId != null && !"".equals(ownerRoleId)) {
        sbJpql.append(" AND a.ownerRoleId = :ownerRoleId");
    }
    if (orderFlg) {
        sbJpql.append(" ORDER BY a.priority, a.outputDate DESC");
    } else {
        sbJpql.append(" ORDER BY a.priority");
    }
    TypedQuery<StatusInfoEntity> typedQuery = em.createQuery(sbJpql.toString(), StatusInfoEntity.class);
    // ID
    if (facilityIds != null && facilityIds.length > 0) {
        typedQuery = HinemosEntityManager.appendParam(typedQuery, "facilityId", facilityIds);
    }
    // ?
    if (outputFromDate != null) {
        typedQuery = typedQuery.setParameter("outputFromDate", outputFromDate);
    }
    // ?
    if (outputToDate != null) {
        typedQuery = typedQuery.setParameter("outputToDate", outputToDate);
    }
    // 
    if (generationFromDate != null) {
        typedQuery = typedQuery.setParameter("generationFromDate", generationFromDate);
    }
    // 
    if (generationToDate != null) {
        typedQuery = typedQuery.setParameter("generationToDate", generationToDate);
    }
    // 
    if (application != null && !"".equals(application)) {
        typedQuery = typedQuery.setParameter("application", application);
    }
    // 
    if (message != null && !"".equals(message)) {
        typedQuery = typedQuery.setParameter("message", message);
    }
    // ID
    if (ownerRoleId != null && !"".equals(ownerRoleId)) {
        typedQuery = typedQuery.setParameter("ownerRoleId", ownerRoleId);
    }
    typedQuery = typedQuery.setMaxResults(1);
    return typedQuery.getResultList();
}

From source file:com.headissue.pigeon.survey.answer.AnswerHandler.java

private void storeAnswer(Survey _survey, Question _question, UserMap map, UserAnswerValue _value,
        EntityManager _manager, Date _timestamp) {
    TypedQuery<Answer> q = _manager.createNamedQuery("answer.findAnswerBySurveyQuestionAndUserMap",
            Answer.class);
    q.setParameter("survey", _survey);
    q.setParameter("question", _question);
    q.setParameter("userMap", map);
    Answer _answer = JPAUtils.getSingleResult(q);
    if (_answer == null) {
        _answer = new Answer();
        _answer.setTimestamp(_timestamp);
        _answer.setSurvey(_survey);//w  w w. ja  v  a 2 s  .  c om
        _answer.setQuestion(_question);
        _answer.setUserMap(map);
    } else {
        _answer.getAnswerValues().clear();
        _answer.getAnswerTexts().clear();
    }
    AnswerTransform _transform = transformFactory.get(_question.getType().toString());
    if (_transform.transfer(_answer, _value)) {
        _manager.persist(_answer);
    } else {
        LogUtils.warn(log,
                "store answer is failed (surveyId=%s, questionId=%s, questionType=%s) user answer '%s'",
                _survey.getId(), _question.getId(), _question.getType(), _value);
    }
}

From source file:cz.fi.muni.pa165.dao.PrintedBookDAOImpl.java

@Override
public List<PrintedBook> findAllPrintedBooksByLoan(Loan loan) {
    if (loan == null) {
        throw new IllegalArgumentException("Book null");
    }//from   ww w .  ja  va2  s.co  m

    try {
        final TypedQuery<PrintedBook> query = em
                .createQuery("SELECT m FROM PrintedBook as m WHERE m.loan.idLoan = :lid", PrintedBook.class);
        query.setParameter("lid", loan.getIdLoan());
        return query.getResultList();
    } catch (RuntimeException E) {
        throw new DAException(E.getMessage());
    }
}

From source file:com.music.dao.Dao.java

public <T> List<T> getByIds(Class<T> clazz, Collection<Long> ids) {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }/*from   w w w  .j  a  va 2s. co m*/
    TypedQuery<T> query = getEntityManager().createQuery("SELECT piece FROM Piece piece WHERE id IN (:ids)",
            clazz);
    query.setParameter("ids", ids);

    return query.getResultList();
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.ConferenceUserDaoImpl.java

@Override
public Set<ConferenceUser> getUsersByPrimaryEmail(String email) {
    final TypedQuery<ConferenceUserImpl> query = this.createQuery(this.getUsersByPrimaryEmailQuery);
    query.setParameter(this.emailParameter, email.toUpperCase());

    final List<ConferenceUserImpl> resultList = query.getResultList();
    return new LinkedHashSet<ConferenceUser>(resultList);
}