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

        service.createProfessorAndAddress(1, "name", 100, "street", "city", "state", "zip");
        service.createCompanyAndAddress("Name", "Street", "City", "State", "Zip");

        System.out.println("Professors and Addresses:");
        for (Professor emp : service.findAllProfessors()) {
            System.out.println(emp);
        }/*from  w w w . j  a va2  s.  c  o m*/
        for (Company c : service.findAllCompanies()) {
            System.out.println(c);
        }
        util.checkData("select * from Professor");
        util.checkData("select * from Company");

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

        emp = service.setProfessorDepartment(emp.getId(), dept.getId());
        System.out.println(emp.getDepartment() + " with Professors:");
        System.out.println(emp.getDepartment().getProfessors());

        Collection<Professor> emps = service.findAllProfessors();
        if (emps.isEmpty()) {
            System.out.println("No Professors found ");
        } else {/*from  ww w .j av  a 2 s . c o m*/
            System.out.println("Found Professors:");
            for (Professor emp1 : emps) {
                System.out.println(emp1);
            }
        }

        Collection<Department> depts = service.findAllDepartments();
        if (depts.isEmpty()) {
            System.out.println("No Departments found ");
        } else {
            System.out.println("Found Departments:");
            for (Department dept1 : depts) {
                System.out.println(dept1 + " with " + dept1.getProfessors().size() + " employees");
            }
        }

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

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

        service.findProfessorsReportingTo(1);

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

        em.getTransaction().commit();/*from w ww  .j  ava2 s .  c om*/
        em.close();
        emf.close();
    }

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

        service.addProfessor("12");

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

        em.getTransaction().commit();/*from  ww w.  ja  v a 2  s .  c  o m*/
        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", 100L);

        Department dept = service.createDepartment("deptName");

        emp = service.setProfessorDepartment(emp.getId(), dept.getId());

        System.out.println(emp.getDepartment() + " with Professors:");
        System.out.println(emp.getDepartment().getProfessors());

        Collection<Professor> emps = service.findAllProfessors();
        if (emps.isEmpty()) {
            System.out.println("No Professors found ");
        } else {//from ww  w. j  a  va  2s  .  c  o  m
            System.out.println("Found Professors:");
            for (Professor emp1 : emps) {
                System.out.println(emp1);
            }
        }
        Collection<Department> depts = service.findAllDepartments();
        if (depts.isEmpty()) {
            System.out.println("No Departments found ");
        } else {
            System.out.println("Found Departments:");
            for (Department dept1 : depts) {
                System.out.println(
                        dept1 + " with " + dept1.getProfessors().size() + " employees " + dept1.getProfessors());
            }
        }

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

        util.checkData("select * from Department");
        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:com.medicaid.mmis.util.CodeMappingLoader.java

/**
 * Reads the code mapping xls and inserts any unmapped row to the legacy mapping table.
 * @throws InvalidFormatException if the input file is not recognized 
 *//*www .  j a v  a  2 s.c om*/
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException, PortalServiceException, InvalidFormatException {
    PropertyConfigurator.configure("log4j.properties");
    logger = Logger.getLogger(CodeMappingLoader.class);
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("cms-data-load");
    EntityManager em = emf.createEntityManager();

    Workbook workbook = WorkbookFactory.create(new File("mapping/CodeMapping.xlsx"));
    SequenceGeneratorBean sequence = new SequenceGeneratorBean();
    sequence.setEm(em);

    try {
        em.getTransaction().begin();
        List<LegacySystemMapping> rs = em.createQuery("Select l from LegacySystemMapping l").getResultList();
        for (LegacySystemMapping mapping : rs) {
            em.remove(mapping);
        }
        importSheet(em, sequence, workbook, "ENROLLMENT_STATUS");
        importSheet(em, sequence, workbook, "RISK_LEVEL");
        importSheet(em, sequence, workbook, "SPECIALTY_CODE");
        importSheet(em, sequence, workbook, "LICENSE_TYPE");
        importSheet(em, sequence, workbook, "ISSUING_BOARD");
        importSheet(em, sequence, workbook, "PROVIDER_TYPE");
        importSheet(em, sequence, workbook, "COUNTY_CODE");
        importSheet(em, sequence, workbook, "BEN_OWNER_TYPE");
        importSheet(em, sequence, workbook, "LICENSE_STATUS");
        importSheet(em, sequence, workbook, "COS");
        em.getTransaction().commit();
    } catch (Throwable t) {
        em.getTransaction().rollback();
        logger.error("Could not complete import", t);
        throw new PortalServiceException("Error during import", t);
    }
}

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", 100L);

        Department dept = service.createDepartment("deptName");

        emp = service.setProfessorDepartment(emp.getId(), dept.getId());

        System.out.println(emp.getDepartment() + " with Professors:");
        System.out.println(emp.getDepartment().getProfessors());

        Collection<Professor> emps = service.findAllProfessors();
        if (emps.isEmpty()) {
            System.out.println("No Professors found ");
        } else {/*  w w w .j  a v  a  2 s .  c  o m*/
            System.out.println("Found Professors:");
            for (Professor emp1 : emps) {
                System.out.println(emp1);
            }
        }

        Collection<Department> depts = service.findAllDepartments();
        if (depts.isEmpty()) {
            System.out.println("No Departments found ");
        } else {
            System.out.println("Found Departments:");
            for (Department dept1 : depts) {
                System.out.println(
                        dept1 + " with " + dept1.getProfessors().size() + " employees " + dept1.getProfessors());
            }
        }

        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("empName", 100);

        Project proj = service.createProject("projName");

        emp = service.addProfessorProject(emp.getId(), proj.getId());

        Collection<Professor> emps = service.findAllProfessors();
        if (emps.isEmpty()) {
            System.out.println("No Professors found ");
        } else {//w w  w . j  a va2  s.  c o m
            System.out.println("Found Professors:");
            for (Professor emp1 : emps) {
                System.out.println(emp1);
            }
        }

        Collection<Project> projs = service.findAllProjects();
        if (projs.isEmpty()) {
            System.out.println("No Projects found ");
        } else {
            System.out.println("Found Projects:");
            for (Project proj1 : projs) {
                System.out.println(proj1);
            }
        }

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

        util.checkData("select * from Project");
        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();

        service.findWithAlias();//w  ww. j  ava 2 s .c  om

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

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

        service.init(3);/*from  w w  w  .j  av  a2  s.c om*/
        service.next();
        service.previous();

        for (Object o : service.getCurrentResults()) {
            System.out.print(o);
        }

        System.out.println("Page " + (service.getCurrentPage() + 1) + " of " + service.getMaxPages());

        if (service.getCurrentPage() == 0) {
            System.out.print("Previous disabled ");
        }

        if (service.getCurrentPage() + 1 == service.getMaxPages()) {
            System.out.print("Next disabled ");
        }

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

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