Example usage for javax.persistence EntityManagerFactory close

List of usage examples for javax.persistence EntityManagerFactory close

Introduction

In this page you can find the example usage for javax.persistence EntityManagerFactory close.

Prototype

public void close();

Source Link

Document

Close the factory, releasing any resources that it holds.

Usage

From source file:Professor.java

public static void main(String[] a) throws Exception {
        JPAUtil util = new JPAUtil();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
        EntityManager em = emf.createEntityManager();
        ProfessorService service = new ProfessorService(em);

        em.getTransaction().begin();/*  w  ww .  j  ava 2s  . c o m*/

        Professor emp = service.createProfessor(1, "name", 100);
        emp = service.createProfessor(2, "name 2", 100);

        Integer userId = 1;

        emp = em.find(Professor.class, userId);
        if (emp == null) {
            System.out.println(userId + " not found! ");
        } else {
            System.out.println("Found " + emp);
        }

        for (Object o : em.createQuery("SELECT u FROM Professor u").getResultList()) {
            System.out.println(o);
        }

        util.checkData("select * from Professor");

        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:AbstractProfessor.java

public static void main(String[] a) throws Exception {
        JPAUtil util = new JPAUtil();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
        EntityManager em = emf.createEntityManager();
        ProfessorService service = new ProfessorService(em);

        em.getTransaction().begin();/*  w w w  .j  ava 2  s.  c  o m*/

        GoodProfessor emp = null;
        emp = new GoodProfessor();
        emp.setId(1);
        emp.setName("good");

        service.createProfessor(emp);

        BadProfessor emp2 = new BadProfessor();
        emp2.setId(2);

        emp2.setName("bad");

        service.createProfessor(emp2);

        System.out.println("Professors: ");
        for (Professor emp1 : service.findAllProfessors()) {
            System.out.print(emp1);
        }

        util.checkData("select * from EMP");

        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:Address.java

public static void main(String[] a) throws Exception {
        JPAUtil util = new JPAUtil();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
        EntityManager em = emf.createEntityManager();
        ProfessorService service = new ProfessorService(em);

        em.getTransaction().begin();/*from   www . j av a 2 s  .  co  m*/

        service.createProfessorAndAddress(1, "name", 100, "street", "city", "state", "zip");
        service.createCompanyAndAddress("Name", "Street", "City", "State", "Zip");

        System.out.println("Professors and Addresses:");
        for (Professor emp : service.findAllProfessors()) {
            System.out.println(emp);
        }
        for (Company c : service.findAllCompanies()) {
            System.out.println(c);
        }
        util.checkData("select * from Professor");
        util.checkData("select * from Company");

        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:Department.java

public static void main(String[] a) throws Exception {
        JPAUtil util = new JPAUtil();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
        EntityManager em = emf.createEntityManager();
        ProfessorService service = new ProfessorService(em);

        em.getTransaction().begin();//from w  w w  .  j  av  a  2 s  .  c  om
        Professor emp = service.createProfessor("empName", 100);

        Department dept = service.createDepartment("deptName");

        service.setProfessorDepartment(emp.getId(), dept.getId());

        Collection<Professor> emps = service.findAllProfessors();
        for (Professor emp1 : emps) {
            System.out.println(emp1);
        }
        Collection<Department> depts = service.findAllDepartments();
        for (Department dept1 : depts) {
            System.out.println(dept1);
        }

        util.checkData("select * from Professor");
        util.checkData("select * from Department");

        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:Department.java

public static void main(String[] a) throws Exception {
        JPAUtil util = new JPAUtil();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
        EntityManager em = emf.createEntityManager();
        ProfessorService service = new ProfessorService(em);

        em.getTransaction().begin();//from   w ww . j  a va2s  .  c  o m

        service.init(3);
        service.next();
        service.previous();

        for (Object o : service.getCurrentResults()) {
            System.out.print(o);
        }

        System.out.println("Page " + (service.getCurrentPage() + 1) + " of " + service.getMaxPages());

        if (service.getCurrentPage() == 0) {
            System.out.print("Previous disabled ");
        }

        if (service.getCurrentPage() + 1 == service.getMaxPages()) {
            System.out.print("Next disabled ");
        }

        util.checkData("select * from Professor");
        util.checkData("select * from Department");

        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:Professor.java

public static void main(String[] a) throws Exception {
        JPAUtil util = new JPAUtil();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
        EntityManager em = emf.createEntityManager();
        ProfessorService service = new ProfessorService(em);

        em.getTransaction().begin();//w w  w  .  ja  v  a  2s . com
        Professor emp = service.createProfessor("empName", 100);

        Project proj = service.createProject("projName");

        emp = service.addProfessorProject(emp.getId(), proj.getId());

        Collection<Professor> emps = service.findAllProfessors();
        if (emps.isEmpty()) {
            System.out.println("No Professors found ");
        } else {
            System.out.println("Found Professors:");
            for (Professor emp1 : emps) {
                System.out.println(emp1);
            }
        }

        Collection<Project> projs = service.findAllProjects();
        if (projs.isEmpty()) {
            System.out.println("No Projects found ");
        } else {
            System.out.println("Found Projects:");
            for (Project proj1 : projs) {
                System.out.println(proj1);
            }
        }

        util.checkData("select * from Professor");

        util.checkData("select * from Project");
        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:Department.java

public static void main(String[] a) throws Exception {
        JPAUtil util = new JPAUtil();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
        EntityManager em = emf.createEntityManager();
        ProfessorService service = new ProfessorService(em);

        em.getTransaction().begin();//w w  w .  j a va2 s  .c o m

        Professor emp = service.createProfessor("empName", 100);
        Department dept = service.createDepartment("deptName");

        emp = service.setProfessorDepartment(emp.getId(), dept.getId());
        System.out.println(emp.getDepartment() + " with Professors:");
        System.out.println(emp.getDepartment().getProfessors());

        Collection<Professor> emps = service.findAllProfessors();
        if (emps.isEmpty()) {
            System.out.println("No Professors found ");
        } else {
            System.out.println("Found Professors:");
            for (Professor emp1 : emps) {
                System.out.println(emp1);
            }
        }

        Collection<Department> depts = service.findAllDepartments();
        if (depts.isEmpty()) {
            System.out.println("No Departments found ");
        } else {
            System.out.println("Found Departments:");
            for (Department dept1 : depts) {
                System.out.println(dept1 + " with " + dept1.getProfessors().size() + " employees");
            }
        }

        util.checkData("select * from Professor");
        util.checkData("select * from Department");

        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:Department.java

public static void main(String[] a) throws Exception {
        JPAUtil util = new JPAUtil();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
        EntityManager em = emf.createEntityManager();
        ProfessorService service = new ProfessorService(em);

        em.getTransaction().begin();//from ww  w  .j  a  v  a 2 s . c  o m

        Professor emp = service.createProfessor("empName", 100L);

        Department dept = service.createDepartment("deptName");

        emp = service.setProfessorDepartment(emp.getId(), dept.getId());

        System.out.println(emp.getDepartment() + " with Professors:");
        System.out.println(emp.getDepartment().getProfessors());

        Collection<Professor> emps = service.findAllProfessors();
        if (emps.isEmpty()) {
            System.out.println("No Professors found ");
        } else {
            System.out.println("Found Professors:");
            for (Professor emp1 : emps) {
                System.out.println(emp1);
            }
        }
        Collection<Department> depts = service.findAllDepartments();
        if (depts.isEmpty()) {
            System.out.println("No Departments found ");
        } else {
            System.out.println("Found Departments:");
            for (Department dept1 : depts) {
                System.out.println(
                        dept1 + " with " + dept1.getProfessors().size() + " employees " + dept1.getProfessors());
            }
        }

        util.checkData("select * from Professor");

        util.checkData("select * from Department");
        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:Department.java

public static void main(String[] a) throws Exception {
        JPAUtil util = new JPAUtil();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
        EntityManager em = emf.createEntityManager();
        ProfessorService service = new ProfessorService(em);

        em.getTransaction().begin();/*from   w ww.j a  va 2s.c o m*/

        Professor emp = service.createProfessor("empName", 100L);

        Department dept = service.createDepartment("deptName");

        emp = service.setProfessorDepartment(emp.getId(), dept.getId());

        System.out.println(emp.getDepartment() + " with Professors:");
        System.out.println(emp.getDepartment().getProfessors());

        Collection<Professor> emps = service.findAllProfessors();
        if (emps.isEmpty()) {
            System.out.println("No Professors found ");
        } else {
            System.out.println("Found Professors:");
            for (Professor emp1 : emps) {
                System.out.println(emp1);
            }
        }

        Collection<Department> depts = service.findAllDepartments();
        if (depts.isEmpty()) {
            System.out.println("No Departments found ");
        } else {
            System.out.println("Found Departments:");
            for (Department dept1 : depts) {
                System.out.println(
                        dept1 + " with " + dept1.getProfessors().size() + " employees " + dept1.getProfessors());
            }
        }

        util.checkData("select * from Professor");

        util.checkData("select * from Department");
        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:com.impetus.kvapps.runner.AppRunner.java

/**
 * After successful processing close entity manager and it's factory instance.
 * //from w w  w.j  a  v  a 2 s  . com
 * @param emf          entity manager factory instance.
 * @param em           entity manager instance
 */
private static void onDestroyDBResources(EntityManagerFactory emf, EntityManager em) {
    if (emf != null) {
        emf.close();
    }

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