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.EmployeeRepositoryImplementation.java

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

        TypedQuery<Employee> query = em.createNamedQuery("Employee.findAllFreeEmployees", 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;
}

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

@Override
public List<Vacancy> getAll() {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Vacancy> list = new LinkedList<>();
    try {/* ww  w  .  j  av a 2 s . co  m*/
        em.getTransaction().begin();

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

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

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

        TypedQuery<UserSkill> query = em.createNamedQuery("UserSkill.findByEmployee", UserSkill.class);
        query.setParameter("employee", employee);
        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;
}

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

/**
 * Retrieves the unique AgencyAttribute rule for the given Agency and
 * Attribute./*ww  w . ja  va2  s  .  c o  m*/
 *
 * @param agency
 *            The Agency the Attribute applies to
 * @param attribute
 *            The Attribute of the Agency
 * @return the unique AgencyAttribute rule, or <code>null</code> if no such
 *         rule exists
 */
public AgencyAttribute getAttribute(EntityManager em, Attribute attribute) {
    return em.createNamedQuery(GET_ATTRIBUTE, AgencyAttribute.class).setParameter("agency", this)
            .setParameter("attribute", attribute).getSingleResult();
}

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

@Override
public List<VacancySkill> getSkills(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<VacancySkill> list = new LinkedList<>();
    try {//from   w w  w . jav a 2  s .c  om
        em.getTransaction().begin();

        TypedQuery<VacancySkill> query = em.createNamedQuery("VacancySkill.findByVacancy", VacancySkill.class);
        query.setParameter("vacancy", vacancy);
        list = query.getResultList();

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

    return list;
}

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

private Optional<JPASieveScript> findActiveSieveScript(User user, EntityManager entityManager)
        throws StorageException {
    try {//from w ww  . java  2  s .c o  m
        JPASieveScript activeSieveScript = entityManager
                .createNamedQuery("findActiveByUsername", JPASieveScript.class)
                .setParameter("username", user.asString()).getSingleResult();
        return Optional.ofNullable(activeSieveScript);
    } 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.OfferBidRepositoryImplementation.java

@Override
public OfferBid getOfferByEmployeeAndVacancy(Employee employee, Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    OfferBid offer = null;//from  w  ww . j  a v a  2  s. c  o m
    try {
        em.getTransaction().begin();

        TypedQuery<OfferBid> query = em.createNamedQuery("OfferBid.findOfferByEmployeeAndVacancy",
                OfferBid.class);
        query.setParameter("employee", employee);
        query.setParameter("vacancy", vacancy);
        offer = query.getSingleResult();

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

    return offer;
}

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

@Override
public OfferBid getBidByEmployeeAndVacancy(Employee employee, Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    OfferBid offer = null;//from  w ww.j  a  v a 2 s  . co m
    try {
        em.getTransaction().begin();

        TypedQuery<OfferBid> query = em.createNamedQuery("OfferBid.findBidByEmployeeAndVacancy",
                OfferBid.class);
        query.setParameter("employee", employee);
        query.setParameter("vacancy", vacancy);
        offer = query.getSingleResult();

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

    return offer;
}

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 {/*  w w  w  .  j a  va2s  .  co  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;
}