Package maissocial.repositorio

Source Code of maissocial.repositorio.BairroRepositorio

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package maissocial.repositorio;

import java.util.List;
import maissocial.entidade.Bairro;
import maissocial.util.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
*
* @author luciano
*/
public class BairroRepositorio {

    SessionFactory factory = HibernateUtil.getSessionFactory();

    public void salvarBairro(Bairro br){
        Session session = factory.openSession();
        Transaction trans = session.beginTransaction();

        trans.begin();

        session.save(br);

        trans.commit();
        session.close();
    }

    public void deletarBairro(int cod){
        Session session = factory.openSession();
        Transaction trans = session.beginTransaction();

        trans.begin();

        Bairro br = pesquisarBairroPorCodigo(cod);
        session.delete(br);

        trans.commit();
        session.close();
    }

    public void deletarBairroPorObj(Bairro br){
        Session session = factory.openSession();
        Transaction trans = session.beginTransaction();

        trans.begin();

        session.delete(br);

        trans.commit();
        session.close();
    }


    public Bairro pesquisarBairroPorCodigo(int cod){
        Session session = factory.openSession();
        SQLQuery query = session.createSQLQuery("SELECT {bairro.*} FROM bairro {bairro} WHERE cod_bairro = "+cod);

        query.addEntity("bairro", Bairro.class);

        Bairro br = null;
        if(query.list().size() > 0){
            br = (Bairro) query.list().get(0);
        }
        session.close();
        return br;
    }

    public Bairro pesquisarBairroPorNome(String nome){
        Session session = factory.openSession();
        SQLQuery query = session.createSQLQuery("SELECT {bairro.*} FROM bairro {bairro} WHERE nome like '"+nome+"'");

        query.addEntity("bairro", Bairro.class);

        Bairro br = null;
        if(query.list().size() > 0){
            br = (Bairro) query.list().get(0);
        }
        session.close();
        return br;
    }


    public List pesquisarBairroTodos(){
        Session session = factory.openSession();
        SQLQuery query = session.createSQLQuery("SELECT {bairro.*} FROM bairro {bairro}");

        query.addEntity("bairro", Bairro.class);

        List br = query.list();

        session.close();
        return br;
    }

    public void alterarBairro(Bairro br){
        Session session = factory.openSession();
        Transaction trans = session.beginTransaction();

        trans.begin();

        Bairro br2 = pesquisarBairroPorCodigo(br.getCodBairro());
        br.setCodBairro(br2.getCodBairro());
        session.update(br);

        trans.commit();
        session.close();
    }



}
TOP

Related Classes of maissocial.repositorio.BairroRepositorio

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.