Anbulategi.GaixoaDAOHibernate.java Source code

Java tutorial

Introduction

Here is the source code for Anbulategi.GaixoaDAOHibernate.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Anbulategi;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author INAKI
 */
public class GaixoaDAOHibernate implements GaixoaDAO {
    public void save(Gaixoa gaixo) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            session.saveOrUpdate(gaixo);
            session.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            session.getTransaction().rollback();
        }
    }

    @Override
    public void delete(Gaixoa gaixo) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            session.delete(gaixo);
            session.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            session.getTransaction().rollback();
        }

    }

    @Override
    public void edit(Gaixoa gaixo) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            session.update(gaixo);
            session.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            session.getTransaction().rollback();
        }
    }

    @Override
    public Gaixoa getGaixoaByGSZ(int GSZ) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            Gaixoa e = (Gaixoa) session.get(Gaixoa.class, GSZ);
            session.getTransaction().commit();
            return e;
        } catch (Exception ex) {
            ex.printStackTrace();
            session.getTransaction().rollback();
            return new Gaixoa();
        }
    }

    @Override
    public List<Errezeta> lortuErrezetak(int GSZ) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            String hql = "From Errezeta errezeta where gaixoa.GSZ = ?";
            Query kontsulta = session.createQuery(hql).setParameter(0, GSZ);
            List<Errezeta> set = kontsulta.list();
            session.getTransaction().commit();
            return set;
        } catch (Exception ex) {
            ex.printStackTrace();
            session.getTransaction().rollback();
            return null;
        }
    }

    @Override
    public List<bajaTxostena> lortuBajaTxostenak(int GSZ) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            String hql = "From bajaTxostena Baja where baja.GSZ = ?";
            Query kontsulta = session.createQuery(hql).setParameter(0, GSZ);
            List<bajaTxostena> set = kontsulta.list();

            session.getTransaction().commit();
            return set;
        } catch (Exception ex) {
            ex.printStackTrace();
            session.getTransaction().rollback();
            return null;
        }
    }

    @Override
    public boolean gaixoaDago(Gaixoa ga) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            String hql = "From Gaixoa gaixo where gaixo.GSZ = ?";
            Query kontsulta = session.createQuery(hql).setParameter(0, ga.getGSZ());
            List<Gaixoa> lista = kontsulta.list();
            session.getTransaction().commit();
            return !lista.isEmpty();
        } catch (Exception ex) {
            ex.printStackTrace();
            session.getTransaction().rollback();
            return false;
        }
    }

    @Override
    public boolean checkGaixoa(int GSZ, String password) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            String hql = "From Gaixoa gaixo where gaixo.GSZ = ? and gaixo.pasahitza = ?";
            Query kontsulta = session.createQuery(hql).setParameter(0, GSZ).setParameter(1, password);
            List<Gaixoa> lista = kontsulta.list();
            session.getTransaction().commit();
            return !lista.isEmpty();
        } catch (Exception ex) {
            ex.printStackTrace();
            session.getTransaction().rollback();
            return false;
        }
    }

}