Example usage for javax.persistence EntityManagerFactory createEntityManager

List of usage examples for javax.persistence EntityManagerFactory createEntityManager

Introduction

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

Prototype

public EntityManager createEntityManager();

Source Link

Document

Create a new application-managed EntityManager.

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();/* ww w.j  ava  2 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:Main.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  .  ja va 2 s .c o m

  service.findAll();
    
    
  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();/*www  . j a v a  2  s. co  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

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  2s  .  co m*/

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

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

From source file:ContractProfessor.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.  ja  v a2s . co  m*/

  Professor emp = null;
  emp = new ContractProfessor();

  emp.setId(1);
  emp.setName("name");
  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 ww  w .j  a v a 2 s  .com

        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: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  .  jav a  2s  .  c o m

        service.createProfessor(1, "empName", 1);

        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  ww  . jav  a  2 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: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 w ww . ja v a  2 s.  com

        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: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 .j a v a  2s.  com*/
        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();
    }