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.beto.test.securityinterceptor.model.dao.generic.AbstractDao.java

public List<T> getListWithNamedQuery(String query, Map<String, Object> inList) throws Exception {
    try {/*w w  w  . j ava2 s  .c  o  m*/
        TypedQuery<T> tq = getEntityManager().createNamedQuery(query, entityClass);
        for (Map.Entry<String, Object> entrySet : inList.entrySet()) {
            String key = entrySet.getKey();
            Object value = entrySet.getValue();
            tq.setParameter(key, value);
        }
        return tq.getResultList();
    } catch (NoResultException e) {
        LOGGER.debug("NO DATA FOUND...");
    }
    return null;
}

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

public <T> List<T> getPagedListByPropertyValue(Class<T> clazz, String propertyName, Object propertyValue,
        int page, int pageSize) {
    String queryString = "SELECT o FROM " + clazz.getName() + " o WHERE " + propertyName + "=:" + propertyName;
    TypedQuery<T> query = getEntityManager().createQuery(queryString, clazz);
    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);/*ww  w.j  av a2 s  .c  o  m*/
    query.setParameter(propertyName, propertyValue);
    return query.getResultList();
}

From source file:org.openmeetings.app.data.user.dao.UserContactsDaoImpl.java

public Long checkUserContacts(Long user_id, Long ownerId) {
    try {//from  w  ww  .  ja va 2 s.c  o m

        String hql = "select count(c.userContactId) from UserContacts c "
                + "where c.contact.user_id = :user_id AND c.owner.user_id = :ownerId ";

        TypedQuery<Long> query = em.createQuery(hql, Long.class);
        query.setParameter("user_id", user_id);
        query.setParameter("ownerId", ownerId);
        List<Long> ll = query.getResultList();

        log.info("checkUserContacts" + ll.get(0));

        return ll.get(0);

    } catch (Exception e) {
        log.error("[checkUserContacts]", e);
    }
    return null;
}

From source file:org.openmeetings.app.data.conference.dao.RoomModeratorsDaoImpl.java

public List<RoomModerators> getRoomModeratorByRoomId(Long roomId) {
    try {/*  ww  w .j ava 2s .  co m*/

        String hql = "select c from RoomModerators as c "
                + "where c.roomId = :roomId AND c.deleted <> :deleted";

        TypedQuery<RoomModerators> query = em.createQuery(hql, RoomModerators.class);

        query.setParameter("deleted", "true");
        query.setParameter("roomId", roomId);

        List<RoomModerators> roomModerators = query.getResultList();

        return roomModerators;

    } catch (Exception ex2) {
        log.error("[getRoomModeratorByRoomId] ", ex2);
        ex2.printStackTrace();
    }
    return null;
}

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

public LicenseType findByName(String licName) {
    String jpql = "select t from LicenseType t where t.licName =:licName ";
    TypedQuery<LicenseType> query = em().createQuery(jpql, LicenseType.class);
    //      query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
    query.setParameter("licName", licName);
    List<LicenseType> list = query.getResultList();
    return list.get(0);
}

From source file:org.openmeetings.app.data.conference.dao.RoomModeratorsDaoImpl.java

/**
 * get all available RoomTypes/*from w w w  . ja  v a 2  s  . c  o  m*/
 * 
 * @return List of RoomTypes
 */
public RoomModerators getRoomModeratorById(Long roomModeratorsId) {
    try {
        String hql = "select c from RoomModerators as c where c.roomModeratorsId = :roomModeratorsId";

        TypedQuery<RoomModerators> query = em.createQuery(hql, RoomModerators.class);

        query.setParameter("roomModeratorsId", roomModeratorsId);

        RoomModerators roomModerators = null;
        try {
            roomModerators = query.getSingleResult();
        } catch (NoResultException ex) {
        }

        return roomModerators;

    } catch (Exception ex2) {
        log.error("[getRoomModeratorById] ", ex2);
    }
    return null;
}

From source file:org.openmeetings.app.data.conference.dao.RoomModeratorsDaoImpl.java

public List<RoomModerators> getRoomModeratorByUserAndRoomId(Long roomId, Long user_id) {
    try {/*w w w .  ja  va 2s  . c o  m*/

        String hql = "select c from RoomModerators as c " + "where c.roomId = :roomId "
                + "AND c.deleted <> :deleted " + "AND c.user.user_id = :user_id";

        TypedQuery<RoomModerators> query = em.createQuery(hql, RoomModerators.class);

        query.setParameter("deleted", "true");
        query.setParameter("roomId", roomId);
        query.setParameter("user_id", user_id);

        List<RoomModerators> roomModerators = query.getResultList();

        return roomModerators;

    } catch (Exception ex2) {
        log.error("[getRoomModeratorByUserAndRoomId] ", ex2);
        ex2.printStackTrace();
    }
    return null;
}

From source file:eu.ggnet.dwoss.report.ReportAgentBean.java

@Override
public long count(SearchParameter search) {
    StringBuilder sb = new StringBuilder("Select Count(l) from ReportLine l");
    if (!StringUtils.isBlank(search.getRefurbishId()))
        sb.append(" where l.refurbishId = :refurbishId");
    L.debug("Using created SearchQuery:{}", sb);
    TypedQuery<Long> q = reportEm.createQuery(sb.toString(), Long.class);
    if (!StringUtils.isBlank(search.getRefurbishId()))
        q.setParameter("refurbishId", search.getRefurbishId().trim());
    return q.getSingleResult();
}

From source file:com.fantasy.stataggregator.entities.dao.AbstractRepository.java

public List<T> findByNamedQuery(Class<T> entity, String name, Map<String, ? extends Object> parameters) {
    NamedQueries nq = entity.getAnnotation(NamedQueries.class);
    NamedQuery[] nqs = nq.value();/*w ww.  j  a  v a  2 s .  c o m*/
    String queryName = null;

    for (NamedQuery qn : nqs) {
        String qname = qn.name();
        if (qname.contains(name)) {
            queryName = qname;
            break;
        }
    }
    List<T> results = new ArrayList(1);
    if (Objects.nonNull(queryName)) {
        TypedQuery<T> query = em.createNamedQuery(queryName, entityClass);
        if (Objects.nonNull(parameters) && !parameters.isEmpty()) {
            parameters.entrySet().stream().forEach((entry) -> {
                query.setParameter((String) entry.getKey(), entry.getValue());
            });
        }
        results = query.getResultList();
    }
    return results;
}

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

@Override
public ExternalResource find(final String name) {
    TypedQuery<ExternalResource> query = entityManager.createQuery(
            "SELECT e " + "FROM " + ExternalResource.class.getSimpleName() + " e " + "WHERE e.name = :name",
            ExternalResource.class);
    query.setParameter("name", name);

    ExternalResource result = null;//  www. j a v a2s . c  o  m
    try {
        result = query.getSingleResult();
    } catch (NoResultException e) {
    }

    return result;
}