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;
}
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);
}
}
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;
}
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);
}
}
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);
}
}
Aggregations