Example usage for javax.persistence EntityManager getTransaction

List of usage examples for javax.persistence EntityManager getTransaction

Introduction

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

Prototype

public EntityTransaction getTransaction();

Source Link

Document

Return the resource-level EntityTransaction object.

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

        service.findAllProfessors();/*from   www. j ava  2s.  co m*/

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

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

From source file:Message.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();

        service.messageCreateAndList();/*  w  w  w .  j  a v a2  s.c  o m*/

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

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

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

        //service.removeDepartment2();

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

        em.getTransaction().commit();//  ww w. j  av  a  2 s  .  c om
        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();

        service.lockAllProfessors();//w  ww .j  a  v  a  2s  .c om

        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();
        util.executeSQLCommand("CREATE SEQUENCE Emp_Seq;");

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

        em.getTransaction().begin();
        Professor emp = service.createProfessor("name", 100);
        em.getTransaction().commit();//from  ww w.  j ava 2 s.c o  m
        System.out.println("Persisted " + emp);

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

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

  service.findAll();/*w ww  .ja va 2 s  .  c om*/
    
    
  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();

        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());
        }//from   w  ww . j ava 2 s  . co  m
        util.checkData("select * from Professor");

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

  Professor emp = null;//from   ww  w . j  a  va  2  s.c  o m
  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();
        Professor emp = service.createProfessor(158, "AAA", 45000);
        em.getTransaction().commit();//from  w  ww  .j a v  a2  s .com
        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();
    }