List of usage examples for javax.persistence Query getSingleResult
Object getSingleResult();
From source file:me.doshou.admin.monitor.web.controller.SQLExecutorController.java
@PageableDefaults(pageNumber = 0, value = 10) @RequestMapping(value = "/sql", method = RequestMethod.POST) public String executeQL(final @RequestParam("sql") String sql, final Model model, final Pageable pageable) { model.addAttribute("sessionFactory", HibernateUtils.getSessionFactory(em)); String lowerCaseSQL = sql.trim().toLowerCase(); final boolean isDML = lowerCaseSQL.startsWith("insert") || lowerCaseSQL.startsWith("update") || lowerCaseSQL.startsWith("delete"); final boolean isDQL = lowerCaseSQL.startsWith("select"); if (!isDML && !isDQL) { model.addAttribute(Constants.ERROR, "SQL????insert?update?delete?select"); return showSQLForm(); }/*from www . j a v a 2 s . c o m*/ try { new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { if (isDML) { Query query = em.createNativeQuery(sql); int updateCount = query.executeUpdate(); model.addAttribute("updateCount", updateCount); } else { String findSQL = sql; String countSQL = "select count(*) count from (" + findSQL + ") o"; Query countQuery = em.createNativeQuery(countSQL); Query findQuery = em.createNativeQuery(findSQL); findQuery.setFirstResult(pageable.getOffset()); findQuery.setMaxResults(pageable.getPageSize()); Page page = new PageImpl(findQuery.getResultList(), pageable, ((BigInteger) countQuery.getSingleResult()).longValue()); model.addAttribute("resultPage", page); em.unwrap(Session.class).doWork(new Work() { @Override public void execute(final Connection connection) throws SQLException { PreparedStatement psst = connection.prepareStatement(sql); ResultSetMetaData metaData = psst.getMetaData(); List<String> columnNames = Lists.newArrayList(); for (int i = 1, l = metaData.getColumnCount(); i <= l; i++) { columnNames.add(metaData.getColumnLabel(i)); } psst.close(); model.addAttribute("columnNames", columnNames); } }); } return null; } }); } catch (Exception e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); model.addAttribute(Constants.ERROR, sw.toString()); } return showSQLForm(); }
From source file:com.telefonica.euro_iaas.paasmanager.dao.impl.NetworkDaoJpaImpl.java
private Network findNetworkWithSubNet(String name, String vdc, String region) throws EntityNotFoundException { Query query = getEntityManager().createQuery("select p from Network p left join " + " fetch p.subNets where p.name = :name and p.vdc = :vdc and p.region =:region"); query.setParameter("name", name); query.setParameter("region", region); if (vdc == null) { query.setParameter("vdc", ""); } else {/*from w ww. j av a2 s.c o m*/ query.setParameter("vdc", vdc); } Network network = null; try { network = (Network) query.getSingleResult(); } catch (NoResultException e) { String message = " No network found in the database with id: " + name + " and vdc " + vdc + " region " + region + " Exception: " + e.getMessage(); throw new EntityNotFoundException(Network.class, "name", name); } return network; }
From source file:org.kuali.mobility.push.dao.DeviceDaoImpl.java
@SuppressWarnings("unchecked") public Long countDevices() { Query query = getEntityManager().createNamedQuery("Device.countDevices"); return (Long) query.getSingleResult(); }
From source file:edu.vt.middleware.gator.JpaConfigManager.java
/** {@inheritDoc}. */ @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public ProjectConfig findProject(final String name) { final EntityManager em = getEntityManager(); final String queryString = "SELECT p FROM ProjectConfig p WHERE p.name = :name"; final Query query = em.createQuery(queryString); query.setParameter("name", name); logger.trace("Executing query " + queryString); logger.trace("Query params: name=" + name); try {//w w w .j ava 2 s . c om final ProjectConfig project = (ProjectConfig) query.getSingleResult(); loadFullProject(project); return project; } catch (NoResultException e) { return null; } }
From source file:org.apache.cxf.fediz.service.idp.service.jpa.IdpDAOJPAImpl.java
@Override public void updateIdp(String realm, Idp idp) { Query query = null; query = em.createQuery("select i from IDP i where i.realm=:realm"); query.setParameter("realm", realm); //@SuppressWarnings("rawtypes") IdpEntity idpEntity = (IdpEntity) query.getSingleResult(); domain2entity(idp, idpEntity);/*w w w . j a v a2s.c o m*/ em.persist(idpEntity); LOG.debug("IDP '{}' updated", idp.getRealm()); }
From source file:in.bookmylab.jpa.JpaDAO.java
/** * @param parseInt/* w w w .j a v a 2s .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:org.energyos.espi.common.repositories.jpa.ResourceRepositoryImpl.java
@Override public <T extends IdentifiedObject> Long findIdByXPath(Long id1, Class<T> clazz) { try {/*w w w .ja v a2 s .c om*/ String findIdByXPath = (String) clazz.getDeclaredField("QUERY_FIND_ID_BY_XPATH").get(String.class); Query query = em.createNamedQuery(findIdByXPath).setParameter("o1Id", id1); return (Long) query.getSingleResult(); } catch (NoSuchFieldException | IllegalAccessException e) { System.out.printf("**** findIdByXPath(Long id1) Exception: %s - %s\n", clazz.toString(), e.toString()); throw new RuntimeException(e); } }
From source file:org.energyos.espi.common.repositories.jpa.ResourceRepositoryImpl.java
@SuppressWarnings("unchecked") @Override/* w w w . j a v a 2 s . c om*/ public <T extends IdentifiedObject> T findByResourceUri(String uri, Class<T> clazz) { try { String findByResourceURI = (String) clazz.getDeclaredField("QUERY_FIND_BY_RESOURCE_URI") .get(String.class); Query query = em.createNamedQuery(findByResourceURI).setParameter("uri", uri); return (T) query.getSingleResult(); } catch (NoSuchFieldException | IllegalAccessException e) { System.out.printf("**** findByResourceUri(String uri) Exception: %s - %s\n", clazz.toString(), e.toString()); throw new RuntimeException(e); } }
From source file:es.ucm.fdi.dalgs.learningGoal.repository.LearningGoalRepository.java
public LearningGoal getLearningGoal(Long id_learningGoal, Long id_competence, Long id_degree) { Degree degree = em.getReference(Degree.class, id_degree); Competence competence = em.getReference(Competence.class, id_competence); Query query = em.createQuery( "Select l from LearningGoal l where l.id=?1 and l.competence=?2 and l.competence.degree=?3"); query.setParameter(1, id_learningGoal); query.setParameter(2, competence);/* w w w . ja v a 2 s . c o m*/ query.setParameter(3, degree); if (query.getResultList().isEmpty()) return null; else return (LearningGoal) query.getSingleResult(); }
From source file:org.kuali.mobility.push.dao.DeviceDaoImpl.java
@SuppressWarnings("unchecked") public Long countDevicesWithoutUsername() { Query query = getEntityManager().createNamedQuery("Device.countDevicesWithoutUsername"); return (Long) query.getSingleResult(); }