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:org.venice.piazza.common.hibernate.dao.dataresource.DataResourceDaoImpl.java

public Page<DataResourceEntity> getDataResourceListByUser(String userName, Pagination pagination) {
    // Query//from w  w w . jav a 2 s .  co m
    String queryString = String.format(USERNAME_DATARESOURCE_QUERY,
            Direction.fromString(pagination.getOrder()));
    Query query = entityManager.createNativeQuery(queryString, DataResourceEntity.class);
    query.setParameter(1, String.format(WILDCARD_STRING_QUERY, userName));
    query.setParameter(2, pagination.getSortBy());
    query.setParameter(3, pagination.getPerPage());
    query.setParameter(4, pagination.getPage() * pagination.getPerPage());
    List<DataResourceEntity> results = query.getResultList();
    // Count
    query = entityManager.createNativeQuery(USERNAME_DATARESOURCE_QUERY_COUNT);
    query.setParameter(1, String.format(WILDCARD_STRING_QUERY, userName));
    long count = ((BigInteger) query.getSingleResult()).longValue();
    return new PageImpl<DataResourceEntity>(results, null, count);
}

From source file:com.expressui.sample.dao.CurrencyDao.java

private List<Currency> findAllWithFxRates() {
    Query query = getEntityManager()
            .createQuery("SELECT c FROM Currency c " + " WHERE c.id in :currenciesWithFxRates ORDER BY c.name");

    query.setParameter("currenciesWithFxRates", ecbfxService.getFXRates().keySet());
    setReadOnly(query);//from w w  w .j av  a 2s  .  co  m

    return query.getResultList();
}

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

@SuppressWarnings("unchecked")
@Override//from www  .  ja  v  a  2  s .  c o  m
public List<DataSetPoint> findByTimeRange(DataSet dataSet, Timestamp startDate, Timestamp endDate) {
    final String queryString = "SELECT p FROM DataSetPoint p WHERE p.dataSet = :dataSet AND p.x >= :startDate AND p.x <= :endDate ORDER BY p.x ASC";
    final Query query = this.em.createQuery(queryString);
    query.setParameter("dataSet", dataSet);
    query.setParameter("startDate", startDate);
    query.setParameter("endDate", endDate);

    final List<DataSetPoint> points = query.getResultList();
    return points;
}

From source file:org.venice.piazza.common.hibernate.dao.dataresource.DataResourceDaoImpl.java

public Page<DataResourceEntity> getDataResourceListByKeyword(String keyword, Pagination pagination) {
    // Query//ww  w  . j av a 2 s . c o  m
    String queryString = String.format(KEYWORD_DATARESOURCE_QUERY, Direction.fromString(pagination.getOrder()));
    Query query = entityManager.createNativeQuery(queryString, DataResourceEntity.class);
    query.setParameter(1, String.format(WILDCARD_STRING_QUERY, keyword));
    query.setParameter(2, String.format(WILDCARD_STRING_QUERY, keyword));
    query.setParameter(3, pagination.getSortBy());
    query.setParameter(4, pagination.getPerPage());
    query.setParameter(5, pagination.getPage() * pagination.getPerPage());
    List<DataResourceEntity> results = query.getResultList();
    // Count
    query = entityManager.createNativeQuery(KEYWORD_DATARESOURCE_QUERY_COUNT);
    query.setParameter(1, String.format(WILDCARD_STRING_QUERY, keyword));
    query.setParameter(2, String.format(WILDCARD_STRING_QUERY, keyword));
    long count = ((BigInteger) query.getSingleResult()).longValue();
    return new PageImpl<DataResourceEntity>(results, null, count);
}

From source file:org.venice.piazza.common.hibernate.dao.dataresource.DataResourceDaoImpl.java

public Page<DataResourceEntity> getDataResourceListByCreatedJobId(String createdByJobId,
        Pagination pagination) {/*from  w  ww .j  ava2 s  . co  m*/
    // Query
    String queryString = String.format(CREATED_BY_JOB_ID_DATARESOURCE_QUERY,
            Direction.fromString(pagination.getOrder()));
    Query query = entityManager.createNativeQuery(queryString, DataResourceEntity.class);
    query.setParameter(1, String.format(WILDCARD_STRING_QUERY, createdByJobId));
    query.setParameter(2, pagination.getSortBy());
    query.setParameter(3, pagination.getPerPage());
    query.setParameter(4, pagination.getPage() * pagination.getPerPage());
    List<DataResourceEntity> results = query.getResultList();
    // Count
    query = entityManager.createNativeQuery(CREATED_BY_JOB_ID_DATARESOURCE_QUERY_COUNT);
    query.setParameter(1, String.format(WILDCARD_STRING_QUERY, createdByJobId));
    long count = ((BigInteger) query.getSingleResult()).longValue();
    return new PageImpl<DataResourceEntity>(results, null, count);
}

From source file:org.messic.server.datamodel.jpaimpl.DAOJPAPlaylist.java

@Override
@Transactional/*from  ww  w .j  ava2 s .  co m*/
public MDOPlaylist get(String username, long sid) {
    Query query = entityManager
            .createQuery("from MDOPlaylist as a where (a.owner.login = :userName AND a.sid = :playlistSid)");
    query.setParameter("userName", username);
    query.setParameter("playlistSid", sid);

    @SuppressWarnings("unchecked")
    List<MDOPlaylist> results = query.getResultList();
    if (results != null && results.size() > 0) {
        return results.get(0);
    }
    return null;
}

From source file:com.branded.holdings.qpc.repository.jpa.JpaOwnerRepositoryImpl.java

/**
 * Important: in the current version of this method, we load Owners with all their Pets and Visits while 
 * we do not need Visits at all and we only need one property from the Pet objects (the 'name' property).
 * There are some ways to improve it such as:
 * - creating a Ligtweight class (example here: https://community.jboss.org/wiki/LightweightClass)
 * - Turning on lazy-loading and using {@link OpenSessionInViewFilter}
 *//*from w w  w.j a  v a2 s .c  o  m*/
@SuppressWarnings("unchecked")
public Collection<Owner> findByLastName(String lastName) {
    // using 'join fetch' because a single query should load both owners and pets
    // using 'left join fetch' because it might happen that an owner does not have pets yet
    Query query = this.em.createQuery(
            "SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName");
    query.setParameter("lastName", lastName + "%");
    return query.getResultList();
}

From source file:pdsanchez.mywebtools.model.dao.jpa.ToolDAOJPAImpl.java

@Override
public List<Tool> findAll(Category category, Subcategory subcategory) {
    Query q = getEntityManager().createQuery(
            "select o from Tool o where lower(o.category.categoryName) = :cname and lower(o.subcategory.subcategoryName) = :scname and o.isvalid = 1");
    q.setParameter("cname", category.getCategoryName().toLowerCase());
    q.setParameter("scname", subcategory.getSubcategoryName().toLowerCase());
    return q.getResultList();
}

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

public Topic findTopicByUserAndName(Long userId, String name) {
    final Query query = this.em
            .createQuery("select t from Topic t where t.userId = :userId and t.name = :name");
    query.setParameter("userId", userId);
    query.setParameter("name", name);

    @SuppressWarnings("unchecked")
    final List<Topic> topics = query.getResultList();

    if (topics.isEmpty()) {
        return null;
    } else {//  w  w w.j  ava 2  s. c  o  m
        return topics.get(0);
    }
}

From source file:com.rambird.repository.springdatajpa.JpaOwnerRepositoryImpl.java

@Override
@SuppressWarnings("unchecked")
public Collection<Owner> findByLastName(String lastName) {
    // using 'join fetch' because a single query should load both owners and pets
    // using 'left join fetch' because it might happen that an owner does not have pets yet
    Query query = this.em.createQuery(
            "SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE upper(owner.lastName) LIKE upper(:lastName)");
    query.setParameter("lastName", lastName + "%");
    return query.getResultList();
}