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

  BadProfessor emp2 = new BadProfessor();
  emp2.setId(2);/*w  w  w. j a v  a 2 s.c  om*/

  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

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.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);
        }//from  www.ja va 2s  . c  o m

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

        service.createProfessor("country", 1, "name");
        System.out.println("Professors:");
        for (Professor emp : service.findAllProfessors()) {
            System.out.print(emp);
            System.out.print(emp.getManager());
        }// w  w  w  .  j  ava  2s . c o m

        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();
        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);
        }/*from  w  w w .  j  a  v  a  2 s. com*/
        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: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 = null;/*w w w  .  ja  va 2s.c o  m*/
        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: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();
  Professor emp = service.createProfessor("name", 100, 
      "street", "city", "state", "zip");
  em.getTransaction().commit();/*  ww  w  .  ja v a  2 s. c om*/
  System.out.println("Persisted " + emp);

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

    
  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();
        Professor emp = service.createProfessor("name", 100, "street", "city", "state", "zip");
        em.getTransaction().commit();//ww  w  .  j a  v  a 2 s . c  o m
        System.out.println("Persisted " + emp);

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

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

        Collection<Professor> l = service.findAllProfessors();

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

        em.getTransaction().commit();/*w w  w  .  j av a  2  s .co  m*/
        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();

        GoodProfessor emp = null;/*from w  w w.j a  v a2 s  . co  m*/
        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: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();

        long l = service.findSalaryForNameAndDepartment("deptName", "empName");

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

        em.getTransaction().commit();//from w w  w.  ja v a 2s  . c  o  m
        em.close();
        emf.close();
    }