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:kirchnerei.note.model.DataService.java

public boolean removeNote(Long id) {
    if (NumberUtils.isEmpty(id)) {
        return false;
    }/*from   ww w  .j  ava 2 s .  c o m*/
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = entityService.get();
        tx = em.getTransaction();
        tx.begin();
        Note note = em.find(Note.class, id);
        if (note == null) {
            return false;
        }
        Category category = note.getCategory();
        category.getNotes().remove(note);
        note.setCategory(null);
        em.remove(note);
        if (category.getNotes().isEmpty()) {
            LogUtils.debug(log, "empty category '%s', then remove it", category.getTitle());
            em.remove(category);
        }
        EntityService.commit(tx);
        return true;
    } catch (Exception e) {
        EntityService.rollback(tx);
        return false;
    } finally {
        EntityService.close(em);
    }
}

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

@Override
public List<Employee> getAll() {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Employee> list = new LinkedList<>();
    try {/*from ww w .java  2 s  .c o  m*/
        em.getTransaction().begin();

        TypedQuery<Employee> query = em.createNamedQuery("Employee.findAll", Employee.class);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(EmployeeRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

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

@Override
public List<Employee> getAllFreeEmployees() {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Employee> list = new LinkedList<>();
    try {//w  w  w.  j a v  a  2s.  com
        em.getTransaction().begin();

        TypedQuery<Employee> query = em.createNamedQuery("Employee.findAllFreeEmployees", Employee.class);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(EmployeeRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

From source file:com.thruzero.domain.jpa.transaction.JpaDatabaseTransactionMgr.java

@Override
public EntityTransaction beginTransaction() {
    EntityManager currentEntityManager = doGetCurrentPersistenceManager(true);

    return currentEntityManager.getTransaction();
}

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

@Override
public List<UserSkill> getSkills(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<UserSkill> list = new LinkedList<>();
    try {//from w w  w .  j a  va  2 s .c o m
        em.getTransaction().begin();

        TypedQuery<UserSkill> query = em.createNamedQuery("UserSkill.findByEmployee", UserSkill.class);
        query.setParameter("employee", employee);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(EmployeeRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

From source file:com.github.jinahya.persistence.ShadowTest.java

@Test(enabled = false, invocationCount = 1)
public void testPuthenticate() {
    final EntityManager manager = LocalPU.createEntityManager();
    try {/*from ww  w .  j a  v a  2 s . com*/
        final EntityTransaction transaction = manager.getTransaction();
        transaction.begin();
        try {
            final String username = newUsername(manager);
            final byte[] password = newPassword();

            final Shadow shadow = persistInstance(manager, username, password);

            Assert.assertTrue(shadow.puthenticate(shadow, password));

            transaction.commit();
        } catch (Exception e) {
            LocalPU.printConstraintViolation(e);
            transaction.rollback();
            e.printStackTrace(System.err);
            Assert.fail(e.getMessage());
        }
    } finally {
        manager.close();
    }
}

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

@Override
public List<Vacancy> getAll() {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Vacancy> list = new LinkedList<>();
    try {//from  w w w.j a va 2 s .  c  om
        em.getTransaction().begin();

        TypedQuery<Vacancy> query = em.createNamedQuery("Vacancy.findAll", Vacancy.class);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(VacancyRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

From source file:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default persist//  w w w.j  a  va  2  s. co m
 * 
 * @param obj
 *            object to persist
 * @throws PersistException
 *             on errors (transaction is being rolled back)
 */
public void persist(T obj) throws PersistException {
    if (obj == null) {
        return;
    }
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

    try {
        t.begin();
        em.persist(obj);
        t.commit();
    } catch (Exception e) {
        t.rollback();
        throw new PersistException(e.getMessage(), e);
    }
}

From source file:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default merge/*from  ww w  . j av  a  2s .  c  om*/
 * 
 * @param obj
 *            object to merge
 * @throws MergeException
 *             on errors (transaction is being rolled back)
 * @return managed object
 */
public T merge(T obj) throws MergeException {
    if (obj == null) {
        return null;
    }
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

    try {
        t.begin();
        obj = em.merge(obj);
        t.commit();
        return obj;
    } catch (Exception e) {
        t.rollback();
        throw new MergeException("Cannot merge changes to " + String.valueOf(obj) + " into the database", e);
    }
}

From source file:com.github.jinahya.persistence.ShadowTest.java

@Test(enabled = true, invocationCount = 1)
public void testNassword0() {
    final EntityManager manager = LocalPU.createEntityManager();
    try {//from  w  w w  .j  a  va2  s . c  om
        final EntityTransaction transaction = manager.getTransaction();
        transaction.begin();
        try {
            final String username = newUsername(manager);
            final byte[] password = newPassword();
            Shadow shadow = persistInstance(manager, username, password);
            Assert.assertTrue(shadow.puthenticate(shadow, password));
            System.out.println("=========================================");
            LOGGER.log(Level.INFO, "mortons: {0}", MORTONS(manager, 0, 1024));
            final byte[] nassword = newPassword();
            shadow.nassword(shadow, password, nassword);
            shadow = manager.merge(shadow);
            manager.flush();
            System.out.println("=========================================");
            LOGGER.log(Level.INFO, "mortons: {0}", MORTONS(manager, 0, 1024));
            Assert.assertFalse(shadow.puthenticate(shadow, password));
            Assert.assertTrue(shadow.puthenticate(shadow, nassword));
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            e.printStackTrace(System.err);
            Assert.fail(e.getMessage());
        }
    } finally {
        manager.close();
    }
}