Example usage for org.hibernate SQLQuery addEntity

List of usage examples for org.hibernate SQLQuery addEntity

Introduction

In this page you can find the example usage for org.hibernate SQLQuery addEntity.

Prototype

SQLQuery<T> addEntity(Class entityType);

Source Link

Document

Declare a "root" entity, without specifying an alias.

Usage

From source file:ModelDao.GenericDAOImpl.java

public List listData(Class clazz, String table) {
    Session session = sessionFactory.openSession();
    Transaction tx = null;/* w  w  w .j a  v a  2s.com*/
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM " + table + " WHERE D_E_L_E_T_E = '0' ORDER BY NOME";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(clazz);
        List result = query.list();
        tx.commit();
        return result;
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    return null;
}

From source file:ModelDao.MotoristaDao.java

public List listData() {
    Session session = sessionFactory.openSession();
    Transaction tx = null;/*from w w w .j a  v a2s.co  m*/
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM motorista ";
        sql += "INNER JOIN pessoa ON motorista.idpessoa = pessoa.idpessoa ";
        sql += "ORDER BY NOME";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(Motorista.class);
        List result = query.list();
        tx.commit();
        return result;
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    return null;
}

From source file:ModelDao.UsuarioDao.java

public Usuario findUser(String login, String password) {
    Session session = sessionFactory.openSession();
    session.beginTransaction();/*from   w w  w.j a  v  a  2s.c om*/
    String sql = "SELECT * FROM usuario ";
    sql += "INNER JOIN pessoa ON pessoa.idpessoa = usuario.idpessoa";
    sql += " WHERE usuario.login = '" + login + "' AND usuario.senha = '" + password + "'";
    SQLQuery query = session.createSQLQuery(sql);
    query.addEntity(Usuario.class);
    List result = query.list();
    session.getTransaction().commit();
    if (!result.isEmpty()) {
        return (Usuario) result.get(0);
    } else {
        return null;
    }
}

From source file:modelo.CalificacionLibroDAO.java

public List<Libro> librosPorCalificar(int id) {
    SessionFactory factory;/*w  w w .j  a  v  a 2s  . c  o  m*/
    List<Libro> misprestamos = null;
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM libro WHERE idlibro IN (SELECT libroidlibro FROM solicitudes WHERE aceptado = TRUE AND califlibro = FALSE AND usridusuario = "
                + id + " )";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(Libro.class);
        misprestamos = query.list();
        tx.commit();
        return misprestamos;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        return null;
    } finally {
        session.close();
    }
}

From source file:modelo.CalificacionLibroDAO.java

public List<Calificacionlibro> calificaciones(int id) {
    SessionFactory factory;/*from ww w .  j  a v a  2 s .co m*/
    List<Calificacionlibro> calificacion = null;
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM calificacionlibro WHERE libroidlibro = " + id;
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(Calificacionlibro.class);
        calificacion = query.list();
        tx.commit();
        return calificacion;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        return null;
    } finally {
        session.close();
    }
}

From source file:modelo.CalificacionUsuarioDAO.java

public List<Libro> misPrestamos(int id) {
    SessionFactory factory;/*  w w  w.ja va 2  s  .c  o  m*/
    List<Libro> misprestamos = null;
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM libro WHERE usridusuario = " + id
                + " AND idlibro IN (SELECT libroidlibro FROM solicitudes WHERE aceptado = TRUE)";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(Libro.class);
        misprestamos = query.list();
        tx.commit();
        return misprestamos;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        return null;
    } finally {
        session.close();
    }
}

From source file:modelo.CalificacionUsuarioDAO.java

public Usuario consumidor(int libro) {
    SessionFactory factory;/*from  w ww .  j ava2s .  co m*/
    Usuario consumidor = null;
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM usuario WHERE idusuario in (SELECT usridusuario FROM solicitudes WHERE libroidlibro ="
                + libro + "AND aceptado = TRUE)";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(Usuario.class);
        consumidor = (Usuario) query.uniqueResult();
        tx.commit();
        return consumidor;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        return null;
    } finally {
        session.close();
    }
}

From source file:modelo.CalificacionUsuarioDAO.java

public List<Calificacionusuario> calificacionesU(int id) {
    SessionFactory factory;//  w w w. j a  va2  s.  c o m
    List<Calificacionusuario> calificacion = null;
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM calificacionusuario WHERE consumidoridusr = " + id;
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(Calificacionusuario.class);
        calificacion = query.list();
        tx.commit();
        return calificacion;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        return null;
    } finally {
        session.close();
    }
}

From source file:modelo.SolicitudesDAO.java

public List<Solicitudes> pendientesUsuario(int id) {
    SessionFactory factory;/*from w  w  w.  jav a2  s  .  c o m*/
    List<Solicitudes> misprestamos = null;
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM solicitudes WHERE aceptado = FALSE AND libroidlibro IN (SELECT idlibro FROM libro WHERE usridusuario = "
                + id + ")";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(Solicitudes.class);
        misprestamos = query.list();
        tx.commit();
        return misprestamos;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        return null;
    } finally {
        session.close();
    }
}

From source file:modelo.SolicitudesDAO.java

public List<Solicitudes> aceptadas(int idusuario) {
    SessionFactory factory;//  w w  w  .  j  av  a2s .  c om
    List<Solicitudes> solicitudes = null;
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String sql = "SELECT * FROM solicitudes WHERE aceptado = TRUE AND libroidlibro IN (SELECT idlibro FROM libro WHERE usridusuario = "
                + idusuario + ")";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(Solicitudes.class);
        solicitudes = query.list();
        tx.commit();
        return solicitudes;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        return null;
    } finally {
        session.close();
    }
}