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

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

        em.getTransaction().commit();//from  www. jav a 2  s  .co m
        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(1, "name", 100);
        emp = service.createProfessor(2, "name 2", 100);

        Integer userId = 1;/* www .  java2 s.  com*/

        emp = em.find(Professor.class, userId);
        if (emp == null) {
            System.out.println(userId + " not found! ");
        } else {
            System.out.println("Found " + emp);
        }

        for (Object o : em.createQuery("SELECT u FROM Professor u").getResultList()) {
            System.out.println(o);
        }

        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();
  Professor emp = service.createProfessor(158, "AAA", 45000,ProfessorType.CONTRACT_EMPLOYEE);
  em.getTransaction().commit();//from w w  w . j  a v a 2 s . com
  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: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.createProfessor(1, "empName", 1);

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

        em.getTransaction().commit();// ww  w  .j  a v a 2 s .  c  o m
        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(158, "AAA", 45000, new Date());
        em.getTransaction().commit();/* w  w w . j a  v  a2 s  .  c  o m*/
        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: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);/*  ww  w.jav a 2s  .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

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();
        Professor emp = service.createProfessor(158, "AAA", 45000, "asdf".getBytes());
        em.getTransaction().commit();//from  w ww . j  a  v a2s.  c om
        System.out.println("Persisted " + emp);

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

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

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

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

From source file:es.us.isa.ideas.utilities.PopulateDatabase.java

public static void main(String[] args) {

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

        BadProfessor emp2 = new BadProfessor();
        emp2.setId(2);//from w  w  w.j  av  a2  s  .c o  m

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

        service.createProfessor(1, "name");
        for (Professor emp : service.findAllProfessors()) {
            System.out.print(emp);
        }/*from   ww w  .j a v  a2s .  c o  m*/
        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();
    }