From bf1c23c1212ce8277ff46b9cf3582c65b3949ea3 Mon Sep 17 00:00:00 2001 From: martin Date: Sat, 21 Jan 2017 13:27:45 +0000 Subject: [PATCH] New release 1.0.1 with fix to support thread local shared entity managers --- build.gradle | 2 +- .../crudrepository/JpaCrudRepository.java | 27 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/build.gradle b/build.gradle index db91560..18ff587 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'com.martinbechtle' -version '1.0.0' +version '1.0.1' apply plugin: 'java' diff --git a/src/main/java/com/martinbechtle/crudrepository/JpaCrudRepository.java b/src/main/java/com/martinbechtle/crudrepository/JpaCrudRepository.java index f326901..92e3a73 100644 --- a/src/main/java/com/martinbechtle/crudrepository/JpaCrudRepository.java +++ b/src/main/java/com/martinbechtle/crudrepository/JpaCrudRepository.java @@ -5,7 +5,6 @@ import javax.persistence.EntityManager; import javax.persistence.EntityNotFoundException; import javax.persistence.Query; -import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaDelete; import javax.persistence.criteria.CriteriaQuery; @@ -24,23 +23,11 @@ public class JpaCrudRepository { private final EntityManager entityManager; private final Class clazz; - private final TypedQuery findAllQuery; - private final Query deleteAllQuery; - @SuppressWarnings("unchecked") public JpaCrudRepository(EntityManager entityManager, Class entityClass) { this.entityManager = entityManager; this.clazz = entityClass; - - CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); - CriteriaQuery cq = criteriaBuilder.createQuery(clazz); - findAllQuery = entityManager.createQuery( - cq.select(cq.from(clazz))); - - CriteriaDelete delete = criteriaBuilder.createCriteriaDelete(clazz); - delete.from(clazz); - deleteAllQuery = entityManager.createQuery(delete); } /** @@ -141,7 +128,13 @@ public boolean exists(I id) { */ public List findAll() { - return findAllQuery.getResultList(); + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(clazz); + criteriaQuery.select(criteriaQuery.from(clazz)); + + return entityManager + .createQuery(criteriaQuery) + .getResultList(); } /** @@ -175,6 +168,12 @@ public void delete(Iterable entities) { */ public void deleteAll() { + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + + CriteriaDelete delete = criteriaBuilder.createCriteriaDelete(clazz); + delete.from(clazz); + + Query deleteAllQuery = entityManager.createQuery(delete); deleteAllQuery.executeUpdate(); }