package org.academiadecodigo.bootcamp.fxlogin.utils;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.RollbackException;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class JpaUtils<T> {
    private EntityManagerFactory emf;

    public JpaUtils(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public void save(T o) {
        EntityManager em = emf.createEntityManager();

        try {
            em.getTransaction().begin();

            em.persist(o);

            em.getTransaction().commit();

        } catch (RollbackException e) {

            em.getTransaction().rollback();

        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

}
