Skip to content

Commit

Permalink
New release 1.0.1 with fix to support thread local shared entity mana…
Browse files Browse the repository at this point in the history
…gers
  • Loading branch information
martin committed Jan 21, 2017
1 parent 7c018dd commit bf1c23c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.martinbechtle'
version '1.0.0'
version '1.0.1'

apply plugin: 'java'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,23 +23,11 @@ public class JpaCrudRepository<T, I extends Serializable> {
private final EntityManager entityManager;
private final Class<T> clazz;

private final TypedQuery<T> findAllQuery;
private final Query deleteAllQuery;

@SuppressWarnings("unchecked")
public JpaCrudRepository(EntityManager entityManager, Class<T> entityClass) {

this.entityManager = entityManager;
this.clazz = entityClass;

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> cq = criteriaBuilder.createQuery(clazz);
findAllQuery = entityManager.createQuery(
cq.select(cq.from(clazz)));

CriteriaDelete<T> delete = criteriaBuilder.createCriteriaDelete(clazz);
delete.from(clazz);
deleteAllQuery = entityManager.createQuery(delete);
}

/**
Expand Down Expand Up @@ -141,7 +128,13 @@ public boolean exists(I id) {
*/
public List<T> findAll() {

return findAllQuery.getResultList();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(clazz);
criteriaQuery.select(criteriaQuery.from(clazz));

return entityManager
.createQuery(criteriaQuery)
.getResultList();
}

/**
Expand Down Expand Up @@ -175,6 +168,12 @@ public void delete(Iterable<? extends T> entities) {
*/
public void deleteAll() {

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

CriteriaDelete<T> delete = criteriaBuilder.createCriteriaDelete(clazz);
delete.from(clazz);

Query deleteAllQuery = entityManager.createQuery(delete);
deleteAllQuery.executeUpdate();
}

Expand Down

0 comments on commit bf1c23c

Please sign in to comment.