Search in sources :

Example 11 with PersistenceException

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

the class EclipseLinkCrudRepository method getScalarResultOfType.

/**
 * {@inheritDoc}
 */
@Override
public <T> T getScalarResultOfType(Class<T> resultClass, QueryType type, String query, List<Object> posParams) {
    T result = null;
    EntityManager em = this.emf.createEntityManager();
    try {
        switch(type) {
            case JPQL:
                result = JpaUtil.setTypedQueryParams(em.createQuery(query, resultClass), posParams).getSingleResult();
                break;
            case NATIVE:
                result = resultClass.cast(JpaUtil.setQueryParams(em.createNativeQuery(query, resultClass), posParams).getSingleResult());
                break;
        }
    } catch (RuntimeException ex) {
        LOGGER.error(ex.getMessage(), ex);
        throw new PersistenceException(ex.getMessage(), ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
    return result;
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(com.adeptj.modules.data.jpa.PersistenceException)

Example 12 with PersistenceException

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

the class EclipseLinkCrudRepository method findByCriteria.

/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> List<T> findByCriteria(ReadCriteria<T> criteria) {
    EntityManager em = this.emf.createEntityManager();
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<T> cq = cb.createQuery(criteria.getEntity());
        Root<T> root = cq.from(criteria.getEntity());
        return em.createQuery(cq.where(cb.and(JpaUtil.getPredicates(criteria.getCriteriaAttributes(), cb, root)))).getResultList();
    } catch (RuntimeException ex) {
        LOGGER.error(ex.getMessage(), ex);
        throw new PersistenceException(ex.getMessage(), ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) PersistenceException(com.adeptj.modules.data.jpa.PersistenceException)

Example 13 with PersistenceException

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

the class EclipseLinkCrudRepository method update.

/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> T update(T entity) {
    T updated;
    EntityManager em = null;
    EntityTransaction txn = null;
    try {
        em = this.emf.createEntityManager();
        txn = JpaUtil.getTransaction(em);
        updated = em.merge(entity);
        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);
    }
    return updated;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceException(com.adeptj.modules.data.jpa.PersistenceException)

Example 14 with PersistenceException

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

the class EclipseLinkCrudRepository method deleteByCriteria.

/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> int deleteByCriteria(DeleteCriteria<T> criteria) {
    EntityManager em = null;
    EntityTransaction txn = null;
    try {
        em = this.emf.createEntityManager();
        txn = JpaUtil.getTransaction(em);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaDelete<T> cd = cb.createCriteriaDelete(criteria.getEntity());
        Root<T> root = cd.from(criteria.getEntity());
        int rowsDeleted = em.createQuery(cd.where(cb.and(JpaUtil.getPredicates(criteria.getCriteriaAttributes(), cb, root)))).executeUpdate();
        txn.commit();
        LOGGER.debug("deleteByCriteria: 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 : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceException(com.adeptj.modules.data.jpa.PersistenceException)

Example 15 with PersistenceException

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

the class EclipseLinkCrudRepository method findByTupleQuery.

/**
 * {@inheritDoc}
 */
@Override
public <T extends BaseEntity> List<Tuple> findByTupleQuery(TupleQueryCriteria<T> criteria) {
    EntityManager em = this.emf.createEntityManager();
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Tuple> cq = cb.createTupleQuery();
        Root<T> root = cq.from(criteria.getEntity());
        return em.createQuery(cq.multiselect(criteria.getSelections().stream().map(root::get).toArray(Selection[]::new)).where(cb.and(JpaUtil.getPredicates(criteria.getCriteriaAttributes(), cb, root)))).getResultList();
    } catch (RuntimeException ex) {
        LOGGER.error(ex.getMessage(), ex);
        throw new PersistenceException(ex.getMessage(), ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) PersistenceException(com.adeptj.modules.data.jpa.PersistenceException) Tuple(javax.persistence.Tuple)

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