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:vlove.dao.impl.GenericDaoImpl.java

private static void applyParameters(Query query, Map<String, Object> parameters) {
    if (parameters != null) {
        for (Entry<String, Object> e : parameters.entrySet()) {
            query.setParameter(e.getKey(), e.getValue());
        }/*w w w  . ja v a 2  s . c o m*/
    }
}

From source file:org.apache.cxf.fediz.service.idp.service.jpa.ApplicationDAOJPAImpl.java

static ApplicationEntity getApplicationEntity(String realm, EntityManager em) {
    Query query = null;
    query = em.createQuery("select a from Application a where a.realm=:realm");
    query.setParameter("realm", realm);

    //@SuppressWarnings("rawtypes")
    return (ApplicationEntity) query.getSingleResult();
}

From source file:com.siberhus.ngai.core.CrudHelper.java

@SuppressWarnings("unchecked")
public final static List<Object> findByExample(EntityManager em, Class<?> entityClass, Object example) {
    if (em == null) {
        throw new IllegalArgumentException("EntityManager is null");
    }/*  ww w. ja va  2s.  c o m*/
    EntityInfo entityInfo = getEntityInfo(entityClass);
    List<Object> params = new ArrayList<Object>();
    StringBuilder sql = new StringBuilder(255);
    sql.append("from ").append(entityInfo.getEntityName()).append(" e where 1=1 ");
    try {
        for (Field field : entityInfo.getFieldSet()) {
            sql.append("and e.").append(field.getName()).append("=? ");
            params.add(field.get(example));
        }
    } catch (IllegalAccessException e) {
        throw new NgaiRuntimeException("Unable to get field value from example model: " + example, e);
    }
    Query query = em.createQuery(sql.toString());
    for (int i = 0; i < params.size(); i++) {
        query.setParameter(i + 1, params.get(i));
    }
    return query.getResultList();
}

From source file:com.mycomm.dao.mydao.base.MyDaoSupport.java

protected static void setQueryParams(Query query, Object[] queryParams) {
    if (queryParams != null && queryParams.length > 0) {
        for (int i = 0; i < queryParams.length; i++) {
            query.setParameter(i + 1, queryParams[i]);
        }//www  .  j ava  2  s. com
    }
}

From source file:ro.allevo.fintpws.security.RolesUtils.java

public static boolean hasReadAuthorityOnQueue(QueueEntity queueEntity) {
    EntityManagerFactory configEntityManagerFactory = Persistence.createEntityManagerFactory("fintpCFG");
    EntityManager entityManagerConfig = configEntityManagerFactory.createEntityManager();
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    UserEntity loggedUser = (UserEntity) authentication.getPrincipal();
    Query query = entityManagerConfig
            .createQuery("SELECT ur.roleid FROM UserRoleEntity ur, QueuesRoleMapEntity qr "
                    + "WHERE ur.roleid = qr.roleid " + "AND ur.userid=:userid " + "AND qr.queueid=:queueid");
    query.setParameter("userid", loggedUser.getUserid());
    query.setParameter("queueid", queueEntity.getGuid());
    List roles = query.getResultList();
    if (roles.isEmpty()) {
        return false;
    }/*from ww  w . j  av a 2  s . co  m*/
    return true;
}

From source file:nc.noumea.mairie.organigramme.core.utility.OrganigrammeUtil.java

public static void setParameter(Query query, String parameter, Object value) {
    LOGGER.debug("addParameter : " + parameter + " = " + value);
    query.setParameter(parameter, value);
}

From source file:org.synyx.hades.dao.query.QueryUtils.java

/**
 * Creates a where-clause referencing the given entities and appends it to
 * the given query string. Binds the given entities to the query.
 * /*w  ww.  j  a v  a 2s.c om*/
 * @param <T>
 * @param queryString
 * @param entities
 * @param entityManager
 * @return
 */
public static <T> Query applyAndBind(String queryString, Collection<T> entities, EntityManager entityManager) {

    Assert.notNull(queryString);
    Assert.notNull(entities);
    Assert.notNull(entityManager);

    Iterator<T> iterator = entities.iterator();

    if (!iterator.hasNext()) {
        return entityManager.createQuery(queryString);
    }

    String alias = detectAlias(queryString);
    StringBuilder builder = new StringBuilder(queryString);
    builder.append(" where");

    for (int i = 0; i < entities.size(); i++) {

        builder.append(String.format(" %s = ?%d", alias, i + 1));

        if (i < entities.size() - 1) {
            builder.append(" or");
        }
    }

    Query query = entityManager.createQuery(builder.toString());

    for (int i = 0; i < entities.size(); i++) {
        query.setParameter(i + 1, iterator.next());
    }

    return query;
}

From source file:ro.allevo.fintpws.security.RolesUtils.java

public static boolean hasWriteAuthorityOnQueue(QueueEntity queueEntity) {
    EntityManagerFactory configEntityManagerFactory = Persistence.createEntityManagerFactory("fintpCFG");
    EntityManager entityManagerConfig = configEntityManagerFactory.createEntityManager();
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    UserEntity loggedUser = (UserEntity) authentication.getPrincipal();
    Query query = entityManagerConfig.createQuery(
            "SELECT ur.roleid FROM UserRoleEntity ur, QueuesRoleMapEntity qr " + "WHERE ur.roleid = qr.roleid "
                    + "AND ur.userid=:userid " + "AND qr.queueid=:queueid " + "AND qr.actiontype = 'RW'");
    query.setParameter("userid", loggedUser.getUserid());
    query.setParameter("queueid", queueEntity.getGuid());
    List roles = query.getResultList();
    if (roles.isEmpty()) {
        return false;
    }/*from ww  w. j  a  va2  s  .  co  m*/
    return true;
}

From source file:com.autentia.common.util.ejb.EntityManagerUtils.java

/**
 * Devuelve una lista de entidades de tipo <code>T</code>, obtenida como resultado de ejectua la consulta
 * <code>query</code>, haciendo la susticucin de parmetros (la sustitucin de parmetros ser posicional).
 * /*from  ww  w  .  j a va  2 s  . co m*/
 * @param <T> tipo de los objetos devueltos por la bsqueda.
 * @param query consulta que se quiere ejecutar.
 * @param queryParams parmetros de la consulta.
 * @return lista de entidades de tipo <code>T</code>.
 */
@SuppressWarnings("unchecked")
public static <T> List<T> find(Query query, Object... queryParams) {
    if (queryParams != null) {
        for (int position = 1, i = 0; i < queryParams.length; i++, position++) {
            query.setParameter(position, queryParams[i]);
        }
    }
    final List<T> entities = query.getResultList();

    if (log.isDebugEnabled())
        log.debug(entities.size() + " entities recovered.");

    return entities;
}

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

/**
 * Applies all parameters necessary to a query.
 * /*from  ww  w. j a va2 s.  c  om*/
 * @param query
 * @param parameters
 * @return
 */
public static Query applyParameters(final Query query, final Map<String, Object> parameters) {
    for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
        query.setParameter(parameter.getKey(), parameter.getValue());
    }
    return query;
}