List of usage examples for javax.persistence EntityManager createNamedQuery
public Query createNamedQuery(String name);
Query
for executing a named query (in the Java Persistence query language or in native SQL). From source file:org.eclipse.smila.recordstorage.impl.RecordStorageImpl.java
/** * {@inheritDoc}// w w w. j av a2s . c om * * @see org.eclipse.smila.recordstorage.RecordStorage#findRecordsBySource(java.lang.String) */ @Override public Iterator<Record> loadRecords(final String source) throws RecordStorageException { if (source == null) { throw new RecordStorageException("parameter source is null"); } if (source.trim().length() == 0) { throw new RecordStorageException("parameter source is an empty String"); } _lock.readLock().lock(); try { final EntityManager em = createEntityManager(); try { final Query query = em.createNamedQuery(RecordDao.NAMED_QUERY_FIND_BY_SOURCE); final List<RecordDao> daos = query.setParameter(RecordDao.NAMED_QUERY_PARAM_SOURCE, source) .getResultList(); if (daos.isEmpty() && _log.isInfoEnabled()) { _log.info("loadRecords could not find any records for source: " + source); } return new RecordIterator(daos.iterator()); } catch (final Exception e) { throw new RecordStorageException(e, "error executing loadRecords with source: " + source); } finally { closeEntityManager(em); } } finally { _lock.readLock().unlock(); } }
From source file:it.infn.ct.futuregateway.apiserver.v1.ApplicationService.java
/** * Removes the application. Delete the application only if there are not * tasks associated with it because tasks must be associated with an * application.//from w w w.j ava 2 s.c om * <p> * Applications with associated tasks can only be disabled to avoid future * execution of new tasks. Nevertheless, a task can be associated with a * disabled application and in this case will stay waiting until the * application is enabled. * * @param id Id of the application to remove */ @DELETE public final void deleteApp(@PathParam("id") final String id) { Application app; EntityManager em = getEntityManager(); try { app = em.find(Application.class, id); if (app == null) { throw new NotFoundException(); } EntityTransaction et = em.getTransaction(); try { et.begin(); List<Object[]> taskForApp = em.createNamedQuery("tasks.forApplication").setParameter("appId", id) .setMaxResults(1).getResultList(); if (taskForApp == null || taskForApp.isEmpty()) { em.remove(app); } else { log.info("Application " + id + " has tasks and cannot be" + " deleted"); throw new WebApplicationException( "The application cannot " + "be removed because there are associated tasks", Response.Status.CONFLICT); } et.commit(); } catch (WebApplicationException wex) { throw wex; } catch (RuntimeException re) { log.error(re); log.error("Impossible to remove the application"); throw new InternalServerErrorException("Error to remove " + "the application " + id); } finally { if (et != null && et.isActive()) { et.rollback(); } } } catch (IllegalArgumentException re) { log.error("Impossible to retrieve the application list"); log.error(re); throw new BadRequestException("Application '" + id + "' " + "does not exist!"); } finally { em.close(); } }
From source file:it.infn.ct.futuregateway.apiserver.v1.InfrastructureService.java
/** * Removes the infrastructure. Delete the infrastructure only if there are * not applications associated with it to avoid inconsistency in the DB. * <p>//from w w w . j a v a 2 s .c o m * Infrastructures with associated applications can only be disabled to * avoid future execution of applications. * * @param id Id of the infrastructure to remove */ @DELETE public final void deleteInfra(@PathParam("id") final String id) { Infrastructure infra; EntityManager em = getEntityManager(); try { infra = em.find(Infrastructure.class, id); if (infra == null) { throw new NotFoundException(); } EntityTransaction et = em.getTransaction(); try { et.begin(); List<Object[]> appsForInfra = em.createNamedQuery("applications.forInfrastructure") .setParameter("infraId", id).setMaxResults(1).getResultList(); if (appsForInfra == null || appsForInfra.isEmpty()) { em.remove(infra); } else { throw new WebApplicationException("The infrastructure " + "cannot be removed because there are associated " + "applications", Response.Status.CONFLICT); } et.commit(); } catch (WebApplicationException wex) { throw wex; } catch (RuntimeException re) { log.error(re); log.error("Impossible to remove the infrastructure"); throw new InternalServerErrorException("Errore to remove " + "the infrastructure " + id); } finally { if (et != null && et.isActive()) { et.rollback(); } } } catch (IllegalArgumentException re) { log.error("Impossible to retrieve the infrastructure list"); log.error(re); throw new BadRequestException("Task '" + id + "' does not exist!"); } finally { em.close(); } }
From source file:in.bookmylab.jpa.JpaDAO.java
/** * @return/*from w w w . ja v a 2 s. c om*/ */ public List<ResourceType> getResourceTypes() { EntityManager em = emf.createEntityManager(); try { Query q = em.createNamedQuery("ResourceType.findAllActive"); return q.getResultList(); } finally { em.close(); } }
From source file:in.bookmylab.jpa.JpaDAO.java
/** * @return//from w w w . j a v a 2 s.c o m */ public List<ResourcePricing> getResourcePrices() { EntityManager em = emf.createEntityManager(); try { Query q = em.createNamedQuery("ResourcePricing.findAllCurrent"); return q.getResultList(); } finally { em.close(); } }
From source file:in.bookmylab.jpa.JpaDAO.java
public boolean authenticate(String email, String hashedPassword) { Long c;//from w ww .j av a 2 s . c om 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:in.bookmylab.jpa.JpaDAO.java
/** * @param parseInt/*w ww . j a v a2 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:in.bookmylab.jpa.JpaDAO.java
public List<ResourceBooking> getSlotsByDate(Date dt) { List<ResourceBooking> list = null; EntityManager em = emf.createEntityManager(); try {/*from w ww . j av a 2 s . co m*/ Query q = em.createNamedQuery("ResourceBooking.findSlotsByDate"); list = q.setParameter("bookingDate", dt).getResultList(); } finally { em.clear(); em.close(); } return list; }
From source file:org.apache.james.user.jpa.JPAUsersRepository.java
/** * Removes a user from the repository//from www .j av a 2 s .c o m * * @param name * the user to remove from the repository * @throws UsersRepositoryException */ public void removeUser(String name) throws UsersRepositoryException { EntityManager entityManager = entityManagerFactory.createEntityManager(); final EntityTransaction transaction = entityManager.getTransaction(); try { transaction.begin(); if (entityManager.createNamedQuery("deleteUserByName").setParameter("name", name).executeUpdate() < 1) { transaction.commit(); throw new UsersRepositoryException("User " + name + " does not exist"); } else { transaction.commit(); } } catch (PersistenceException e) { getLogger().debug("Failed to remove user", e); if (transaction.isActive()) { transaction.rollback(); } throw new UsersRepositoryException("Failed to remove user " + name, e); } finally { entityManager.close(); } }
From source file:in.bookmylab.jpa.JpaDAO.java
public User getUserByLogin(String email) { User u = null;/*from w ww. j a v a 2s . c om*/ EntityManager em = emf.createEntityManager(); try { Query q = em.createNamedQuery("User.findByEmail"); List<User> l = q.setParameter("email", email).getResultList(); if (l.size() == 1) { u = l.get(0); } } finally { em.clear(); em.close(); } return u; }