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:by.bsuir.akkerman.mobile.server.dao.DaoUniversal.java

public List<EntityHelper> read(EntityHelper obj) {
    List<EntityHelper> objectsList = new ArrayList<>();
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();/*from   www . j ava 2 s.  co m*/
    try {
        objectsList = session.createQuery("from " + obj.getClass().getSimpleName()).list();
    } catch (HibernateException ex) {
        System.out.println(ex.getMessage());
    } finally {
        session.close();
    }
    return objectsList;
}

From source file:by.bsuir.akkerman.mobile.server.dao.DaoUniversal.java

public boolean delete(EntityHelper obj) {
    boolean isSuccessfully = false;
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();//from w  w  w  .  j  a v a 2  s. c  o m
    try {
        obj = (EntityHelper) session.get(obj.getClass(), obj.getId());
        session.delete(obj);
        if (!obj.getParent().isEmpty()) {
            Set<EntityHelper> set = new HashSet(obj.getParent());
            for (Iterator it = set.iterator(); it.hasNext();) {
                EntityHelper parent_obj = (EntityHelper) session.get(it.next().getClass(),
                        ((EntityHelper) it.next()).getId());
                parent_obj.getChild().add(obj);
                session.update(parent_obj);
            }
        }
        if (obj.getParent() != null) {
            Set<EntityHelper> set = new HashSet(obj.getParent());
            for (Iterator it = set.iterator(); it.hasNext();) {
                String[] lines = it.next().getClass().getName().split("_");
                EntityHelper parent_obj = (EntityHelper) session.get(lines[0],
                        ((EntityHelper) it.next()).getId());
                parent_obj.getChild().remove(obj);
                session.update(parent_obj);
            }

        }
        isSuccessfully = true;
    } catch (HibernateException ex) {
        System.out.println(ex.getMessage());
    } finally {
        session.getTransaction().commit();
        session.close();
    }
    return isSuccessfully;
}

From source file:c.servicios.PersonaServicio.java

public static List<Persona> getPersona() {
    SessionFactory sf = HUtil.getSessionFactory();
    Session ses = sf.openSession();
    return ses.createCriteria(Persona.class).list();
}

From source file:c.servicios.UsuarioServicio.java

public static List<Usuario> getUsuarios() {
    SessionFactory sf = HUtil.getSessionFactory();
    Session ses = sf.openSession();

    List<Usuario> list = new ArrayList<Usuario>();
    list = ses.createCriteria(Usuario.class).list();
    if (list.isEmpty())
        list.add(new Usuario(0, "def", "def"));
    return list;/*from w  w  w .  ja  v a2 s. c om*/
}

From source file:caipsfa.app.modelo.GestionDiagnostico.java

public ArrayList<Diagnostico> getAllDiagnostico() {
    SessionFactory sesFact = HibernateUtil.getSessionFactory();
    Session ses = sesFact.openSession();
    diagnostico = new ArrayList<Diagnostico>();
    String sql = "from Diagnostico";
    diagnostico = (ArrayList<Diagnostico>) ses.createQuery(sql).list();
    return diagnostico;
}

From source file:caipsfa.app.modelo.GestionDiagnostico.java

public ArrayList<Diagnostico> geOne(String codigoDiagnostico) {
    SessionFactory sesFact = HibernateUtil.getSessionFactory();
    Session ses = sesFact.openSession();
    diagnostico = new ArrayList<Diagnostico>();
    String sql = "from Diagnostico where codigoDiagnostico = '" + codigoDiagnostico + "'";
    diagnostico = (ArrayList<Diagnostico>) ses.createQuery(sql).list();
    return diagnostico;
}

From source file:caipsfa.app.modelo.GestionDiagnostico.java

public boolean addDiagnostic(DiagnosticoForm diag) {
    boolean estado = false;
    try {/*  w w w  .  j  a  v a  2  s  .c  om*/
        SessionFactory sesFact = HibernateUtil.getSessionFactory();
        Session ses = sesFact.openSession();
        Transaction trans = ses.beginTransaction();
        Diagnostico diagnostic = new Diagnostico();
        diagnostic.setCodigoDiagnostico(0);
        diagnostic.setNombre(diag.getNombre());
        diagnostic.setDescripcion(diag.getDescripcion());
        ses.save(diagnostic);
        trans.commit();
        ses.close();
        estado = true;
        return estado;
    } catch (Exception e) {
        System.out.println(e);
        return estado;
    }
}

From source file:caipsfa.app.modelo.GestionDiagnostico.java

public boolean editDiagnostic(DiagnosticoForm diag) {
    boolean estado = false;
    try {//from ww  w.  ja v  a  2s. c om
        SessionFactory sesFact = HibernateUtil.getSessionFactory();
        Session ses = sesFact.openSession();
        Transaction trans = ses.beginTransaction();
        Diagnostico diagnostic = new Diagnostico();
        diagnostic.setCodigoDiagnostico(Integer.parseInt(diag.getCodigoDiagnostico()));
        diagnostic.setNombre(diag.getNombre());
        diagnostic.setDescripcion(diag.getDescripcion());
        ses.update(diagnostic);
        trans.commit();
        ses.close();
        estado = true;
        return estado;
    } catch (Exception e) {
        System.out.println(e);
        return estado;
    }
}

From source file:caipsfa.app.modelo.GestionDiagnostico.java

public boolean deleteDiagnostic(int id) {
    boolean estado = false;
    try {/*from  w w w  .  ja  v a2s .c  o m*/
        SessionFactory sesFact = HibernateUtil.getSessionFactory();
        Session ses = sesFact.openSession();
        Transaction trans = ses.beginTransaction();
        Diagnostico diagnostic = (Diagnostico) ses.get(Diagnostico.class, id);
        ses.delete(diagnostic);
        trans.commit();
        ses.close();
        estado = true;
        return estado;
    } catch (Exception e) {
        System.out.println(e);
        return estado;
    }
}

From source file:caipsfa.app.modelo.GestionDoctores.java

public ArrayList<Doctores> getAllDoctors() {
    SessionFactory sesFact = HibernateUtil.getSessionFactory();
    Session ses = sesFact.openSession();
    doctores = new ArrayList<Doctores>();
    String sql = "from Doctores";
    doctores = (ArrayList<Doctores>) ses.createQuery(sql).list();
    return doctores;
}