/**
 * Created by Siclait on 19/6/16.
 */

package Service;

import org.hibernate.QueryException;

import javax.persistence.*;
import javax.persistence.criteria.CriteriaQuery;

public class GenericORMService<T> {

    private static  EntityManagerFactory emf;
    private Class<T> classEntity;

    // Consturctor
    public GenericORMService(Class<T> classEntity){

        if(emf == null)
            emf = Persistence.createEntityManagerFactory("PersistenceUnit");

        this.classEntity = classEntity;
    }

    public EntityManager GetEntityManager(){ return emf.createEntityManager(); }

    // Class Methods
    public void Create(T entity){

        EntityManager em = GetEntityManager();

        em.getTransaction().begin();

        try{

            em.persist(entity);
            em.getTransaction().commit();

        } catch (EntityNotFoundException exp){
            System.out.println("\n\nEntity ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw  exp;
        } catch (TransactionRequiredException exp) {
            System.out.println("\n\nTransaction ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw  exp;
        } catch (PersistenceException exp){
            System.out.println("\n\nPersistence ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw  exp;
        } catch (Exception exp){
            System.out.println("\n\nGeneral ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw  exp;
        } finally{
            em.close();
        }
    }

    public void Edit(T entity) {

        EntityManager em = GetEntityManager();

        em.getTransaction().begin();

        try {

            em.merge(entity);
            em.getTransaction().commit();

        } catch (EntityNotFoundException exp) {
            System.out.println("\n\nEntity ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (TransactionRequiredException exp) {
            System.out.println("\n\nTransaction ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (PersistenceException exp) {
            System.out.println("\n\nPersistence ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (Exception exp) {
            System.out.println("\n\nGeneral ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw exp;
        } finally {
            em.close();
        }
    }

    public void Delete(T entity){

        EntityManager em = GetEntityManager();

        em.getTransaction().begin();

        try {

            em.remove(em.contains(entity) ? entity : em.merge(entity));
            em.getTransaction().commit();

        } catch (EntityNotFoundException exp) {
            System.out.println("\n\nEntity ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (TransactionRequiredException exp) {
            System.out.println("\n\nTransaction ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (PersistenceException exp) {
            System.out.println("\n\nPersistence ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (Exception exp) {
            System.out.println("\n\nGeneral ERROR! --> " + exp.getMessage() +  "\n");
            em.getTransaction().rollback();
            throw exp;
        } finally {
            em.close();
        }
    }

    // Can only be sent primary keys
    public T Find(Object id) {

        EntityManager em = GetEntityManager();

        try {

            return em.find(classEntity, id);

        } catch (EntityNotFoundException exp) {
            System.out.println("\n\nEntity ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (PersistenceException exp) {
            System.out.println("\n\nPersistence ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (Exception exp) {
            System.out.println("\n\nGeneral ERROR! --> " + exp.getMessage() + "\n");
            em.getTransaction().rollback();
            throw exp;
        } finally {
            em.close();
        }
    }
}

@@ -168,16 +168,39 @@
 iaQuery;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -4696,11 +4696,1376 @@
  %7D%0A    %7D
+%0A%0A    public List%3CT%3E FindAll() %7B%0A%0A        EntityManager em = GetEntityManager();%0A%0A        try %7B%0A%0A            CriteriaQuery%3CT%3E criteriaQuery = em.getCriteriaBuilder().createQuery(classEntity);%0A            criteriaQuery.select(criteriaQuery.from(classEntity));%0A%0A            return em.createQuery(criteriaQuery).getResultList();%0A%0A        %7D catch (EntityNotFoundException exp) %7B%0A            System.out.println(%22%5Cn%5CnEntity ERROR! --%3E %22 + exp.getMessage() + %22%5Cn%22);%0A            em.getTransaction().rollback();%0A            throw exp;%0A        %7D catch (QueryTimeoutException exp) %7B%0A            System.out.println(%22%5Cn%5CnQuery Timeout ERROR! --%3E %22 + exp.getMessage() + %22%5Cn%22);%0A            em.getTransaction().rollback();%0A            throw exp;%0A        %7Dcatch (QueryException exp) %7B%0A            System.out.println(%22%5Cn%5CnQuery ERROR! --%3E %22 + exp.getMessage() + %22%5Cn%22);%0A            em.getTransaction().rollback();%0A            throw exp;%0A        %7D catch ( PersistenceException exp) %7B%0A            System.out.println(%22%5Cn%5CnPersistence ERROR! --%3E %22 + exp.getMessage() + %22%5Cn%22);%0A            em.getTransaction().rollback();%0A            throw exp;%0A        %7D catch (Exception exp) %7B%0A            System.out.println(%22%5Cn%5CnGeneral ERROR! --%3E %22 + exp.getMessage() + %22%5Cn%22);%0A            em.getTransaction().rollback();%0A            throw exp;%0A        %7D finally %7B%0A            em.close();%0A        %7D%0A    %7D
 %0A%7D%0A

Add FindAll function