manejadorDB.controlador.CargoControlador.java Source code

Java tutorial

Introduction

Here is the source code for manejadorDB.controlador.CargoControlador.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package manejadorDB.controlador;

import entidad.Cargo;
import java.util.List;
import manejadorDB.Interfaz.MetodosCargo;
import manejadorDB.Sesion;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author juani
 */
public class CargoControlador implements MetodosCargo {

    @Override
    public void crear(Cargo cargo) {

        SessionFactory factory = Sesion.init();
        if (factory != null) {

            try {
                //crear sesion
                Session session = factory.getCurrentSession();

                //transaccion
                session.beginTransaction();

                //guardar aeropuerto
                session.save(cargo);

                //commitear transaccion
                session.getTransaction().commit();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Sesion.close();
            }
        }

    }

    @Override
    public List<Cargo> todos() {

        List<Cargo> cargos = null;

        SessionFactory factory = Sesion.init();
        if (factory != null) {

            try {
                //crear sesion
                Session session = factory.getCurrentSession();

                //transaccion
                session.beginTransaction();

                //obtener lista 
                cargos = session.createNamedQuery("Cargo.findAll").list();

                //commitear transaccion
                session.getTransaction().commit();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Sesion.close();
            }
        }

        return cargos;
    }

    @Override
    public int cantidad() {
        List<Cargo> cargos = null;

        SessionFactory factory = Sesion.init();
        if (factory != null) {

            try {
                //crear sesion
                Session session = factory.getCurrentSession();

                //transaccion
                session.beginTransaction();

                //obtener lista 
                cargos = session.createNamedQuery("Cargo.findAll").list();

                //commitear transaccion
                session.getTransaction().commit();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Sesion.close();
            }
        }

        if (cargos == null)
            return 0;
        else
            return cargos.size();
    }

    @Override
    public Cargo devolverCargo(Integer idCargo) {
        List<Cargo> cargos = null;

        SessionFactory factory = Sesion.init();
        if (factory != null) {

            try {
                //crear sesion
                Session session = factory.getCurrentSession();

                //transaccion
                session.beginTransaction();

                //obtener lista 
                cargos = session.createNamedQuery("Cargo.findByIdcargo").setParameter("idcargo", idCargo).list();

                //commitear transaccion
                session.getTransaction().commit();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Sesion.close();
            }
        }

        if (cargos != null && cargos.size() > 0) {
            return cargos.get(0);
        } else {
            return null;
        }
    }

}