main.java.Contexto.ContextoAlianza.java Source code

Java tutorial

Introduction

Here is the source code for main.java.Contexto.ContextoAlianza.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 main.java.Contexto;

import java.util.List;
import main.java.modelos.Alianza;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author XDA
*/

public class ContextoAlianza extends ContextoBase implements IConsultable<Alianza> {
    //<editor-fold desc="Atributos">

    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Constructores">

    public ContextoAlianza(String NombreConf) throws HibernateException {
        super(NombreConf);
    }

    public ContextoAlianza() throws HibernateException {
        super();
    }

    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Metodos CRUD">

    @Override
    public void insertar(Alianza o) {
        Session ses = this.getFactory().openSession();
        Transaction tx = ses.beginTransaction();
        ses.save(o);
        tx.commit();
        ses.close();
    }

    @Override
    public Alianza buscar(long Id) {
        Session ses = this.getFactory().openSession();
        Transaction tx = ses.beginTransaction();
        Query q = ses.createQuery("from Alianza p where p.id=:id");
        q.setLong("id", Id);
        Alianza p = (Alianza) q.uniqueResult();
        tx.commit();
        ses.close();
        return p;
    }

    @Override
    public List<Alianza> seleccionarTodos() {
        Session ses = this.getFactory().openSession();
        Transaction tx = ses.beginTransaction();
        Query query = ses.createQuery("from Alianza p " + "where p.estado=:estado");
        query.setBoolean("estado", true);
        List<Alianza> alianzas = query.list();
        tx.commit();
        ses.close();
        return alianzas;
    }

    @Override
    public void eliminar(int Id) {
        Session ses = this.getFactory().openSession();
        Transaction tx = ses.beginTransaction();
        Alianza p = this.buscar(Id);
        p.setEstado(false);
        ses.update(p);
        tx.commit();
        ses.close();
    }

    @Override
    public void actualizar(Alianza elem) {
        Session ses = this.getFactory().openSession();
        Transaction tx = ses.beginTransaction();
        ses.update(elem);
        tx.commit();
        ses.close();
    }

}