package com.marioaliaga;

import com.marioaliaga.modelo.Employee;

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

/**
 * Created by maliaga on 6/8/17.
 */
public class EmployeeTest {

    private static EntityManager manager;
    private static EntityManagerFactory factory;

    public static void main(String ... args){

        factory = Persistence.createEntityManagerFactory("Persistencia");
        manager = factory.createEntityManager();

        insertInitial();
        printAll();

        manager.getTransaction().begin();
        Employee e = manager.find(Employee.class, 1L);
        e.setNombre("RAFAEL");
        e.setApellidos("ALIAGA");
        manager.getTransaction().commit();

        printAll();

    }

    private static void insertInitial() {
        Employee e = new Employee(1L, "Aliaga", "Mario", new GregorianCalendar(1979, 11, 7).getTime());

        manager.getTransaction().begin();
        manager.persist(e);
        manager.getTransaction().commit();
    }

    public static void printAll(){
        List<Employee> employeeList = manager.createQuery("FROM Employee").getResultList();

        System.out.println("La cantidad de empleados es : " + employeeList.size());

        for (Employee e :
                employeeList) {
            System.out.println(e.toString());
        }
    }
}

@@ -351,37 +351,45 @@
 ager
- manager;%0A    private static 
+Factory factory  = Persistence.create
 Enti
@@ -404,24 +404,32 @@
 rFactory
- factory
+(%22Persistencia%22)
 ;%0A%0A    p
@@ -481,82 +481,21 @@
     
- factory = Persistence.createEntityManagerFactory(%22Persistencia%22);%0A       
+EntityManager
  man
@@ -800,24 +800,49 @@
  printAll();
+%0A        manager.close();
 %0A%0A    %7D%0A%0A   
@@ -876,24 +876,88 @@
 Initial() %7B%0A
+        EntityManager manager = factory.createEntityManager();%0A%0A
         Empl
@@ -1158,24 +1158,50 @@
 ).commit();%0A
+        manager.close();%0A%0A
     %7D%0A%0A    p
@@ -1230,16 +1230,80 @@
 tAll()%7B%0A
+        EntityManager manager = factory.createEntityManager();%0A%0A
         
@@ -1585,16 +1585,42 @@
        %7D
+%0A%0A        manager.close();
 %0A    %7D%0A%7D

Refactor EntityManager EmployeeTest