Babysu.Dao.CompraBabysuDao.java Source code

Java tutorial

Introduction

Here is the source code for Babysu.Dao.CompraBabysuDao.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 Babysu.Dao;

import Babysu.Entidades.CompraBabysu;
import Hibernate.HibernateUtil;
import Interacao.Alertas;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author BRUNO
 */
public class CompraBabysuDao {

    private final Alertas alerta = new Alertas();
    private Session sessao;
    private Transaction trans;

    public void addCompra(CompraBabysu cb) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            trans = sessao.beginTransaction();

            sessao.save(cb);
            trans.commit();
            alerta.salvo();
        } catch (Exception e) {
            alerta.daoMsg(e);
        } finally {
            sessao.close();
        }
    }

    public void atualizarCompra(CompraBabysu cb) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            trans = sessao.beginTransaction();

            sessao.update(cb);
            trans.commit();
            alerta.update();
        } catch (Exception e) {
            alerta.daoMsg(e);
        } finally {
            sessao.close();
        }
    }

    public void removerCompra(CompraBabysu cb) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            trans = sessao.beginTransaction();

            sessao.delete(cb);
            trans.commit();
            alerta.update();
        } catch (Exception e) {
            alerta.daoMsg(e);
        } finally {
            sessao.close();
        }
    }

    public List<CompraBabysu> consulta() {
        sessao = HibernateUtil.getSessionFactory().openSession();
        List<CompraBabysu> lista = new ArrayList<>();
        try {
            trans = sessao.beginTransaction();
            Criteria c = sessao.createCriteria(CompraBabysu.class);
            lista = c.list();
        } catch (Exception e) {
            alerta.daoMsg(e);
        } finally {
            sessao.close();
        }

        return lista;
    }

    public CompraBabysu consultaId(long id) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        List<CompraBabysu> list = new ArrayList<>();
        CompraBabysu cb = new CompraBabysu();
        try {
            trans = sessao.beginTransaction();
            Criteria c = sessao.createCriteria(CompraBabysu.class);
            list = c.list();

            for (CompraBabysu compra : list) {
                if (compra.getId() == id) {
                    cb = compra;
                }
            }

        } catch (Exception e) {
            alerta.daoMsg(e);
        } finally {
            sessao.close();
        }

        return cb;
    }
}