/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package maissocial.repositorio;
import java.util.List;
import maissocial.entidade.GrauEscolar;
import maissocial.util.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author luciano
*/
public class GrauEscolarRepositorio {
SessionFactory factory = HibernateUtil.getSessionFactory();
public void salvarGrauEscolar(GrauEscolar ge){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.save(ge);
trans.commit();
session.close();
}
public void deletarGrauEscolar(int cod){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
GrauEscolar ge = pesquisarGrauEscolarPorCodigo(cod);
session.delete(ge);
trans.commit();
session.close();
}
public void deletarGrauEscolarPorObj(GrauEscolar ge){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.delete(ge);
trans.commit();
session.close();
}
public GrauEscolar pesquisarGrauEscolarPorCodigo(int cod){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {grau_escolar.*} FROM grau_escolar {grau_escolar} WHERE cod_grau_escolar = "+cod);
query.addEntity("grau_escolar", GrauEscolar.class);
GrauEscolar ge = null;
if(query.list().size() > 0){
ge = (GrauEscolar) query.list().get(0);
}
session.close();
return ge;
}
public GrauEscolar pesquisarGrauEscolarPorDescricao(String desc){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {grau_escolar.*} FROM grau_escolar {grau_escolar} WHERE descricao like '"+desc+"'");
query.addEntity("grau_escolar", GrauEscolar.class);
GrauEscolar ge = null;
if(query.list().size() > 0){
ge = (GrauEscolar) query.list().get(0);
}
session.close();
return ge;
}
public List pesquisarGrauEscolarTodos(){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {grau_escolar.*} FROM grau_escolar {grau_escolar}");
query.addEntity("grau_escolar", GrauEscolar.class);
List ge = query.list();
session.close();
return ge;
}
public void alterarGrauEscolar(GrauEscolar ge){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
GrauEscolar ge2 = pesquisarGrauEscolarPorCodigo(ge.getCodGrauEscolar());
ge.setCodGrauEscolar(ge2.getCodGrauEscolar());
session.update(ge);
trans.commit();
session.close();
}
}