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.acmeair.jpa.service.FlightServiceImpl.java

@Override
public List<Flight> getFlightByAirports(String fromAirport, String toAirport) {
    Query q = em.createQuery("SELECT obj FROM FlightSegment obj where obj.destPort=?1 and obj.originPort=?2");
    q.setParameter(1, toAirport);
    q.setParameter(2, fromAirport);/*  ww  w . j  a va 2 s . c  om*/

    List<Flight> flights = new ArrayList<Flight>();

    List<FlightSegment> results = (List<FlightSegment>) q.getResultList();
    for (FlightSegment seg : results) {
        Query qq = em.createQuery("SELECT obj FROM Flight obj where obj.pkey.flightSegmentId=?1");
        qq.setParameter(1, seg.getFlightName());

        List<Flight> foundFlights = (List<Flight>) qq.getResultList();
        for (Flight flight : foundFlights) {
            flight.setFlightSegment(seg);
            flights.add(flight);
        }
    }

    return flights;
}

From source file:es.ucm.fdi.dalgs.degree.repository.DegreeRepository.java

public Degree existByCode(String code) {
    Query query = em.createQuery("select d from Degree d where d.info.code=?1");
    query.setParameter(1, code);
    if (query.getResultList().isEmpty())
        return null;
    else/*from   w  w  w  . ja  va 2 s  .c o  m*/
        return (Degree) query.getSingleResult();
}

From source file:net.chrissearle.flickrvote.dao.JpaChallengeDao.java

/**
 * Method getClosedChallenges returns all challenges in a closed state.
 *
 * @return all closed challenges./*from  w  ww.  ja v  a  2  s  . com*/
 */
@SuppressWarnings("unchecked")
public List<Challenge> getClosedChallenges() {
    Query query = entityManager.createQuery("select c from Challenge c where c.endDate < :now");
    query.setParameter("now", new Date());
    return (List<Challenge>) query.getResultList();
}

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

@Override
public List<Model> findByTypeAndState(ModelType type, ModelState state) {
    final Query query = this.em
            .createQuery("SELECT m FROM Model m WHERE m.state = :state AND m.type = :type ORDER BY m.id ASC");
    query.setParameter("type", type);
    query.setParameter("state", state);

    @SuppressWarnings("unchecked")
    final List<Model> models = (List<Model>) query.getResultList();

    return models;
}

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

@Override
public boolean hasChildren(IPage page) {
    Query query = getEntityManager().createNamedQuery("page:hasChildren");
    query.setParameter("parent", page);
    return ((Number) query.getSingleResult()).intValue() > 0;
}

From source file:net.chrissearle.flickrvote.dao.JpaChallengeDao.java

/**
 * Method findByTag returns the challenge with the given tag. Null if no matching challenge found.
 *
 * @param tag of type String//from ww  w  . j a  va2  s . co m
 * @return Challenge
 */
@SuppressWarnings("unchecked")
public Challenge findByTag(String tag) {
    Query query = entityManager.createQuery("select c from Challenge c where c.tag = :tag");
    query.setParameter("tag", tag);

    try {
        return (Challenge) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:net.urlgrey.mythpodcaster.dao.MythRecordingsDAOImpl.java

@SuppressWarnings("unchecked")
@Override/*from   w ww  .  j av a2s .  com*/
@Transactional(readOnly = true)
public RecordedSeries findRecordedSeries(String seriesId) {
    final Query namedQuery = entityManager.createNamedQuery("MYTH_RECORDINGS.findRecordedSeries");
    namedQuery.setParameter("seriesId", seriesId);

    final List<RecordedSeries> seriesResultsList = namedQuery.getResultList();
    if (seriesResultsList != null && seriesResultsList.size() == 1) {
        final Query seriesProgramsQuery = entityManager
                .createNamedQuery("MYTH_RECORDINGS.findRecordedProgramsForSeries");
        seriesProgramsQuery.setParameter("seriesId", seriesId);

        final RecordedSeries series = seriesResultsList.get(0);
        series.setRecordedPrograms(seriesProgramsQuery.getResultList());
        return series;
    }

    return null;
}

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

@Override
@Transactional/*from   w  w  w. j ava  2 s  .  c  om*/
public List<MDOGenre> getAll(String username) {
    Query query = entityManager
            .createQuery("from MDOGenre as a where (a.owner.login= :userName) ORDER BY UPPER(a.name)");
    query.setParameter("userName", username);
    @SuppressWarnings("unchecked")
    List<MDOGenre> results = query.getResultList();
    return results;
}

From source file:com.acmeair.jpa.service.FlightServiceImpl.java

@Override
public List<Flight> getFlightByAirportsAndDepartureDate(String fromAirport, String toAirport, Date deptDate) {
    List<Flight> flights = new ArrayList<Flight>();

    Query q = em.createQuery("SELECT obj FROM FlightSegment obj where obj.destPort=?1 and obj.originPort=?2");
    q.setParameter(1, toAirport);
    q.setParameter(2, fromAirport);/*w w  w . j a  va  2s .  co  m*/

    List<FlightSegment> results = (List<FlightSegment>) q.getResultList();
    for (FlightSegment seg : results) {
        Query qq = em.createQuery(
                "SELECT obj FROM Flight obj where  obj.scheduledDepartureTime=?1 and obj.pkey.flightSegmentId=?2");
        qq.setParameter(1, deptDate);
        qq.setParameter(2, seg.getFlightName());

        List<Flight> foundFlights = (List<Flight>) qq.getResultList();
        for (Flight flight : foundFlights) {
            flight.setFlightSegment(seg);
            flights.add(flight);
        }
    }

    return flights;
}

From source file:com.cimpoint.mes.server.repositories.PartRepository.java

@SuppressWarnings("unchecked")
public Set<String> findRevisionsByPartName(String partName) throws Exception {
    try {//www .  ja  v a 2  s .  c  o m
        Set<String> strSet = new HashSet<String>();

        Query query = entityManager.createQuery("SELECT e FROM EPart e where e.name = ?1");
        query.setParameter(1, partName);
        List<EPart> parts = (List<EPart>) query.getResultList();
        if (parts != null && parts.size() > 0) {
            for (EPart e : parts) {
                strSet.add(e.getRevision());
            }
        }
        return strSet;
    } catch (NoResultException ex) {
        return new HashSet<String>();
    } catch (Exception ex) {
        ex.printStackTrace();
        return new HashSet<String>();
    }
}