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:cn.newgxu.lab.core.repository.impl.AbstractCommonDaoImpl.java

/**
 * ??<b style="color: red;"> ? </b>????
 * @param hql hql/*ww  w  . j ava2  s.  c  o  m*/
 * @param type 
 * @param objects ?, null?
 * @return ?
 */
protected T executeQuery(String hql, Class<T> clazz, Object... objects) {
    TypedQuery<T> query = em.createQuery(hql, clazz);
    if (objects != null) {
        for (int i = 0; i < objects.length; i++) {
            query.setParameter(i + 1, objects[i]);
        }
    }
    return query.getSingleResult();
}

From source file:cn.newgxu.lab.core.repository.impl.AbstractCommonDaoImpl.java

/**
 * ?<b style="color: red;"> :xxx </b>?????
 * @param hql hql/*from   w  w  w  .j  a  v a2s .c  o  m*/
 * @param clazz 
 * @param offset ??????
 * @param number ????
 * @param params ?map?
 * @return 
 */
protected List<T> executeQuery(String hql, Class<T> clazz, int offset, int number, Map<String, Object> params) {
    offset = rangeCheck(offset, clazz);
    TypedQuery<T> query = em.createNamedQuery(hql, clazz);
    if (params != null) {
        for (String name : params.keySet()) {
            query.setParameter(name, params.get(name));
        }
    }
    return query.setFirstResult(offset).setMaxResults(number).getResultList();
}

From source file:com.softwarecorporativo.monitoriaifpe.servico.AtividadeService.java

@PermitAll
@Override/*from   w  ww.j  a v a 2s. co m*/
public Atividade verificarExistencia(Atividade entidadeNegocio) {
    StringBuilder jpql = new StringBuilder();
    jpql.append("select a from ");
    jpql.append(getClasseEntidade().getSimpleName());
    jpql.append(" as a where a.monitoria = ?1 ");
    jpql.append(" and a.dataInicio = ?2 ");
    TypedQuery<Atividade> query = super.entityManager.createQuery(jpql.toString(), getClasseEntidade());
    query.setParameter(1, entidadeNegocio.getMonitoria());
    query.setParameter(2, entidadeNegocio.getDataInicio(), TemporalType.DATE);
    return query.getSingleResult();
}

From source file:com.pingdu.dao.licenseDao.LicenseDao.java

public List<License> searchLicenses(String searchType, Object keyWord, int page, int rows) {
    String jpql = new String();
    int head = (page - 1) * rows;
    if (searchType.equals("1")) {
        jpql = "select lic from License lic where lic.entCode =:keyWord order by lic.licenseCode";
    }//from  ww w .  ja v a  2s.  c o m
    if (searchType.equals("???")) {
        jpql = "select lic from License lic where lic.licTypeCode =:keyWord order by lic.licenseCode";
    }
    if (searchType.equals("")) {
        jpql = "select lic from License lic where lic.uploadDate =:keyWord order by lic.licenseCode";
    }
    if (searchType.equals("")) {
        jpql = "select lic from License lic where lic.expireDate =:keyWord order by lic.licenseCode";
    }
    if (searchType.equals("?")) {
        jpql = "select lic from License lic where lic.isExpire =:keyWord order by lic.licenseCode";
    }
    TypedQuery<License> query = em().createQuery(jpql, License.class);
    //query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
    query.setParameter("keyWord", keyWord);
    query.setFirstResult(head);
    query.setMaxResults(rows);
    List<License> list = query.getResultList();
    return list;
}

From source file:net.awired.generic.jpa.dao.impl.NestedSetDaoImpl.java

@Override
public List<ENTITY> findByThreadId(KEY threadId) {
    TypedQuery<ENTITY> query = entityManager.createQuery(
            "select r from " + entityClass.getName() + " r where (r.threadId = :threadId or r.id = :threadId)",
            entityClass);//from  w  w w  .j  a  va2s.c o  m
    query.setParameter("threadId", threadId);
    return findList(query);
}

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

/**
 * selects a state by its id//from w  w  w. j a v  a2  s .c om
 * 
 * @param state_id
 * @return the state-object or null
 */
public States getStateById(long state_id) {
    try {
        TypedQuery<States> query = em.createQuery(
                "select c from States as c where c.state_id = :state_id AND c.deleted <> :deleted",
                States.class);
        query.setParameter("state_id", state_id);
        query.setParameter("deleted", "true");
        List<States> ll = query.getResultList();
        if (ll.size() > 0) {
            return ll.get(0);
        }
    } catch (Exception ex2) {
        log.error("getStateById", ex2);
    }
    return null;
}

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

public CategoryData findCategory(String category) {
    EntityManager em = null;/*  w ww  . j a  v a 2  s .c o  m*/
    try {
        em = entityService.get();
        TypedQuery<Category> q = em.createNamedQuery("findCategory", Category.class);
        q.setParameter("category", category);
        Category cat = getSingleResult(q);
        if (cat == null) {
            return new CategoryView();
        } else {
            return new CategoryView(cat.getId(), cat.getTitle());
        }
    } finally {
        EntityService.close(em);
    }
}

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

/**
 * Gets all relevant set covers./*from   w ww  . j  ava2 s  . co m*/
 * @param em the {@link EntityManager} to use.
 * @param ownerId the ID of the owner.
 * @param baseCorpus the {@link DocumentCorpus} which is the base corpus for the set cover.
 * @param entityClass the specific type of set covers to be retrieved; must be annotated with the {@link Entity} annotation.
 * @return a {@link List} containing {@link String} representations of the UUIDs.
 */
public <T extends DocumentSetCover> List<String> getAllSetCovers(EntityManager em, String ownerId,
        DocumentCorpus baseCorpus, Class<T> entityClass) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(ownerId, CannedMessages.NULL_ARGUMENT, "ownerId");
    Validate.notNull(baseCorpus, CannedMessages.NULL_ARGUMENT, "baseCorpus");
    Validate.notNull(entityClass, CannedMessages.NULL_ARGUMENT, "entityClass");

    TypedQuery<byte[]> query = em.createQuery("SELECT sc.id FROM DocumentSetCover sc "
            + "WHERE sc.ownerId=:ownerId " + "AND TYPE(sc)=:type " + "AND sc.baseStore=:baseCorpus",
            byte[].class);
    query.setParameter("ownerId", ownerId).setParameter("type", entityClass).setParameter("baseCorpus",
            baseCorpus);

    List<byte[]> results = query.getResultList();
    return Lists.newArrayList(Iterables.transform(results, UuidUtils.uuidBytesToStringFunction()));
}

From source file:org.wte4j.impl.WordTemplateRepository.java

private PersistentTemplate getPersistentTemplate(String documentName, String language)
        throws NoResultException {

    String queryString = "select t from PersistentTemplate t where t.documentName=:documentName "
            + "and t.language= :language";
    TypedQuery<PersistentTemplate> query = em.createQuery(queryString, PersistentTemplate.class);
    query.setParameter("documentName", documentName);
    query.setParameter("language", language);
    return query.getSingleResult();
}

From source file:cn.newgxu.lab.core.repository.impl.AbstractCommonDaoImpl.java

/**
 * ?<b style="color: red;"> ? </b>?????
 * @param hql hql//ww w.  j  av  a2  s.co  m
 * @param clazz 
 * @param offset ??????
 * @param number ????
 * @param objects ?null?
 * @return 
 */
protected List<T> executeQuery(String hql, Class<T> clazz, int offset, int number, Object... objects) {
    offset = rangeCheck(offset, clazz);
    TypedQuery<T> query = em.createNamedQuery(hql, clazz);
    if (objects != null) {
        for (int i = 0; i < objects.length; i++) {
            query.setParameter(i + 1, objects[i]);
        }
    }
    return query.setFirstResult(offset).setMaxResults(number).getResultList();
}