com.bolao.persistencia.dao.GrupoDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.bolao.persistencia.dao.GrupoDAO.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 com.bolao.persistencia.dao;

import com.bolao.persistencia.connection.HibernateFactory;
import com.bolao.persistencia.entidades.Grupo;
import com.bolao.web.uteis.ModoConsulta;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Diovani B da Motta
 */
public class GrupoDAO {

    List<Grupo> lista = new ArrayList<>();

    public String persist(Grupo grupo) {
        try {
            Session session = HibernateFactory.getSessionFactory();
            session.beginTransaction();
            session.persist(grupo);
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            return HibernateFactory.FALHA;
        }
        return HibernateFactory.SUCESSO;
    }

    public String refresh(Grupo grupo) {
        try {
            Session session = HibernateFactory.getSessionFactory();
            session.beginTransaction();
            session.merge(grupo);
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            return HibernateFactory.FALHA;
        }
        return HibernateFactory.SUCESSO;
    }

    public String delete(Grupo grupo) {
        try {
            Session session = HibernateFactory.getSessionFactory();
            session.beginTransaction();
            session.delete(grupo);
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            return HibernateFactory.FALHA;
        }
        return HibernateFactory.SUCESSO;
    }

    public List<Grupo> list(ModoConsulta mc, Object object) {
        Session session = HibernateFactory.getSessionFactory();
        session.beginTransaction();
        if (mc == ModoConsulta.DEFAULT || object == null) {
            lista.clear();
            Query query = session.createQuery(" from Grupo");
            lista = query.list();
        } else if (mc == ModoConsulta.CODIGO && object != null) {
            lista.clear();
            Query query = session.createQuery(" from Grupo where codigo =:XCODIGO");
            query.setInteger("XCODIGO", Integer.parseInt(String.valueOf(object)));
            lista = query.list();
        } else if (mc == ModoConsulta.GRUPO && object != null) {
            lista.clear();
            Query query = session.createQuery(" from Grupo where descricao like :XDESCRICAO");
            query.setString("XDESCRICAO", "%" + String.valueOf(object) + "%");
            lista = query.list();
        }
        session.getTransaction().commit();
        session.close();
        return lista;
    }
}