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.ewcms.publication.PublishIntegratedTest.java

private void updateResourceStutas() {
    JpaTemplate template = createJpaTemplate();
    template.execute(new JpaCallback<Object>() {
        @Override//from ww w. ja  va 2s. c  o m
        public Object doInJpa(EntityManager em) throws PersistenceException {
            em.getTransaction().begin();
            String hql = "Update Resource o Set o.status= ?1 Where o.status = ?2";
            Query query = em.createQuery(hql);
            query.setParameter(1, Resource.Status.NORMAL);
            query.setParameter(2, Resource.Status.RELEASED);
            query.executeUpdate();
            em.getTransaction().commit();
            return null;
        }
    });
}

From source file:com.ewcms.publication.PublishIntegratedTest.java

private void updateArticleStutas() {
    JpaTemplate template = createJpaTemplate();
    template.execute(new JpaCallback<Object>() {
        @Override/*from   ww w  .j a va 2s .  c  o  m*/
        public Object doInJpa(EntityManager em) throws PersistenceException {
            em.getTransaction().begin();
            String hql = "Update Article o Set o.status=?1 Where o.status = ?2";
            Query query = em.createQuery(hql);
            query.setParameter(1, Article.Status.PRERELEASE);
            query.setParameter(2, Article.Status.RELEASE);
            query.executeUpdate();
            em.getTransaction().commit();
            return null;
        }
    });
}

From source file:com.medigy.persist.Ejb3TestCase.java

protected void tearDown() throws Exception {
    final EntityManager entityManager = getEntityManager();
    final EntityTransaction transaction = entityManager.getTransaction();
    SetupTestConfiguration.getInstance().getPostInsertEventListener().deleteEntityList(entityManager);
    transaction.commit();/* w ww . j  a v  a 2  s  .  c  om*/
    entityManager.close();
    SessionManager.getInstance().popActiveSession();
}

From source file:com.appdynamics.loan.persistence.BasePersistenceImpl.java

@Transactional
public void delete(Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();/*from   ww  w .ja va 2s  .  c  om*/
    try {
        entityManager.remove(object);
    } catch (Exception ex) {
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }
}

From source file:com.appdynamics.loan.persistence.BasePersistenceImpl.java

/**
 * The method to update the serailizable business objects into the database.
 * /*from  w w  w.  ja v a2  s . c om*/
 * @param object --
 *            serializable object
 * @throws PersistenceException
 */
@Transactional
public void update(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();
    try {
        entityManager.merge(object);
    } catch (Exception ex) {
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }
}

From source file:com.appdynamics.loan.persistence.BasePersistenceImpl.java

@Transactional
public void save(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();//  w  ww.  ja v a2  s .c o m
    try {
        entityManager.persist(object);
    } catch (Exception ex) {
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }

}

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

@Override
public Admin getById(int id) {
    EntityManager em = entityManagerFactory.createEntityManager();
    Admin admin;//from w  w  w. j a v a 2s . com
    try {
        em.getTransaction().begin();
        admin = em.find(Admin.class, id);
        em.getTransaction().commit();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return admin;
}

From source file:br.com.i9torpedos.model.service.repository.BaseRepository.java

public T saveOrUpdate(T obj) throws PersistenceException {
    EntityManager em = EntityManagerUtil.getEntityManager();
    em.getTransaction().begin();
    Boolean situacao = false;//from w w  w.  j  ava 2 s . c o  m

    if (obj != null) {

        try {
            // if (obj != null) {
            obj = em.merge(obj);

            log.info("Dados Atualizado com Sucesso. " + obj.getClass().getName());
            situacao = true;

            /*} else {
            em.persist(obj);
                    
            log.info("Dados Gravado com Sucesso. " + obj.getClass().getName());
            situacao = true;
            }*/

        } catch (Exception e) {
            situacao = false;
            log.fatal("Falha em Atualizar ou Gravar dados " + e.getMessage());

            //Devida a falha de Persistencia ira Realizar um Rolback
            em.getTransaction().rollback();
            throw new PersistenceException("Falha ao Persistir Dados", e);

        } finally {

            //se for verdadeiro ento realiza o commit e fecha meu entitymanager
            if (situacao) {
                em.getTransaction().commit();
                //em.flush();
                // em.clear();
            }
            em.close();
            log.info("EntityManager fechado com Sucesso. Metodo saveOrUpdate");
        }
    }

    return obj;

}

From source file:com.sixsq.slipstream.persistence.Run.java

public static Run abortOrReset(String abortMessage, String nodename, String uuid) {
    EntityManager em = PersistenceUtil.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();//from w  w  w  .j  a  v  a  2 s.c o  m

    Run run = Run.abortOrReset(abortMessage, nodename, em, uuid);

    transaction.commit();
    em.close();

    return run;
}

From source file:io.kloudwork.controller.LoginController.java

public String postRegister(Request request, Response response) throws IOException, FileUploadException {

    String salt = BCrypt.gensalt();
    String hash = BCrypt.hashpw(request.queryParams("password"), salt);

    User user = new User();
    user.setUsername(request.queryParams("username"));
    user.setPassword(hash);/*w  w  w  .  j  a va  2 s.c o  m*/

    EntityManager entityManager = Container.getInstance().getEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(user);
    entityManager.getTransaction().commit();
    return "registered";
}