/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package maissocial.repositorio;
import java.util.List;
import maissocial.controle.TurmaPis;
import maissocial.util.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author luciano
*/
public class TurmaPisRepositorio {
SessionFactory factory = HibernateUtil.getSessionFactory();
public void salvarTurmaPis(TurmaPis trp){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.save(trp);
trans.commit();
session.close();
}
public void deletarTurmaPis(int cod){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
TurmaPis trp = pesquisarTurmaPisPorCodigo(cod);
session.delete(trp);
trans.commit();
session.close();
}
public void deletarTurmaPisPorObj(TurmaPis trp){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.delete(trp);
trans.commit();
session.close();
}
public TurmaPis pesquisarTurmaPisPorCodigo(int cod){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {turma_pis.*} FROM turma_pis {turma_pis} WHERE cod_turma_pis = "+cod);
query.addEntity("turma_pis", TurmaPis.class);
TurmaPis trp = null;
if(query.list().size() > 0){
trp = (TurmaPis) query.list().get(0);
}
session.close();
return trp;
}
public List pesquisarTurmaPisTodos(){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {turma_pis.*} FROM turma_pis {turma_pis}");
query.addEntity("turma_pis", TurmaPis.class);
List trp = query.list();
session.close();
return trp;
}
public void alterarTurmaPis(TurmaPis trp){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
TurmaPis trp2 = pesquisarTurmaPisPorCodigo(trp.getCodTurmaPis());
trp.setCodTurmaPis(trp2.getCodTurmaPis());
session.update(trp);
trans.commit();
session.close();
}
}