Example usage for javax.persistence Query setParameter

List of usage examples for javax.persistence Query setParameter

Introduction

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

Prototype

Query setParameter(int position, Object value);

Source Link

Document

Bind an argument value to a positional parameter.

Usage

From source file:com.github.peholmst.springsecuritydemo.services.impl.CategoryServiceImpl.java

/**
 * TODO Document me!//  www .j  a v  a 2s . co m
 * 
 * @param category
 * @return
 */
protected boolean containsCategory(Category category) {
    assert category != null : "category must not be null";
    if (category.getId() == null) {
        return false;
    } else {
        Query query = getEntityManager().createQuery("SELECT count(c) FROM Category c WHERE c.id = :id");
        query.setParameter("id", category.getId());
        Object result = query.getSingleResult();
        if (result instanceof Integer) {
            return ((Integer) result).intValue() == 1;
        } else {
            return ((Long) result).longValue() == 1;
        }
    }
}

From source file:com.taobao.ad.easyschedule.base.JPABaseDAO.java

public List queryForListIsNotEmpty(String queryString, final Map<String, Object> map, final PageInfo pageInfo,
        String orderBy) {/*from w  w  w . jav  a2 s  .co m*/
    final List<String> keyList = getKeyList(map);
    final String queryStringCallBack = getQueryString(queryString, keyList, orderBy);
    return (List) this.getJpaTemplate().execute(new JpaCallback() {
        @Override
        public List doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createQuery(queryStringCallBack);
            for (int i = 0; i < keyList.size(); i++) {
                query.setParameter((i + 1), map.get(keyList.get(i)));
            }
            // query.setFirstResult(((pageInfo.getToPage() == 0 ? 1 :
            // pageInfo.getToPage()) - 1) * pageInfo.getPerPageSize());
            query.setFirstResult(pageInfo.getStartRow());
            query.setMaxResults(pageInfo.getPerPageSize());
            return query.getResultList();
        }
    });
}

From source file:eu.domibus.common.dao.MessageLogDao.java

public List<String> findUndownloadedUserMessages() {

    final Query query = this.em.createNamedQuery("MessageLogEntry.findUndeletedMessages");
    query.setParameter("MSH_ROLE", MSHRole.RECEIVING);
    query.setParameter("MESSAGE_TYPE", MessageType.USER_MESSAGE);
    //noinspection unchecked
    return query.getResultList();
}

From source file:org.messic.server.datamodel.jpaimpl.DAOJPAAuthor.java

@Override
@Transactional//  ww  w .  ja v a  2  s . c  om
public MDOAuthor getByName(String authorName, String username) {
    Query query = entityManager.createQuery(
            "from MDOAuthor as a where (UPPER(a.name) = :authorName) AND (a.owner.login = :userName)");
    query.setParameter("userName", username);
    query.setParameter("authorName", authorName.toUpperCase());

    @SuppressWarnings("unchecked")
    List<MDOAuthor> results = query.getResultList();
    if (results != null && results.size() > 0) {
        return results.get(0);
    }
    return null;
}

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

@Override
public List<PrintedBook> findAllBorrowedPrintedBooks() {
    try {/*ww  w.  j av  a2 s .  c o m*/
        final Query query = em.createQuery("SELECT m FROM PrintedBook as m WHERE m.state = :i");
        query.setParameter("i", Boolean.TRUE);
        return query.getResultList();
    } catch (RuntimeException E) {
        throw new DAException(E.getMessage());
    }
}

From source file:org.kuali.mobility.push.dao.PreferenceDaoImpl.java

@Override
public Preference findPreference(String username, Long senderId) {
    try {//from ww  w.  ja va 2 s  .co m
        Query query = getEntityManager().createNamedQuery("Preference.findPreferenceWithUsernameAndSenderId");
        query.setParameter("username", username);
        query.setParameter("pushSenderID", senderId);
        return (Preference) query.getSingleResult();
    } catch (NoResultException nre) {
        // If there is no result we return nothing
        return null;
    }
}

From source file:es.ucm.fdi.dalgs.competence.repository.CompetenceRepository.java

public Competence getCompetenceByName(String name) {
    Query query = em.createQuery("select c from Competence c where c.info.name=?1");
    query.setParameter(1, name);

    return (Competence) query.getResultList().get(0);

}

From source file:es.ucm.fdi.dalgs.group.repository.GroupRepository.java

public boolean deleteGroupsFromCourses(Collection<Course> coursesList) {
    try {/*w  w  w . j  ava2  s .  c o  m*/
        Query query = em.createQuery("UPDATE Group g SET g.isDeleted = true where g.course in ?1");

        query.setParameter(1, coursesList);
        query.executeUpdate();

    } catch (Exception e) {
        logger.error(e.getMessage());

        return false;
    }
    return true;
}

From source file:com.ilkgunel.hastaneotomasyonu.facade.AbstractFacade.java

@Transactional
public List<T> findListByNamedQuery(String namedQuery, Map parameters) throws Exception {
    Query q = getEntityManager().createNamedQuery(namedQuery);
    Iterator iterator = parameters.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry entry = (Map.Entry) iterator.next();
        q.setParameter(entry.getKey().toString(), entry.getValue());
    }/* www.  j  a v a 2s  .  com*/
    return q.getResultList();
}

From source file:com.taobao.ad.easyschedule.base.JPABaseDAO.java

public Object executeSingleIsNotEmpty(String queryString, final Map<String, Object> map, String orderBy) {

    final List<String> keyList = getKeyList(map);
    final String queryStringCallBack = getQueryString(queryString, keyList, orderBy);
    return this.getJpaTemplate().execute(new JpaCallback() {

        @Override//from w  w  w  . ja v a2s  .  c o m
        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createQuery(queryStringCallBack);
            for (int i = 0; i < keyList.size(); i++) {
                query.setParameter((i + 1), map.get(keyList.get(i)));
            }
            query.setMaxResults(1);
            try {
                return query.getSingleResult();
            } catch (Exception e) {
                return null;
            }
        }
    });

}