rafaelvicio

Untitled

Jun 30th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. package br.com.arena.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.persistence.EntityManager;
  6. import javax.persistence.NoResultException;
  7. import javax.persistence.Query;
  8. import javax.persistence.TypedQuery;
  9.  
  10. import br.com.arena.model.Pessoa;
  11. import br.com.arena.util.JPAUtil;
  12.  
  13. public class PessoaDAO {
  14.  
  15. private EntityManager em;
  16.  
  17.  
  18. public PessoaDAO() {
  19. setEm(JPAUtil.getEntityManager());
  20. }
  21.  
  22. public void cadastrar(Pessoa pessoa){
  23. getEm().getTransaction().begin();
  24. getEm().persist(pessoa);
  25. getEm().getTransaction().commit();
  26. }
  27.  
  28. public void atualizar(Pessoa pessoa){
  29. getEm().getTransaction().begin();
  30. getEm().merge(pessoa);
  31. getEm().getTransaction().commit();
  32. }
  33.  
  34. public List<Pessoa> listaTodasPessoas(){
  35. Query q = em.createQuery("select p from Pessoa p");
  36. List<Pessoa> pessoas = q.getResultList();
  37. return pessoas;
  38. }
  39.  
  40.  
  41. /* public Pessoa validaLogin(String usuario){
  42. try {
  43. String consulta = "select p from Pessoa p where p.nomeUsuario = '"+usuario+"'";
  44. TypedQuery<Pessoa> query=getEm().createQuery(consulta,Pessoa.class);
  45. Pessoa pessoa = (Pessoa)query.getResultList();
  46. return pessoa;
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. return null;
  50. }
  51.  
  52. }*/
  53.  
  54. /* public boolean verificaUsuarioSenha(String nome, String senha){
  55. Query q = em.createQuery("select p from Pessoa p where p.nomeUsuario='"+nome+"' and p.senhaUsuario='"+senha+"'");
  56. Pessoa pessoa = (Pessoa) q.getSingleResult();
  57. System.out.println("-------------------------------"+pessoa.toString());
  58. if(!pessoa.equals(null)){
  59. return true;
  60. }
  61. return false;
  62. }*/
  63.  
  64. /* public Pessoa getPessoa(String nomeUsuario, String senhaUsuario) {
  65.  
  66. try {
  67. Pessoa pessoa = (Pessoa) em
  68. .createQuery(
  69. "SELECT u from Pessoa u where u.nomeUsuario = :name and u.senhaUsuario = :senha")
  70. .setParameter("name", nomeUsuario)
  71. .setParameter("senha", senhaUsuario).getSingleResult();
  72.  
  73. return pessoa;
  74. } catch (NoResultException e) {
  75. return null;
  76. }
  77. }*/
  78.  
  79. public boolean existe(Pessoa pessoa) {
  80.  
  81. String consulta = "select u from Usuario u where u.email = :pEmail and u.senha = :pSenha";
  82. TypedQuery<Pessoa> query=getEm().createQuery(consulta,Pessoa.class);
  83. // TypedQuery<Pessoa> query = em.createQuery("select u from Usuario u where u.email = :pEmail and u.senha = :pSenha",Pessoa.class);
  84.  
  85. query.setParameter("pEmail", pessoa.getNomeUsuario());
  86. query.setParameter("pSenha", pessoa.getSenhaUsuario());
  87.  
  88. Pessoa resultado = query.getSingleResult();
  89.  
  90. return resultado != null;
  91. }
  92.  
  93. public void removerPessoa(Pessoa pessoa){
  94. getEm().getTransaction().begin();
  95. getEm().find(Pessoa.class, pessoa.getId());
  96. getEm().remove(pessoa);
  97. getEm().getTransaction().commit();
  98. }
  99.  
  100.  
  101. public EntityManager getEm() {
  102. return em;
  103. }
  104. public void setEm(EntityManager em) {
  105. this.em = em;
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112. }
Add Comment
Please, Sign In to add comment