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 com.bolao.persistencia.dao; import com.bolao.persistencia.connection.HibernateFactory; import com.bolao.persistencia.entidades.Apostador; import com.bolao.web.uteis.ConversorData; 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 ApostadorDAO { List<Apostador> lista = new ArrayList<>(); ConversorData conversorData = new ConversorData(); public String persist(Apostador apostador) { try { Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); session.persist(apostador); session.getTransaction().commit(); session.close(); } catch (Exception e) { e.printStackTrace(); return HibernateFactory.FALHA; } return HibernateFactory.SUCESSO; } public String refresh(Apostador apostador) { try { Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); session.merge(apostador); session.getTransaction().commit(); session.close(); } catch (Exception e) { return HibernateFactory.FALHA; } return HibernateFactory.SUCESSO; } public String delete(Apostador apostador) { try { Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); session.delete(apostador); session.getTransaction().commit(); session.close(); } catch (Exception e) { return HibernateFactory.FALHA; } return HibernateFactory.SUCESSO; } public List<Apostador> list(ModoConsulta mc, Object object) { Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); if (mc == ModoConsulta.DEFAULT || object == null) { lista.clear(); Query query = session.createQuery(" from Apostador"); lista = query.list(); } else if (mc == ModoConsulta.NOME_APOSTADOR && object != null) { lista.clear(); Query query = session.createQuery(" from Apostador where nome like :XNOME"); query.setString("XNOME", "%" + String.valueOf(object) + "%"); lista = query.list(); } else if (mc == ModoConsulta.DATA_NASC && object != null) { lista.clear(); Query query = session.createQuery(" from Apostador where datanascimento =:XDATA"); query.setDate("XDATA", conversorData.converterData(String.valueOf(object))); lista = query.list(); } else if (mc == ModoConsulta.EMAIL && object != null) { lista.clear(); Query query = session.createQuery(" from Apostador where email like :XEMAIL"); query.setString("XEMAIL", "%" + String.valueOf(object) + "%"); lista = query.list(); } else if (mc == ModoConsulta.APELIDO && object != null) { lista.clear(); Query query = session.createQuery(" from Apostador where apelido like :XAPELIDO"); query.setString("XAPELIDO", "%" + String.valueOf(object) + "%"); lista = query.list(); } session.getTransaction().commit(); session.close(); return lista; } }