List of usage examples for org.hibernate Query setString
@Deprecated @SuppressWarnings("unchecked") default Query<R> setString(String name, String val)
From source file:com.esteban.cmms.maven.model.Maquinas_Model.java
/** * Actualiza los estados de las mquinas para que sigan o no disponibles en * el funcionamiento del sistema/*from ww w . j a va 2s . co m*/ * @param estado El estado al que se desea actualizar el registro, para este * sistema puede se 'Activo' o 'Inactivo' * @param id el entero (int) correspondiente al id del registro en la tabla * de la base de datos * @param user el usuario que realiza la accin */ public void estadoMaquina(String estado, Integer id, String user) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); Query query = session.createQuery( "UPDATE Maquinas SET " + "Estado = :estado, " + "UserAction = :user " + "WHERE Id = :id"); query.setString("estado", estado); query.setString("user", user); query.setInteger("id", id); query.executeUpdate(); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } }
From source file:com.esteban.cmms.maven.model.Proveedores_Model.java
/** * Actualiza los estados de los proveedores para que sigan o no disponibles en * el funcionamiento del sistema/*from w w w.ja v a2 s. c om*/ * @param estado El estado al que se desea actualizar el registro, para este * sistema puede se 'Activo' o 'Inactivo' * @param id el entero (int) correspondiente al id del registro en la tabla * de la base de datos * @param user el usuario que realiza la accin */ public void estadoProveedor(String estado, Integer id, String user) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); Query query = session.createQuery( "UPDATE Proveedores SET " + "Estado = :estado, " + "UserAction = :user " + "WHERE Id = :id"); query.setString("estado", estado); query.setString("user", user); query.setInteger("id", id); query.executeUpdate(); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } }
From source file:com.esteban.cmms.maven.model.Usuarios_Model.java
/** * Veta los usuarios en el sistema para que no tengan ms acceso al mismo * * @param estado Estado que se le asigna el usuario 'Activo' o 'Vetado' * @param id entero correspondiente al registro en la base de datos *///from w w w . ja v a 2s .c o m public void vetarUsuario(String estado, Integer id) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); Query query = session.createQuery("UPDATE Usuarios " + "SET Estado = :estado " + "WHERE Id = :id"); query.setString("estado", estado); query.setInteger("id", id); query.executeUpdate(); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } }
From source file:com.esteban.cmms.maven.model.Usuarios_Model.java
/** * Obtiene un nico registro de la base de datos en la tabla Usuarios, deben * coincidir en detalle los datos con los de los registros * * @param usuario nombre de usuario para buscar en la base de datos, es un * parmetro case sensitive/* www .j a va2s.com*/ * @param contrasena nombre de usuario para buscar en la base de datos, es * un parmetro case sensitive * @return un nico resultado tipo Usuarios con los datos del registro. */ public Object getUsuarioByUC(String usuario, String contrasena) { Object obj = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); String queryString = "from Usuarios as users" + " left join fetch users.roles" + " where users.usuario = :usuario and" + " users.contrasena = :contrasena and" + " users.estado = 'Activo'"; Query query = session.createQuery(queryString); query.setString("usuario", usuario); query.setString("contrasena", contrasena); obj = query.uniqueResult(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return obj; }
From source file:com.eurodyn.uns.dao.hibernate.HibernateChannelDao.java
License:Mozilla Public License
@Override public List findAllChannelsByMode(String mode, String orderProperty, String order) throws DAOException { Session session = null;/*ww w . j av a2 s . c o m*/ try { session = getSession(); Query query = session.createQuery( "select c from Channel as c where c.mode=:mode order by c." + orderProperty + " " + order); query.setString("mode", mode); return query.list(); } catch (HibernateException e) { throw new DAOException(e); } finally { closeSession(session); } }
From source file:com.eurodyn.uns.dao.hibernate.HibernateChannelDao.java
License:Mozilla Public License
@Override public List findAllChannelsByModeAndCreator(String mode, User creator, String orderProperty, String order) throws DAOException { Session session = null;/*from www .j a v a 2 s. com*/ try { session = getSession(); Query query = session .createQuery("select c from Channel as c where c.mode=:mode and c.creator=:creator order by c." + orderProperty + " " + order); query.setString("mode", mode); query.setEntity("creator", creator); return query.list(); } catch (HibernateException e) { throw new DAOException(e); } finally { closeSession(session); } }
From source file:com.eurodyn.uns.dao.hibernate.HibernateChannelDao.java
License:Mozilla Public License
@Override public List getSubscriptions(String channelId) throws DAOException { List result = null;// w w w .j a va 2s.co m Session session = null; try { session = getSession(); String query = null; query = "select s from Subscription as s where s.channel.id = :channelId"; Query q = session.createQuery(query); q.setString("channelId", channelId); result = q.list(); } catch (HibernateException e) { throw new DAOException(e); } finally { closeSession(session); } return result; }
From source file:com.eurodyn.uns.dao.hibernate.HibernateChannelDao.java
License:Mozilla Public License
@Override public Channel findChannel(String secondaryId) throws DAOException { Session session = null;//from w w w .ja v a 2 s .co m Channel channel = null; try { session = getSession(); Query query = session.createQuery("select c from Channel as c where c.secondaryId=:secondaryId"); query.setString("secondaryId", secondaryId); List list = query.list(); if (list.size() > 0) { channel = (Channel) list.get(0); } Hibernate.initialize(channel.getDeliveryTypes()); Hibernate.initialize(channel.getRoles()); Hibernate.initialize(channel.getMetadataElements()); } catch (HibernateException e) { throw new DAOException(e); } finally { closeSession(session); } return channel; }
From source file:com.eurodyn.uns.dao.hibernate.HibernateUserDao.java
License:Mozilla Public License
public User findUser(String username) throws DAOException { Session session = null;//from w w w.j a v a 2 s . c o m User user = null; try { session = getSession(); Query query = session.createQuery("select u from User as u where u.externalId=:username"); query.setString("username", username); List list = query.list(); if (list.size() > 0) { user = (User) list.get(0); Hibernate.initialize(user.getSubscriptions()); Hibernate.initialize(user.getDeliveryAddresses()); Collection subscriptions = user.getSubscriptions().values(); for (Iterator iter = subscriptions.iterator(); iter.hasNext();) { Subscription subcription = (Subscription) iter.next(); Hibernate.initialize(subcription.getFilters()); } } } catch (HibernateException e) { throw new DAOException(e); } finally { closeSession(session); } return user; }
From source file:com.evolveum.midpoint.repo.sql.AddGetObjectTest.java
License:Apache License
@Test public void test100AddUserWithoutAssignmentIds() throws Exception { OperationResult result = new OperationResult("test100AddUserWithoutAssignmentIds"); PrismObject<UserType> user = PrismTestUtil.parseObject(new File(FOLDER_BASIC, "user-big.xml")); //remove ids from assignment values PrismContainer container = user.findContainer(UserType.F_ASSIGNMENT); for (PrismContainerValue value : (List<PrismContainerValue>) container.getValues()) { value.setId(null);//www .j a v a 2 s.c o m } final String OID = repositoryService.addObject(user, null, result); result.computeStatusIfUnknown(); //get user user = repositoryService.getObject(UserType.class, OID, null, result); result.computeStatusIfUnknown(); container = user.findContainer(UserType.F_ASSIGNMENT); List<Short> xmlShorts = new ArrayList<>(); for (PrismContainerValue value : (List<PrismContainerValue>) container.getValues()) { AssertJUnit.assertNotNull(value.getId()); xmlShorts.add(value.getId().shortValue()); } Collections.sort(xmlShorts); Session session = open(); try { Query query = session.createSQLQuery("select id from m_assignment where owner_oid=:oid"); query.setString("oid", OID); List<Short> dbShorts = new ArrayList<>(); for (Number n : (List<Number>) query.list()) { dbShorts.add(n.shortValue()); } Collections.sort(dbShorts); LOGGER.info("assigments ids: expected {} db {}", Arrays.toString(xmlShorts.toArray()), Arrays.toString(dbShorts.toArray())); AssertJUnit.assertArrayEquals(xmlShorts.toArray(), dbShorts.toArray()); } finally { close(session); } }