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:de.voolk.marbles.persistence.services.PageService.java

@Override
public Page getRootPage(IUser user) {
    Query query = getEntityManager().createNamedQuery("page:rootPage");
    query.setParameter("user", user);
    try {/*  w  w  w .j  a  va  2  s.c  o m*/
        return (Page) query.getSingleResult();
    } catch (NoResultException e) {
        Page page = new Page();
        page.setName("ROOT");
        StringBuilder sb = new StringBuilder();
        sb.append("Root Page").append("\n").append("---------").append("\n\n");
        sb.append("This is the ROOT Page please edit ...").append("\n\n");
        sb.append("%%ThisIsASubpage").append("\n\n");
        sb.append("This is a list:").append("\n\n");
        sb.append("* item1").append("\n");
        sb.append("* item2").append("\n");
        sb.append("* item3").append("\n");
        sb.append("\n");
        sb.append("for more see [Markdown](http://en.wikipedia.org/wiki/Markdown)");
        page.setContent(sb.toString());
        page.setUser(resolve(User.class, user));
        persist(page);
        return page;
    }
}

From source file:es.ucm.fdi.dalgs.user.repository.UserRepository.java

@SuppressWarnings("unchecked")
public List<String> getAllByRole(String user_role) {
    Query query = em.createQuery("select u from User u join u.roles ur where ur.role = ?1 order by u.id");
    query.setParameter(1, user_role);
    return query.getResultList();
}

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

@Override
public List<String> findUsersThatAllowedSender(String senderKey) {

    // Get all the usernames of users that blocked senders, because we only save users that opted out
    Query query = getEntityManager().createNamedQuery("Preference.findUsersThatBlockedSenderKey");
    query.setParameter("senderKey", senderKey);
    List<String> blockedUsernames = (List<String>) query.getResultList();

    // Find all the users that has devices
    query = getEntityManager().createNamedQuery("Device.getDeviceUsernames");
    List<String> allowedUsers = (List<String>) query.getResultList();

    // Now remove all the users that blocked
    allowedUsers.removeAll(blockedUsernames);

    // Those that remained should receive the notifications
    return allowedUsers;
}

From source file:com.mothsoft.alexis.dao.DataSetDaoImpl.java

@Override
public DataSet findSystemDataSet(DataSetType type, String name) {
    final Query query = this.em.createQuery("FROM DataSet ds WHERE ds.userId IS NULL AND ds.name = :name");
    query.setParameter("name", name);
    @SuppressWarnings("unchecked")
    List<DataSet> dataSets = query.getResultList();

    if (dataSets.isEmpty()) {
        return null;
    } else if (dataSets.size() > 1) {
        throw new IllegalStateException("Expecting 0 or 1 data sets, found: " + dataSets.size());
    } else {//from   ww  w.  j  a  va 2 s. c o  m
        return dataSets.get(0);
    }
}

From source file:com.mothsoft.alexis.dao.DataSetDaoImpl.java

@Override
public TopicActivityDataSet findTopicActivityDataSet(Long topicId) {
    final Query query = this.em.createQuery("FROM TopicActivityDataSet tads WHERE tads.topic.id = :topicId");
    query.setParameter("topicId", topicId);

    final List<?> results = query.getResultList();

    if (results == null || results.isEmpty()) {
        return null;
    }/*  w  ww  . j  av  a 2  s  .c o  m*/

    return (TopicActivityDataSet) results.get(0);
}

From source file:com.taobao.ad.easyschedule.base.JPABaseDAO.java

public int getQueryCount(String queryString, final Map<String, Object> map) {
    final List<String> keyList = getKeyList(map);
    final String queryStringCallBack = getQueryString(queryString, keyList, null);
    return (Integer) this.getJpaTemplate().execute(new JpaCallback() {

        @Override/* w w w.  j  a  va 2  s.c  o m*/
        public Integer 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 ((Long) query.getSingleResult()).intValue();
        }
    });
}

From source file:de.voolk.marbles.persistence.services.PageService.java

@Override
public IPage findPageByUserAndParentAndName(IUser user, IPage parent, String name) {
    Query query = getEntityManager().createNamedQuery("page:byUserAndParentAndName");
    query.setParameter("user", user);
    query.setParameter("parent", resolve(Page.class, parent));
    query.setParameter("name", name);
    try {/*from www. java  2  s. c  om*/
        return (Page) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:com.solidmaps.webapp.dao.LicensePFDAO.java

public List<LicensePFEntity> findByCompany(Integer idCompany) {

    StringBuilder sb = new StringBuilder();
    sb.append("from LicensePFEntity e where e.company.idCompany =:idCompany ");
    sb.append("order by e.dateInsert desc ");

    Query query = super.getEm().createQuery(sb.toString());

    query.setParameter("idCompany", idCompany);

    @SuppressWarnings("unchecked")
    List<LicensePFEntity> list = query.getResultList();

    return list;//from  w w  w . j a v  a  2s.  co  m
}

From source file:com.bia.ccm.dao.impl.OrderDetailDaoImpl.java

@Override
public PagedResult<OrderDetail> findOrderDetails(Long organization, Date startDate, Date endDate,
        OrderStatus orderStatus, String customer, int start, int max) {
    PagingParams pagingParams = new PagingParams(start, max);
    PagedResult pagedResult = new PagedResult(0, null, pagingParams);
    String ql = "SELECT s FROM OrderDetail s WHERE s.organization = ?1 ";
    boolean isValidDate = false;
    if (startDate != null && endDate != null) {
        ql += "AND s.createDate BETWEEN ?2 AND ?3 ";
        isValidDate = true;//from  ww w  .  j  a va2  s .c om
    }
    boolean isValidStatus = false;
    if (orderStatus != null) {
        ql += "AND s.orderStatus LIKE ?4 ";
        isValidStatus = true;
    }
    boolean isValidCustomer = false;
    if (customer != null && customer.trim().length() > 0) {
        ql += "AND (s.customerUserId = ?5 OR s.customerUsername LIKE ?6 OR s.customerEmail LIKE ?7 "
                + "OR s.customerName LIKE ?8 OR s.customerPhone LIKE ?9) ";
        isValidCustomer = true;
    }
    ql += "ORDER BY s.createDate";
    Query query = em.createQuery(ql).setFirstResult(start).setMaxResults(max);
    query.setParameter(1, organization);
    if (isValidDate) {
        query.setParameter(2, startDate);
        query.setParameter(3, endDate);
    }
    if (isValidStatus) {
        query.setParameter(4, orderStatus);
    }
    if (isValidCustomer) {
        Long userId = 0L;
        try {
            userId = Long.parseLong(customer);
        } catch (Exception ex) {
        }
        query.setParameter(5, userId);
        query.setParameter(6, customer);
        query.setParameter(7, customer);
        query.setParameter(8, customer);
        query.setParameter(9, customer);
    }
    List<OrderDetail> list = query.getResultList();
    pagedResult.setResults(list);
    return pagedResult;
}

From source file:com.sct.descubriendoturuta.service.UsuarioServiceImpl.java

@Override
@Transactional//w  w w . j  ava2s .co m
public List<Invitacion> obtenerRutasInvitadas(int usuarioId) {

    Query query = em.createQuery("select i from Invitacion i WHERE i.usuario.id = :p");

    query.setParameter("p", usuarioId);

    return query.getResultList();
}