com.cfs.controller.AuthoritiesDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.cfs.controller.AuthoritiesDAO.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.cfs.controller;

import com.cfs.model.Authorities;
import com.cfs.util.HibernateUtil;
import com.cfs.util.Utilities;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author cfsbs_000
 */
public class AuthoritiesDAO extends GenericDAO implements Serializable {

    public List<Authorities> listaAuthorities() throws Exception {
        List<Authorities> lista = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Authorities.class);
            criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            lista = criteria.list();
            //System.out.println(Utilities.getInstance().getLineNumber() + "RESULTADO DO PRIMEIRO - "+lista.get(1).getAuthority());
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        } finally {
            session.close();
        }
        return lista;
    }

    public Authorities buscaAuthorities(String authority) throws Exception {
        Authorities retorno = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Authorities.class);
            criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            criteria.add(Restrictions.like("authority", authority));
            if (criteria.list().size() > 1) {
                System.out.println(Utilities.getInstance().getLineNumber() + "[buscaAuthorities (" + authority
                        + ")][ERRO] criteria.list().size() = " + criteria.list().size());
            }
            retorno = (Authorities) criteria.uniqueResult();
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        } finally {
            session.close();
        }
        return retorno;
    }
}