Java tutorial
/* * 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 dao; import dominio.Aluno; import java.util.ArrayList; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import util.HibernateSessionFactory; /** * * @author weberton */ public class AlunoJPADAo implements Persistencia<Aluno> { @Override public void gravar(Aluno objeto) throws BancoDadosException { Session session = HibernateSessionFactory.getSession(); session.beginTransaction().begin(); session.persist(objeto); session.beginTransaction().commit(); session.close(); } @Override public ArrayList<Aluno> getTodosAlunos() throws BancoDadosException { String hql = "from Aluno a order by a.nome"; Session session = HibernateSessionFactory.getSession(); Query query = session.createQuery(hql); ArrayList<Aluno> alunos = (ArrayList<Aluno>) query.list(); session.close(); return alunos; } @Override public ArrayList<Aluno> consultarPorCPF(String cpf) throws BancoDadosException { String hql = "select a from Aluno a where a.cpf = :cpf"; Session session = HibernateSessionFactory.getSession(); Query query = session.createQuery(hql); query.setParameter("cpf", cpf); ArrayList<Aluno> alunos = (ArrayList<Aluno>) query.list(); session.close(); return alunos; } }