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:org.isatools.isatab.commandline.UserAddShellCommand.java

public static void main(String[] args) {

    try {//from   w  w w.  j a v  a2  s.  c  o  m
        Options clopts = createCommonOptions();
        PermissionManager.createUserDefOptions(clopts);

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UserAddShellCommand.class);

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

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

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

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

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

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        Person user = permMgr.createNewUserFromOptions(args[0], cmdl);
        permMgr.addUser(user);

        transaction.commit();
        entityManager.close();

        log.info(PermissionManager.formatUser(user));
        log.info("User added.");
        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

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

public static void main(String[] args) {

    try {//from   ww  w.j  a  v a  2s  .com
        Options clopts = createCommonOptions();
        PermissionManager.createUserDefOptions(clopts);

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UserModShellCommand.class);

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

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

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

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

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

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        Person user = permMgr.createNewUserFromOptions(args[0], cmdl);
        permMgr.updateUser(user);

        transaction.commit();

        log.info("User changed, new details:");
        log.info(PermissionManager.formatUser(permMgr.getUserByLogin(user.getUserName())));
        log.info("\n");

        entityManager.close();
        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

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

@SuppressWarnings("static-access")
public static void main(String[] args) {
    try {/*from ww  w.j a  v a2 s.c  o m*/
        Options clopts = createCommonOptions();
        clopts.addOption(OptionBuilder.withArgName("directory-path")
                .withDescription("Exports to local path (instead of the BII repository), useful for checking")
                .hasArg().withLongOpt("to-dir").create("d"));
        clopts.addOption("s", "skip-data-files", false,
                "Avoids to export (-to-dir option) or backup (BII repo export) the data files");
        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                ISATABExportShellCommand.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(ISATABExportShellCommand.class);

        String exportPath = cmdl.getOptionValue("d");
        boolean skipDataFiles = cmdl.hasOption("s");

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "manual");
        // We assume this, since we are unloading something that is supposed to already exist (including during tests).
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");

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

        ISATABDBExporter dbexp = new ISATABDBExporter(daoFactory, args);

        if (exportPath != null) {
            dbexp.exportToPath(exportPath, skipDataFiles);
        } else {
            dbexp.exportToRepository(skipDataFiles);
        }

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the ISATAB exporter: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

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.j  av a  2 s  . com*/
        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);
        }
        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();/*from ww w. ja v a2  s. c o m*/

        Professor emp = null;
        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:org.isatools.isatab.commandline.UnloadShellCommand.java

public static void main(String[] args) {

    try {//from www  .  jav a  2s . c o  m
        Options clopts = createCommonOptions();

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UnloadShellCommand.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(UnloadShellCommand.class);

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

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

        ISATABUnloader unloader = null;

        for (String studyAcc : args) {
            unloader = new ISATABUnloader(DaoFactory.getInstance(entityManager), studyAcc);
            log.info("\n\n " + i18n.msg("unloader_unloading_study", studyAcc));
            unloader.unload();

            Set<String> messages = unloader.getMessages();
            log.info("\n\n  Unloading of " + studyAcc + ", messages: ");
            for (String message : messages) {
                log.info(message);
            }
            log.info("\n\n");
        }

        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the ISATAB unloader: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

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

@SuppressWarnings("static-access")
public static void main(String[] args) {

    try {//  w w  w . ja v  a  2 s.co m
        Options clopts = createCommonOptions();
        clopts.addOption(OptionBuilder.withDescription("List all the studies and their permissions/ownership")
                .withLongOpt("studies").create("s"));
        clopts.addOption(OptionBuilder.withDescription("List all the users").withLongOpt("users").create("u"));

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                ContentsShellCommand.class);

        boolean isStudies = cmdl.hasOption("s"), isUsers = cmdl.hasOption("u");
        if (!isStudies && !isUsers) {
            printUsage(clopts);
            System.exit(1);
        }

        args = cmdl.getArgs();
        setup(args);
        setupLog4JPath(cmdl, null);

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

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

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

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        if (isStudies) {
            List<Study> studies = permMgr.getStudies();
            if (studies == null || studies.size() == 0) {
                log.info("No study in the BII DB");
            } else {
                for (Study s : studies) {
                    log.info(PermissionManager.formatStudy(s));
                }
            }
        } else {
            List<Person> persons = permMgr.getUsers();
            if (persons == null || persons.size() == 0) {
                log.info("No user in the BII DB");
            } else {
                for (Person u : persons) {
                    log.info(PermissionManager.formatUser(u));
                }
            }
        }

        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + 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  a v a2s . c o m
        Professor emp = service.createProfessor("name", 100, "street", "city", "state", "zip");
        em.getTransaction().commit();
        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();//from   w w  w  . j  a  va 2  s.  co  m

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

        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  ww. j av  a 2  s.c  o m*/
  Professor emp = service.createProfessor("name", 100, 
      "street", "city", "state", "zip");
  em.getTransaction().commit();
  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();
}