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:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteProduct(int id) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Product p = entityManager.find(Product.class, id);

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//w  ww. j av a 2s .  com
    entityManager.remove(p);
    entityTransaction.commit();
}

From source file:nl.b3p.viewer.admin.stripes.LayoutManagerActionBean.java

public Resolution removeComponent() {

    EntityManager em = Stripersist.getEntityManager();

    try {//ww w.  ja va  2  s .c o m
        component = (ConfiguredComponent) em
                .createQuery("from ConfiguredComponent where application = :application and name = :name")
                .setParameter("application", application).setParameter("name", name).getSingleResult();

        em.remove(component);
    } catch (NoResultException e) {
    }
    em.getTransaction().commit();
    return new ForwardResolution("/WEB-INF/jsp/application/layoutmanager.jsp");
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteCategory(int catid) {

    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Category c = entityManager.find(Category.class, catid);

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/*from   w w  w .ja  va  2s  .  com*/
    entityManager.remove(c);
    entityTransaction.commit();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteUser(int userid) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    FStoreUser u = entityManager.find(FStoreUser.class, userid);

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//from   w w  w. j a v  a  2  s. c o m

    entityManager.remove(u);

    entityTransaction.commit();
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}// www. j  a v a2 s.  com
 */
@Override
public <T extends BaseEntity> T insert(T entity) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        em.getTransaction().begin();
        em.persist(entity);
        em.getTransaction().commit();
        return entity;
    } catch (Exception ex) { // NOSONAR
        Transactions.markRollback(em);
        throw new JpaException(ex);
    } finally {
        Transactions.rollback(em);
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}/*from  w w  w .  j  av  a  2 s  . c o  m*/
 */
@Override
public <T extends BaseEntity> T update(T entity) {
    T updated;
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        em.getTransaction().begin();
        updated = em.merge(entity);
        em.getTransaction().commit();
    } catch (Exception ex) { // NOSONAR
        Transactions.markRollback(em);
        throw new JpaException(ex);
    } finally {
        Transactions.rollback(em);
        JpaUtil.closeEntityManager(em);
    }
    return updated;
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}/*from w ww .ja v  a2s  . c o  m*/
 */
@Override
public <T extends BaseEntity> int deleteAll(Class<T> entity) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        em.getTransaction().begin();
        int rowsDeleted = em.createQuery(em.getCriteriaBuilder().createCriteriaDelete(entity)).executeUpdate();
        em.getTransaction().commit();
        LOGGER.debug("deleteAll: No. of rows deleted: [{}]", rowsDeleted);
        return rowsDeleted;
    } catch (Exception ex) { // NOSONAR
        Transactions.markRollback(em);
        throw new JpaException(ex);
    } finally {
        Transactions.rollback(em);
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}/* ww w  .j av a2  s .c om*/
 */
@Override
public <T> T executeInTransaction(JpaCallback<T> action) {
    T result;
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        em.getTransaction().begin();
        result = action.doInJpa(em);
        em.getTransaction().commit();
    } catch (Exception ex) { // NOSONAR
        Transactions.markRollback(em);
        throw new JpaException(ex);
    } finally {
        Transactions.rollback(em);
        JpaUtil.closeEntityManager(em);
    }
    return result;
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteProperty(int propid) {

    EntityManager entityManager = entityManagerFactory.createEntityManager();
    FStoreProperty c = entityManager.find(FStoreProperty.class, propid);

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/*from  w w  w.j a  v a  2s . c o m*/
    entityManager.remove(c);
    entityTransaction.commit();

}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}//from  w ww. j av  a2  s  . co  m
 */
@Override
public <T extends BaseEntity> int deleteByJpaNamedQuery(CrudDTO<T> crudDTO) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        em.getTransaction().begin();
        TypedQuery<T> typedQuery = em.createNamedQuery(crudDTO.getNamedQuery(), crudDTO.getEntity());
        JpaUtil.bindQueryParams(typedQuery, crudDTO.getPosParams());
        int rowsDeleted = typedQuery.executeUpdate();
        em.getTransaction().commit();
        LOGGER.debug("deleteByJpaNamedQuery: No. of rows deleted: [{}]", rowsDeleted);
        return rowsDeleted;
    } catch (Exception ex) { // NOSONAR
        Transactions.markRollback(em);
        throw new JpaException(ex);
    } finally {
        Transactions.rollback(em);
        JpaUtil.closeEntityManager(em);
    }
}