Advertisement
WalterTravassos

Untitled

May 31st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. package controller;
  2.  
  3. import java.util.ArrayList;
  4.  
  5.  
  6. import javax.persistence.EntityManager;
  7. import javax.persistence.EntityManagerFactory;
  8. import javax.persistence.Persistence;
  9. import javax.persistence.Query;
  10.  
  11. import model.Pessoa;
  12.  
  13. public class Repositorio {
  14. EntityManagerFactory fabrica;
  15. EntityManager gerente;
  16.  
  17.  
  18.  
  19. public EntityManagerFactory getFabrica() {
  20. return fabrica;
  21. }
  22.  
  23. public void setFabrica(EntityManagerFactory fabrica) {
  24. this.fabrica = fabrica;
  25. }
  26.  
  27. public EntityManager getGerente() {
  28. return gerente;
  29. }
  30.  
  31. public void setGerente(EntityManager gerente) {
  32. this.gerente = gerente;
  33. }
  34.  
  35. public Repositorio() {
  36. fabrica = Persistence.createEntityManagerFactory("bdjava");
  37. gerente = fabrica.createEntityManager();
  38. }
  39.  
  40. public void salvar (Pessoa p) {
  41. System.out.println("iniciando persistencia...");
  42. gerente.getTransaction().begin();
  43. gerente.persist(p);
  44. gerente.getTransaction().commit();
  45. System.out.println("Pessoa persistida");
  46. }
  47.  
  48. public Pessoa obterPessoaPorId(int id) {
  49. gerente.getTransaction().begin();
  50. Pessoa p = gerente.find(Pessoa.class, id);
  51. gerente.getTransaction().commit();
  52. return p;
  53. }
  54.  
  55. //Listar todos as pessoas
  56. public ArrayList<Pessoa> listarTodos() {
  57. gerente.getTransaction().begin();
  58. Query consulta = gerente.createQuery("select pessoa from Pessoa pessoa");
  59. ArrayList<Pessoa> pessoas;
  60. pessoas = (ArrayList<Pessoa>) consulta.getResultList();
  61. gerente.getTransaction().commit();
  62. return pessoas;
  63. }
  64.  
  65. //Salvar ou atualizar pessoa
  66. public void atualizar(Pessoa p) {
  67. gerente.getTransaction().begin();
  68. gerente.merge(p);
  69. gerente.getTransaction().commit();
  70. }
  71.  
  72. //Remover paciente
  73. public void remover(Pessoa p) {
  74. gerente.getTransaction().begin();
  75. gerente.remove(p);
  76. gerente.getTransaction().commit();
  77. }
  78.  
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement