/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package maissocial.repositorio;
import java.util.List;
import maissocial.entidade.Profissao;
import maissocial.util.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author luciano
*/
public class ProfissaoRepositorio {
SessionFactory factory = HibernateUtil.getSessionFactory();
public void salvarProfissao(Profissao pf){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.save(pf);
trans.commit();
session.close();
}
public void deletarProfissao(int cod){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
Profissao pf = pesquisarProfissaoPorCodigo(cod);
session.delete(pf);
trans.commit();
session.close();
}
public void deletarProfissaoPorObj(Profissao pf){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.delete(pf);
trans.commit();
session.close();
}
public Profissao pesquisarProfissaoPorCodigo(int cod){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {profissao.*} FROM profissao {profissao} WHERE cod_profissao = "+cod);
query.addEntity("profissao", Profissao.class);
Profissao pf = null;
if(query.list().size() > 0){
pf = (Profissao) query.list().get(0);
}
session.close();
return pf;
}
public Profissao pesquisarProfissaoPorDescricao(String desc){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {profissao.*} FROM profissao {profissao} WHERE descricao like '"+desc+"'");
query.addEntity("profissao", Profissao.class);
Profissao pf = null;
if(query.list().size() > 0){
pf = (Profissao) query.list().get(0);
}
session.close();
return pf;
}
public List pesquisarProfissaoTodos(){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {profissao.*} FROM profissao {profissao}");
query.addEntity("profissao", Profissao.class);
List pf = query.list();
session.close();
return pf;
}
public void alterarProfissao(Profissao pf){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
Profissao pf2 = pesquisarProfissaoPorCodigo(pf.getCodProfissao());
pf.setCodProfissao(pf2.getCodProfissao());
session.update(pf);
trans.commit();
session.close();
}
}