/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package maissocial.repositorio;
import java.util.List;
import maissocial.entidade.GrupoUs;
import maissocial.util.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author luciano
*/
public class GrupoUsRepositorio {
SessionFactory factory = HibernateUtil.getSessionFactory();
public void salvarGrupoUs(GrupoUs gu){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.save(gu);
trans.commit();
session.close();
}
public void deletarGrupoUs(int cod){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
GrupoUs gu = pesquisarGrupoUsPorCodigo(cod);
session.delete(gu);
trans.commit();
session.close();
}
public void deletarGrupoUsPorObj(GrupoUs gu){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.delete(gu);
trans.commit();
session.close();
}
public GrupoUs pesquisarGrupoUsPorCodigo(int cod){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {grupo_us.*} FROM grupo_us {grupo_us} WHERE cod_grupo_us = "+cod);
query.addEntity("grupo_us", GrupoUs.class);
GrupoUs gu = null;
if(query.list().size() > 0){
gu = (GrupoUs) query.list().get(0);
}
session.close();
return gu;
}
public GrupoUs pesquisarGrupoUsPorDescricao(String desc){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {grupo_us.*} FROM grupo_us {grupo_us} WHERE descricao like '"+desc+"'");
query.addEntity("grupo_us", GrupoUs.class);
GrupoUs gu = null;
if(query.list().size() > 0){
gu = (GrupoUs) query.list().get(0);
}
session.close();
return gu;
}
public List pesquisarGrupoUsTodos(){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {grupo_us.*} FROM grupo_us {grupo_us}");
query.addEntity("grupo_us", GrupoUs.class);
List gu = query.list();
session.close();
return gu;
}
public void alterarGrupoUs(GrupoUs gu){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
GrupoUs gu2 = pesquisarGrupoUsPorCodigo(gu.getCodGrupoUs());
gu.setCodGrupoUs(gu2.getCodGrupoUs());
session.update(gu);
trans.commit();
session.close();
}
}