/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package maissocial.repositorio;
import java.util.List;
import maissocial.controle.Uo;
import maissocial.util.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author luciano
*/
public class UoRepositorio {
SessionFactory factory = HibernateUtil.getSessionFactory();
public void salvarUo(Uo uo){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.save(uo);
trans.commit();
session.close();
}
public void deletarUo(int cod){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
Uo uo = pesquisarUoPorCodigo(cod);
session.delete(uo);
trans.commit();
session.close();
}
public void deletarUoPorObj(Uo uo){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.delete(uo);
trans.commit();
session.close();
}
public Uo pesquisarUoPorCodigo(int cod){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {uo.*} FROM uo {uo} WHERE cod_uo = "+cod);
query.addEntity("uo", Uo.class);
Uo uo = null;
if(query.list().size() > 0){
uo= (Uo) query.list().get(0);
}
session.close();
return uo;
}
public Uo pesquisarUoPorNome(String nome){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {uo.*} FROM uo {uo} WHERE nome like '"+nome+"'");
query.addEntity("uo", Uo.class);
Uo uo = null;
if(query.list().size() > 0){
uo = (Uo) query.list().get(0);
}
session.close();
return uo;
}
public List pesquisarUoTodos(){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {uo.*} FROM uo {uo}");
query.addEntity("uo", Uo.class);
List uo = query.list();
session.close();
return uo;
}
public void alterarUo(Uo uo){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
Uo uo2 = pesquisarUoPorCodigo(uo.getCodUo());
uo.setCodUo(uo2.getCodUo());
session.update(uo);
trans.commit();
session.close();
}
}