com.cfs.controller.UsersDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.cfs.controller.UsersDAO.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.Users;
import com.cfs.util.HibernateUtil;
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 UsersDAO extends GenericDAO implements Serializable {

    private Users usuario = new Users();

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

    public List<Users> usuarioPorNome(String nome) throws Exception {
        List<Users> lista = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Users.class);
            criteria.add(Restrictions.ilike("nome", "%" + nome + "%"));
            //criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            lista = criteria.list();
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        } finally {
            session.close();
        }
        return lista;
    }

    public Users buscaUsuarioPorUSERNAME(String username) {
        usuario = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Users.class);
            criteria.add(Restrictions.eq("username", username));
            criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            usuario = (Users) criteria.uniqueResult();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return usuario;
    }
}