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: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();/*from   www.j  ava2 s  .co  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 EMP");

        em.getTransaction().commit();
        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();/*ww w.j  av a  2s . co  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();//from ww w.ja v a2  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: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();//  ww w  .j a v  a  2 s  . co 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();/*w  w  w  .j  av a2s. c  om*/
        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 ww w.  j  a  v a 2 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();/*  w ww .  j  a v  a2 s .c o m*/
        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  a v a2  s  . 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;/*www.  j  a  v  a2s  .  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();// ww w.j ava 2 s  .  c  om

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