Guest User

Untitled

a guest
Jun 27th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.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.Query;
  7.  
  8. import br.com.arena.model.Pessoa;
  9. import br.com.arena.util.JPAUtil;
  10.  
  11. public class PessoaDAO {
  12.  
  13. private EntityManager em;
  14.  
  15.  
  16. public PessoaDAO() {
  17. setEm(JPAUtil.getEntityManager());
  18. }
  19.  
  20. public void cadastrar(Pessoa pessoa){
  21. getEm().getTransaction().begin();
  22. getEm().persist(pessoa);
  23. getEm().getTransaction().commit();
  24. }
  25.  
  26. public void atualizar(Pessoa pessoa){
  27. getEm().getTransaction().begin();
  28. getEm().merge(pessoa);
  29. getEm().getTransaction().commit();
  30. }
  31.  
  32. public List<Pessoa> listaTodasPessoas(){
  33. Query q = em.createQuery("select p from Pessoa p");
  34. List<Pessoa> pessoas = q.getResultList();
  35. return pessoas;
  36. }
  37.  
  38. public void removerPessoa(Pessoa pessoa){
  39. getEm().getTransaction().begin();
  40. getEm().find(Pessoa.class, pessoa.getId());
  41. getEm().remove(pessoa);
  42. getEm().getTransaction().commit();
  43. }
  44.  
  45.  
  46. public EntityManager getEm() {
  47. return em;
  48. }
  49. public void setEm(EntityManager em) {
  50. this.em = em;
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. }
Add Comment
Please, Sign In to add comment