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.taobao.ad.easyschedule.base.JPABaseDAO.java

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

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

        @Override/*w w  w .j  a  v  a2s.  co  m*/
        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)));
            }
            return query.getResultList();
        }
    });
}

From source file:es.ucm.fdi.dalgs.module.repository.ModuleRepository.java

public boolean deleteModulesForDegree(Degree d) {
    try {/*from   w ww .j a  v a  2  s .  c o m*/
        Query query = em.createQuery("UPDATE Module m SET m.isDeleted = true where m.degree=?1");

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

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

From source file:org.syncope.core.persistence.dao.impl.RoleDAOImpl.java

@Override
public void delete(final Long id) {
    SyncopeRole role = find(id);/*from ww  w .j a  v  a  2  s . com*/
    if (role == null) {
        return;
    }

    Query query = entityManager.createQuery("SELECT r FROM SyncopeRole r WHERE r.parent.id=:id");
    query.setParameter("id", id);
    List<SyncopeRole> childrenRoles = query.getResultList();
    for (SyncopeRole child : childrenRoles) {
        delete(child.getId());
    }

    for (Membership membership : findMemberships(role)) {
        membership.setSyncopeRole(null);
        membership.getSyncopeUser().removeMembership(membership);
        membership.setSyncopeUser(null);

        entityManager.remove(membership);
    }

    role.getEntitlements().clear();

    role.setParent(null);
    entityManager.remove(role);

    entitlementDAO.delete(EntitlementUtil.getEntitlementNameFromRoleId(id));
}

From source file:gwap.rest.UserPicture.java

protected void persistPicturerating(String idString, String payloadString) {
    JSONObject jsonObject = parse(payloadString);

    Query query = entityManager.createNamedQuery("person.byDeviceId");
    query.setParameter("deviceId", jsonObject.get("userid").toString());
    Person person = (Person) query.getSingleResult();

    ArtResourceRating artResourceRating = new ArtResourceRating();
    artResourceRating.setPerson(person);

    if (jsonObject.containsKey("likes"))
        artResourceRating.setRating(1L);
    else/*from ww  w . jav  a  2  s. co  m*/
        artResourceRating.setRating(-1L);

    entityManager.persist(artResourceRating);
    ArtResource artResource = entityManager.find(ArtResource.class, Long.parseLong(idString));
    artResource.getRatings().add(artResourceRating);
    artResourceRating.setResource(artResource);
    entityManager.flush();
    log.info("Added ArtResourceRating #0", artResourceRating.getId());

    log.info("Updated UserPicture #0", artResource.getId());
}

From source file:edu.iu.m.news.dao.NewsDaoImpl.java

@Override
public List<NewsSource> getAllActiveNewsSourcesByLocationCode(String locationCode) {
    Query query = entityManager.createQuery(
            "select r from MaintRss r where r.active = 1 and r.type like 'NEWS' and (r.campus like :campusCode or r.campus like 'UA') order by r.order");
    query.setParameter("campusCode", locationCode);
    @SuppressWarnings("unchecked")
    List<MaintRss> maintRss = query.getResultList();
    List<NewsSource> sources = new ArrayList<NewsSource>();
    for (MaintRss rss : maintRss) {
        sources.add(convertMaintRssToNewsSource(rss));
    }//ww  w  . j a v a  2s.c  o  m
    return sources;
}

From source file:es.ucm.fdi.dalgs.academicTerm.repository.AcademicTermRepository.java

@SuppressWarnings("unchecked")
public List<AcademicTerm> getAcademicTermsByDegree(Degree degree) {
    Query query = em.createQuery("select a from AcademicTerm a where a.isDeleted='false' and  a.degree=?1");
    query.setParameter(1, degree);

    return (List<AcademicTerm>) query.getResultList();

}

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

private void setMessageStatus(final String messageId, MessageStatus messageStatus) {

    final Query query = this.em.createNamedQuery("MessageLogEntry.setMessageStatus");
    query.setParameter("MESSAGE_ID", messageId);
    query.setParameter("TIMESTAMP", new Date());
    query.setParameter("MESSAGE_STATUS", messageStatus);
    final int result = query.executeUpdate();
    if (result != 1) {
        this.em.getTransaction().setRollbackOnly();
        BasicDao.LOG.error("Could not set message " + messageId + " as " + messageStatus);
    }//from ww w.  ja  v  a  2s  .  c om
}

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

public Preference findPreference(String username, String shortName) {
    Query query = getEntityManager().createNamedQuery("Preference.findPreferenceWithUsernameAndShortname");
    query.setParameter("username", username);
    query.setParameter("shortName", shortName);
    Preference result;// ww  w .  j  a  v  a 2 s  .  co m
    try {
        result = (Preference) query.getSingleResult();
    } catch (Exception e) {
        LOG.info("No Preference Found: " + username + ", " + shortName);
        result = null;
    }
    return result;
}

From source file:org.syncope.core.persistence.dao.impl.ReportExecDAOImpl.java

private ReportExec findLatest(final Report report, final String field) {

    Query query = entityManager.createQuery("SELECT e " + "FROM " + ReportExec.class.getSimpleName() + " e "
            + "WHERE e.report=:report " + "ORDER BY e." + field + " DESC");
    query.setParameter("report", report);
    query.setMaxResults(1);//from ww  w .  ja va2 s  . c  o m

    List<ReportExec> result = query.getResultList();
    return result == null || result.isEmpty() ? null : result.iterator().next();
}

From source file:org.thingsplode.TestBaseWithRepos.java

/**
 * Executes a native SQL query for checking test results.
 *
 * @param sqlQuery the native sql query you need to execute (use "?" for
 * parameters)/*from   www .  ja  v  a  2s .  co  m*/
 * @param maxResults the max. number of results to be returned (in case the
 * database contains 100 rows, but you limit here to 10, it will return the
 * first 10 rows)
 * @param paramValues the parameter values for the query. you can use null,
 * if the query has no parameters, or you need to specify in the order the
 * query references them.
 * @return
 */
protected List<Object> getNativeSQLQueryResult(final String sqlQuery, final int maxResults,
        final String... paramValues) {
    Query q = entityManager.createNativeQuery(sqlQuery);
    if (paramValues != null && paramValues.length > 0) {
        int i = 0;
        for (String param : paramValues) {
            q.setParameter(i, param);
            i += 1;
        }
    }
    q.setMaxResults(maxResults);
    return q.getResultList();
}