Advertisement
Figureight

OpcoesJpaDAO

Sep 5th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Model;
  7.  
  8. import java.util.List;
  9. import javax.persistence.EntityManager;
  10. import javax.persistence.EntityManagerFactory;
  11. import javax.persistence.Persistence;
  12.  
  13. /**
  14.  *
  15.  * @author Ismae
  16.  */
  17. public class OpcoesJpaDAO {
  18.     private static OpcoesJpaDAO instance;
  19.     protected EntityManager entityManager;
  20.    
  21.     public static OpcoesJpaDAO getInstance(){
  22.         if(instance == null){
  23.             instance = new OpcoesJpaDAO();
  24.         }
  25.         return instance;
  26.     }
  27.    
  28.     public OpcoesJpaDAO(){
  29.         entityManager = getEntityManager();
  30.     }
  31.    
  32.     private EntityManager getEntityManager(){
  33.         EntityManagerFactory factory = Persistence.createEntityManagerFactory("ConcurseiroPU");
  34.         if(entityManager == null){
  35.             entityManager = factory.createEntityManager();
  36.         }
  37.         return entityManager;
  38.     }
  39.     //Talvez não funcione por se tratar de uma função sql
  40.     public Opcoes getByID(final int id){
  41.         return entityManager.find(Opcoes.class, id);
  42.     }
  43.    
  44.     @SupressWarnings("unchecked")
  45.    
  46.     public List<Opcoes> findAll(){
  47.         return entityManager.createQuery("FROM" + Opcoes.class.getName()).getResultList();
  48.     }
  49.    
  50.     public void persist(Opcoes opcoes){
  51.         try{
  52.            entityManager.getTransaction().begin();
  53.            entityManager.persist(opcoes);
  54.            entityManager.getTransaction().commit();
  55.         }catch(Exception ex){
  56.             ex.printStackTrace();
  57.             entityManager.getTransaction().rollback();
  58.         }
  59.     }
  60.        
  61.     public void merge(Opcoes opcoes){
  62.         try{
  63.             entityManager.getTransaction().begin();
  64.             entityManager.merge(opcoes);
  65.             entityManager.getTransaction().commit();
  66.         }catch(Exception ex){
  67.             ex.printStackTrace();
  68.             entityManager.getTransaction().rollback();
  69.         }
  70.     }
  71.    
  72.     public void remove(Opcoes opcoes){
  73.         try{
  74.             entityManager.getTransaction().begin();
  75.             entityManager.find(Opcoes.class, opcoes.getId());
  76.             entityManager.getTransaction().commit();
  77.         }catch(Exception ex){
  78.             ex.printStackTrace();
  79.             entityManager.getTransaction().rollback();
  80.         }
  81.     }
  82.    
  83.     public void removeById(final int id){
  84.         try{
  85.             Opcoes opcoes;
  86.             opcoes = getByID(id);
  87.             remove(opcoes);
  88.         }catch(Exception ex){
  89.             ex.printStackTrace();
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement