Example usage for javax.persistence TypedQuery setParameter

List of usage examples for javax.persistence TypedQuery setParameter

Introduction

In this page you can find the example usage for javax.persistence TypedQuery setParameter.

Prototype

TypedQuery<X> setParameter(int position, Object value);

Source Link

Document

Bind an argument value to a positional parameter.

Usage

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
@Nullable//  ww  w.  jav  a 2s.  c  o  m
public Message findPartlyFailedMessage(int interval) {
    // find message that was lastly processed before specified interval
    Date lastUpdateLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.state = '"
            + MsgStateEnum.PARTLY_FAILED + "'" + "     AND m.lastUpdateTimestamp < :lastTime"
            + " ORDER BY m.msgTimestamp";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("lastTime", new Timestamp(lastUpdateLimit.getTime()));
    q.setMaxResults(1);
    List<Message> messages = q.getResultList();

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

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
@Nullable//from w w  w  . j  a  va 2  s  .c  om
public Message findPostponedMessage(int interval) {
    // find message that was lastly processed before specified interval
    Date lastUpdateLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.state = '"
            + MsgStateEnum.POSTPONED + "'" + "     AND m.lastUpdateTimestamp < :lastTime"
            + " ORDER BY m.msgTimestamp";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("lastTime", new Timestamp(lastUpdateLimit.getTime()));
    q.setMaxResults(1);
    List<Message> messages = q.getResultList();

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

From source file:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.LexiconBuilderController.java

/**
 * Gets the previously seen tokens for the given {@link LexiconBuilderDocumentStore}.
 * @param em the {@link EntityManager} to use.
 * @param builder the identifier of the {@link LexiconBuilderDocumentStore} to use.
 * @return the {@link List} of {@link LexiconDocument} objects.
 *///  w w  w .  j  a  va2  s  . co  m
public List<LexiconDocument> getSeenTokens(EntityManager em, LexiconBuilderDocumentStore builder) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(builder, CannedMessages.NULL_ARGUMENT, "builder");

    TypedQuery<LexiconDocument> query = em
            .createQuery("SELECT doc FROM LexiconDocument doc WHERE doc.store=:builder", LexiconDocument.class);
    query.setParameter("builder", builder);

    return query.getResultList();
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
@Nullable//from  w  w  w  .  j  a  v  a  2s .  c  om
public Message findByCorrelationId(String correlationId, @Nullable ExternalSystemExtEnum sourceSystem) {
    Assert.notNull(correlationId, "the correlationId must not be null");

    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m "
            + "WHERE m.correlationId = :correlationId";

    if (sourceSystem != null) {
        jSql += " AND m.sourceSystemInternal = :sourceSystem";
    }

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("correlationId", correlationId);

    if (sourceSystem != null) {
        q.setParameter("sourceSystem", sourceSystem.getSystemName());
    }

    // we search by unique key - it's not possible to have more records
    List<Message> messages = q.getResultList();
    if (messages.isEmpty()) {
        return null;
    } else {
        return messages.get(0); // if find more items then return first one only
    }
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
public Boolean updateMessageForLock(final Message msg) {
    Assert.notNull(msg, "the msg must not be null");

    // acquire pessimistic lock firstly
    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.msgId = :msgId"
            + "     AND (m.state = '" + MsgStateEnum.PARTLY_FAILED + "' " + "     OR m.state = '"
            + MsgStateEnum.POSTPONED + "')";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("msgId", msg.getMsgId());
    // note: https://blogs.oracle.com/carolmcdonald/entry/jpa_2_0_concurrency_and
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    Message dbMsg = q.getSingleResult();

    if (dbMsg != null) {
        // change message's state to PROCESSING
        msg.setState(MsgStateEnum.PROCESSING);
        Date currDate = new Date();
        msg.setStartProcessTimestamp(currDate);
        msg.setLastUpdateTimestamp(currDate);

        update(msg);//from ww w.  j  a  v  a  2  s  .  co  m
    }

    return true;
}

From source file:com.music.dao.PieceDao.java

public List<Piece> getFeedEntryPiecesInRange(DateTime start, DateTime end) {
    TypedQuery<Piece> query = getEntityManager().createQuery(
            "SELECT e.piece FROM FeedEntry e WHERE e.inclusionTime > :start AND e.inclusionTime < :end ORDER BY e.piece.generationTime DESC",
            Piece.class);
    query.setParameter("start", start);
    query.setParameter("end", end);

    return query.getResultList();
}

From source file:io.hops.hopsworks.common.dao.certificates.CertsFacade.java

public ProjectGenericUserCerts findProjectGenericUserCerts(String projectGenericUsername) {
    TypedQuery<ProjectGenericUserCerts> query = em.createNamedQuery(
            "ProjectGenericUserCerts.findByProjectGenericUsername", ProjectGenericUserCerts.class);
    query.setParameter("projectGenericUsername", projectGenericUsername);

    try {/*  www .ja v  a 2s  .  c o  m*/
        return query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public List<Vacancy> getBidsForEmployee(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Vacancy> list = new LinkedList<>();
    try {/*w  ww.j ava 2  s . c o m*/
        em.getTransaction().begin();

        TypedQuery<Vacancy> query = em.createNamedQuery("OfferBid.findVacancyBidsForEmployee", Vacancy.class);
        query.setParameter("employee", employee);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public List<Vacancy> getOffersForEmployee(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Vacancy> list = new LinkedList<>();
    try {/*from   w  w  w.  ja va 2s .c o  m*/
        em.getTransaction().begin();

        TypedQuery<Vacancy> query = em.createNamedQuery("OfferBid.findVacancyOffersForEmployee", Vacancy.class);
        query.setParameter("employee", employee);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public List<Employee> getBidsForVacancy(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Employee> list = new LinkedList<>();
    try {/*  ww  w  . j  ava 2s  .c o m*/
        em.getTransaction().begin();

        TypedQuery<Employee> query = em.createNamedQuery("OfferBid.findEmployeeBidsForVacancy", Employee.class);
        query.setParameter("vacancy", vacancy);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}