/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaprimitiva;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Adrian
 */
public class controladorBD {

    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaPrimitivaPU");

    public static void add(Object object) {
        //EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaPrimitivaPU");
        EntityManager em = emf.createEntityManager();
        
        em.getTransaction().begin();
        try {
            em.merge(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public static void setUpDB() {  
        Jugador p = new Jugador();
        add(p);
    }

    public static List getData() {
        //EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaPrimitivaPU");
        return emf.createEntityManager().createQuery("SELECT p FROM Jugador p").getResultList();
    }
}
