List of usage examples for javax.persistence EntityManager close
public void close();
From source file:in.bookmylab.jpa.JpaDAO.java
public boolean authenticate(String email, String hashedPassword) { Long c;// w ww .j a v a 2 s . co m EntityManager em = emf.createEntityManager(); try { Query q = em.createNamedQuery("User.authenticate"); c = (Long) q.setParameter("email", email).setParameter("password", hashedPassword).getSingleResult(); } finally { em.close(); } return c == 1; }
From source file:br.nom.abdon.gastoso.rest.server.MaisRs.java
@GET @Path("/saldos/{id}") public Response saldo(final @Context Request request, final @Context HttpHeaders httpHeaders, final @PathParam("id") Integer contaId, final @QueryParam("dia") LocalDate dia) { final EntityManager entityManager = emf.createEntityManager(); Response response;//ww w . j av a2 s . c om if (contaId == null || dia == null) throw new WebApplicationException(Response.Status.BAD_REQUEST); try { final Conta conta = new Conta(contaId); final Saldo saldo = aggregateDao.findSaldo(entityManager, conta, dia); response = buildResponse(request, httpHeaders, saldo); } catch (DalException ex) { response = dealWith(ex); } finally { entityManager.close(); } return response; }
From source file:in.bookmylab.jpa.JpaDAO.java
/** * @param parseInt/*from ww w . jav a 2 s . c om*/ * @return */ public ResourceBooking getBookingById(int bookingId) { EntityManager em = emf.createEntityManager(); try { Query q = em.createNamedQuery("ResourceBooking.findById"); q.setParameter("bookingId", bookingId); return (ResourceBooking) q.getSingleResult(); } finally { em.close(); } }
From source file:com.epam.training.taranovski.web.project.repository.implementation.UserRepositoryImplementation.java
@Override public boolean create(User user) { EntityManager em = entityManagerFactory.createEntityManager(); boolean success = false; try {/*from w w w .j av a 2 s . c o m*/ em.getTransaction().begin(); em.persist(user); em.getTransaction().commit(); success = true; } catch (RuntimeException e) { Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e); success = false; } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return success; }
From source file:edu.csueb.cs6320.utils.UserService.java
/** * doesnt set password!/* w w w. j a v a 2 s .c o m*/ * @param alteredUser */ public boolean updateUser(long userid, User alteredUser) { EntityManager em = Persistence.createEntityManagerFactory("TestPU").createEntityManager(); User u = em.find(User.class, userid); if (u == null) { return false; } em.getTransaction().begin(); u.setEmail(alteredUser.getEmail()); u.setFirstName(alteredUser.getFirstName()); u.setLastName(alteredUser.getLastName()); u.setRole(alteredUser.getRole()); em.getTransaction().commit(); em.close(); return true; }
From source file:in.bookmylab.jpa.JpaDAO.java
/** * @param spmId // ww w . j a v a 2s .com * @return */ public SpmLabBooking getSpmBooking(Long spmId) { EntityManager em = emf.createEntityManager(); SpmLabBooking spm = null; try { Query q = em.createNamedQuery("SpmLabBooking.findById"); spm = (SpmLabBooking) q.setParameter("spmId", spmId).getSingleResult(); } finally { em.close(); } return spm; }
From source file:in.bookmylab.jpa.JpaDAO.java
/** * @param xrdId //w w w. ja v a 2 s. c om * @return */ public XrdLabBooking getXrdBooking(Long xrdId) { EntityManager em = emf.createEntityManager(); XrdLabBooking xrd = null; try { Query q = em.createNamedQuery("XrdLabBooking.findById"); xrd = (XrdLabBooking) q.setParameter("xrdId", xrdId).getSingleResult(); } finally { em.close(); } return xrd; }
From source file:com.epam.training.taranovski.web.project.repository.implementation.CheckDocumentRepositoryImplementation.java
@Override public boolean create(CheckDocument something) { EntityManager em = entityManagerFactory.createEntityManager(); boolean success = false; try {/*from ww w . j ava 2 s . c o m*/ em.getTransaction().begin(); em.persist(something); em.getTransaction().commit(); success = true; } catch (RuntimeException e) { Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e); success = false; } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return success; }
From source file:it.infn.ct.futuregateway.apiserver.v1.ApplicationService.java
/** * Retrieves the application details./*from w w w . ja v a 2s. c o m*/ * Application details include all the fields an application * consist of as described in the documentation. This include all the * information included in the task collection and many others. * * @param id The application id. This is a path parameter retrieved from * the URL * @return The application */ @GET @Produces(Constants.INDIGOMIMETYPE) public final Application getAppDetails(@PathParam("id") final String id) { Application app; EntityManager em = getEntityManager(); try { app = em.find(Application.class, id); log.debug("Find the application " + id + " associated with " + app.getInfrastructureIds().size() + " infrastructures " + "and " + app.getParameters().size() + " parameters"); } catch (IllegalArgumentException re) { log.error("Impossible to retrieve the application"); log.error(re); throw new RuntimeException("Impossible to access the application " + "list"); } finally { em.close(); } if (app == null) { throw new NotFoundException(); } else { return app; } }
From source file:cz.fi.muni.pa165.dto.BookDAOTest.java
@Test public void testInsert() { EntityManager em = emf.createEntityManager(); BookDAOImpl bdao = new BookDAOImpl(); bdao.setManager(em);/*from w w w. j av a 2 s . co m*/ Book book = new Book(); book.setAuthors("Author"); book.setDepartment(Department.Sport); book.setDescription("Description"); book.setISBN("123456456"); book.setName("Name"); em.getTransaction().begin(); bdao.insert(book); em.getTransaction().commit(); List<Book> books = em.createQuery("SELECT b FROM Book b", Book.class).getResultList(); em.close(); assertEquals(books.size(), 2); }