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: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();/*  w  w  w .  j  av a2 s  .c om*/

        GoodProfessor emp = null;
        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();//from  ww  w  .  j av  a2s .c  o m

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

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

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

From source file:org.isatools.isatab.commandline.PersistenceShellCommand.java

public static void main(String[] args) {
    EntityTransaction transaction = null;
    try {//from   ww  w  . ja  v  a2 s. c o m

        Options clopts = createCommonOptions();
        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                PersistenceShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length == 0) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(PersistenceShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "manual");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        BIIObjectStore store = loadIsaTab();
        log.info(i18n.msg("mapping_done_now_persisting", store.size()));

        ISATABPersister persister = new ISATABPersister(store, DaoFactory.getInstance(entityManager));
        transaction = entityManager.getTransaction();
        transaction.begin();
        Timestamp ts = persister.persist(sourceDirPath);
        transaction.commit();
        entityManager.close();

        reindexStudies(store, hibProps);

        log.info("\n\n" + i18n.msg("mapping_done_data_saved_in_db"));
        log.info("\n\n" + i18n.msg("submission_done_ts_reported", "" + ts.getTime(),
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(ts) + "." + ts.getNanos()) + "\n");
        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the ISATAB loader: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

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();//from  w  w w.  j  ava  2  s .  c o  m

        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);
        }
        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();/*  w  w w.  ja v  a 2 s  . c om*/

        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 {
            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();/*from  w w w.ja v  a  2 s.  co  m*/

        service.findProfessorsReportingTo(1);

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

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

        service.addProfessor("12");

        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();// w ww.  j a v a 2 s  . c o m

        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 {
            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: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();// w  ww .  jav  a2  s.  c  o  m

        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 {
            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();//www .  jav  a2  s.  c o  m
        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 {
            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();
    }