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:it.volaconnoi.bean.RouteManagerBean.java

@Override
public List<Route> getListTimesByAirportSourceAirportDest(String airport_source, String airport_dest) {
    TypedQuery<Route> query = em.createQuery(
            "SELECT r FROM Route r WHERE r.airport_city_source.name = :airport_source AND r.airport_city_dest.name = :airport_dest",
            Route.class);

    return query.setParameter("airport_source", airport_source).setParameter("airport_dest", airport_dest)
            .getResultList();//from w w  w .j  a va 2  s  . c o  m
}

From source file:models.Group.java

/**
  * //from  w  w  w . j a va 2  s  .co m
  * 
  * @param page 1
  * @param pageSize ??
  * @param industryId Id?
  * @param privacy ???
  * @param skillTag ?
  * @param groupName ???
  * @param memberUserId ?Id?
  * @param isOwner ????
  * @param orderBy ??????SQL
  * @param isDesc ???
  * @return Page
 */
public static Page<Group> queryByPage(int page, int pageSize, Long industryId, GroupPriv privacy,
        String skillTag, String groupName, List<Type> typeList, Long memberUserId, Boolean isOwner,
        String orderBy, Boolean isDesc) {
    StringBuilder hql = new StringBuilder();
    Map<String, Object> paramMap = new HashMap<>();
    hql.append("from Group g ");
    if (memberUserId != null) {
        hql.append(" left join g.groupmembers gm where gm.userId = :memberUserId ");
        paramMap.put("memberUserId", memberUserId);
    } else {
        hql.append(" where 1 = 1");
    }
    if (memberUserId != null && isOwner != null) {
        hql.append(" and ((g.owner.userId ").append(isOwner ? " = " : " <> ")
                .append(" :ownerUserId and g.type = :hasOwnerType) or g.type <> :hasOwnerType) ");
        paramMap.put("hasOwnerType", Type.NORMAL);
        paramMap.put("ownerUserId", memberUserId);
    }
    if (CollectionUtils.isNotEmpty(typeList)) {
        hql.append(" and g.type in (:typeList) ");
        paramMap.put("typeList", typeList);
    }
    if (null != industryId) {
        hql.append(" and g.industry.id = :industryId ");
        paramMap.put("industryId", industryId);
    }
    if (null != privacy) {
        hql.append(" and g.groupPriv = :privacy ");
        paramMap.put("privacy", privacy);
    }
    if (StringUtils.isNotBlank(skillTag)) {
        hql.append(" and g.tags like :skillTag ");
        paramMap.put("skillTag", "%" + skillTag + "%");
    }
    if (StringUtils.isNotBlank(groupName)) {
        hql.append(" and g.groupName like :groupName ");
        paramMap.put("groupName", "%" + groupName + "%");
    }

    String countHql = "select count(g.id) " + hql.toString();

    // order
    if (StringUtils.isNotBlank(orderBy)) {
        hql.append(" order by g.");
        hql.append(orderBy);
        hql.append(BooleanUtils.isFalse(isDesc) ? " asc " : " desc ");
    } else {
        hql.append(" order by countMem desc, id desc");
    }
    hql.insert(0, "select g ");

    TypedQuery<Group> listTypedQuery = JPA.em().createQuery(hql.toString(), Group.class)
            .setFirstResult((page - 1) * pageSize).setMaxResults(pageSize);
    TypedQuery<Long> countTypedQuery = JPA.em().createQuery(countHql, Long.class);
    for (Entry<String, Object> e : paramMap.entrySet()) {
        listTypedQuery.setParameter(e.getKey(), e.getValue());
        countTypedQuery.setParameter(e.getKey(), e.getValue());
    }

    List<Group> resultList = listTypedQuery.getResultList();
    Long count = countTypedQuery.getSingleResult();

    return new Page<Group>(Constants.SUCESS, count, resultList);
}

From source file:com.sjsu.bikelet.service.TenantLicensePolicyServiceImpl.java

@Override
public List<TenantLicensePolicy> verifyLicensePolicyDates(TenantLicensePolicy tenantLicensePolicy) {

    TypedQuery<TenantLicensePolicy> licenseQuery = TenantLicensePolicy.entityManager()
            .createQuery("from TenantLicensePolicy tlc "
                    + " where ((tlc.licenseStartDate between :newStartDate and :newEndDate) or tlc.licenseEndDate >= :newEndDate) and tlc.tenantId.id= :tenantId",
                    TenantLicensePolicy.class);
    licenseQuery.setParameter("newStartDate", tenantLicensePolicy.getLicenseStartDate());
    licenseQuery.setParameter("newEndDate", tenantLicensePolicy.getLicenseEndDate());
    licenseQuery.setParameter("tenantId", tenantLicensePolicy.getTenantId().getId());
    List<TenantLicensePolicy> activeLicensePolicy = licenseQuery.getResultList();
    return activeLicensePolicy;
}

From source file:eu.domibus.ebms3.common.dao.PModeDao.java

@Override
public Agreement getAgreement(final String pModeKey) {
    final TypedQuery<Agreement> query = this.entityManager.createNamedQuery("Agreement.findByName",
            Agreement.class);
    query.setParameter("NAME", this.getAgreementRefNameFromPModeKey(pModeKey));
    return query.getSingleResult();
}

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

public UserContacts getContactsByHash(String hash) {
    try {//w  w w  .  j  a  v a2 s  . co  m

        String hql = "select c from UserContacts c " + "where c.hash like :hash ";

        TypedQuery<UserContacts> query = em.createQuery(hql, UserContacts.class);
        query.setParameter("hash", hash);
        List<UserContacts> ll = query.getResultList();

        if (ll.size() > 0) {
            return ll.get(0);
        }

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

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

public List<UserContacts> getContactsByUserAndStatus(Long ownerId, Boolean pending) {
    try {//from  ww  w . j  a v  a 2s  .c o  m

        String hql = "select c from UserContacts c " + "where c.owner.user_id = :ownerId "
                + "AND c.pending = :pending " + "AND c.contact.deleted <> 'true'";

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

        return ll;

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

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

public List<UserContacts> getContactsByShareCalendar(Long contactId, Boolean shareCalendar) {
    try {//from w ww.  j  a v  a2 s .co m

        String hql = "select c from UserContacts c " + "where c.contact.user_id = :contactId "
                + "AND c.shareCalendar = :shareCalendar " + "AND c.contact.deleted <> 'true'";

        TypedQuery<UserContacts> query = em.createQuery(hql, UserContacts.class);
        query.setParameter("contactId", contactId);
        query.setParameter("shareCalendar", shareCalendar);
        List<UserContacts> ll = query.getResultList();

        return ll;

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

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

public List<UserContacts> getContactRequestsByUserAndStatus(Long user_id, Boolean pending) {
    try {//  w  w w.  j av  a 2  s . c  om

        String hql = "select c from UserContacts c " + "where c.contact.user_id = :user_id "
                + "AND c.pending = :pending " + "AND c.contact.deleted <> 'true'";

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

        return ll;

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

From source file:eu.europa.ec.fisheries.uvms.exchange.dao.bean.ExchangeLogDaoBean.java

@Override
public List<ExchangeLogStatus> getExchangeLogStatusHistory(String sql, ExchangeHistoryListQuery searchQuery)
        throws ExchangeDaoException {
    try {//from   w  ww.ja  v a2s  .  c  o  m
        LOG.debug("SQL query for status history " + sql);
        TypedQuery<ExchangeLogStatus> query = em.createQuery(sql, ExchangeLogStatus.class);
        if (searchQuery.getStatus() != null && !searchQuery.getStatus().isEmpty()) {
            query.setParameter("status", searchQuery.getStatus());
        }
        if (searchQuery.getType() != null && !searchQuery.getType().isEmpty()) {
            query.setParameter("type", searchQuery.getType());
        }
        if (searchQuery.getTypeRefDateFrom() != null) {
            Date from = searchQuery.getTypeRefDateFrom();
            query.setParameter("from", from);
        }
        if (searchQuery.getTypeRefDateTo() != null) {
            Date to = searchQuery.getTypeRefDateTo();
            query.setParameter("to", to);
        }
        return query.getResultList();
    } catch (IllegalArgumentException e) {
        LOG.error("[ Error getting exchangelog status list ] " + e.getMessage());
        throw new ExchangeDaoException("[ Error when getting search list ] ");
    } catch (Exception e) {
        LOG.error("[ Error getting exchangelog status list " + e.getMessage());
        throw new ExchangeDaoException("[ Error when getting search list ] ");
    }
}

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

public UserContacts getUserContacts(Long userContactId) {
    try {/*from w ww.j  av a  2s  .co  m*/

        String hql = "select c from UserContacts c " + "where c.userContactId = :userContactId";

        TypedQuery<UserContacts> query = em.createQuery(hql, UserContacts.class);
        query.setParameter("userContactId", userContactId);
        UserContacts userContacts = null;
        try {
            userContacts = query.getSingleResult();
        } catch (NoResultException ex) {
        }

        return userContacts;

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