/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package maissocial.repositorio;
import java.util.List;
import maissocial.entidade.Setor;
import maissocial.util.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author luciano
*/
public class SetorRepositorio {
SessionFactory factory = HibernateUtil.getSessionFactory();
public void salvarSetor(Setor st){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.save(st);
trans.commit();
session.close();
}
public void deletarSetor(int cod){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
Setor st = pesquisarSetorPorCodigo(cod);
session.delete(st);
trans.commit();
session.close();
}
public void deletarSetorPorObj(Setor st){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.delete(st);
trans.commit();
session.close();
}
public Setor pesquisarSetorPorCodigo(int cod){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {setor.*} FROM setor {setor} WHERE cod_setor = "+cod);
query.addEntity("setor", Setor.class);
Setor st = null;
if(query.list().size() > 0){
st = (Setor) query.list().get(0);
}
session.close();
return st;
}
public Setor pesquisarSetorPorDescricao(String desc){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {setor.*} FROM setor {setor} WHERE descricao like '"+desc+"'");
query.addEntity("setor", Setor.class);
Setor st = null;
if(query.list().size() > 0){
st = (Setor) query.list().get(0);
}
session.close();
return st;
}
public List pesquisarSetorTodos(){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {setor.*} FROM setor {setor}");
query.addEntity("setor", Setor.class);
List st = query.list();
session.close();
return st;
}
public void alterarSetor(Setor st){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
Setor st2 = pesquisarSetorPorCodigo(st.getCodSetor());
st.setCodSetor(st2.getCodSetor());
session.update(st);
trans.commit();
session.close();
}
}