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