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:com.pet.demo.repository.jpa.JpaAccountRepositoryImpl.java

public Account findByUsername(String username) throws DataAccessException {
    Query query = this.em.createQuery("SELECT account FROM Account account WHERE account.userName =:username ");
    query.setParameter("username", username);

    try {/* w  w w .jav  a 2 s  .  c  o m*/
        throw new Exception("jpa1");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    Collection<Account> results = query.getResultList();
    if (results.size() == 1) {
        return results.iterator().next();
    }
    return null;
}

From source file:org.simbasecurity.core.domain.repository.ConditionDatabaseRepository.java

@SuppressWarnings("unchecked")
@Override// ww w . j a v  a 2s  .c  om
public Collection<Policy> findPolicies(Condition condition) {
    Query query = entityManager.createQuery(
            "SELECT policy FROM PolicyEntity policy WHERE :condition in elements(policy.conditions) ");
    query.setParameter("condition", condition);
    return new ArrayList<Policy>(query.getResultList());
}

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

public List<Comment> getComments(long experimentID, String experimentPhaseID) {
    Query query = manager.createQuery("from CommentImpl where experimentID=:expid and parentID=:parentid");
    query.setParameter("expid", experimentID);
    query.setParameter("parentid", -1l);
    return (List<Comment>) query.getResultList();
}

From source file:cz.muni.fi.pa165.deliveryservice.dao.jpa.JPACourierDAO.java

@Override
public Courier findByUsername(String username) {
    Query query = em.createQuery("SELECT c FROM Customer c WHERE c.user.username = :username");
    query.setParameter("username", username);

    Courier courier = (Courier) query.getSingleResult();
    return courier;
}

From source file:de.berlios.jhelpdesk.dao.jpa.TicketFilterDAOJpa.java

@Transactional(readOnly = false)
public void delete(final Long filterId) {
    this.jpaTemplate.execute(new JpaCallback<TicketFilter>() {
        public TicketFilter doInJpa(EntityManager em) throws PersistenceException {
            Query q = em.createQuery("DELETE FROM TicketFilter f WHERE f.ticketFilterId = ?1");
            q.setParameter(1, filterId);
            q.executeUpdate();//from w  w  w .  ja v a2  s . c o  m
            return null;
        }
    });
}

From source file:com.exp.tracker.services.impl.JavaMailEmailService.java

/**
 * Sends the settlement email./*from  w w  w . j  a va  2  s  .  c o  m*/
 */
@SuppressWarnings("unchecked")
@Async
public void sendSettlementEmail(Long sid, byte[] settlementReport, byte[] expenseReport) {
    // do not send email if sid is not valid
    if (sid != 0l) {
        Query queryGetSettlementForId = em.createNamedQuery("getSettlementForId");
        queryGetSettlementForId.setParameter("id", sid);
        SettlementEntity se = (SettlementEntity) queryGetSettlementForId.getSingleResult();
        SettlementBean sb = new SettlementBean(se);
        //
        Query querygetAllUsers = em.createNamedQuery("getAllUsers");
        List<UserEntity> uel = querygetAllUsers.getResultList();
        List<UserBean> ubl = new ArrayList<UserBean>();
        for (UserEntity ue : uel) {
            if (UserEntity.USER_ENABLED == ue.getEnabled() && null != ue.getEmailId()) {
                if (!("".equalsIgnoreCase(ue.getEmailId()))) {
                    ubl.add(new UserBean(ue));
                }
            }
        }
        // send email
        sendSettlementNotice(sb, ubl, settlementReport, expenseReport);
    }
}

From source file:cz.muni.fi.pa165.airport.dao.dao.PlaneDAO.java

@Override
public List<Plane> getAll() {
    Query query = em.createQuery("SELECT f FROM Plane f WHERE f.archived = :archived  ORDER BY f.name ASC");
    query.setParameter("archived", false);
    return (List<Plane>) query.getResultList();
}

From source file:org.venice.piazza.common.hibernate.dao.service.ServiceDaoImpl.java

public Page<ServiceEntity> getServiceList(Pagination pagination) {
    // Query//from   www.  j a  v a 2  s.  c om
    String queryString = String.format(SERVICE_QUERY, Direction.fromString(pagination.getOrder()));
    Query query = entityManager.createNativeQuery(queryString, ServiceEntity.class);
    query.setParameter(1, pagination.getSortBy());
    query.setParameter(2, pagination.getPerPage());
    query.setParameter(3, pagination.getPage() * pagination.getPerPage());
    List<ServiceEntity> results = query.getResultList();
    // Count
    query = entityManager.createNativeQuery(SERVICE_QUERY_COUNT);
    long count = ((BigInteger) query.getSingleResult()).longValue();
    return new PageImpl<ServiceEntity>(results, null, count);
}

From source file:com.pet.demo.repository.jpa.JpaOwnerRepositoryImpl.java

public Owner findById(int id) {
    // 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 owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id");
    query.setParameter("id", id);
    return (Owner) query.getSingleResult();
}

From source file:DAO.MessagesDAOImpl.java

@Transactional(readOnly = true)
@Override//from  w  w  w.j a v a2  s.  c o m
public MessagesEntity find(int id) {
    Query q = em.createQuery("SELECT m FROM MessagesEntity m where m.id = ?");
    q.setParameter(1, id);
    try {
        return (MessagesEntity) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}