List of usage examples for javax.persistence EntityManager createQuery
public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass);
TypedQuery
for executing a Java Persistence query language statement. From source file:org.traccar.web.server.model.DataServiceImpl.java
@Override public User login(String login, String password) { EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { TypedQuery<User> query = entityManager.createQuery("SELECT x FROM User x WHERE x.login = :login", User.class); query.setParameter("login", login); List<User> results = query.getResultList(); if (!results.isEmpty() && password.equals(results.get(0).getPassword())) { User user = results.get(0);// w ww. j a v a 2 s . c om setSessionUser(user); return user; } throw new IllegalStateException(); } }
From source file:org.traccar.web.server.model.DataServiceImpl.java
private ApplicationSettings getApplicationSettings() { if (applicationSettings == null) { EntityManager entityManager = getServletEntityManager(); synchronized (entityManager) { TypedQuery<ApplicationSettings> query = entityManager .createQuery("SELECT x FROM ApplicationSettings x", ApplicationSettings.class); List<ApplicationSettings> resultList = query.getResultList(); if (resultList == null || resultList.isEmpty()) { applicationSettings = new ApplicationSettings(); entityManager.getTransaction().begin(); try { entityManager.persist(applicationSettings); entityManager.getTransaction().commit(); } catch (RuntimeException e) { entityManager.getTransaction().rollback(); throw e; }/* ww w .j a v a2s. co m*/ } else { applicationSettings = resultList.get(0); } } } return applicationSettings; }
From source file:org.traccar.web.server.model.DataServiceImpl.java
@Override public List<User> getUsers() { EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { List<User> users = new LinkedList<User>(); users.addAll(entityManager.createQuery("SELECT x FROM User x", User.class).getResultList()); return users; }//from ww w . j a v a 2 s . c o m }
From source file:org.traccar.web.server.model.DataServiceImpl.java
@Override public List<Position> getPositions(Device device, Date from, Date to) { EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { List<Position> positions = new LinkedList<Position>(); TypedQuery<Position> query = entityManager.createQuery( "SELECT x FROM Position x WHERE x.device = :device AND x.time BETWEEN :from AND :to", Position.class); query.setParameter("device", device); query.setParameter("from", from); query.setParameter("to", to); positions.addAll(query.getResultList()); return positions; }/*from w ww . j ava 2 s . c om*/ }
From source file:org.traccar.web.server.model.DataServiceImpl.java
@Override public List<Position> getLatestPositions() { EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { List<Position> positions = new LinkedList<Position>(); User user = getSessionUser();// w ww .ja v a 2 s . c o m if (user.getDevices() != null && !user.getDevices().isEmpty()) { TypedQuery<Position> query = entityManager.createQuery("SELECT x FROM Position x WHERE x.id IN (" + "SELECT y.latestPosition FROM Device y WHERE y IN (:devices))", Position.class); query.setParameter("devices", user.getDevices()); positions.addAll(query.getResultList()); } return positions; } }
From source file:info.dolezel.jarss.rest.v1.FeedsService.java
@POST @Consumes(MediaType.APPLICATION_JSON)/*from ww w .j a va2 s .c o m*/ public Response subscribeFeed(@Context SecurityContext context, FeedSubscriptionData data) { FeedCategory fc = null; EntityManager em; EntityTransaction tx; User user; FeedData feedData; Feed f; boolean createdNewFD = false; if (data.getUrl() == null) { return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorDescription("Feed URL missing")) .build(); } user = (User) context.getUserPrincipal(); em = HibernateUtil.getEntityManager(); tx = em.getTransaction(); tx.begin(); try { if (data.getCategoryId() != 0) { try { fc = (FeedCategory) em .createQuery("select fc from FeedCategory fc where fc.id = :id", FeedCategory.class) .setParameter("id", data.getCategoryId()).getSingleResult(); } catch (NoResultException e) { return Response.status(Response.Status.NOT_FOUND) .entity(new ErrorDescription("Feed category not found")).build(); } if (!fc.getUser().equals(user)) { return Response.status(Response.Status.FORBIDDEN) .entity(new ErrorDescription("Feed category not owned by user")).build(); } } // Try to look up existing FeedData try { feedData = (FeedData) em.createNamedQuery("FeedData.getByUrl").setParameter("url", data.getUrl()) .getSingleResult(); } catch (NoResultException e) { feedData = new FeedData(); feedData.setUrl(data.getUrl()); try { loadFeedDetails(feedData); } catch (Exception ex) { e.printStackTrace(); return Response.status(Response.Status.BAD_GATEWAY) .entity(new ErrorDescription("Cannot fetch the feed")).build(); } em.persist(feedData); createdNewFD = true; } f = new Feed(); f.setUser(user); f.setFeedCategory(fc); f.setData(feedData); f.setName(feedData.getTitle()); em.persist(f); tx.commit(); if (createdNewFD) FeedsEngine.getInstance().submitFeedRefresh(feedData); return Response.noContent().build(); } finally { if (tx.isActive()) tx.rollback(); em.close(); } }
From source file:au.edu.uq.cmm.paul.servlet.WebUIController.java
private DatafileMetadata fetchMetadata(File file) { EntityManager entityManager = createEntityManager(); try {//ww w . j ava2s. com TypedQuery<DatafileMetadata> query = entityManager.createQuery( "from DatafileMetadata d where d.capturedFilePathname = :pathName", DatafileMetadata.class); query.setParameter("pathName", file.getAbsolutePath()); return query.getSingleResult(); } catch (NoResultException ex) { return null; } finally { entityManager.close(); } }
From source file:it.attocchi.jpa2.JpaController.java
public <T extends Serializable> List<T> findBy(Class<T> clazz, String query) throws Exception { List<T> res = new ArrayList<T>(); testClazz(clazz);/*from w w w. ja va 2s . c om*/ EntityManager em = getEntityManager(); try { res = em.createQuery(query, clazz).getResultList(); } catch (Exception e) { throw e; } finally { // Close the database connection: if (!globalTransactionOpen) { // if (em.getTransaction().isActive()) // em.getTransaction().rollback(); closeEm(); // em.close(); } } return res; }
From source file:it.attocchi.jpa2.JpaController.java
/** * /*from ww w.j a v a2 s .co m*/ * @param clazz * @param query * a query with Ordinal Parameters (?index) * @param params * @return * @throws Exception */ public <T extends Serializable> List<T> findBy(Class<T> clazz, String query, Object... params) throws Exception { List<T> res = new ArrayList<T>(); testClazz(clazz); EntityManager em = getEntityManager(); try { TypedQuery<T> q = em.createQuery(query, clazz); if (params != null) { int i = 1; for (Object o : params) { q.setParameter(i, o); i++; } } res = q.getResultList(); } catch (Exception e) { throw e; } finally { // Close the database connection: if (!globalTransactionOpen) { // if (em.getTransaction().isActive()) // em.getTransaction().rollback(); closeEm(); // em.close(); } } return res; }
From source file:it.attocchi.jpa2.JpaController.java
public <T extends Serializable> T findFirst(Class<T> clazz, String query, Object... params) throws Exception { T res = null;// w w w .jav a2 s . c o m List<T> list = null; testClazz(clazz); EntityManager em = getEntityManager(); try { TypedQuery<T> q = em.createQuery(query, clazz); if (params != null) { int i = 1; for (Object o : params) { q.setParameter(i, o); i++; } } list = q.getResultList(); if (list != null && list.size() > 0) { res = list.get(0); } } catch (Exception e) { throw e; } finally { // Close the database connection: if (!globalTransactionOpen) { // if (em.getTransaction().isActive()) // em.getTransaction().rollback(); closeEm(); // em.close(); } } return res; }