Search in sources :

Example 1 with PersistenceException

use of com.adeptj.modules.data.jpa.PersistenceException in project adeptj-modules by AdeptJ.

the class EclipseLinkCrudRepository method insert.

/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> T insert(T entity) {
    EntityManager em = null;
    EntityTransaction txn = null;
    try {
        em = this.emf.createEntityManager();
        txn = JpaUtil.getTransaction(em);
        em.persist(entity);
        txn.commit();
        return entity;
    } catch (RuntimeException ex) {
        JpaUtil.setRollbackOnly(txn);
        LOGGER.error(ex.getMessage(), ex);
        throw new PersistenceException(ex.getMessage(), ex);
    } finally {
        JpaUtil.rollbackTransaction(txn);
        JpaUtil.closeEntityManager(em);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceException(com.adeptj.modules.data.jpa.PersistenceException)

Example 2 with PersistenceException

use of com.adeptj.modules.data.jpa.PersistenceException in project adeptj-modules by AdeptJ.

the class EclipseLinkCrudRepository method deleteAll.

/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> int deleteAll(Class<T> entity) {
    EntityManager em = null;
    EntityTransaction txn = null;
    try {
        em = this.emf.createEntityManager();
        txn = JpaUtil.getTransaction(em);
        int rowsDeleted = em.createQuery(em.getCriteriaBuilder().createCriteriaDelete(entity)).executeUpdate();
        txn.commit();
        LOGGER.debug("deleteAll: No. of rows deleted: [{}]", rowsDeleted);
        return rowsDeleted;
    } catch (RuntimeException ex) {
        JpaUtil.setRollbackOnly(txn);
        LOGGER.error(ex.getMessage(), ex);
        throw new PersistenceException(ex.getMessage(), ex);
    } finally {
        JpaUtil.rollbackTransaction(txn);
        JpaUtil.closeEntityManager(em);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceException(com.adeptj.modules.data.jpa.PersistenceException)

Example 3 with PersistenceException

use of com.adeptj.modules.data.jpa.PersistenceException in project adeptj-modules by AdeptJ.

the class EclipseLinkCrudRepository method delete.

/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> void delete(Class<T> entity, Object primaryKey) {
    EntityManager em = null;
    EntityTransaction txn = null;
    try {
        em = this.emf.createEntityManager();
        txn = JpaUtil.getTransaction(em);
        T entityToDelete = em.find(entity, primaryKey);
        if (entityToDelete == null) {
            LOGGER.warn("Entity couldn't be deleted as it doesn't exists in DB: [{}]", entity);
        } else {
            em.remove(entityToDelete);
            txn.commit();
        }
    } catch (RuntimeException ex) {
        JpaUtil.setRollbackOnly(txn);
        LOGGER.error(ex.getMessage(), ex);
        throw new PersistenceException(ex.getMessage(), ex);
    } finally {
        JpaUtil.rollbackTransaction(txn);
        JpaUtil.closeEntityManager(em);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceException(com.adeptj.modules.data.jpa.PersistenceException)

Example 4 with PersistenceException

use of com.adeptj.modules.data.jpa.PersistenceException in project adeptj-modules by AdeptJ.

the class EclipseLinkCrudRepository method deleteByJpaNamedQuery.

/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> int deleteByJpaNamedQuery(CrudDTO<T> crudDTO) {
    EntityManager em = null;
    EntityTransaction txn = null;
    try {
        em = this.emf.createEntityManager();
        txn = JpaUtil.getTransaction(em);
        TypedQuery<T> typedQuery = em.createNamedQuery(crudDTO.getNamedQuery(), crudDTO.getEntity());
        int rowsDeleted = JpaUtil.setTypedQueryParams(typedQuery, crudDTO.getPosParams()).executeUpdate();
        txn.commit();
        LOGGER.debug("deleteByJpaNamedQuery: No. of rows deleted: [{}]", rowsDeleted);
        return rowsDeleted;
    } catch (RuntimeException ex) {
        JpaUtil.setRollbackOnly(txn);
        LOGGER.error(ex.getMessage(), ex);
        throw new PersistenceException(ex.getMessage(), ex);
    } finally {
        JpaUtil.rollbackTransaction(txn);
        JpaUtil.closeEntityManager(em);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceException(com.adeptj.modules.data.jpa.PersistenceException)

Example 5 with PersistenceException

use of com.adeptj.modules.data.jpa.PersistenceException in project adeptj-modules by AdeptJ.

the class EclipseLinkCrudRepository method updateByCriteria.

/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> int updateByCriteria(UpdateCriteria<T> criteria) {
    EntityManager em = null;
    EntityTransaction txn = null;
    try {
        em = this.emf.createEntityManager();
        txn = JpaUtil.getTransaction(em);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaUpdate<T> cu = cb.createCriteriaUpdate(criteria.getEntity());
        criteria.getUpdateAttributes().forEach(cu::set);
        Root<T> root = cu.from(criteria.getEntity());
        int rowsUpdated = em.createQuery(cu.where(cb.and(JpaUtil.getPredicates(criteria.getCriteriaAttributes(), cb, root)))).executeUpdate();
        txn.commit();
        LOGGER.debug("No. of rows updated: {}", rowsUpdated);
        return rowsUpdated;
    } catch (RuntimeException ex) {
        JpaUtil.setRollbackOnly(txn);
        LOGGER.error(ex.getMessage(), ex);
        throw new PersistenceException(ex.getMessage(), ex);
    } finally {
        JpaUtil.rollbackTransaction(txn);
        JpaUtil.closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceException(com.adeptj.modules.data.jpa.PersistenceException)

Aggregations

PersistenceException (com.adeptj.modules.data.jpa.PersistenceException)15 EntityManager (javax.persistence.EntityManager)15 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 EntityTransaction (javax.persistence.EntityTransaction)7 Tuple (javax.persistence.Tuple)1 Selection (javax.persistence.criteria.Selection)1