package br.com.rlsystem.DAO;

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

import org.hibernate.Session;
import org.hibernate.jpa.internal.EntityManagerImpl;

public class DAO {
	
	private EntityManagerFactory emf;
	
	public EntityManager getEntityManager() {
		return emf.createEntityManager();
	}
	
	public DAO() {
		emf = Persistence.createEntityManagerFactory("sistema");
	}
	
	public Session getSession() {
		Session session = null;
		
		if ( getEntityManager().getDelegate() instanceof EntityManagerImpl ) {
			EntityManagerImpl entityManagerImp = (EntityManagerImpl) getEntityManager();
		    return entityManagerImp.getSession();
		} else {
			return (Session) getEntityManager().getDelegate();
		}
	}
	

}
