Example usage for org.hibernate Session createQuery

List of usage examples for org.hibernate Session createQuery

Introduction

In this page you can find the example usage for org.hibernate Session createQuery.

Prototype

@Override
    org.hibernate.query.Query createQuery(CriteriaDelete deleteQuery);

Source Link

Usage

From source file:abd.p1.bd.UserDAO.java

public List<Usuario> findNearestByName(String name, Double lat, Double lng) {
    Session s = sf.openSession();
    Transaction tr = s.beginTransaction();
    List<Usuario> userList = s.createQuery("from " + Usuario.class.getName() + " as user"
            + " where user.nombre like :name"
            + " order by (user.latitud - :lat) * (user.latitud - :lat) + (user.longitud - :lng) * (user.longitud - :lng)")
            .setString("name", "%" + name + "%").setDouble("lat", lat).setDouble("lng", lng).setMaxResults(20)
            .list();/*www  .  j a v a2 s .c o m*/
    tr.commit();
    s.close();

    return userList;
}

From source file:abd.p1.bd.UsuarioDAO.java

public boolean comprobarUsuario(String user, String password) {
    String sql = "SELECT * FROM usuarios WHERE correo = " + user + " AND contrasena = " + password;
    Session session = sf.openSession();
    Query query = session.createQuery(sql);
    List<Usuario> users = (List<Usuario>) query.list();
    if (users.isEmpty())
        return false;
    else/*from  www.  j a  va2 s  .c o  m*/
        return true;
}

From source file:abd.p1.bd.UsuarioDAO.java

public ArrayList obtenerUsuarios() {
    String sql = "SELECT nombre, fecha_nac, foto FROM usuarios";
    Session sesion = sf.openSession();
    Query consulta = sesion.createQuery(sql);
    ArrayList<Usuario> usuarios = (ArrayList<Usuario>) consulta.list();
    return usuarios;
}

From source file:acceptance.hibernate.HibernateReferenceTest.java

License:Open Source License

protected void tearDown() {
    try {//from w w w  .j a va 2s.  c  o  m
        final Session session = getSessionFactory().getCurrentSession();
        session.beginTransaction();
        final Division div = (Division) session.createQuery("from Division").uniqueResult();
        session.delete(div);
        session.getTransaction().commit();
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
}

From source file:acceptance.hibernate.HibernateReferenceTest.java

License:Open Source License

public void testObjectGraphWithReferences() {
    final Division memory = setupNonpersistentDivision();
    final Division persisted = setupPersistentDivision();

    final String expectedXml = xstream.toXML(memory);
    final String persistedXml = xstream.toXML(persisted);

    final Session session = getSessionFactory().getCurrentSession();
    session.beginTransaction();/* ww  w .  j av  a2  s  . c  om*/
    final Division loaded = (Division) session.createQuery("from Division").uniqueResult();
    final String loadedXml = xstream.toXML(loaded);
    session.flush();
    session.getTransaction().commit();
    assertEquals(expectedXml, persistedXml);
    assertEquals(expectedXml, loadedXml);
}

From source file:acceptance.hibernate.HibernateReferenceTest.java

License:Open Source License

public void testLazyProxyWithReferences() {
    setupPersistentDivision();/*from   www  .  j  av  a 2 s .c  o m*/

    final Session session = getSessionFactory().getCurrentSession();
    session.beginTransaction();
    final Division loaded = (Division) session.createQuery("from Division").uniqueResult();
    final Department dept = (Department) loaded.getDepartments().iterator().next();
    final Person person = (Person) dept.getPeople().iterator().next();
    final Site site = person.getSite();
    assertTrue(HibernateProxy.class.isAssignableFrom(site.getClass()));
    final String loadedXml = xstream.toXML(site);
    session.flush();
    session.getTransaction().commit();

    final String expectedXml = "" + "<site>\n" + "  <name>Site1</name>\n" + "  <people>\n" + "    <person>\n"
            + "      <name>Tom</name>\n" + "      <department>\n" + "        <name>Dep1</name>\n"
            + "        <division>\n" + "          <name>Div1</name>\n" + "          <departments>\n"
            + "            <department reference=\"../../..\"/>\n" + "          </departments>\n"
            + "        </division>\n" + "        <people>\n" + "          <person reference=\"../../..\"/>\n"
            + "        </people>\n" + "      </department>\n" + "      <site reference=\"../../..\"/>\n"
            + "    </person>\n" + "  </people>\n" + "</site>";
    assertEquals(expectedXml, loadedXml);
}

From source file:acceptance.hibernate.HibernateReferenceTest.java

License:Open Source License

/**
 * Load object graph with Hibernate from the database.
 *///  w  w  w. j  av  a  2 s . co m
private Division getPersistentDivision() {
    final Session session = getSessionFactory().getCurrentSession();
    session.beginTransaction();
    final Division div = (Division) session.createQuery("from Division").uniqueResult();
    session.getTransaction().commit();
    return div;
}

From source file:accesobd.AccesoRoles.java

License:BSD License

/**
 * Retorna la lista de roles para un sistema particular.
 * @param idSistema El identificador del sistema.
 * @return //  ww  w  .j  a  va 2 s  . c o m
 */
public List<AdmRol> obtenerRolesSistema(int idSistema) {
    List<AdmRol> listaRoles = null;

    Session sesion;
    sesion = HibernateUtil.getSessionFactory().openSession();

    try {

        Query q = sesion.createQuery("from AdmRol where admSistema.id = :idSistema");
        q.setParameter("idSistema", idSistema);

        listaRoles = (List<AdmRol>) q.list();

    } catch (Exception e) {
        System.out.println(e.toString());
    }

    sesion.close();

    return listaRoles;

}

From source file:accesobd.AccesoRolesVistas.java

License:BSD License

/**
 * Retorna la lista de roles-vistas para un rol y una vista determinada.
 *
 * @param rol El rol.//  w  ww  .ja va  2s  .c  om
 * @param vista La vista.
 * @return
 */
public List<AdmRolVista> obtenerRolesVistas(AdmRol rol, AdmVista vista) {
    List<AdmRolVista> listaRolVistas = null;

    Session sesion;
    sesion = HibernateUtil.getSessionFactory().openSession();

    try {

        Query q = sesion.createQuery("from AdmRolVista where admRol = :rol and admVista = :vista");
        q.setParameter("rol", rol);
        q.setParameter("vista", vista);

        listaRolVistas = (List<AdmRolVista>) q.list();

    } catch (Exception e) {
        System.out.println(e.toString());
    }

    sesion.close();

    return listaRolVistas;

}

From source file:accesobd.AccesoRolesVistas.java

License:BSD License

/**
 * Retorna la lista de roles-vista asociadas a una vista.
 *
 * @param vista La vista./*from w  w w  .  ja  v a2  s  .  co m*/
 * @return
 */
public List<AdmRolVista> obtenerRolesVistasParaUnaVista(AdmVista vista) {
    List<AdmRolVista> listaRolVistas = null;

    Session sesion;
    sesion = HibernateUtil.getSessionFactory().openSession();

    try {

        Query q = sesion.createQuery("from AdmRolVista where admVista = :vista");
        q.setParameter("vista", vista);

        listaRolVistas = (List<AdmRolVista>) q.list();

        for (AdmRolVista rolVista : listaRolVistas) {
            Hibernate.initialize(rolVista.getAdmRol());
            Hibernate.initialize(rolVista.getAdmVista());
        }

    } catch (Exception e) {
        System.out.println(e.toString());
    }

    sesion.close();

    return listaRolVistas;

}