/**
 * 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\n");
            em.getTransaction().rollback();
            throw  exp;
        } catch (TransactionRequiredException exp) {
            System.out.println("\n\nTransaction ERROR! --> " + exp.getMessage() + "\n\n");
            em.getTransaction().rollback();
            throw  exp;
        } catch (PersistenceException exp){
            System.out.println("\n\nPersistence ERROR! --> " + exp.getMessage() + "\n\n");
            em.getTransaction().rollback();
            throw  exp;
        } catch (Exception exp){
            System.out.println("\n\nGeneral ERROR! --> " + exp.getMessage() + "\n\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\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (TransactionRequiredException exp) {
            System.out.println("\n\nTransaction ERROR! --> " + exp.getMessage() + "\n\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (PersistenceException exp) {
            System.out.println("\n\nPersistence ERROR! --> " + exp.getMessage() + "\n\n");
            em.getTransaction().rollback();
            throw exp;
        } catch (Exception exp) {
            System.out.println("\n\nGeneral ERROR! --> " + exp.getMessage() + "\n\n");
            em.getTransaction().rollback();
            throw exp;
        } finally {
            em.close();
        }
    }
    
}

@@ -941,38 +941,36 @@
 tMessage() + %22%5Cn
-%5Cn
 %22);%0A
+
             em.g
@@ -1151,34 +1151,32 @@
 tMessage() + %22%5Cn
-%5Cn
 %22);%0A            
@@ -1352,34 +1352,32 @@
 tMessage() + %22%5Cn
-%5Cn
 %22);%0A            
@@ -1538,34 +1538,32 @@
 tMessage() + %22%5Cn
-%5Cn
 %22);%0A            
@@ -2006,34 +2006,32 @@
 tMessage() + %22%5Cn
-%5Cn
 %22);%0A            
@@ -2215,34 +2215,32 @@
 tMessage() + %22%5Cn
-%5Cn
 %22);%0A            
@@ -2416,34 +2416,32 @@
 tMessage() + %22%5Cn
-%5Cn
 %22);%0A            
@@ -2610,18 +2610,16 @@
 () + %22%5Cn
-%5Cn
 %22);%0A    
@@ -2741,15 +2741,1130 @@
 %7D%0A    %7D%0A
+%0A
     
+public void Delete(T entity)%7B%0A%0A        EntityManager em = GetEntityManager();%0A%0A        em.getTransaction().begin();%0A%0A        try %7B%0A            %0A            em.remove(em.contains(entity) ? entity : em.merge(entity));%0A            em.getTransaction().commit();%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 (TransactionRequiredException exp) %7B%0A            System.out.println(%22%5Cn%5CnTransaction 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 Delete function