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:net.chrissearle.flickrvote.dao.JpaPhotographyDao.java

/**
 * Method findByUsername finds the photographer with the given username. Null if none found.
 *
 * @param username of type String//  w ww  .  j  a v  a 2s  . co  m
 * @return Photographer
 */
public Photographer findByUsername(String username) {
    if (log.isLoggable(Level.FINE)) {
        log.fine("findByUsername : " + username);
    }

    Query query = entityManager.createQuery("select p from Photographer p where p.username = :username");
    query.setParameter("username", username);

    try {
        return (Photographer) query.getSingleResult();
    } catch (NoResultException e) {
        if (log.isLoggable(Level.FINE)) {
            log.fine("No matching user found");
        }

        // Just means that there is no photographer yet present
        return null;
    }
}

From source file:org.cleverbus.core.common.asynch.confirm.ConfirmationPollExecutorTest.java

@SuppressWarnings("unchecked")
@Nullable//from  w  ww .  j  a v a 2s .  c o  m
private ExternalCall findConfirmation(String correlationId) {
    String jSql = "SELECT c " + "FROM ExternalCall c " + "WHERE c.entityId = ?1";

    Query q = em.createQuery(jSql);
    q.setParameter(1, correlationId);
    q.setMaxResults(1);
    List<ExternalCall> extCalls = (List<ExternalCall>) q.getResultList();

    if (extCalls.isEmpty()) {
        return null;
    } else {
        return extCalls.get(0);
    }
}

From source file:org.opens.tanaguru.entity.dao.tgol.contract.TanaguruSurveyContractDAOImpl.java

/**
 * Native sql query ://from  w  w  w. java  2s.co m
 * SELECT count(Id_Contract) FROM TGSI_CONTRACT as tc
 *       INNER JOIN TGSI_USER as tu on (tu.Id_User=tc.USER_Id_User)
 *       WHERE tu.Email1 like :prefix;
 * 
 * @param prefix
 * @return
 *      the number of contract for user whom Email1 field starts with prefix
 */
@Override
public int findNumberOfContractsFromPrefix(String prefix) {
    StringBuilder queryString = new StringBuilder();
    queryString.append(FIND_NUMBER_OF_CONTRACT_QUERY);
    Query query = entityManager.createNativeQuery(queryString.toString());
    query.setParameter("prefix", prefix + "%");
    Object result;
    try {
        result = query.getSingleResult();
    } catch (NoResultException e) {
        return Integer.valueOf(0);
    }
    if (result instanceof BigInteger) {
        return ((BigInteger) result).intValue();
    } else if (result instanceof Integer) {
        return ((Integer) result);
    } else {
        return Integer.valueOf(0);
    }
}

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

public LicenseEXEntity findByCompanyExpirationDate(Integer idCompany, Calendar dateExpiration) {

    StringBuilder sb = new StringBuilder();
    sb.append("from LicenseEXEntity e where e.company.idCompany =:idCompany ");
    sb.append("and e.isActive =:active ");
    sb.append("and e.dateExpiration >= :dateExpiration ");
    sb.append("order by dateExpiration desc ");

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

    query.setParameter("idCompany", idCompany);
    query.setParameter("dateExpiration", dateExpiration);
    query.setParameter("active", Boolean.TRUE);

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

    if (list != null && !list.isEmpty()) {
        return list.get(0);
    }//w ww . j  a v a  2 s  .com

    // TODO Refatorar essa Gambi aqui. Jogar pra uma Service

    // Se no possui Licena vigente, verifica se a ltima est protocolada
    LicenseEXEntity lastProtocoled = this.findByLastProtocoled(idCompany);
    if (lastProtocoled != null && StringUtils.isNotBlank(lastProtocoled.getProtocolRenovation())) {
        return lastProtocoled;
    }

    return null;

}

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

@Transactional
@Override/*  ww w  .  j a v  a2  s .  c o m*/
public void remove(User user) {
    Query query = getEntityManager()
            .createQuery("UPDATE Account a SET a.assignedTo = null WHERE a.assignedTo = :user");
    query.setParameter("user", user);
    query.executeUpdate();

    query = getEntityManager()
            .createQuery("UPDATE Contact c SET c.assignedTo = null WHERE c.assignedTo = :user");
    query.setParameter("user", user);
    query.executeUpdate();

    query = getEntityManager()
            .createQuery("UPDATE Opportunity o SET o.assignedTo = null WHERE o.assignedTo = :user");
    query.setParameter("user", user);
    query.executeUpdate();

    super.remove(user);
}

From source file:com.linksinnovation.elearning.repository.ProgressReportRepository.java

public List<ProgressReport> findReport(QuizConditionDTO conditionDTO, String username) {
    if (null != conditionDTO.getCategory()) {
        String queryString = query + " AND category_id=:cat";
        if (null != conditionDTO.getSubcategory()) {
            queryString = queryString + " AND sub_category_id=:subcat";
        }/*  ww w.  j a  va2s. c om*/
        if (null != conditionDTO.getCourse()) {
            queryString = queryString + " AND course_id=:course";
        }
        if (null != conditionDTO.getStart() && null != conditionDTO.getEnd()) {
            queryString = queryString + " AND update_date BETWEEN :start AND :end";
        }
        queryString = queryString + " ORDER BY name,course_id ASC";

        Query q = em.createNativeQuery(queryString);
        q.setParameter("cat", conditionDTO.getCategory());
        q.setParameter("creator", username);
        if (null != conditionDTO.getSubcategory()) {
            q.setParameter("subcat", conditionDTO.getSubcategory());
        }
        if (null != conditionDTO.getCourse()) {
            q.setParameter("course", conditionDTO.getCourse());
        }
        if (null != conditionDTO.getStart() && null != conditionDTO.getEnd()) {
            q.setParameter("start", conditionDTO.getStart());
            q.setParameter("end", conditionDTO.getEnd());
        }
        return mapObject(q.getResultList());
    } else if (null == conditionDTO.getCategory()) {
        String queryString = query;
        if (null != conditionDTO.getStart() && null != conditionDTO.getEnd()) {
            queryString = queryString + " AND update_date BETWEEN :start AND :end";
        }
        queryString = queryString + " ORDER BY name,course_id ASC";
        Query q = em.createNativeQuery(queryString);
        q.setParameter("creator", username);
        if (null != conditionDTO.getStart() && null != conditionDTO.getEnd()) {
            q.setParameter("start", conditionDTO.getStart());
            q.setParameter("end", conditionDTO.getEnd());
        }
        return mapObject(q.getResultList());
    } else {
        String queryString = query + " ORDER BY name,course_id ASC";
        Query q = em.createNativeQuery(queryString);
        q.setParameter("creator", username);
        return mapObject(q.getResultList());
    }
}

From source file:com.linksinnovation.elearning.repository.ProgressReportRepository.java

public List<ProgressReport> findUserReport(QuizConditionDTO conditionDTO, String username) {
    if (null != conditionDTO.getCategory()) {
        String queryString = queryUser + " AND category_id=:cat";
        if (null != conditionDTO.getSubcategory()) {
            queryString = queryString + " AND sub_category_id=:subcat";
        }//from   ww w .  j  a v  a2s .co m
        if (null != conditionDTO.getCourse()) {
            queryString = queryString + " AND course_id=:course";
        }
        if (null != conditionDTO.getStart() && null != conditionDTO.getEnd()) {
            queryString = queryString + " AND update_date BETWEEN :start AND :end";
        }
        queryString = queryString + " ORDER BY name,course_id ASC";

        Query q = em.createNativeQuery(queryString);
        q.setParameter("cat", conditionDTO.getCategory());
        q.setParameter("username", username);
        if (null != conditionDTO.getSubcategory()) {
            q.setParameter("subcat", conditionDTO.getSubcategory());
        }
        if (null != conditionDTO.getCourse()) {
            q.setParameter("course", conditionDTO.getCourse());
        }
        if (null != conditionDTO.getStart() && null != conditionDTO.getEnd()) {
            q.setParameter("start", conditionDTO.getStart());
            q.setParameter("end", conditionDTO.getEnd());
        }
        return mapObject(q.getResultList());
    } else if (null == conditionDTO.getCategory()) {
        String queryString = queryUser;
        if (null != conditionDTO.getStart() && null != conditionDTO.getEnd()) {
            queryString = queryString + " AND update_date BETWEEN :start AND :end";
        }
        queryString = queryString + " ORDER BY name,course_id ASC";
        Query q = em.createNativeQuery(queryString);
        q.setParameter("username", username);
        if (null != conditionDTO.getStart() && null != conditionDTO.getEnd()) {
            q.setParameter("start", conditionDTO.getStart());
            q.setParameter("end", conditionDTO.getEnd());
        }
        return mapObject(q.getResultList());
    } else {
        String queryString = queryUser + " ORDER BY name,course_id ASC";
        Query q = em.createNativeQuery(queryString);
        q.setParameter("username", username);
        return mapObject(q.getResultList());
    }
}

From source file:com.brienwheeler.svc.usergroups.impl.UserGroupMemberDao.java

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
@SuppressWarnings("unchecked")
public List<UserGroupMember> findForUserAndType(User user, String groupType) {
    Query query = entityManager
            .createQuery("from UserGroupMember u where u.user = :user and u.userGroup.groupType = :groupType");
    query.setParameter("user", user);
    query.setParameter("groupType", groupType);
    return (List<UserGroupMember>) query.getResultList();
}

From source file:com.epam.ipodromproject.repository.jpa.JPAUserRepository.java

@Override
public void cancelBetsByCompetition(Long competitionID) {
    Query queryForUsers = entityManager.createNamedQuery("User.findMoneyBetByUsersByCompetition");
    queryForUsers.setParameter("competitionID", competitionID);
    queryForUsers.setParameter("betResult", BetResult.NOT_CONSIDERED);
    List<Object> result = queryForUsers.getResultList();
    Query query = entityManager.createNamedQuery("Person.returnMoneyToPerson");
    for (Object objects : result) {
        Object[] resultRow = (Object[]) objects;
        query.setParameter("id", (Long) resultRow[0]);
        query.setParameter("moneyReturned", (Double) resultRow[1]);
        query.executeUpdate();/*from  w  w  w  .  j  a  va 2s.c  om*/
    }

}

From source file:eu.planets_project.tb.impl.persistency.ServiceRecordPersistencyImpl.java

@SuppressWarnings("unchecked")
public ServiceRecordImpl findServiceRecordByHashcode(String hashcode) {
    log.info("Looking for service records matching hashcode: " + hashcode);
    Query query = manager.createQuery("from ServiceRecordImpl where serviceHash=:hashcode");
    query.setParameter("hashcode", hashcode);
    List<ServiceRecordImpl> results = (List<ServiceRecordImpl>) query.getResultList();
    if (results == null || results.size() == 0)
        return null;
    log.info("Found " + results.size() + " services with the given hashcode.");
    return results.get(0);
}