Example usage for org.hibernate SessionFactory openSession

List of usage examples for org.hibernate SessionFactory openSession

Introduction

In this page you can find the example usage for org.hibernate SessionFactory openSession.

Prototype

Session openSession() throws HibernateException;

Source Link

Document

Open a Session .

Usage

From source file:ClasseDao.VendaDao.java

public java.util.List listarNomes() {
    try {//from  w ww .ja v a2s . c  om
        SessionFactory tp = new Configuration().configure().buildSessionFactory();
        Session sessao = tp.openSession();
        java.util.List lista = new ArrayList();
        lista = sessao.createQuery("SELECT nomeCliente from Cliente ").list();
        sessao.close();

        return lista;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Erro ao preencher tabela" + e);
    }
    return null;
}

From source file:cn.fql.template.hibernate.HibernateTest.java

License:Open Source License

protected void setUp() throws Exception {
    Configuration config = new Configuration().configure();
    SessionFactory sessionFactory = config.buildSessionFactory();
    session = sessionFactory.openSession();
}

From source file:co.edu.udea.profarq.labcinco.dao.hibernate.impl.AbstractEntityDAO.java

public Serializable delete(IEntityContext entityContext) throws LabCincoProfArqDAOException {
    Session session;/*from w  ww .j ava 2 s  .  co  m*/
    Transaction transaction = null;

    try {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        session.delete(entityContext);
        transaction.commit();
        session.close();
    } catch (Exception ex) {
        if (transaction != null) {
            transaction.rollback();
        }

        throw new LabCincoProfArqDAOException(String.format("Error during procedure's \"%s\" for class: %s",
                "Delete", entityContext.getClass().getSimpleName()), ex);
    }

    return (entityContext.getPrimaryKey());
}

From source file:co.edu.udea.profarq.labcinco.dao.hibernate.impl.AbstractEntityDAO.java

public IEntityContext find(Class<? extends IEntityContext> c, Serializable primaryKey)
        throws LabCincoProfArqDAOException {
    IEntityContext entityContext = null;
    Session session;/*www  . j av  a2 s.co m*/

    try {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        session = sessionFactory.openSession();
        entityContext = (IEntityContext) session.get(c, primaryKey);
        session.close();
    } catch (Exception ex) {
        throw new LabCincoProfArqDAOException(
                String.format("Error during procedure's \"%s\" for class: %s", "Find", c.getSimpleName()), ex);
    }

    return (entityContext);
}

From source file:co.edu.udea.profarq.labcinco.dao.hibernate.impl.AbstractEntityDAO.java

@SuppressWarnings(value = { "unchecked" })
public List<IEntityContext> findAll(Class<? extends IEntityContext> c) throws LabCincoProfArqDAOException {
    List<IEntityContext> entitesContextFoundList = new ArrayList<>();

    Criteria criteria;//w  w  w  .j  ava 2 s.co  m
    Session session;

    try {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        session = sessionFactory.openSession();
        criteria = session.createCriteria(c);
        entitesContextFoundList = criteria.list();
        session.close();
    } catch (Exception ex) {
        throw new LabCincoProfArqDAOException(
                String.format("Error during procedure's \"%s\" for class: %s", "Find All", c.getSimpleName()),
                ex);
    }

    return (entitesContextFoundList);
}

From source file:co.edu.udea.profarq.labcinco.dao.hibernate.impl.AbstractEntityDAO.java

public Serializable save(IEntityContext entityContext) throws LabCincoProfArqDAOException {
    Serializable primaryKey = null;
    Session session;/*ww w .j  a va  2s.c  o m*/
    Transaction transaction = null;

    try {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        primaryKey = session.save(entityContext);
        transaction.commit();
        session.close();
    } catch (Exception ex) {
        if (transaction != null) {
            transaction.rollback();
        }

        throw new LabCincoProfArqDAOException(String.format("Error during procedure's \"%s\" for class: %s",
                "Save", entityContext.getClass().getSimpleName()), ex);
    }

    return (primaryKey);
}

From source file:co.edu.udea.profarq.labcinco.dao.hibernate.impl.AbstractEntityDAO.java

public Serializable update(IEntityContext entityContext) throws LabCincoProfArqDAOException {
    Session session;/*from w w  w .ja  v a2  s .  c  o m*/
    Transaction transaction = null;

    try {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        session.update(entityContext);
        transaction.commit();
        session.close();
    } catch (Exception ex) {
        if (transaction != null) {
            transaction.rollback();
        }

        throw new LabCincoProfArqDAOException(String.format("Error during procedure's \"%s\" for class: %s",
                "Update", entityContext.getClass().getSimpleName()), ex);
    }

    return (entityContext.getPrimaryKey());
}

From source file:co.udea.edu.proyectointegrador.gr11.parqueaderoapp.data.dao.implement.EstadisticaDaoImpl.java

@Override
public List<TipoVehiculoEstadistica> getIngresosByTipoVehiculo(Date fechaInicio, Date fechaFin)
        throws PersistentException {
    List<TipoVehiculoEstadistica> listaDeTipoVehiculo = new ArrayList<>();
    try {/*  w  ww  . j av a  2  s .  co  m*/
        SessionFactory sf = HibernateUtil.getSessionFactory();
        session = sf.openSession();
        Query query = session
                .createQuery("SELECT I.usuarioVehiculo.vehiculo.tipoVehiculo , count(*) FROM Ingreso as I "
                        + "where I.id.fechaIngreso between :fechaInicio and :fechaFin "
                        + "GROUP BY I.usuarioVehiculo.vehiculo.tipoVehiculo");
        query.setParameter("fechaInicio", fechaInicio).setParameter("fechaFin", fechaFin);
        List<Object[]> results = query.list();

        for (Object[] result : results) {
            TipoVehiculo tipoVehiculo = (TipoVehiculo) result[0];
            long numeroDeIngresos = (long) result[1];
            TipoVehiculoEstadistica estadistica = new TipoVehiculoEstadistica(tipoVehiculo, numeroDeIngresos);
            listaDeTipoVehiculo.add(estadistica);
        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.out.println(e.getCause());
        throw new PersistentException("Se genero un problema con el manejo "
                + "de la base de datos, mensaje del sistema: " + e.getMessage());
    } finally {
        if (session != null) {
            session.close();
        }
    }

    return listaDeTipoVehiculo;
}

From source file:co.udea.edu.proyectointegrador.gr11.parqueaderoapp.data.dao.implement.EstadisticaDaoImpl.java

@Override
public List<TipoUsuarioEstadistica> getIngresosByTipoUsuario(Date fechaInicio, Date fechaFin)
        throws PersistentException {
    List<TipoUsuarioEstadistica> listaDeTipoUsuario = new ArrayList<>();
    try {/*w w w. ja  v a  2 s. c o  m*/
        SessionFactory sf = HibernateUtil.getSessionFactory();
        session = sf.openSession();
        Query query = session
                .createQuery("SELECT I.usuarioVehiculo.usuario.tipoUsuario , count(*) FROM Ingreso as I "
                        + "where I.id.fechaIngreso between :fechaInicio and :fechaFin "
                        + "GROUP BY I.usuarioVehiculo.usuario.tipoUsuario");
        query.setParameter("fechaInicio", fechaInicio).setParameter("fechaFin", fechaFin);
        List<Object[]> results = query.list();

        for (Object[] result : results) {
            TipoUsuario tipoUsuario = (TipoUsuario) result[0];
            long numeroDeIngresos = (long) result[1];
            TipoUsuarioEstadistica estadistica = new TipoUsuarioEstadistica(tipoUsuario, numeroDeIngresos);
            listaDeTipoUsuario.add(estadistica);
        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.out.println(e.getCause());
        throw new PersistentException("Se genero un problema con el manejo "
                + "de la base de datos, mensaje del sistema: " + e.getMessage());
    } finally {
        if (session != null) {
            session.close();
        }
    }

    return listaDeTipoUsuario;

}

From source file:co.udea.edu.proyectointegrador.gr11.parqueaderoapp.data.dao.implement.EstadisticaDaoImpl.java

@Override
public List<HoraDelDiaEstadistica> getIngresosByHoraDelDia(Date fechaInicio, Date fechaFin)
        throws PersistentException {
    List<HoraDelDiaEstadistica> listaDeHoras = new ArrayList<>();
    try {/*from  www. j  av a 2  s .  c  om*/
        SessionFactory sf = HibernateUtil.getSessionFactory();
        session = sf.openSession();
        Query query = session.createQuery("Select HOUR(I.id.fechaIngreso), count(*) from Ingreso as I "
                + "where I.id.fechaIngreso between :fechaInicio and :fechaFin "
                + "group by  HOUR(I.id.fechaIngreso) " + "order by  HOUR(I.id.fechaIngreso) ");
        query.setParameter("fechaInicio", fechaInicio).setParameter("fechaFin", fechaFin);
        List<Object[]> results = query.list();

        for (Object[] result : results) {
            int horaDelDia = (int) result[0];
            long numeroDeIngresos = (long) result[1];
            HoraDelDiaEstadistica estadistica = new HoraDelDiaEstadistica(horaDelDia, numeroDeIngresos);
            listaDeHoras.add(estadistica);
        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.out.println(e.getCause());
        throw new PersistentException("Se genero un problema con el manejo "
                + "de la base de datos, mensaje del sistema: " + e.getMessage());
    } finally {
        if (session != null) {
            session.close();
        }
    }

    return listaDeHoras;

}