Example usage for javax.persistence EntityManager createNamedQuery

List of usage examples for javax.persistence EntityManager createNamedQuery

Introduction

In this page you can find the example usage for javax.persistence EntityManager createNamedQuery.

Prototype

public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass);

Source Link

Document

Create an instance of TypedQuery for executing a Java Persistence query language named query.

Usage

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

@Override
public CheckDocument findByVacancy(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();

    CheckDocument checkDocument = null;/*ww w . j  a v  a2s.c  om*/
    try {
        em.getTransaction().begin();

        TypedQuery<CheckDocument> query = em.createNamedQuery("CheckDocument.findByVacancy",
                CheckDocument.class);
        query.setParameter("vacancy", vacancy);
        checkDocument = query.getSingleResult();

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

    return checkDocument;
}

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

@Override
public List<CheckDocument> findAllForEmployer(Employer employer) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<CheckDocument> list = new LinkedList<>();
    try {//from   w  w  w  .  j a v  a2  s .  com
        em.getTransaction().begin();

        TypedQuery<CheckDocument> query = em.createNamedQuery("CheckDocument.findAllForEmployer",
                CheckDocument.class);
        query.setParameter("employer", employer);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.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.UserRepositoryImplementation.java

/**
 *
 * @param name/* w w w.ja  v  a  2  s .  c o m*/
 * @param password
 * @return
 */
@Override
public User getByNameAndPassword(String name, String password) {
    EntityManager em = entityManagerFactory.createEntityManager();
    User user = null;
    boolean exists = false;

    try {
        em.getTransaction().begin();

        TypedQuery<User> query = em.createNamedQuery("User.findByUserLoginAndPassword", User.class);
        query.setParameter("login", name);
        query.setParameter("password", encryptionService.encrypt(password));

        user = query.getSingleResult();

        em.getTransaction().commit();
        exists = true;
    } catch (NoResultException ex) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(ex);
        exists = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    if (exists) {
        return user;
    } else {
        return null;
    }

}

From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java

public FeedSLAAlertBean getFeedAlertInstance(String feedName, String clusterName, Date nominalTime) {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);/*from  ww  w.  j  a  v a2s .c o  m*/
    TypedQuery<FeedSLAAlertBean> q = entityManager
            .createNamedQuery(PersistenceConstants.GET_FEED_ALERT_INSTANCE, FeedSLAAlertBean.class);
    q.setParameter("feedName", feedName);
    q.setParameter("clusterName", clusterName);
    q.setParameter("nominalTime", nominalTime);
    try {
        return q.getSingleResult();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java

public PendingInstanceBean getPendingInstance(String feedName, String clusterName, Date nominalTime) {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);/*from  w w w  .java  2s .  c  om*/
    TypedQuery<PendingInstanceBean> q = entityManager
            .createNamedQuery(PersistenceConstants.GET_PENDING_INSTANCE, PendingInstanceBean.class);
    q.setParameter("feedName", feedName);

    q.setParameter("clusterName", clusterName);
    q.setParameter("nominalTime", nominalTime);
    try {
        return q.getSingleResult();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

From source file:br.nom.abdon.gastoso.dal.ContasDao.java

@Override
protected void validar(EntityManager em, Conta conta) throws DalException {
    final String nome = conta.getNome();

    if (StringUtils.isBlank(nome)) {
        throw new DalException(ERRO_NOME_VAZIO);
    }/*from  www .  j  a v a 2  s. com*/

    if (nome.length() > Conta.NOME_MAX_LEN) {
        throw new DalException(ERRO_NOME_GRANDE);
    }

    final Boolean nomeEmUso = em.createNamedQuery("Conta.nomeEmUso", Boolean.class).setParameter("nome", nome)
            .getSingleResult();

    if (nomeEmUso) {
        throw new DalException(ERRO_NOME_EM_USO, nome);
    }
}

From source file:org.apache.james.sieve.jpa.JPASieveRepository.java

private Optional<JPASieveQuota> findQuotaForUser(String username, EntityManager entityManager) {
    try {//from  ww w  .j a  v  a2 s.c  o m
        JPASieveQuota sieveQuota = entityManager.createNamedQuery("findByUsername", JPASieveQuota.class)
                .setParameter("username", username).getSingleResult();
        return Optional.of(sieveQuota);
    } catch (NoResultException e) {
        return Optional.empty();
    }
}

From source file:com.chiralbehaviors.CoRE.agency.Agency.java

public List<ProductNetwork> getQualifiedEntityNetworkRules(EntityManager em) {
    return em.createNamedQuery(QUALIFIED_ENTITY_NETWORK_RULES, ProductNetwork.class)
            .setParameter("agency", this).getResultList();
}

From source file:org.apache.james.sieve.jpa.JPASieveRepository.java

private Optional<JPASieveScript> findSieveScript(User user, ScriptName scriptName,
        EntityManager entityManager) {
    try {/*from   ww w .  ja va 2 s  .c  om*/
        JPASieveScript sieveScript = entityManager.createNamedQuery("findSieveScript", JPASieveScript.class)
                .setParameter("username", user.asString()).setParameter("scriptName", scriptName.getValue())
                .getSingleResult();
        return Optional.ofNullable(sieveScript);
    } catch (NoResultException e) {
        LOGGER.debug("Sieve script not found for user {}", user.asString());
        return Optional.empty();
    }
}

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

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

        TypedQuery<Employee> query = em.createNamedQuery("Employee.findAll", Employee.class);
        list = query.getResultList();

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

    return list;
}