List of usage examples for org.hibernate Query setParameter
@SuppressWarnings("unchecked") Query<R> setParameter(int position, Object val);
From source file:br.pro.x87.dao.MaquinaDao.java
public void deleteMaquina(int id) { Session session = null;/*from ww w.j a v a2 s .c o m*/ try { session = HibernateConnector.getInstance().getSession(); Transaction beginTransaction = session.beginTransaction(); Query createQuery = session.createQuery("delete from Maquina s where s.id =:id"); createQuery.setParameter("id", id); createQuery.executeUpdate(); beginTransaction.commit(); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } }
From source file:br.sp.telesul.dao.FuncionarioDAOImpl.java
@Override public FilterReturn searchPage(int pageNumber, int pageSize, String filter) { String countQ = "Select count (f.id) from Funcionario f"; Session session = this.sessionFactory.getCurrentSession(); Query countQuery = session.createQuery(countQ); Long countResults = (Long) countQuery.uniqueResult(); int lastPageNumber = (int) ((countResults / pageSize) + 1); FilterReturn filterReturn = new FilterReturn(); filterReturn.setTotalEntities(countResults); Query selectQuery = session.createQuery("From Funcionario f Where nome LIKE :filter"); selectQuery.setParameter("filter", "%" + filter + "%"); selectQuery.setFirstResult((pageNumber - 1) * pageSize); selectQuery.setMaxResults(pageSize); List<Funcionario> funcionarios = selectQuery.list(); filterReturn.setEmployees(funcionarios); return filterReturn; }
From source file:br.ufc.ivela.commons.dao.GenericDaoImpl.java
License:Open Source License
public List paginatedFind(final String query, final Object[] params, final int pageSize, final int page) { HibernateCallback callback = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query q = session.createQuery(query).setFirstResult((page * pageSize) - pageSize) .setMaxResults(pageSize + 1); if (params != null) { for (int i = 0; i < params.length; i++) { Object object = params[i]; q.setParameter(i, object); }/*from w ww .j a v a2s . c om*/ } return q.list(); } }; return (List) getHibernateTemplate().execute(callback); }
From source file:br.ufc.ivela.commons.dao.GenericDaoImpl.java
License:Open Source License
public int getCount(final String query, final Object[] countParams) { HibernateCallback callback = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query q = session.createQuery(query); if (countParams != null) { for (int i = 0; i < countParams.length; i++) { Object object = countParams[i]; q.setParameter(i, object); }/*from www . j a va 2 s .co m*/ } return q.uniqueResult(); } }; return ((Long) getHibernateTemplate().execute(callback)).intValue(); }
From source file:br.ufg.reqweb.dao.ReportDao.java
@Transactional(readOnly = true) public List<Map<String, ?>> listTotalRequerimento(PerfilEnum perfil, Curso curso, List<RequerimentoStatusEnum> statusList, List<TipoRequerimentoEnum> tipoRequerimentoList) { List<String> tipoRequerimentoNames = new ArrayList<>(); List<String> statusNames = new ArrayList<>(); for (TipoRequerimentoEnum tipoRequerimento : tipoRequerimentoList) { tipoRequerimentoNames.add(tipoRequerimento.name()); }//from w ww .ja v a2 s. c om for (RequerimentoStatusEnum status : statusList) { statusNames.add(status.name()); } Query query; if (perfil == null) { query = this.sessionFactory.getCurrentSession() .createSQLQuery("select c.nome as curso, r.tiporequerimento as requerimento,\n" + "r.status as status, count(r.tiporequerimento) as total\n" + "from requerimento r\n" + "join usuario u on u.id=r.usuario_id\n" + "join perfil p on p.usuario_id=u.id\n" + "join curso c on c.id=p.curso_id\n" + "group by c.id, c.nome, r.tiporequerimento, r.status\n" + "having r.status in :status\n" + "and r.tiporequerimento in :tipoRequerimento\n" + (curso == null ? "" : "and c.id = :cursoId\n") + "order by c.nome asc, r.tiporequerimento asc"); if (curso != null) { query.setLong("cursoId", curso.getId()); } } else { query = this.sessionFactory.getCurrentSession() .createSQLQuery("select coord.email, cdis.nome as curso,\n" + "r.tiporequerimento as requerimento,\n" + "r.status as status,\n" + "count(r.tiporequerimento) as total\n" + "from requerimento r\n" + "join usuario dis on dis.id=r.usuario_id\n" + "join perfil pdis on pdis.usuario_id=dis.id\n" + "join curso cdis on cdis.id=pdis.curso_id\n" + "join perfil pcoord on pcoord.curso_id=cdis.id\n" + "and pcoord.tipoperfil = :perfil\n" + "join usuario coord on pcoord.usuario_id=coord.id\n" + "group by coord.email, cdis.nome, r.tiporequerimento, r.status\n" + "having r.status in :status\n" + "and r.tiporequerimento in :tipoRequerimento\n" + "order by cdis.nome asc, r.tiporequerimento asc;\n"); query.setParameter("perfil", perfil.name()); } query.setParameterList("status", statusNames); query.setParameterList("tipoRequerimento", tipoRequerimentoNames); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); return query.list(); }
From source file:br.una.zisc.consultas.Consultas.java
@SuppressWarnings("unchecked") public Usuario buscaUsuario(String email) { Session s = HibernateUtil.getSessionFactory().getCurrentSession(); s.beginTransaction();//from www .ja v a2 s. c o m Query q = s.createQuery("from Usuario u where u.email = :email"); q.setParameter("email", email); List<Usuario> lista = (List<Usuario>) q.list(); System.err.println("Acessando banco!"); try { Usuario usuario = lista.get(0); s.getTransaction().commit(); System.err.println("Commit!"); return usuario; } catch (IndexOutOfBoundsException e) { s.getTransaction().commit(); Usuario u = null; return u; } }
From source file:br.una.zisc.consultas.Consultas.java
@SuppressWarnings("unchecked") public Usuario requisicaoLogin(String email, String senha) throws UnsupportedEncodingException { Usuario user = new Usuario(); Session s = HibernateUtil.getSessionFactory().getCurrentSession(); s.beginTransaction();/*from ww w. j a v a 2 s . co m*/ Query q = s.createQuery("from Seguranca seg where seg.usuario.email = :email"); // Query q = s.createSQLQuery("SELECT U.nome AS [Nome], U.email AS [Email] , S.\"hash\" AS [Password]\n" // + "FROM SEGURANCA AS S INNER JOIN USUARIO AS U ON S.idusuario = U.idusuario\n" // + " WHERE U.email like :email"); q.setParameter("email", email); List<Seguranca> lista = (List<Seguranca>) q.list(); if (lista.get(0).getHash().equals(senha)) { s.getTransaction().commit(); return buscaUsuario(email); } s.getTransaction().commit(); return user; }
From source file:br.unisal.dao.ConsultaDao.java
public Consulta getById(Consulta c) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); Consulta consulta = new Consulta(); try {/* w ww .j a v a2 s. c o m*/ tx.begin(); Query query = session.createQuery("FROM Consulta WHERE idConsulta = :id"); query.setParameter("id", c.getIdConsulta()); consulta = (Consulta) query.uniqueResult(); tx.commit(); } catch (HibernateException e) { System.out.println("Exception ConsultaDao.getById(): " + e.getMessage()); tx.rollback(); } finally { session.close(); } return consulta; }
From source file:br.unisal.dao.EquipamentoDao.java
@Override public Equipamento getById(Integer id) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); Equipamento equipamento = new Equipamento(); try {/* w w w .j a va2 s . c om*/ tx.begin(); Query query = session.createQuery("FROM Equipamento WHERE id_equipamento = :id"); query.setParameter("id", id); equipamento = (Equipamento) query.uniqueResult(); tx.commit(); } catch (HibernateException e) { System.out.println("Exception EquipamentoDao.getById(): " + e.getMessage()); tx.rollback(); } finally { session.close(); } return equipamento; }
From source file:br.unisal.dao.MedicoDao.java
public Medico getById(Medico m) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); Medico medico = new Medico(); try {//from w w w . jav a 2s . c om tx.begin(); Query query = session.createQuery("FROM Medico WHERE idMedico = :id"); query.setParameter("id", m.getIdMedico()); medico = (Medico) query.uniqueResult(); tx.commit(); } catch (HibernateException e) { System.out.println("Exception MedicoDao.getById(): " + e.getMessage()); tx.rollback(); } finally { session.close(); } return medico; }