/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package maissocial.repositorio;
import java.util.List;
import maissocial.controle.Uss;
import maissocial.util.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author luciano
*/
public class UssRepositorio {
SessionFactory factory = HibernateUtil.getSessionFactory();
public void salvarUss(Uss uss){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.save(uss);
trans.commit();
session.close();
}
public void deletarUss(int cod){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
Uss uss = pesquisarUssPorCodigo(cod);
session.delete(uss);
trans.commit();
session.close();
}
public void deletarUssPorObj(Uss uss){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.delete(uss);
trans.commit();
session.close();
}
public Uss pesquisarUssPorCodigo(int cod){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {uss.*} FROM uss {uss} WHERE cod_uss = "+cod);
query.addEntity("uss", Uss.class);
Uss uss = null;
if(query.list().size() > 0){
uss = (Uss) query.list().get(0);
}
session.close();
return uss;
}
public Uss pesquisarUssPorNome(String nome){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {uss.*} FROM uss {uss} WHERE nome like '"+nome+"'");
query.addEntity("uss", Uss.class);
Uss uss = null;
if(query.list().size() > 0){
uss = (Uss) query.list().get(0);
}
session.close();
return uss;
}
public Uss pesquisarUssPorCpf(String cpf){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {uss.*} FROM uss {uss} WHERE cpf_uss = "+cpf);
query.addEntity("uss", Uss.class);
Uss uss = null;
if(query.list().size() > 0){
uss = (Uss) query.list().get(0);
}
session.close();
return uss;
}
public List pesquisarUssTodos(){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {uss.*} FROM uss {uss}");
query.addEntity("uss", Uss.class);
List uss = query.list();
session.close();
return uss;
}
public void alterarUss(Uss uss){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
Uss uss2 = pesquisarUssPorCodigo(uss.getCodUss());
uss.setCodUss(uss2.getCodUss());
session.update(uss);
trans.commit();
session.close();
}
}