/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import daoI.IGeologBeneficiarDao;
import bo.HibernateUtil;
import bo.GeologBeneficiar;
import java.util.Collections;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class GeologBeneficiarDao implements IGeologBeneficiarDao {
Session session = null;
public List getGeologBeneficiariByGeologId(int geologID) {
List<Integer> geologBeneficiari = null;
try {
session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("select distinct geoBeneficiari.idBeneficiar from GeologBeneficiar as geoBeneficiari where geoBeneficiari.idGeolog = '" + geologID + "'");
geologBeneficiari = (List<Integer>) q.list();
Collections.reverse(geologBeneficiari);
} catch (Exception e) {
e.printStackTrace();
}
return geologBeneficiari;
}
public void saveOrUpdate(GeologBeneficiar geologBeneficiar) {
try {
session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
if (geologBeneficiar.getIdGeologBeneficiar() == null) {
session.save(geologBeneficiar);
} else {
session.merge(geologBeneficiar);
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete(int idGeolog, int idBeneficiar) {
List<GeologBeneficiar> list = null;
GeologBeneficiar gb = null;
try {
session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from GeologBeneficiar as lang where lang.idGeolog='" + idGeolog + "'and lang.idBeneficiar='" + idBeneficiar + "' ");
list = (List<GeologBeneficiar>) q.list();
gb = ((GeologBeneficiar) list.get(0));
session.delete(gb);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}