Package maissocial.repositorio

Source Code of maissocial.repositorio.UfRepositorio

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

package maissocial.repositorio;

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

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

    SessionFactory factory = HibernateUtil.getSessionFactory();

    public void salvarUf(Uf uf){
        Session session = factory.openSession();
        Transaction trans = session.beginTransaction();

        trans.begin();

        session.save(uf);

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

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

        trans.begin();

        Uf uf = pesquisarUfPorCodigo(cod);
        session.delete(uf);

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

    public void deletarUfPorObj(Uf uf){
        Session session = factory.openSession();
        Transaction trans = session.beginTransaction();

        trans.begin();

        session.delete(uf);

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


    public Uf pesquisarUfPorCodigo(int cod){
        Session session = factory.openSession();
        SQLQuery query = session.createSQLQuery("SELECT {uf.*} FROM uf {uf} WHERE cod_uf = "+cod);

        query.addEntity("uf", Uf.class);

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

    public Uf pesquisarUfPorSigla(String sigla){
        Session session = factory.openSession();
        SQLQuery query = session.createSQLQuery("SELECT {uf.*} FROM uf {uf} WHERE sigla like '%"+sigla+"%'");

        query.addEntity("uf", Uf.class);

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


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

        query.addEntity("uf", Uf.class);

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

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

        query.addEntity("uf", Uf.class);

        List uf = query.list();

        session.close();
        return uf;
    }

    public void alterarUf(Uf uf){
        Session session = factory.openSession();
        Transaction trans = session.beginTransaction();

        trans.begin();

        Uf uf2 = pesquisarUfPorCodigo(uf.getCodUf());
        uf.setCodUf(uf2.getCodUf());
        session.update(uf);

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



}
TOP

Related Classes of maissocial.repositorio.UfRepositorio

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.