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:com.edugility.substantia.substance.TestCasePerson.java

@Test
public void testingJPA() throws Exception {
    final EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
    assertNotNull(emf);//from   ww  w . j a  v a  2s  .c  o m

    final EntityManager em = emf.createEntityManager();
    assertNotNull(em);

    final EntityTransaction et = em.getTransaction();
    assertNotNull(et);
    et.begin();

    final Person p = new Person();
    em.persist(p);
    em.flush();
    assertFalse(p.isTransient());
    assertTrue(p.isVersioned());
    assertEquals(Long.valueOf(1L), p.getId());
    assertEquals(Integer.valueOf(1), p.getVersion());

    et.commit();
    et.begin();

    final Person p2 = em.find(Person.class, 1L, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
    assertNotNull(p2);
    assertFalse(p2.isTransient());
    assertTrue(p2.isVersioned());
    assertEquals(Long.valueOf(1L), p2.getId());
    assertEquals(Integer.valueOf(1), p2.getVersion());

    et.commit();
    et.begin();

    final Person p3 = em.getReference(Person.class, 1L);
    assertNotNull(p3);
    assertFalse(p3.isTransient());
    assertTrue(p3.isVersioned());
    assertEquals(Long.valueOf(1L), p3.getId());
    assertEquals(Integer.valueOf(2), p3.getVersion());

    et.commit();
    et.begin();

    assertTrue(em.contains(p));
    em.remove(p);

    et.commit();

    em.close();

    emf.close();
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.UserSkillRepositoryImplementation.java

@Override
public boolean delete(UserSkill userSkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    UserSkill managedUserSkill = null;/*w w w .jav a2  s . c o  m*/
    try {
        em.getTransaction().begin();
        managedUserSkill = em.merge(userSkill);
        em.remove(managedUserSkill);
        em.getTransaction().commit();

        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:facades.PersonFacadeDB.java

@Override
public Person delete(Integer id) throws NotFoundException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();//w w  w.  j a  v a2s . c om

    Person p = em.find(Person.class, id);

    if (p == null) {
        transaction.rollback();
        throw new NotFoundException("No person for the given id");
    } else {
        em.remove(p);
        transaction.commit();
    }

    return p;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.UserRepositoryImplementation.java

@Override
public boolean create(User user) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = false;

    try {//  w w  w.  ja  v a  2 s  .co  m
        em.getTransaction().begin();
        em.persist(user);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return success;
}

From source file:facades.PersonFacadeDB.java

@Override
public Person addPerson(String json) {
    //make person from Json
    Person p = gson.fromJson(json, Person.class);

    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();//from ww  w.j  ava 2  s. c  o  m

    try {
        em.persist(p);
        transaction.commit();
    } catch (Exception e) {
        transaction.rollback();
    } finally {
        em.close();
    }
    return p;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.CheckDocumentRepositoryImplementation.java

@Override
public boolean create(CheckDocument something) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = false;

    try {/*from   w ww  . j a v a 2 s .  c  om*/
        em.getTransaction().begin();
        em.persist(something);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return success;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.UserRepositoryImplementation.java

@Override
public boolean nameExistsInDB(String name) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean exists = false;

    try {/*from w  w w .j  av  a2  s . c  om*/
        em.getTransaction().begin();

        TypedQuery<User> query = em.createNamedQuery("User.findByLogin", User.class);
        query.setParameter("login", name);

        query.getSingleResult();

        em.getTransaction().commit();
        exists = true;
    } catch (NoResultException ex) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(ex);
        exists = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return exists;
}

From source file:facades.PersonFacadeDB.java

@Override
public String getPerson(Integer id) throws NotFoundException {
    String result = "";
    //get person with this id from DB
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();/* w  w  w  .  j  ava 2 s  .  c  o  m*/
    try {
        Person p = em.find(Person.class, id);

        result = om.writeValueAsString(p);
        //            Query query = em.createNamedQuery("Person.findById").setParameter("id", id);
        //            List<Person> people = query.getResultList();

        //result = gson.toJson(people.get(0));
        //result = om.writeValueAsString(people.get(0));
    } catch (Exception e) {
        throw new NotFoundException("No person exists for the given id");
    } finally {
        em.close();
    }
    return result;
}

From source file:facades.PersonFacadeDB.java

@Override
public String getPersons() {

    String result = "";
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();/*from  www.ja v a 2s .  com*/

    try {
        Query query = em.createNamedQuery("Person.findAll");
        List<Person> people = query.getResultList();

        try {
            result = om.writeValueAsString(people);
        } catch (JsonProcessingException ex) {
            Logger.getLogger(PersonFacadeDB.class.getName()).log(Level.SEVERE, null, ex);
        }

    } finally {
        em.close();
    }
    return result;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancySkillRepositoryImplementation.java

@Override
public boolean delete(VacancySkill vacancySkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    VacancySkill managedVacancySkill = null;
    try {//w w  w . j  ava 2 s  . com
        em.getTransaction().begin();
        managedVacancySkill = em.merge(vacancySkill);
        em.remove(managedVacancySkill);
        em.getTransaction().commit();

        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}