/*
 * 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 DAO;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Eduardo
 */
public class GenericoDAO<T extends EntidadeBase> {
    
    public EntityManager getEM() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("SystemAutonetPU");
        return factory.createEntityManager();
    }

    public T salvar(Class<T> clazz , T t) throws Exception {
        EntityManager em = getEM();

        try {
            em.getTransaction().begin();
            if (t.getId() == null) {
                em.persist(t); // executa insert
            } else {
                if (!em.contains(t)) {
                    if (em.find(clazz, t.getId()) == null) {
                        throw new Exception("Erro ao alterar o cadastro.");
                    }
                }
                t = em.merge(t); // executa update
            }
            em.getTransaction().commit();
        } finally {
            em.close();
        }
        return t;
    }

    public void remover(Class<T> clazz,T tt) throws Exception{
        EntityManager em = getEM();

        T t = em.find(clazz, tt.getId());

        try {
            em.getTransaction().begin();
            em.remove(t);  // executa o delete
            em.getTransaction().commit();        
        } finally {
            em.close();
        }
    }
    
    public  T consutarPorId(Class<T> clazz,T tt){
        EntityManager em = getEM();
        T t = null; 
        
        try{
            t = em.find(clazz, tt.getId()); // execulta o select no banco de dados
            
        }finally{
            em.close();
        }
        return t;
    }
}

@@ -199,16 +199,52 @@
 DAO;%0D%0A%0D%0A
+import javafx.scene.control.Alert;%0D%0A
 import j
@@ -362,16 +362,45 @@
 stence;%0D
+%0Aimport utilitarios.Alertas;%0D
 %0A%0D%0A/**%0D%0A
@@ -480,20 +480,16 @@
 ase%3E %7B%0D%0A
-    
 %0D%0A    pu
@@ -547,16 +547,53 @@
 Factory 
+factory;%0D%0A        try %7B%0D%0A            
 factory 
@@ -658,24 +658,28 @@
 );%0D%0A        
+    
 return facto
@@ -701,24 +701,275 @@
 Manager();%0D%0A
+        %7D catch (Exception ex) %7B%0D%0A            Alertas alert = new Alertas();%0D%0A            alert.alerta(Alert.AlertType.ERROR, %22Erro de Conex%C3%A3o com Banco de Dados%22, %22N%C3%A3o foi possivel conectar com o Banco de Dados%22);%0D%0A        %7D%0D%0A        return null;%0D%0A%0D%0A
     %7D%0D%0A%0D%0A   
@@ -999,17 +999,16 @@
 T%3E clazz
- 
 , T t) t
@@ -1559,24 +1559,159 @@
 .commit();%0D%0A
+        %7D catch (Exception ex) %7B%0D%0A            System.out.println(%22Erro ao salvar%22);%0D%0A            System.out.println(ex.getMessage());%0D%0A
         %7D fi
@@ -1818,24 +1818,25 @@
 ss%3CT%3E clazz,
+ 
 T tt) throws
@@ -1845,16 +1845,17 @@
 xception
+ 
 %7B%0D%0A     
@@ -2079,24 +2079,26 @@
 ommit();
+%0D%0A
         
 %0D%0A      
@@ -2081,32 +2081,158 @@
 mit();%0D%0A        
+%7D catch (Exception ex) %7B%0D%0A            System.out.println(%22Erro ao remover%22);%0D%0A            System.out.println(ex.getMessage());
 %0D%0A        %7D fina
@@ -2281,20 +2281,16 @@
 %0A    %7D%0D%0A
-    
 %0D%0A    pu
@@ -2294,17 +2294,16 @@
  public 
- 
 T consut
@@ -2325,21 +2325,23 @@
 %3E clazz,
+ 
 T tt)
+ 
 %7B%0D%0A     
@@ -2391,27 +2391,18 @@
  = null;
- 
 %0D%0A
-        
 %0D%0A      
@@ -2406,16 +2406,17 @@
      try
+ 
 %7B%0D%0A     
@@ -2494,24 +2494,26 @@
  dados%0D%0A
+%0D%0A
         
     %0D%0A  
@@ -2504,20 +2504,144 @@
         
-    
+%7D catch (Exception ex) %7B%0D%0A            System.out.println(%22Erro ao consultar%22);%0D%0A            System.out.println(ex.getMessage());
 %0D%0A      
@@ -2643,23 +2643,25 @@
        %7D
+ 
 finally
+ 
 %7B%0D%0A     

Ajuste na conexão com o banco. Ajustado bug da conexão em rede