Example usage for javax.persistence EntityManager close

List of usage examples for javax.persistence EntityManager close

Introduction

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

Prototype

public void close();

Source Link

Document

Close an application-managed entity manager.

Usage

From source file:com.sdl.odata.datasource.jpa.JPADatasourceProvider.java

private <T> List<T> executeQueryListResult(JPAQuery jpaQuery) {
    EntityManager em = entityManagerFactory.createEntityManager();

    String queryString = jpaQuery.getQueryString();
    Query query = em.createQuery(queryString);
    int nrOfResults = jpaQuery.getLimitCount();
    int startPosition = jpaQuery.getSkipCount();
    Map<String, Object> queryParams = jpaQuery.getQueryParams();

    try {//from  ww  w.  j av a 2s  .c o  m
        if (nrOfResults > 0) {
            query.setMaxResults(nrOfResults);
        }

        if (startPosition > 0) {
            query.setFirstResult(startPosition);
        }

        for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
            query.setParameter(entry.getKey(), tryConvert(entry.getValue()));
        }

        return query.getResultList();
    } finally {
        em.close();
    }
}

From source file:ec.edu.chyc.manejopersonal.controller.PersonaJpaController.java

public List<Firma> findFirmas(List<String> firmasAutores) throws Exception {
    EntityManager em = null;
    try {//from  w ww .  ja  v  a2  s .co m
        em = getEntityManager();
        Query q = em.createQuery(
                "select distinct f from Firma f left join fetch f.personasFirmaCollection where f.nombre in :firmas order by f.nombre asc");
        q.setParameter("firmas", firmasAutores);
        List<Firma> list = q.getResultList();
        return list;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

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

@Override
public boolean delete(OfferBid something) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    OfferBid managedManagedOfferBid = null;
    try {//from   ww  w.j a v a  2  s  . c o  m
        em.getTransaction().begin();
        managedManagedOfferBid = em.merge(something);
        em.remove(managedManagedOfferBid);
        em.getTransaction().commit();

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

    return success;
}

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

/**
 *
 * @param name/*  w  w w . j  a  v  a  2s .  co  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:ec.edu.chyc.manejopersonal.controller.PersonaJpaController.java

public List<Persona> findPersonaConFirma(List<String> firmasAutores) throws Exception {
    EntityManager em = null;
    try {//from   ww  w  .  j  a v  a 2s.  co m
        em = getEntityManager();
        Query q = em.createQuery(
                "select distinct p from Persona p join fetch p.personaFirmasCollection pf where pf.firma.nombre in :firmas order by pf.firma.nombre asc");
        q.setParameter("firmas", firmasAutores);
        List<Persona> list = q.getResultList();
        return list;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

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

@Override
public boolean deleteAllBidsForVacancy(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {/*from  w w w .  java  2 s. co  m*/
        em.getTransaction().begin();
        Query query = em.createNamedQuery("OfferBid.deleteByVacancy");
        query.setParameter("vacancy", vacancy);
        query.executeUpdate();
        em.getTransaction().commit();

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

    return success;
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.test.CommandITCase.java

public void printMetaCats() throws Exception {
    System.out.println("\n\n\n");
    EntityManager em = emf.createEntityManager();
    try {//  w ww . java2 s.c  o m
        EntityTransaction et = em.getTransaction();
        et.begin();
        Query query = em.createQuery(new StringBuilder().append("SELECT x FROM metaCategory x").toString());
        List<NewsMetaCategory> metaList = query.getResultList();
        for (NewsMetaCategory temp : metaList) {
            System.out.println("META: " + temp.getName() + " / " + temp.getLastmodified());
        }
        et.commit();
    } finally {
        em.close();
    }
    System.out.println("\n\n\n");

}

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

@Override
public boolean deleteAllOffersForEmployee(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {//from  ww w .  ja v a  2 s.c  o m
        em.getTransaction().begin();
        Query query = em.createNamedQuery("OfferBid.deleteByEmployee");
        query.setParameter("employee", employee);
        query.executeUpdate();
        em.getTransaction().commit();

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

    return success;
}

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 {//  ww  w. j av a 2s.  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<Employee> getBidsForVacancy(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Employee> list = new LinkedList<>();
    try {/*from   w w w .  j a  v a 2  s  .  co 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;
}