Package dao

Source Code of dao.ProiectDao

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao;

import bo.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import java.util.List;
import daoI.IProiectDao;
import bo.Proiect;

public class ProiectDao implements IProiectDao {

    Session session = null;

    public Proiect getProiectByID(int proiectID) {

        Proiect proiect = null;
        try {
            this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            proiect = (Proiect) session.get(Proiect.class, proiectID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proiect;
    }

    public List<Proiect> getProiecteByGeologId(int geologID) {
        List<Proiect> proiecte = null;
        try {
            this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Proiect as proiect where proiect.idGeolog = '" + geologID + "'");
            proiecte = (List<Proiect>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proiecte;
    }

    public List<Proiect> getProiecteByGeologIdAndBeneficiarId(int geologID, int beneficiarID) {
        List<Proiect> proiecte = null;
        try {
            this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Proiect as proiect where proiect.idGeolog = '" + geologID + "'and proiect.idBeneficiar='" + beneficiarID + "'");
            proiecte = (List<Proiect>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proiecte;
    }

    public Proiect getUltimulProiectAdaugat(int geologID) {
        List<Proiect> proiecte = null;
        Proiect proiect = null;
        try {
            this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Proiect as proiect where proiect.idGeolog = '" + geologID + "'");
            proiecte = (List<Proiect>) q.list();
            //Collections.reverse(proiecte);
            proiect = (Proiect) proiecte.get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proiect;
    }

    public void saveOrUpdate(Proiect proiect) {
        try {
            this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            if (proiect.getIdProiect() == null) {
                session.save(proiect);
            } else {
                session.merge(proiect);
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delete(int idProiect) {
        List<Proiect> list = null;
        Proiect p = null;
        try {
            this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Proiect as lang where lang.idProiect='" + idProiect + "' ");
            list = (List<Proiect>) q.list();
            p = ((Proiect) list.get(0));
            session.delete(p);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
TOP

Related Classes of dao.ProiectDao

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.