/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package maissocial.repositorio;
import java.util.List;
import maissocial.entidade.Org;
import maissocial.util.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author luciano
*/
public class OrgRepositorio {
SessionFactory factory = HibernateUtil.getSessionFactory();
public void salvarOrg(Org org){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.save(org);
trans.commit();
session.close();
}
public void deletarOrg(int cod){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
Org org = pesquisarOrgPorCodigo(cod);
session.delete(org);
trans.commit();
session.close();
}
public void deletarOrgPorObj(Org org){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
session.delete(org);
trans.commit();
session.close();
}
public Org pesquisarOrgPorCodigo(int cod){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {org.*} FROM org {org} WHERE cod_org = "+cod);
query.addEntity("org", Org.class);
Org org = null;
if(query.list().size() > 0){
org = (Org) query.list().get(0);
}
session.close();
return org;
}
public Org pesquisarOrgPorCnpj(String cnpj){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {org.*} FROM org {org} WHERE cnpj = "+cnpj);
query.addEntity("org", Org.class);
Org org = null;
if(query.list().size() > 0){
org = (Org) query.list().get(0);
}
session.close();
return org;
}
public Org pesquisarOrgPorNome(String nome){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {org.*} FROM org {org} WHERE nome like '"+nome+"'");
query.addEntity("org", Org.class);
Org org = null;
if(query.list().size() > 0){
org = (Org) query.list().get(0);
}
session.close();
return org;
}
public List pesquisarOrgTodos(){
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT {org.*} FROM org {org}");
query.addEntity("org", Org.class);
List org = query.list();
session.close();
return org;
}
public void alterarOrg(Org org){
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
trans.begin();
Org org2 = pesquisarOrgPorCodigo(org.getCodOrg());
org.setCodOrg(org2.getCodOrg());
session.update(org);
trans.commit();
session.close();
}
}