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();/*  www . j  ava  2 s  .com*/
  Professor emp = service.createProfessor(158, "AAA", 45000,ProfessorType.CONTRACT_EMPLOYEE);
  em.getTransaction().commit();
  System.out.println("Persisted " + emp);

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

  // remove an employee
  em.getTransaction().begin();
  service.removeProfessor(158);
  em.getTransaction().commit();
  System.out.println("Removed Professor 158");

  util.checkData("select * from Professor");
    
  em.close();
  emf.close();
}

From source file:BadProfessor.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  a  va2 s  .c  o m

        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 BAD_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();/* www .j ava2s  . c o m*/

        service.createProfessor("country", 1, "name", 100);

        Professor emp = service.findProfessor("country", 1);
        System.out.println("Found " + emp);

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

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

        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 v  a 2 s. c o  m*/
        Professor emp = service.createProfessor(158, "AAA", 45000);
        em.getTransaction().commit();
        System.out.println("Persisted " + emp);

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

        // remove an employee
        em.getTransaction().begin();
        service.removeProfessor(158);
        em.getTransaction().commit();
        System.out.println("Removed Professor 158");

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

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

From source file:Professor.java

License:asdf

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 .  ja  v  a2 s .c  om
        Professor emp = service.createProfessor(158, "AAA", 45000, "asdf".getBytes());
        em.getTransaction().commit();
        System.out.println("Persisted " + emp);

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

        // remove an employee
        em.getTransaction().begin();
        service.removeProfessor(158);
        em.getTransaction().commit();
        System.out.println("Removed Professor 158");

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

        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 ww w .j ava2s  .com*/
        Professor emp = service.createProfessor(158, "AAA", 45000, new Date());
        em.getTransaction().commit();
        System.out.println("Persisted " + emp);

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

        // remove an employee
        em.getTransaction().begin();
        service.removeProfessor(158);
        em.getTransaction().commit();
        System.out.println("Removed Professor 158");

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

        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   ww w .j ava  2s  . c  o  m

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

        Collection emps = em.createQuery("SELECT e FROM Professor e").getResultList();
        for (Iterator i = emps.iterator(); i.hasNext();) {
            Professor e = (Professor) i.next();
            System.out.println("Professor " + e.getId() + ", " + e.getName());
        }
        util.checkData("select * from Professor");

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

From source file:es.us.isa.ideas.utilities.PopulateDatabase.java

public static void main(String[] args) {

    ApplicationContext ctx;//ww  w.ja v a2  s . co m
    EntityManagerFactory emf;
    EntityManager em;
    EntityTransaction et;

    ctx = new ClassPathXmlApplicationContext("utilities/PopulateDatabase.xml");

    emf = Persistence.createEntityManagerFactory("persistenceUnit");
    em = emf.createEntityManager();
    et = em.getTransaction();

    et.begin();
    try {
        for (Entry<String, Object> entry : ctx.getBeansWithAnnotation(Entity.class).entrySet()) {
            em.persist(entry.getValue());
            System.out.println(String.format("Persisting (%s, %s@%d)", entry.getKey(),
                    entry.getValue().getClass().getName(), entry.getValue().hashCode()));
        }
        et.commit();
    } catch (Exception oops) {
        oops.printStackTrace();
        et.rollback();
        oops.printStackTrace();
    } finally {
        if (em.isOpen())
            em.close();
        if (emf.isOpen())
            emf.close();
        ((ClassPathXmlApplicationContext) ctx).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   ww  w . j av a2 s  .c o m

        service.createProfessor(1, "name");
        for (Professor emp : service.findAllProfessors()) {
            System.out.print(emp);
        }
        service.changeProfessorName(1, "newname");
        for (Professor emp : service.findAllProfessors()) {
            System.out.print(emp);
        }
        service.removeProfessor(1);

        for (Professor emp : service.findAllProfessors()) {
            System.out.print(emp);
        }

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

        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 . jav  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();
    }