package dao;
import bo.Foraj;
import bo.HibernateUtil;
import daoI.IForajDao;
import java.util.Collections;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
/**
*
* @author oana
*/
public class ForajDao implements IForajDao {
Session session = null;
public void saveOrUpdate(Foraj foraj) {
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
if (foraj.getIdForaj() == null) {
session.save(foraj);
} else {
session.merge(foraj);
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public List getAll(int idAmplasament) {
List<Foraj> foraj = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Foraj as foraj where foraj.amplasament.idAmplasament ='" + idAmplasament + "'");
foraj = (List<Foraj>) q.list();
Collections.reverse(foraj);
} catch (Exception e) {
e.printStackTrace();
}
return foraj;
}
public Foraj getForajByID(int forajID) {
Foraj foraj = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
foraj = (Foraj) session.get(Foraj.class, forajID);
} catch (Exception e) {
e.printStackTrace();
}
return foraj;
}
}