Example usage for javax.persistence EntityManager close

List of usage examples for javax.persistence EntityManager close

Introduction

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

Prototype

public void close();

Source Link

Document

Close an application-managed entity manager.

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();//from ww  w  .  j av a2  s  .co m

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

        emp = new BadProfessor();
        emp.setId(2);
        emp.setName("bad");
        service.createProfessor(emp);

        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: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();//from  w  w  w. j  a  va  2 s  .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();/*from w  w w.j  a  va2  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();//ww w. j  ava  2s .c  o 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();/* w w  w . j a  v a2  s. co m*/
        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:elaborate.editor.backend.Indexer.java

@SuppressWarnings("boxing")
public static void main(String[] args) {
    boolean wipeIndexFirst = args.length == 0 ? false : "-w".equals(args[0]);
    StopWatch sw = new StopWatch();
    sw.start();//  w w  w .  j  av  a  2  s  .  c  o  m
    ElaborateSolrIndexer solr = new ElaborateSolrIndexer();
    if (wipeIndexFirst) {
        Log.info("clearing index");
        solr.clear();
    }
    EntityManager entityManager = HibernateUtil.getEntityManager();
    try {
        ProjectEntryService projectEntryService = ProjectEntryService.instance();
        projectEntryService.setEntityManager(entityManager);
        List<ProjectEntry> projectentries = projectEntryService.getAll();
        int size = projectentries.size();
        Log.info("indexing {} projectEntries", size);
        int n = 1;
        for (ProjectEntry projectEntry : projectentries) {
            Log.info("indexing projectEntry {} ({}/{} = {}%) (est. time remaining: {})", //
                    new Object[] { //
                            projectEntry.getId(), n, size, //
                            percentage(n, size), //
                            time_remaining(n, size, sw.getTime()) //
                    } //
            );
            solr.index(projectEntry, autoCommit(n));
            n++;
        }
    } finally {
        entityManager.close();
    }
    solr.commit();
    sw.stop();
    Log.info("done in {}", convert(sw.getTime()));
}

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 v a  2 s.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();//from   w ww.  j a v  a 2 s.c  o m
        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();//from www .ja v  a 2 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  w  w 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();
    }