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:es.us.isa.ideas.utilities.PopulateDatabase.java

public static void main(String[] args) {

    ApplicationContext ctx;/*  w w w  .  ja va  2s.c  om*/
    EntityManagerFactory emf;
    EntityManager em;
    EntityTransaction et;

    ctx = new ClassPathXmlApplicationContext("utilities/PopulateDatabase.xml");

    emf = Persistence.createEntityManagerFactory("persistenceUnit");
    em = emf.createEntityManager();
    et = em.getTransaction();

    et.begin();
    try {
        for (Entry<String, Object> entry : ctx.getBeansWithAnnotation(Entity.class).entrySet()) {
            em.persist(entry.getValue());
            System.out.println(String.format("Persisting (%s, %s@%d)", entry.getKey(),
                    entry.getValue().getClass().getName(), entry.getValue().hashCode()));
        }
        et.commit();
    } catch (Exception oops) {
        oops.printStackTrace();
        et.rollback();
        oops.printStackTrace();
    } finally {
        if (em.isOpen())
            em.close();
        if (emf.isOpen())
            emf.close();
        ((ClassPathXmlApplicationContext) ctx).close();
    }
}

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

        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 BAD_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   w w  w  .j a  va 2 s  .  c om*/

        service.createProfessor(1, "name");
        for (Professor emp : service.findAllProfessors()) {
            System.out.print(emp);
        }
        service.changeProfessorName(1, "newname");
        for (Professor emp : service.findAllProfessors()) {
            System.out.print(emp);
        }
        service.removeProfessor(1);

        for (Professor emp : service.findAllProfessors()) {
            System.out.print(emp);
        }

        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();/* w  w  w. jav  a2 s.  co  m*/
  Professor emp = service.createProfessor(158, "AAA", 45000,ProfessorType.CONTRACT_EMPLOYEE);
  em.getTransaction().commit();
  System.out.println("Persisted " + emp);

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

  // remove an employee
  em.getTransaction().begin();
  service.removeProfessor(158);
  em.getTransaction().commit();
  System.out.println("Removed Professor 158");

  util.checkData("select * from Professor");
    
  em.close();
  emf.close();
}

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

public static void main(String[] args) {

    try {// w w w  .  j  a  v  a2s  . c om
        Options clopts = createCommonOptions();

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

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

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

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        BIIObjectStore store = new BIIObjectStore();
        PermissionManager permMgr = new PermissionManager(entityManager);

        List<Study> studies = permMgr.getStudies();
        if (studies == null || studies.size() == 0) {
            log.info("No study in the BII DB");
        } else {
            for (Study s : studies) {
                store.put(Study.class, s.getAcc(), s);
            }
        }
        PersistenceShellCommand.reindexStudies(store, hibProps);
        log.info("\n");

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

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  w  w  w. j a  v a  2 s  .c om

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

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

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

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

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

public static void main(String[] args) {

    try {//from   w ww. j  a v  a  2  s .co m
        Options clopts = createCommonOptions();

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UserDelShellCommand.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(UserDelShellCommand.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);
        permMgr.deleteUser(args[0]);

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

        log.info("User deleted.");
        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.PermModShellCommand.java

public static void main(String[] args) {

    try {//  w  w w. j ava2s . c o m
        Options clopts = createCommonOptions();
        PermissionManager.createPermModOptions(clopts);

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

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

        args = cmdl.getArgs();
        setup(args);

        setupLog4JPath(cmdl, null);
        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(PermModShellCommand.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);
        permMgr.setStudyOwners(cmdl);
        permMgr.setVisibility(cmdl);

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

        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: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   w  ww .  j ava2  s  . c om*/

        service.createProfessor("country", 1, "name");
        System.out.println("Professors:");
        for (Professor emp : service.findAllProfessors()) {
            System.out.print(emp);
            System.out.print(emp.getManager());
        }

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

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